From 2e3dc282c76d4cdaaed514f95eae1f83b8a8aaad Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Fri, 3 Jun 2022 11:58:02 +0300 Subject: [PATCH] Change the array's memory handling --- include/array.h | 1 + src/array/base-copy.c | 12 ++++----- src/array/extra.c | 58 ++++++++++++++++++++----------------------- src/array/get.c | 17 ++++++++----- src/array/include.h | 16 +----------- src/array/sort.c | 2 +- 6 files changed, 47 insertions(+), 59 deletions(-) diff --git a/include/array.h b/include/array.h index b438ba7..4582711 100644 --- a/include/array.h +++ b/include/array.h @@ -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) +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); diff --git a/src/array/base-copy.c b/src/array/base-copy.c index a017b41..fe4dbcf 100644 --- a/src/array/base-copy.c +++ b/src/array/base-copy.c @@ -17,7 +17,7 @@ arr_t array_copy(const arr_t* s) { void *p, *v, *e; 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; e = array_end(&x); @@ -35,7 +35,7 @@ arr_t array_copy(const arr_t* s) { v += vtype_size(x.type); } 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; @@ -54,7 +54,7 @@ arr_t* array_duplicate(const arr_t* s) { void *p, *v, *e; 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; e = array_end(x); @@ -72,7 +72,7 @@ arr_t* array_duplicate(const arr_t* s) { v += vtype_size(x->type); } 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)); return x; @@ -89,7 +89,7 @@ void array_copy_init(arr_t* x, const arr_t* s) { void *p, *v, *e; 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; e = array_end(x); @@ -107,6 +107,6 @@ void array_copy_init(arr_t* x, const arr_t* s) { v += vtype_size(x->type); } 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)); } diff --git a/src/array/extra.c b/src/array/extra.c index c94e0d0..bfc662a 100644 --- a/src/array/extra.c +++ b/src/array/extra.c @@ -17,52 +17,48 @@ ssize_t array_find(const arr_t* x, const void* v, vtype vt) { index = -1; - if (x->size) { - void* p = x->mem; - void* e = array_end(x); + if (!x->size) + return index; - assert(!is_null(x->mem)); + void* p = x->mem; + void* e = array_end(x); - if (x->type != vt) { - do { - ++index; - c = vtype_compare(p, x->type, v, vt); + assert(!is_null(x->mem)); - if (c == 0) - return index; + if (x->type != vt) { + do { + ++index; + c = vtype_compare(p, x->type, v, vt); - p += vtype_size(x->type); - } while (p < e); - } else { - do { - ++index; - c = vtype_compare_eq(p, v, vt); + if (c == 0) + return index; - if (c == 0) - return index; + p += vtype_size(x->type); + } while (p < e); + } else { + do { + ++index; + c = vtype_compare_eq(p, v, vt); - p += vtype_size(x->type); - } while (p < e); - } + if (c == 0) + return index; + + p += vtype_size(x->type); + } while (p < e); } return -1; } 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); - if ((ssize_t)array_allocated_nmemb(x) - vtype_size(x->type) < 0) { - ++x->size; - x->mem = realloc(x->mem, array_allocated_nmemb(x)); - } else ++x->size; + x->mem = realloc(x->mem, ++x->size * vtype_size(x->type)); + memcpy(array_internal_at(x, i), vnode_peek(&n, x->type), vtype_size(x->type)); - memcpy(array_at(x, i), vnode_peek(&n, x->type), vtype_size(x->type)); - - if (x->type < VTYPE_STRING) vnode_free(&n, x->type); + if (vtype_size(x->type) > sizeof(void*) && x->type < VTYPE_STRING) + vnode_free(&n, x->type); return i; } diff --git a/src/array/get.c b/src/array/get.c index 3c8cdae..23a2323 100644 --- a/src/array/get.c +++ b/src/array/get.c @@ -5,6 +5,11 @@ #include "../__internal/assert.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) { 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 (!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); } 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 { i = -1; 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->size = n; - x->mem = malloc(array_allocated_nmemb(x)); + x->mem = malloc(x->size*vtype_size(x->type)); if (!cut && s->type >= VTYPE_STRING) { void *p, *v, *e; void (*init)(void*, const void*); p = x->mem; - v = array_at(s, i); - e = array_at(x, n); + v = array_internal_at(s, i); + e = array_internal_at(x, n); switch (s->type) { default: abort(); 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); v += vtype_size(x->type); } 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); } else { diff --git a/src/array/include.h b/src/array/include.h index 06badcf..a9c17b8 100644 --- a/src/array/include.h +++ b/src/array/include.h @@ -8,20 +8,6 @@ #ifndef 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)) { void* v = x->mem + i*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); } -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); } diff --git a/src/array/sort.c b/src/array/sort.c index 415f868..19622c2 100644 --- a/src/array/sort.c +++ b/src/array/sort.c @@ -81,7 +81,7 @@ void array_sort(arr_t* x) { void array_reverse(arr_t* x) { if (x->size >= 2) { 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)]; do {