From 1f2584ccea42a45e2f7d092917fa5d6c7e4d1911 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Sat, 21 Dec 2024 12:36:01 +0100 Subject: [PATCH] Port to new memory model --- m-array.h | 24 ++++++++++++------------ m-bitset.h | 12 ++++++------ m-bstring.h | 8 ++++---- m-buffer.h | 12 ++++++------ m-c-mempool.h | 14 +++++++------- m-core.h | 20 ++++++++++---------- m-deque.h | 12 +++++++++--- m-dict.h | 24 +++++++++++++----------- m-genint.h | 4 ++-- m-shared-ptr.h | 8 ++++---- m-shared.h | 4 ++-- m-snapshot.h | 8 ++++---- m-string.h | 11 ++++++----- m-tree.h | 8 ++++---- m-worker.h | 4 ++-- tests/test-obj-except.h | 8 ++++---- 16 files changed, 95 insertions(+), 86 deletions(-) diff --git a/m-array.h b/m-array.h index dac946ea..dcb7309b 100644 --- a/m-array.h +++ b/m-array.h @@ -227,7 +227,7 @@ { \ M_ARRA4_CONTRACT(v); \ M_F(name, _reset)(v); \ - M_CALL_FREE(oplist, v->ptr); \ + M_CALL_FREE(oplist, type, v->ptr, v->alloc); \ /* This is so reusing the object implies an assertion failure */ \ v->alloc = 1; \ v->ptr = NULL; \ @@ -242,7 +242,7 @@ if (M_UNLIKELY (d == s)) return; \ if (s->size > d->alloc) { \ const size_t alloc = s->size; \ - type *ptr = M_CALL_REALLOC(oplist, type, d->ptr, alloc); \ + type *ptr = M_CALL_REALLOC(oplist, type, d->ptr, d->alloc, alloc); \ if (M_UNLIKELY_NOMEM (ptr == NULL)) { \ M_MEMORY_FULL(sizeof (type) * alloc); \ return ; \ @@ -329,7 +329,7 @@ return NULL; \ } \ M_ASSERT (alloc > v->size); \ - type *ptr = M_CALL_REALLOC(oplist, type, v->ptr, alloc); \ + type *ptr = M_CALL_REALLOC(oplist, type, v->ptr, v->alloc, alloc); \ if (M_UNLIKELY_NOMEM (ptr == NULL) ) { \ M_MEMORY_FULL(sizeof (type) * alloc); \ return NULL; \ @@ -398,7 +398,7 @@ return ; \ } \ M_ASSERT (alloc > v->size); \ - type *ptr = M_CALL_REALLOC(oplist, type, v->ptr, alloc); \ + type *ptr = M_CALL_REALLOC(oplist, type, v->ptr, v->alloc, alloc); \ if (M_UNLIKELY_NOMEM (ptr == NULL) ) { \ M_MEMORY_FULL(sizeof (type) * alloc); \ return; \ @@ -430,7 +430,7 @@ /* Increase size of array */ \ if (size > v->alloc) { \ size_t alloc = size ; \ - type *ptr = M_CALL_REALLOC(oplist, type, v->ptr, alloc); \ + type *ptr = M_CALL_REALLOC(oplist, type, v->ptr, v->alloc, alloc); \ if (M_UNLIKELY_NOMEM (ptr == NULL) ) { \ M_MEMORY_FULL(sizeof (type) * alloc); \ return; \ @@ -457,11 +457,11 @@ alloc = v->size; \ } \ if (M_UNLIKELY (alloc == 0)) { \ - M_CALL_FREE(oplist, v->ptr); \ + M_CALL_FREE(oplist, type, v->ptr, v->alloc); \ v->size = v->alloc = 0; \ v->ptr = NULL; \ } else { \ - type *ptr = M_CALL_REALLOC(oplist, type, v->ptr, alloc); \ + type *ptr = M_CALL_REALLOC(oplist, type, v->ptr, v->alloc, alloc); \ if (M_UNLIKELY_NOMEM (ptr == NULL) ) { \ M_MEMORY_FULL(sizeof (type) * alloc); \ return; \ @@ -488,7 +488,7 @@ M_MEMORY_FULL(sizeof (type) * alloc); \ return NULL; \ } \ - type *ptr = M_CALL_REALLOC(oplist, type, v->ptr, alloc); \ + type *ptr = M_CALL_REALLOC(oplist, type, v->ptr, v->alloc, alloc); \ if (M_UNLIKELY_NOMEM (ptr == NULL) ) { \ M_MEMORY_FULL(sizeof (type) * alloc); \ return NULL; \ @@ -618,7 +618,7 @@ M_MEMORY_FULL(sizeof (type) * alloc); \ return ; \ } \ - type *ptr = M_CALL_REALLOC(oplist, type, v->ptr, alloc); \ + type *ptr = M_CALL_REALLOC(oplist, type, v->ptr, v->alloc, alloc); \ if (M_UNLIKELY_NOMEM (ptr == NULL) ) { \ M_MEMORY_FULL(sizeof (type) * alloc); \ return; \ @@ -908,13 +908,13 @@ if (M_UNLIKELY (l->size < 2)) \ return; \ /* NOTE: if size is <= 4, no need to perform an allocation */ \ - type *temp = M_CALL_REALLOC(oplist, type, NULL, l->size); \ + type *temp = M_CALL_REALLOC(oplist, type, NULL, 0, l->size); \ if (M_UNLIKELY_NOMEM (temp == NULL)) { \ M_MEMORY_FULL(sizeof (type) * l->size); \ return ; \ } \ M_C3(m_arra4_,name,_stable_sort_noalloc)(l->ptr, l->size, temp); \ - M_CALL_FREE(oplist, temp); \ + M_CALL_FREE(oplist, type, temp, l->size); \ } \ ,) /* IF SWAP & SET methods */ \ \ @@ -966,7 +966,7 @@ should have exhausted all memory before reaching such sizes. */ \ M_ASSERT_INDEX(a1->size, newSize); \ if (newSize > a1->alloc) { \ - type *ptr = M_CALL_REALLOC(oplist, type, a1->ptr, newSize); \ + type *ptr = M_CALL_REALLOC(oplist, type, a1->ptr, a1->alloc, newSize); \ if (M_UNLIKELY_NOMEM (ptr == NULL) ) { \ M_MEMORY_FULL(sizeof (type) * newSize); \ } \ diff --git a/m-bitset.h b/m-bitset.h index bd7a7c97..1e32710e 100644 --- a/m-bitset.h +++ b/m-bitset.h @@ -105,7 +105,7 @@ M_INLINE void m_bitset_clear(m_bitset_t t) { m_bitset_reset(t); - M_MEMORY_FREE(t->ptr); + M_MEMORY_FREE(m_b1tset_limb_ct, t->ptr, t->alloc); // This is not really needed, but is safer // This representation is invalid and will be detected by the contract. // A C compiler should be able to optimize out theses initializations. @@ -124,7 +124,7 @@ m_bitset_set(m_bitset_t d, const m_bitset_t s) if (M_LIKELY (s->size > 0)) { // Test if enough space in target if (s->size > M_B1TSET_FROM_ALLOC (d->alloc)) { - m_b1tset_limb_ct *ptr = M_MEMORY_REALLOC (m_b1tset_limb_ct, d->ptr, needAlloc); + m_b1tset_limb_ct *ptr = M_MEMORY_REALLOC (m_b1tset_limb_ct, d->ptr, d->alloc, needAlloc); if (M_UNLIKELY_NOMEM (ptr == NULL)) { M_MEMORY_FULL(needAlloc); return ; @@ -216,7 +216,7 @@ m_bitset_push_back (m_bitset_t v, bool x) return; } // Alloc memory - m_b1tset_limb_ct *ptr = M_MEMORY_REALLOC (m_b1tset_limb_ct, v->ptr, needAlloc); + m_b1tset_limb_ct *ptr = M_MEMORY_REALLOC (m_b1tset_limb_ct, v->ptr, v->alloc, needAlloc); // Check if success if (M_UNLIKELY_NOMEM (ptr == NULL) ) { M_MEMORY_FULL(needAlloc * sizeof(m_b1tset_limb_ct)); @@ -255,7 +255,7 @@ m_bitset_resize (m_bitset_t v, size_t size) size_t newAlloc = M_B1TSET_TO_ALLOC (size); if (newAlloc > v->alloc) { // Allocate more limbs to store the bitset. - m_b1tset_limb_ct *ptr = M_MEMORY_REALLOC (m_b1tset_limb_ct, v->ptr, newAlloc); + m_b1tset_limb_ct *ptr = M_MEMORY_REALLOC (m_b1tset_limb_ct, v->ptr, v->alloc, newAlloc); if (M_UNLIKELY_NOMEM (ptr == NULL) ) { M_MEMORY_FULL(newAlloc * sizeof(m_b1tset_limb_ct)); return; @@ -302,12 +302,12 @@ m_bitset_reserve (m_bitset_t v, size_t alloc) } if (M_UNLIKELY (newAlloc == 0)) { // Free all memory used by the bitsets - M_MEMORY_FREE (v->ptr); + M_MEMORY_FREE (m_b1tset_limb_ct, v->ptr, v->alloc); v->size = v->alloc = 0; v->ptr = NULL; } else { // Allocate more memory or reduce memory usage - m_b1tset_limb_ct *ptr = M_MEMORY_REALLOC (m_b1tset_limb_ct, v->ptr, newAlloc); + m_b1tset_limb_ct *ptr = M_MEMORY_REALLOC (m_b1tset_limb_ct, v->ptr, v->alloc, newAlloc); if (M_UNLIKELY_NOMEM (ptr == NULL) ) { M_MEMORY_FULL(newAlloc * sizeof(m_b1tset_limb_ct)); return; diff --git a/m-bstring.h b/m-bstring.h index 25182cf0..85d16b83 100644 --- a/m-bstring.h +++ b/m-bstring.h @@ -78,7 +78,7 @@ M_INLINE void m_bstring_clear(m_bstring_t s) { M_BSTRING_CONTRACT(s); - M_MEMORY_FREE(s->ptr); + M_MEMORY_FREE(uint8_t, s->ptr, s->alloc); s->offset = (size_t) -1; } @@ -166,7 +166,7 @@ m_bstr1ng_fit2size (m_bstring_t v, size_t size_alloc, bool exact_alloc) if (M_UNLIKELY_NOMEM (alloc < size_alloc)) { goto allocation_error; } - uint8_t *ptr = M_MEMORY_REALLOC (uint8_t, v->ptr, alloc); + uint8_t *ptr = M_MEMORY_REALLOC (uint8_t, v->ptr, v->alloc, alloc); if (M_UNLIKELY_NOMEM (ptr == NULL)) { goto allocation_error; } @@ -401,13 +401,13 @@ m_bstring_reserve (m_bstring_t v, size_t n) n = v->offset + size; } if (n == 0) { - M_MEMORY_FREE(v->ptr); + M_MEMORY_FREE(uint8_t, v->ptr, v->alloc); v->offset = 0; v->size = 0; v->alloc = 0; v->ptr = NULL; } else { - uint8_t *ptr = M_MEMORY_REALLOC (uint8_t, v->ptr, n); + uint8_t *ptr = M_MEMORY_REALLOC (uint8_t, v->ptr, v->alloc, n); if (M_UNLIKELY_NOMEM (ptr == NULL)) { M_MEMORY_FULL(n); } diff --git a/m-buffer.h b/m-buffer.h index c4f8c573..20a3b01a 100644 --- a/m-buffer.h +++ b/m-buffer.h @@ -319,7 +319,7 @@ M_F(name, _init)(buffer_t v, size_t size) \ } \ \ M_BUFF3R_IF_CTE_SIZE(m_size)( /* Statically allocated */ , \ - v->data = M_CALL_REALLOC(oplist, M_F(name, _el_ct), NULL, M_BUFF3R_SIZE(m_size)); \ + v->data = M_CALL_REALLOC(oplist, M_F(name, _el_ct), NULL, 0, M_BUFF3R_SIZE(m_size)); \ if (M_UNLIKELY_NOMEM (v->data == NULL)) { \ M_MEMORY_FULL (M_BUFF3R_SIZE(m_size)*sizeof(M_F(name, _el_ct))); \ return; \ @@ -371,7 +371,7 @@ M_F(name, _init)(buffer_t v, size_t size) \ M_BUFF3R_CONTRACT(v,m_size); \ M_C3(m_buff3r_,name,_clear_obj)(v); \ M_BUFF3R_IF_CTE_SIZE(m_size)( , \ - M_CALL_FREE(oplist, v->data); \ + M_CALL_FREE(oplist, M_F(name, _el_ct), v->data, M_BUFF3R_SIZE(m_size)); \ v->data = NULL; \ ) \ v->overwrite = 0; \ @@ -906,7 +906,7 @@ M_F(name, _init)(buffer_t v, size_t size) \ atomic_init(&buffer->ProdIdx, (unsigned int) size); \ atomic_init(&buffer->ConsoIdx, (unsigned int) size); \ buffer->size = (unsigned int) size; \ - buffer->Tab = M_CALL_REALLOC(oplist, M_F(name, _el_ct), NULL, size); \ + buffer->Tab = M_CALL_REALLOC(oplist, M_F(name, _el_ct), NULL, 0, size); \ if (M_UNLIKELY_NOMEM (buffer->Tab == NULL)) { \ M_MEMORY_FULL (size*sizeof(M_F(name, _el_ct) )); \ return; \ @@ -940,7 +940,7 @@ M_F(name, _init)(buffer_t v, size_t size) \ j = 0; \ } \ } \ - M_CALL_FREE(oplist, buffer->Tab); \ + M_CALL_FREE(oplist, M_F(name, _el_ct), buffer->Tab, M_BUFF3R_SIZE(m_size)); \ buffer->Tab = NULL; /* safer */ \ buffer->size = 3; \ } \ @@ -1248,7 +1248,7 @@ M_F(name, _init)(buffer_t v, size_t size) \ atomic_init(&buffer->prodIdx, (unsigned int) size); \ atomic_init(&buffer->consoIdx, (unsigned int) size); \ buffer->size = (unsigned int) size; \ - buffer->Tab = M_CALL_REALLOC(oplist, M_F(name, _el_ct), NULL, size); \ + buffer->Tab = M_CALL_REALLOC(oplist, M_F(name, _el_ct), NULL, 0, size); \ if (M_UNLIKELY_NOMEM (buffer->Tab == NULL)) { \ M_MEMORY_FULL (size*sizeof(M_F(name, _el_ct) )); \ return; \ @@ -1281,7 +1281,7 @@ M_F(name, _init)(buffer_t v, size_t size) \ j = 0; \ } \ } \ - M_CALL_FREE(oplist, buffer->Tab); \ + M_CALL_FREE(oplist, M_F(name, _el_ct), buffer->Tab, M_BUFF3R_SIZE(m_size)); \ buffer->Tab = NULL; /* safer */ \ buffer->size = 3; \ } \ diff --git a/m-c-mempool.h b/m-c-mempool.h index bfae37a1..be1f3501 100644 --- a/m-c-mempool.h +++ b/m-c-mempool.h @@ -464,7 +464,7 @@ M_BEGIN_PROTECTED_CODE { \ const size_t max_thread = gc_mem->max_thread; \ /* Initialize the thread data of the mempool */ \ - mem->thread_data = M_MEMORY_REALLOC(M_F(name, _lfmp_thread_ct), NULL, max_thread); \ + mem->thread_data = M_MEMORY_REALLOC(M_F(name, _lfmp_thread_ct), NULL, 0, max_thread); \ if (M_UNLIKELY_NOMEM (mem->thread_data == NULL)) { \ M_MEMORY_FULL(max_thread * sizeof(M_F(name, _lfmp_thread_ct))); \ return; \ @@ -497,7 +497,7 @@ M_BEGIN_PROTECTED_CODE for(unsigned i = 0; i < max_thread;i++) { \ M_F(name, _lfmp_thread_clear)(&mem->thread_data[i]); \ } \ - M_MEMORY_FREE(mem->thread_data); \ + M_MEMORY_FREE(M_F(name, _lfmp_thread_ct), mem->thread_data, mem->gc_mem->max_thread); \ mem->thread_data = NULL; \ M_F(name, _lflist_clear)(mem->empty); \ M_F(name, _lflist_clear)(mem->free); \ @@ -587,7 +587,7 @@ m_gc_init(m_gc_t gc_mem, size_t max_thread) atomic_init(&gc_mem->ticket, 0UL); m_genint_init(gc_mem->thread_alloc, (unsigned int) max_thread); - gc_mem->thread_data = M_MEMORY_REALLOC(m_gc_lfmp_thread_ct, NULL, max_thread); + gc_mem->thread_data = M_MEMORY_REALLOC(m_gc_lfmp_thread_ct, NULL, 0, max_thread); if (M_UNLIKELY_NOMEM (gc_mem->thread_data == NULL)) { M_MEMORY_FULL(max_thread * sizeof(m_gc_lfmp_thread_ct)); return; @@ -608,7 +608,7 @@ m_gc_clear(m_gc_t gc_mem) for(m_gc_tid_t i = 0; i < gc_mem->max_thread;i++) { m_core_backoff_clear(gc_mem->thread_data[i].bkoff); } - M_MEMORY_FREE(gc_mem->thread_data); + M_MEMORY_FREE(m_gc_lfmp_thread_ct, gc_mem->thread_data, gc_mem->max_thread); gc_mem->thread_data = NULL; m_genint_clear(gc_mem->thread_alloc); } @@ -757,7 +757,7 @@ m_vlapool_init(m_vlapool_t mem, m_gc_t gc_mem) const size_t max_thread = gc_mem->max_thread; /* Initialize the thread data of the vlapool */ - mem->thread_data = M_MEMORY_REALLOC(m_vlapool_lfmp_thread_ct, NULL, max_thread); + mem->thread_data = M_MEMORY_REALLOC(m_vlapool_lfmp_thread_ct, NULL, 0, max_thread); if (M_UNLIKELY_NOMEM (mem->thread_data == NULL)) { M_MEMORY_FULL(max_thread * sizeof(m_vlapool_lfmp_thread_ct)); return; @@ -784,7 +784,7 @@ m_vlapool_clear(m_vlapool_t mem) for(unsigned i = 0; i < max_thread;i++) { m_vlapool_lfmp_thread_clear(&mem->thread_data[i]); } - M_MEMORY_FREE(mem->thread_data); + M_MEMORY_FREE(m_vlapool_lfmp_thread_ct, mem->thread_data, mem->gc_mem->max_thread); mem->thread_data = NULL; m_vlapool_lflist_clear(mem->empty); M_ASSERT(m_vlapool_lflist_empty_p(mem->to_be_reclaimed)); @@ -807,7 +807,7 @@ m_vlapool_new(m_vlapool_t mem, m_gc_tid_t id, size_t size) size += offsetof(struct m_vlapool_slist_node_s, data); // Simply wrap around a system call to get the memory - char *ptr = M_MEMORY_REALLOC(char, NULL, size); + char *ptr = M_MEMORY_REALLOC(char, NULL, 0, size); return (ptr == NULL) ? NULL : M_ASSIGN_CAST(void *, ptr + offsetof(struct m_vlapool_slist_node_s, data)); } diff --git a/m-core.h b/m-core.h index 64537ff5..65f5e0e8 100644 --- a/m-core.h +++ b/m-core.h @@ -379,21 +379,21 @@ M_BEGIN_PROTECTED_CODE #endif /* Define allocators for array - * void *M_MEMORY_REALLOC(type, ptr, n): Return a pointer to a new array of 'n' object of type 'type' - * If ptr is NULL, it creates a new array. - * If ptr is not null, it reallocates the given array to the new size. + * void *M_MEMORY_REALLOC(type, ptr, o, n): Return a pointer to a new array of 'n' object of type 'type' + * If ptr is NULL, it creates a new array of type 'type' and size 'n' + * If ptr is not null, it reallocates the given array of type 'type' and size 'o' to the new size. * It returns NULL in case of memory allocation failure. - * void M_MEMORY_FREE(ptr): Free the object associated to the array. + * void M_MEMORY_FREE(type, ptr, o): Free the object associated to the array of type 'type' at base 'ptr' of size 'o'. */ #ifndef M_MEMORY_REALLOC #ifdef __cplusplus # include -# define M_MEMORY_REALLOC(type, ptr, n) \ +# define M_MEMORY_REALLOC(type, ptr, o, n) \ ((type*) (M_UNLIKELY ((n) > SIZE_MAX / sizeof(type)) ? NULL : std::realloc ((ptr), (n)*sizeof (type)))) -# define M_MEMORY_FREE(ptr) std::free(ptr) +# define M_MEMORY_FREE(type, ptr, o) std::free(ptr) #else -# define M_MEMORY_REALLOC(type, ptr, n) (M_UNLIKELY ((n) > SIZE_MAX / sizeof(type)) ? NULL : realloc ((ptr), (n)*sizeof (type))) -# define M_MEMORY_FREE(ptr) free(ptr) +# define M_MEMORY_REALLOC(type, ptr, o, n) (M_UNLIKELY ((n) > SIZE_MAX / sizeof(type)) ? NULL : realloc ((ptr), (n)*sizeof (type))) +# define M_MEMORY_FREE(type, ptr, o) free(ptr) #endif #endif @@ -3942,8 +3942,8 @@ M_INLINE size_t m_core_cstr_hash(const char str[]) #define M_FALSE_DEFAULT(...) false #define M_NEW_DEFAULT(a) M_MEMORY_ALLOC(a) #define M_DEL_DEFAULT(a) M_MEMORY_DEL(a) -#define M_REALLOC_DEFAULT(t,p,s) M_MEMORY_REALLOC(t,p,s) -#define M_FREE_DEFAULT(a) M_MEMORY_FREE(a) +#define M_REALLOC_DEFAULT(t,p,o,s) M_MEMORY_REALLOC(t,p,o,s) +#define M_FREE_DEFAULT(t,a,o) M_MEMORY_FREE(t,a,o) #define M_EQUAL_DEFAULT(a,b) ((a) == (b)) #define M_CMP_DEFAULT(a,b) ((a) < (b) ? -1 : (a) > (b)) #define M_ADD_DEFAULT(a,b,c) ((a) = (b) + (c)) diff --git a/m-deque.h b/m-deque.h index d21bcb19..54a7c898 100644 --- a/m-deque.h +++ b/m-deque.h @@ -127,7 +127,12 @@ The interfaces are compatible. \ */ \ /* FIXME: How can I separate public types and private implementation? */ \ - M_ILIST_DEF(M_F(name, _node_list), node_t, (DEL(M_GET_FREE oplist)) ) \ + static inline void M_F(name, _del_node)(node_t *ptr) \ + { \ + /* FIXME: ptr->size can be incorrect as old size ! */ \ + M_CALL_FREE(oplist, char, ptr, sizeof(node_t) + ptr->size * sizeof(type)); \ + } \ + M_ILIST_DEF(M_F(name, _node_list), node_t, (DEL(M_F(name, _del_node))) ) \ \ /* Define an internal iterator */ \ typedef struct M_F(name, _it2_s) { \ @@ -182,7 +187,7 @@ } \ /* Alloc a new node with dynamic size */ \ node_t*n = (node_t*) (void*) \ - M_CALL_REALLOC(oplist, char, NULL, \ + M_CALL_REALLOC(oplist, char, NULL, 0, \ sizeof(node_t) + def * sizeof(type) ); \ if (M_UNLIKELY_NOMEM (n==NULL)) { \ M_MEMORY_FULL(sizeof(node_t)+def * sizeof(type)); \ @@ -711,7 +716,8 @@ /* Node deletion */ \ M_ASSERT(d->count > 1); \ M_F(name, _node_list_unlink)(n); \ - M_CALL_FREE(oplist, n); \ + /* FIXME: oldsize broken as size is corrupted! */ \ + M_CALL_FREE(oplist, char, n, sizeof(node_t) + n->size * sizeof(type) ); \ } else { \ memmove(&n->data[it->index], &n->data[it->index+1], \ sizeof(type) * (it->node->size - it->index - 1)); \ diff --git a/m-dict.h b/m-dict.h index 05e4f06c..f988a258 100644 --- a/m-dict.h +++ b/m-dict.h @@ -337,14 +337,14 @@ M_ARRAY_DEF(m_array_index, m_indexhash_t, M_POD_OPLIST) map->count_delete = 0; \ M_C3(m_d1ct_,name,_update_limit)(map, M_D1CT_INITIAL_SIZE); \ /* The first 2 buckets are reserved for (empty) and (deleted) access. Allocation could be avoided */ \ - map->data = M_CALL_REALLOC(key_oplist, M_F(name, _freelist_ct), NULL, (size_t) 1+2+map->upper_limit); \ + map->data = M_CALL_REALLOC(key_oplist, M_F(name, _freelist_ct), NULL, 0, (size_t) 1+2+map->upper_limit); \ if (M_UNLIKELY_NOMEM (map->data == NULL)) { \ M_MEMORY_FULL( (2+M_D1CT_INITIAL_SIZE) * sizeof (M_F(name, _freelist_ct))); \ return ; \ } \ map->freelist_first_data = 0; \ map->freelist_count = 2; \ - map->index = M_CALL_REALLOC(key_oplist, m_indexhash_t, NULL, (size_t)(0+M_D1CT_INITIAL_SIZE)); \ + map->index = M_CALL_REALLOC(key_oplist, m_indexhash_t, NULL, 0, (size_t)(0+M_D1CT_INITIAL_SIZE)); \ if (M_UNLIKELY_NOMEM (map->index == NULL)) { \ M_MEMORY_FULL( (2+M_D1CT_INITIAL_SIZE) * sizeof (m_indexhash_t)); \ return ; \ @@ -371,8 +371,9 @@ M_ARRAY_DEF(m_array_index, m_indexhash_t, M_POD_OPLIST) M_CALL_CLEAR(value_oplist, map->data[d].pair.value); \ } \ } \ - M_CALL_FREE(key_oplist, map->index); \ - M_CALL_FREE(key_oplist, map->data); \ + M_CALL_FREE(key_oplist, m_indexhash_t, map->index, map->mask+1); \ + /* FIXME: How much? */ \ + M_CALL_FREE(key_oplist, M_F(name, _freelist_ct), map->data, -1); \ /* Mark the dictionary as cleared */ \ map->data = NULL; \ map->index = NULL; \ @@ -471,13 +472,14 @@ M_ARRAY_DEF(m_array_index, m_indexhash_t, M_POD_OPLIST) /* We need to allocate '2' for the dummy first two entries in the table (not used), \ then we can have at maximum only up to 'upper_limit+1' data */ \ if (1+2+h->upper_limit > h->freelist_count) { \ - h->data = M_CALL_REALLOC(key_oplist, M_F(name, _freelist_ct), h->data, (size_t) 1+2+h->upper_limit); \ + /*FIXME; oldsize incorrect */ \ + h->data = M_CALL_REALLOC(key_oplist, M_F(name, _freelist_ct), h->data, -1, (size_t) 1+2+h->upper_limit); \ if (M_UNLIKELY_NOMEM (h->data == NULL) ) { \ M_MEMORY_FULL((2+newSize)*sizeof (M_F(name, _freelist_ct))); \ return ; \ } \ } \ - m_indexhash_t *index = M_MEMORY_REALLOC(m_indexhash_t, h->index,(size_t) 0+newSize); \ + m_indexhash_t *index = M_CALL_REALLOC(key_oplist, m_indexhash_t, h->index, oldSize, (size_t)0+newSize); \ if (M_UNLIKELY_NOMEM (index == NULL) ) { \ M_MEMORY_FULL(newSize*sizeof(m_indexhash_t)); \ return ; \ @@ -599,7 +601,7 @@ M_ARRAY_DEF(m_array_index, m_indexhash_t, M_POD_OPLIST) if (newSize != oldSize) { \ h->mask = newSize-1; \ M_C3(m_d1ct_,name,_update_limit)(h, newSize); \ - h->index = M_CALL_REALLOC(key_oplist, m_indexhash_t, h->index, (size_t)0+newSize); \ + h->index = M_CALL_REALLOC(key_oplist, m_indexhash_t, h->index, oldSize, (size_t)0+newSize); \ M_ASSERT (h->index != NULL); \ /* FIXME: What to do for h->data ? */ \ } \ @@ -1485,7 +1487,7 @@ enum m_d1ct_oa_element_e { dict->count = 0; \ dict->count_delete = 0; \ M_C3(m_d1ct_,name,_update_limit)(dict, M_D1CT_INITIAL_SIZE); \ - dict->data = M_CALL_REALLOC(key_oplist, M_F(name, _pair_ct), NULL, M_D1CT_INITIAL_SIZE); \ + dict->data = M_CALL_REALLOC(key_oplist, M_F(name, _pair_ct), NULL, 0, M_D1CT_INITIAL_SIZE); \ if (M_UNLIKELY_NOMEM (dict->data == NULL)) { \ M_MEMORY_FULL(sizeof (M_F(name, _pair_ct)) * M_D1CT_INITIAL_SIZE); \ return ; \ @@ -1509,7 +1511,7 @@ enum m_d1ct_oa_element_e { M_CALL_CLEAR(value_oplist, dict->data[i].value); \ } \ } \ - M_CALL_FREE(key_oplist, dict->data); \ + M_CALL_FREE(key_oplist, M_F(name, _pair_ct), dict->data, dict->mask+1); \ /* Not really needed, but safer */ \ dict->mask = 0; \ dict->data = NULL; \ @@ -1566,7 +1568,7 @@ enum m_d1ct_oa_element_e { M_F(name, _pair_ct) *data = h->data; \ /* resize can be called just to delete the items */ \ if (newSize > oldSize) { \ - data = M_CALL_REALLOC(key_oplist, M_F(name, _pair_ct), data, newSize); \ + data = M_CALL_REALLOC(key_oplist, M_F(name, _pair_ct), data, oldSize, newSize); \ if (M_UNLIKELY_NOMEM (data == NULL) ) { \ M_MEMORY_FULL(sizeof (M_F(name, _pair_ct)) * newSize); \ return ; \ @@ -1833,7 +1835,7 @@ enum m_d1ct_oa_element_e { if (newSize != oldSize) { \ h->mask = newSize-1; \ M_C3(m_d1ct_,name,_update_limit)(h, newSize); \ - h->data = M_CALL_REALLOC(key_oplist, M_F(name, _pair_ct), data, newSize); \ + h->data = M_CALL_REALLOC(key_oplist, M_F(name, _pair_ct), data, oldSize, newSize); \ M_ASSERT (h->data != NULL); \ } \ M_IF_DEBUG (M_ASSERT (M_C3(m_d1ct_,name,_control_after_resize)(h));) \ diff --git a/m-genint.h b/m-genint.h index 24f1138d..1bacf991 100644 --- a/m-genint.h +++ b/m-genint.h @@ -109,7 +109,7 @@ m_genint_init(m_genint_t s, unsigned int n) M_ASSERT (s != NULL && n > 0 && n <= M_GENINT_MAX_ALLOC); const size_t alloc = (n + M_GEN1NT_LIMBSIZE - 1) / M_GEN1NT_LIMBSIZE; const unsigned int index = n % M_GEN1NT_LIMBSIZE; - atomic_ullong *ptr = M_MEMORY_REALLOC (atomic_ullong, NULL, alloc); + atomic_ullong *ptr = M_MEMORY_REALLOC (atomic_ullong, NULL, 0, alloc); if (M_UNLIKELY_NOMEM (ptr == NULL)) { M_MEMORY_FULL(alloc); return; @@ -130,7 +130,7 @@ M_INLINE void m_genint_clear(m_genint_t s) { M_GEN1NT_CONTRACT(s); - M_MEMORY_FREE(s->data); + M_MEMORY_FREE(atomic_ullong, s->data, s->alloc); s->data = NULL; } diff --git a/m-shared-ptr.h b/m-shared-ptr.h index 401d823f..fe928523 100644 --- a/m-shared-ptr.h +++ b/m-shared-ptr.h @@ -618,7 +618,7 @@ M_IF_METHOD(INIT, oplist)( \ M_MEMORY_FULL(sizeof (shared_t)); \ abort(); \ } \ - M_ON_EXCEPTION( M_CALL_FREE(oplist, out) ) \ + M_ON_EXCEPTION( M_CALL_DEL(oplist, out) ) \ M_CALL_INIT(oplist, out->data); \ M_F(name, _init_lock)(out); \ return out; \ @@ -635,7 +635,7 @@ M_IF_METHOD(INIT_SET, oplist)( \ abort(); \ } \ M_F(name, _read_lock)(src); \ - M_ON_EXCEPTION( M_F(name, _read_unlock)(src), M_CALL_FREE(oplist, out) ) \ + M_ON_EXCEPTION( M_F(name, _read_unlock)(src), M_CALL_DEL(oplist, out) ) \ M_CALL_INIT_SET(oplist, out->data, src->data); \ M_F(name, _read_unlock)(src); \ M_F(name, _init_lock)(out); \ @@ -651,7 +651,7 @@ M_IF_METHOD(INIT_SET, oplist)( \ M_MEMORY_FULL(sizeof (shared_t)); \ abort(); \ } \ - M_ON_EXCEPTION( M_CALL_FREE(oplist, out) ) \ + M_ON_EXCEPTION( M_CALL_DEL(oplist, out) ) \ M_CALL_INIT_SET(oplist, out->data, src); \ M_F(name, _init_lock)(out); \ return out; \ @@ -709,7 +709,7 @@ M_PAIR_2 name_attr shared_t *function_name(M_EMPLACE_LIST_TYPE_VAR_ALTER(a, exp_ M_MEMORY_FULL(sizeof (shared_t)); \ abort(); \ } \ - M_ON_EXCEPTION( M_CALL_FREE(oplist, out) ) \ + M_ON_EXCEPTION( M_CALL_DEL(oplist, out) ) \ M_EMPLACE_CALL_FUNC(a, init_func, oplist, out->data, exp_emplace_type); \ M_F(M_PAIR_1 name_attr, _init_lock)(out); \ return out; \ diff --git a/m-shared.h b/m-shared.h index 1bd4c9b5..6becc2e0 100644 --- a/m-shared.h +++ b/m-shared.h @@ -445,7 +445,7 @@ M_INLINE int m_shar3d_integer_cref(int *p) { return *p; } { \ M_ASSERT(s != NULL); \ M_ASSERT (n > 0 && n < UINT_MAX); \ - s->buffer = M_CALL_REALLOC(oplist, M_F(name, _atype_ct), NULL, n); \ + s->buffer = M_CALL_REALLOC(oplist, M_F(name, _atype_ct), NULL, 0, n); \ if (M_UNLIKELY_NOMEM (s->buffer == NULL)) { \ M_MEMORY_FULL(sizeof(M_F(name, _atype_ct)) * n); \ return; \ @@ -466,7 +466,7 @@ M_INLINE int m_shar3d_integer_cref(int *p) { return *p; } for(size_t i = 0; i < n; i++) { \ M_CALL_CLEAR(oplist, s->buffer[i].x); \ } \ - M_CALL_FREE(oplist, s->buffer); \ + M_CALL_FREE(oplist, M_F(name, _atype_ct), s->buffer, n); \ s->buffer = NULL; \ m_genint_clear(s->core); \ } \ diff --git a/m-snapshot.h b/m-snapshot.h index afb69262..fd03fc52 100644 --- a/m-snapshot.h +++ b/m-snapshot.h @@ -391,7 +391,7 @@ m_snapsh0t_mrsw_init(m_snapsh0t_mrsw_ct s, size_t n) n += M_SNAPSH0T_SPMC_EXTRA_BUFFER; // Initialize the counters to zero (no reader use it) - atomic_uint *ptr = M_MEMORY_REALLOC (atomic_uint, NULL, n); + atomic_uint *ptr = M_MEMORY_REALLOC (atomic_uint, NULL, 0, n); if (M_UNLIKELY_NOMEM (ptr == NULL)) { M_MEMORY_FULL(sizeof (atomic_uint) * n); return; @@ -419,7 +419,7 @@ M_INLINE void m_snapsh0t_mrsw_clear(m_snapsh0t_mrsw_ct s) { M_SNAPSH0T_SPMC_INT_CONTRACT(s); - M_MEMORY_FREE (s->cptTab); + M_MEMORY_FREE (atomic_uint, s->cptTab, s->n_reader-M_SNAPSH0T_SPMC_EXTRA_BUFFER); m_genint_clear(s->freeList); s->cptTab = NULL; s->n_reader = 0; @@ -645,7 +645,7 @@ m_snapsh0t_mrsw_read_end(m_snapsh0t_mrsw_ct s, unsigned int idx) M_ASSERT (snap != NULL); \ M_ASSERT (nReader > 0 && nReader <= M_SNAPSH0T_SPMC_MAX_READER); \ snap->data = M_CALL_REALLOC(oplist, M_F(name, _aligned_type_ct), \ - NULL, nReader+M_SNAPSH0T_SPMC_EXTRA_BUFFER); \ + NULL, 0, nReader+M_SNAPSH0T_SPMC_EXTRA_BUFFER); \ if (M_UNLIKELY_NOMEM (snap->data == NULL)) { \ M_MEMORY_FULL(sizeof(M_F(name, _aligned_type_ct)) * \ (nReader+M_SNAPSH0T_SPMC_EXTRA_BUFFER)); \ @@ -666,7 +666,7 @@ m_snapsh0t_mrsw_read_end(m_snapsh0t_mrsw_ct s, unsigned int idx) for(size_t i = 0; i < nReader + M_SNAPSH0T_SPMC_EXTRA_BUFFER; i++) { \ M_CALL_CLEAR(oplist, snap->data[i].x); \ } \ - M_CALL_FREE(oplist, snap->data); \ + M_CALL_FREE(oplist, M_F(name, _aligned_type_ct), snap->data, nReader + M_SNAPSH0T_SPMC_EXTRA_BUFFER); \ m_snapsh0t_mrsw_clear(snap->core); \ } \ \ diff --git a/m-string.h b/m-string.h index 836c203b..e4c29078 100644 --- a/m-string.h +++ b/m-string.h @@ -286,7 +286,7 @@ m_string_clear(m_string_t v) { M_STR1NG_CONTRACT(v); if (!m_str1ng_embedded_p(v)) { - M_MEMORY_FREE(v->u.heap.ptr); + M_MEMORY_FREE(char, v->u.heap.ptr, m_string_capacity(v)); v->u.heap.ptr = NULL; } /* This is not needed but is safer to make @@ -311,7 +311,7 @@ m_string_clear_get_cstr(m_string_t v) p = &v->u.buffer[0]; // Need to allocate a heap string to return the copy. size_t alloc = m_string_size(v)+1; - char *ptr = M_MEMORY_REALLOC (char, NULL, alloc); + char *ptr = M_MEMORY_REALLOC (char, NULL, 0, alloc); if (M_UNLIKELY_NOMEM (ptr == NULL)) { M_MEMORY_FULL(sizeof (char) * alloc); return NULL; @@ -392,7 +392,8 @@ m_str1ng_fit2size (m_string_t v, size_t size_alloc) return NULL; } char *ptr = m_str1ng_embedded_p(v) ? NULL : v->u.heap.ptr; - ptr = M_MEMORY_REALLOC (char, ptr, alloc); + //FIXME: old_alloc may not be NULL if ptr is NULL. + ptr = M_MEMORY_REALLOC (char, ptr, old_alloc, alloc); if (M_UNLIKELY_NOMEM (ptr == NULL)) { M_MEMORY_FULL(sizeof (char) * alloc); // NOTE: Return is currently broken. @@ -439,7 +440,7 @@ m_string_reserve(m_string_t v, size_t alloc) char *oldptr = v->u.heap.ptr; memcpy(ptr, oldptr, size+1); // Free the allocated memory on the heap - M_MEMORY_FREE(oldptr); + M_MEMORY_FREE(char, oldptr, m_string_capacity(v)); v->u.buffer[sizeof(m_string_t)-2] = 0; v->u.buffer[sizeof(m_string_t)-1] = (char) size; } else { @@ -455,7 +456,7 @@ m_string_reserve(m_string_t v, size_t alloc) return; } char *ptr = m_str1ng_embedded_p(v) ? NULL : v->u.heap.ptr; - ptr = M_MEMORY_REALLOC (char, ptr, r_alloc); + ptr = M_MEMORY_REALLOC (char, ptr, m_string_capacity(v), r_alloc); if (M_UNLIKELY_NOMEM (ptr == NULL) ) { M_MEMORY_FULL(sizeof (char) * alloc); return; diff --git a/m-tree.h b/m-tree.h index d9ff20ec..929a47c6 100644 --- a/m-tree.h +++ b/m-tree.h @@ -245,7 +245,7 @@ typedef int32_t m_tr33_index_t; M_F(name, _clear)(tree_t tree) { \ M_F(name, _reset)(tree); \ struct M_F(name,_node_s)*ptr = tree->tab == NULL ? NULL : tree->tab-1;\ - M_CALL_FREE(oplist, ptr); \ + M_CALL_FREE(oplist, struct M_F(name, _node_s), ptr, tree->capacity+1); \ /* This is so reusing the object implies an assertion failure */ \ tree->size = 1; \ tree->tab = NULL; \ @@ -268,7 +268,7 @@ typedef int32_t m_tr33_index_t; as M_TR33_NO_NODE is -1. This enables avoiding testing for \ M_TR33_NO_NODE in some cases, performing branchless code. */ \ struct M_F(name,_node_s)*ptr = tree->tab == NULL ? NULL : tree->tab-1;\ - ptr = M_CALL_REALLOC(oplist, struct M_F(name, _node_s), ptr, alloc+1);\ + ptr = M_CALL_REALLOC(oplist, struct M_F(name, _node_s), ptr, tree->capacity+1, alloc+1); \ if (M_UNLIKELY_NOMEM (ptr == NULL) ) { \ M_MEMORY_FULL(sizeof (struct M_F(name, _node_s)) * alloc); \ return; \ @@ -322,7 +322,7 @@ typedef int32_t m_tr33_index_t; as M_TR33_NO_NODE is -1. This enables avoiding testing for \ M_TR33_NO_NODE in some cases, performing branchless code. */ \ struct M_F(name,_node_s)*ptr = tree->tab == NULL ? NULL : tree->tab-1; \ - ptr = M_CALL_REALLOC(oplist, struct M_F(name, _node_s), ptr, alloc+1); \ + ptr = M_CALL_REALLOC(oplist, struct M_F(name, _node_s), ptr, tree->capacity+1, alloc+1); \ if (M_UNLIKELY_NOMEM (ptr == NULL) ) { \ M_MEMORY_FULL(sizeof (struct M_F(name, _node_s)) * alloc); \ return M_TR33_NO_NODE; \ @@ -1134,7 +1134,7 @@ typedef int32_t m_tr33_index_t; tree->tab = NULL; \ } else { \ struct M_F(name, _node_s) *ptr = \ - M_CALL_REALLOC(oplist, struct M_F(name, _node_s), NULL, alloc+1); \ + M_CALL_REALLOC(oplist, struct M_F(name, _node_s), NULL, 0, alloc+1); \ if (M_UNLIKELY_NOMEM (ptr == NULL) ) { \ M_MEMORY_FULL(sizeof(struct M_F(name, _node_s)) * alloc); \ return; \ diff --git a/m-worker.h b/m-worker.h index 26ff02de..e78d213e 100644 --- a/m-worker.h +++ b/m-worker.h @@ -409,7 +409,7 @@ m_worker_init(m_worker_t g, int numWorker, unsigned int extraQueue, void (*reset // numWorker can still be 0 if it is a single core cpu (no worker available) M_ASSERT(numWorker >= 0); size_t numWorker_st = (size_t) numWorker; - g->worker = M_MEMORY_REALLOC(m_work3r_thread_ct, NULL, numWorker_st); + g->worker = M_MEMORY_REALLOC(m_work3r_thread_ct, NULL, 0, numWorker_st); if (M_UNLIKELY_NOMEM (g->worker == NULL)) { M_MEMORY_FULL(sizeof (m_work3r_thread_ct) * numWorker_st); return; @@ -454,7 +454,7 @@ m_worker_clear(m_worker_t g) m_thread_join(g->worker[i].id); } // Clear memory - M_MEMORY_FREE(g->worker); + M_MEMORY_FREE(m_work3r_thread_ct, g->worker, g->numWorker_g); m_mutex_clear(g->lock); m_cond_clear(g->a_thread_ends); m_work3r_queue_clear(g->queue_g); diff --git a/tests/test-obj-except.h b/tests/test-obj-except.h index 512108d7..b09457d5 100644 --- a/tests/test-obj-except.h +++ b/tests/test-obj-except.h @@ -39,14 +39,14 @@ extern int test_obj_except__malloc_counter; # include # define M_MEMORY_ALLOC(type) (test_obj_except_trigger(), test_obj_except__malloc_counter++, (type*)std::malloc (sizeof (type))) # define M_MEMORY_DEL(ptr) (test_obj_except__malloc_counter-=(ptr != NULL), std::free(ptr)) -# define M_MEMORY_REALLOC(type, ptr, n) \ +# define M_MEMORY_REALLOC(type, ptr, o, n) \ ((type*) (test_obj_except_trigger(), test_obj_except__malloc_counter+= (ptr == NULL), std::realloc ((ptr), (n)*sizeof (type)))) -# define M_MEMORY_FREE(ptr) (test_obj_except__malloc_counter-=(ptr != NULL), std::free(ptr)) +# define M_MEMORY_FREE(type, ptr, o) (test_obj_except__malloc_counter-=(ptr != NULL), std::free(ptr)) #else # define M_MEMORY_ALLOC(type) (test_obj_except_trigger(), test_obj_except__malloc_counter++, malloc (sizeof (type))) # define M_MEMORY_DEL(ptr) (test_obj_except__malloc_counter-=(ptr != NULL), free(ptr)) -# define M_MEMORY_REALLOC(type, ptr, n) (test_obj_except_trigger(), test_obj_except__malloc_counter+=(ptr == NULL), realloc ((ptr), (n)*sizeof (type))) -# define M_MEMORY_FREE(ptr) (test_obj_except__malloc_counter-=(ptr != NULL), free(ptr)) +# define M_MEMORY_REALLOC(type, ptr, o, n) (test_obj_except_trigger(), test_obj_except__malloc_counter+=(ptr == NULL), realloc ((ptr), (n)*sizeof (type))) +# define M_MEMORY_FREE(type, ptr, o) (test_obj_except__malloc_counter-=(ptr != NULL), free(ptr)) #endif // Include M*LIB headers