Change the array's memory handling
This commit is contained in:
		
							parent
							
								
									a0a7299fe0
								
							
						
					
					
						commit
						2e3dc282c7
					
				| @ -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); | ||||
| 
 | ||||
| 
 | ||||
|  | ||||
| @ -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)); | ||||
| } | ||||
|  | ||||
| @ -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; | ||||
| } | ||||
|  | ||||
| @ -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 { | ||||
|  | ||||
| @ -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); | ||||
| } | ||||
| 
 | ||||
|  | ||||
| @ -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 { | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user