Change the array's memory handling

This commit is contained in:
Gregory Lirent 2022-06-03 11:58:02 +03:00
parent a0a7299fe0
commit 2e3dc282c7
6 changed files with 47 additions and 59 deletions

View File

@ -22,6 +22,7 @@ extern void array_reverse(vtype_array* x) LIBCDSB_nt__ LIBCDSB_nn1__;
#define in_array(x, value) (array_indexof(x, value) >= 0) #define in_array(x, value) (array_indexof(x, value) >= 0)
extern void* array_at (const vtype_array* s, ssize_t index);
extern ssize_t array_get(vtype_array* s, vtype_value* x, ssize_t index, _Bool cut); extern ssize_t array_get(vtype_array* s, vtype_value* x, ssize_t index, _Bool cut);

View File

@ -17,7 +17,7 @@ arr_t array_copy(const arr_t* s) {
void *p, *v, *e; void *p, *v, *e;
void (*init)(void*, const void*); void (*init)(void*, const void*);
x.mem = p = malloc(array_allocated_nmemb(s)); x.mem = p = malloc(x.size*vtype_size(x.type));
v = s->mem; v = s->mem;
e = array_end(&x); e = array_end(&x);
@ -35,7 +35,7 @@ arr_t array_copy(const arr_t* s) {
v += vtype_size(x.type); v += vtype_size(x.type);
} while (p < e); } while (p < e);
} else x.mem = memndup(s->mem, array_allocated_nmemb(s)); } else x.mem = memndup(s->mem, x.size*vtype_size(x.type));
} }
return x; return x;
@ -54,7 +54,7 @@ arr_t* array_duplicate(const arr_t* s) {
void *p, *v, *e; void *p, *v, *e;
void (*init)(void*, const void*); void (*init)(void*, const void*);
x->mem = p = malloc(array_allocated_nmemb(s)); x->mem = p = malloc(x->size*vtype_size(x->type));
v = s->mem; v = s->mem;
e = array_end(x); e = array_end(x);
@ -72,7 +72,7 @@ arr_t* array_duplicate(const arr_t* s) {
v += vtype_size(x->type); v += vtype_size(x->type);
} while (p < e); } while (p < e);
} else x->mem = memndup(s->mem, array_allocated_nmemb(s)); } else x->mem = memndup(s->mem, x->size*vtype_size(x->type));
} else memset(x, 0, sizeof(*x)); } else memset(x, 0, sizeof(*x));
return x; return x;
@ -89,7 +89,7 @@ void array_copy_init(arr_t* x, const arr_t* s) {
void *p, *v, *e; void *p, *v, *e;
void (*init)(void*, const void*); void (*init)(void*, const void*);
x->mem = p = malloc(array_allocated_nmemb(s)); x->mem = p = malloc(x->size*vtype_size(x->type));
v = s->mem; v = s->mem;
e = array_end(x); e = array_end(x);
@ -107,6 +107,6 @@ void array_copy_init(arr_t* x, const arr_t* s) {
v += vtype_size(x->type); v += vtype_size(x->type);
} while (p < e); } while (p < e);
} else x->mem = memndup(s->mem, array_allocated_nmemb(s)); } else x->mem = memndup(s->mem, x->size*vtype_size(x->type));
} else memset(x, 0, sizeof(*x)); } else memset(x, 0, sizeof(*x));
} }

View File

@ -17,7 +17,9 @@ ssize_t array_find(const arr_t* x, const void* v, vtype vt) {
index = -1; index = -1;
if (x->size) { if (!x->size)
return index;
void* p = x->mem; void* p = x->mem;
void* e = array_end(x); void* e = array_end(x);
@ -44,25 +46,19 @@ ssize_t array_find(const arr_t* x, const void* v, vtype vt) {
p += vtype_size(x->type); p += vtype_size(x->type);
} while (p < e); } while (p < e);
} }
}
return -1; return -1;
} }
ssize_t array_push(arr_t* x, const void* v, vtype vt) { ssize_t array_push(arr_t* x, const void* v, vtype vt) {
void* p;
ssize_t i = x->size; ssize_t i = x->size;
vnode_t n = vnode_tcreate(x->type, v, vt); vnode_t n = vnode_tcreate(x->type, v, vt);
if ((ssize_t)array_allocated_nmemb(x) - vtype_size(x->type) < 0) { x->mem = realloc(x->mem, ++x->size * vtype_size(x->type));
++x->size; memcpy(array_internal_at(x, i), vnode_peek(&n, x->type), vtype_size(x->type));
x->mem = realloc(x->mem, array_allocated_nmemb(x));
} else ++x->size;
memcpy(array_at(x, i), vnode_peek(&n, x->type), vtype_size(x->type)); if (vtype_size(x->type) > sizeof(void*) && x->type < VTYPE_STRING)
vnode_free(&n, x->type);
if (x->type < VTYPE_STRING) vnode_free(&n, x->type);
return i; return i;
} }

View File

@ -5,6 +5,11 @@
#include "../__internal/assert.h" #include "../__internal/assert.h"
#include "../__internal/vnode.h" #include "../__internal/vnode.h"
void* array_at(const arr_t* x, ssize_t i) {
if (i < 0 && (i += x->size) < 0) i = 0;
return x->mem + i*vtype_size(x->type);
}
ssize_t array_get(arr_t* x, val_t* d, ssize_t i, _Bool cut) { ssize_t array_get(arr_t* x, val_t* d, ssize_t i, _Bool cut) {
if (i < 0 && (i += x->size) < 0) i = 0; if (i < 0 && (i += x->size) < 0) i = 0;
@ -14,12 +19,12 @@ ssize_t array_get(arr_t* x, val_t* d, ssize_t i, _Bool cut) {
if (cut) { if (cut) {
if (!is_null(d)) { if (!is_null(d)) {
vnode_t n = vnode_create(array_at(x, i), x->type); vnode_t n = vnode_create(array_internal_at(x, i), x->type);
value_set(d, n, x->type, VF_WRITEABLE|VF_REMOVABLE); value_set(d, n, x->type, VF_WRITEABLE|VF_REMOVABLE);
} }
array_cut(x, i, 1); array_cut(x, i, 1);
} else value_set(d, array_at(x, i), x->type, VF_WRITEABLE); } else value_set(d, array_internal_at(x, i), x->type, VF_WRITEABLE);
} else { } else {
i = -1; i = -1;
memset(d, 0, sizeof(*d)); memset(d, 0, sizeof(*d));
@ -41,15 +46,15 @@ _Bool array_slice(arr_t* x, arr_t* s, ssize_t i, size_t n, _Bool cut) {
x->type = s->type; x->type = s->type;
x->size = n; x->size = n;
x->mem = malloc(array_allocated_nmemb(x)); x->mem = malloc(x->size*vtype_size(x->type));
if (!cut && s->type >= VTYPE_STRING) { if (!cut && s->type >= VTYPE_STRING) {
void *p, *v, *e; void *p, *v, *e;
void (*init)(void*, const void*); void (*init)(void*, const void*);
p = x->mem; p = x->mem;
v = array_at(s, i); v = array_internal_at(s, i);
e = array_at(x, n); e = array_internal_at(x, n);
switch (s->type) { default: abort(); switch (s->type) { default: abort();
case VTYPE_STRING: init = (void*)string_copy_init; break; case VTYPE_STRING: init = (void*)string_copy_init; break;
@ -64,7 +69,7 @@ _Bool array_slice(arr_t* x, arr_t* s, ssize_t i, size_t n, _Bool cut) {
p += vtype_size(x->type); p += vtype_size(x->type);
v += vtype_size(x->type); v += vtype_size(x->type);
} while (p < e); } while (p < e);
} else memcpy(x->mem, array_at(s, i), n*vtype_size(x->type)); } else memcpy(x->mem, array_internal_at(s, i), n*vtype_size(x->type));
if (cut) array_cut(s, i, n); if (cut) array_cut(s, i, n);
} else { } else {

View File

@ -8,20 +8,6 @@
#ifndef LIBCDSB_SRC_ARRAY_INCLUDE_H #ifndef LIBCDSB_SRC_ARRAY_INCLUDE_H
#define LIBCDSB_SRC_ARRAY_INCLUDE_H #define LIBCDSB_SRC_ARRAY_INCLUDE_H
#define ARRAY_MEM_BLOCK_SIZE 256
ainline(size_t array_nblocks(const arr_t* x)) {
size_t n = x->size*vtype_size(x->type);
if (n%vtype_size(x->type)) {
return n / ARRAY_MEM_BLOCK_SIZE + 1;
} else return n / ARRAY_MEM_BLOCK_SIZE;
}
ainline(size_t array_allocated_nmemb(const arr_t* x)) {
return array_nblocks(x) * ARRAY_MEM_BLOCK_SIZE;
}
ainline(void array_cut(arr_t* x, size_t i, size_t n)) { ainline(void array_cut(arr_t* x, size_t i, size_t n)) {
void* v = x->mem + i*vtype_size(x->type); void* v = x->mem + i*vtype_size(x->type);
void* e = v + n*vtype_size(x->type); void* e = v + n*vtype_size(x->type);
@ -34,7 +20,7 @@ ainline(void* array_end(const arr_t* x)) {
return x->mem + x->size*vtype_size(x->type); return x->mem + x->size*vtype_size(x->type);
} }
ainline(void* array_at(const arr_t* x, size_t i)) { ainline(void* array_internal_at(const arr_t* x, size_t i)) {
return x->mem + i*vtype_size(x->type); return x->mem + i*vtype_size(x->type);
} }

View File

@ -81,7 +81,7 @@ void array_sort(arr_t* x) {
void array_reverse(arr_t* x) { void array_reverse(arr_t* x) {
if (x->size >= 2) { if (x->size >= 2) {
void *l = x->mem; void *l = x->mem;
void *r = array_at(x, x->size-1); void *r = array_internal_at(x, x->size-1);
char b[vtype_size(x->type)]; char b[vtype_size(x->type)];
do { do {