From 50e8d1f4696df0579aa4e2b997dface617b7e743 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Mon, 23 Dec 2024 22:30:08 +0100 Subject: [PATCH] Fix old size parameter for memory allocation --- m-buffer.h | 4 ++-- m-deque.h | 9 ++++----- m-dict.h | 13 ++++++------- m-genint.h | 2 +- m-snapshot.h | 2 +- m-tree.h | 6 +++--- 6 files changed, 17 insertions(+), 19 deletions(-) diff --git a/m-buffer.h b/m-buffer.h index 16936596..16c23780 100644 --- a/m-buffer.h +++ b/m-buffer.h @@ -940,7 +940,7 @@ M_F(name, _init)(buffer_t v, size_t size) \ j = 0; \ } \ } \ - M_CALL_FREE(oplist, M_F(name, _el_ct), buffer->Tab, M_BUFF3R_SIZE(m_size)); \ + M_CALL_FREE(oplist, M_F(name, _el_ct), buffer->Tab, buffer->size); \ buffer->Tab = NULL; /* safer */ \ buffer->size = 3; \ } \ @@ -1281,7 +1281,7 @@ M_F(name, _init)(buffer_t v, size_t size) \ j = 0; \ } \ } \ - M_CALL_FREE(oplist, M_F(name, _el_ct), buffer->Tab, M_BUFF3R_SIZE(m_size)); \ + M_CALL_FREE(oplist, M_F(name, _el_ct), buffer->Tab, buffer->size); \ buffer->Tab = NULL; /* safer */ \ buffer->size = 3; \ } \ diff --git a/m-deque.h b/m-deque.h index 268436ea..2bb79e1a 100644 --- a/m-deque.h +++ b/m-deque.h @@ -116,7 +116,7 @@ \ typedef struct M_F(name, _node_s) { \ M_ILIST_INTERFACE(M_F(name, _node_list), struct M_F(name, _node_s)); \ - size_t size; \ + size_t size, capacity; \ type data[M_MIN_FLEX_ARRAY_SIZE]; \ } node_t; \ \ @@ -129,8 +129,7 @@ /* FIXME: How can I separate public types and private implementation? */ \ 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_CALL_FREE(oplist, char, ptr, sizeof(node_t) + ptr->capacity * sizeof(type)); \ } \ M_ILIST_DEF(M_F(name, _node_list), node_t, (DEL(M_F(name, _del_node))) ) \ \ @@ -196,6 +195,7 @@ } \ /* Initialize the node */ \ n->size = def; \ + n->capacity = def; \ M_F(name, _node_list_init_field)(n); \ /* Increase the next bucket allocation */ \ /* Do not increase it too much if there are few items */ \ @@ -716,8 +716,7 @@ /* Node deletion */ \ M_ASSERT(d->count > 1); \ M_F(name, _node_list_unlink)(n); \ - /* FIXME: oldsize broken as size is corrupted! */ \ - M_CALL_FREE(oplist, char, n, sizeof(node_t) + n->size * sizeof(type) ); \ + M_CALL_FREE(oplist, char, n, sizeof(node_t) + n->capacity * 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 e7ff592d..cfec705f 100644 --- a/m-dict.h +++ b/m-dict.h @@ -296,7 +296,7 @@ M_ARRAY_DEF(m_array_index, m_indexhash_t, M_POD_OPLIST) m_index_t count, count_delete; \ m_index_t upper_limit, lower_limit; \ m_index_t mask; \ - m_index_t freelist_first_data, freelist_count; \ + m_index_t freelist_first_data, freelist_count, freelist_cap; \ m_indexhash_t *index; \ M_F(name, _freelist_ct) *data; \ } dict_t[1]; \ @@ -344,6 +344,7 @@ M_ARRAY_DEF(m_array_index, m_indexhash_t, M_POD_OPLIST) } \ map->freelist_first_data = 0; \ map->freelist_count = 2; \ + map->freelist_cap = 1+2+map->upper_limit; \ 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(m_indexhash_t, 2+M_D1CT_INITIAL_SIZE); \ @@ -372,8 +373,7 @@ M_ARRAY_DEF(m_array_index, m_indexhash_t, M_POD_OPLIST) } \ } \ 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); \ + M_CALL_FREE(key_oplist, M_F(name, _freelist_ct), map->data, map->freelist_cap); \ /* Mark the dictionary as cleared */ \ map->data = NULL; \ map->index = NULL; \ @@ -467,17 +467,16 @@ M_ARRAY_DEF(m_array_index, m_indexhash_t, M_POD_OPLIST) if (newSize > oldSize) { \ /* h->data is not always the same size than h->index: it may be bigger \ if there is some erase of items: h->index will shrink, but not h->data \ - To have an estimate of the size, we look at the maximum index of it: \ if it is bigger than our new size, then we don't need to reallocate */ \ /* 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) { \ - /*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 (1+2+h->upper_limit > h->freelist_cap) { \ + h->data = M_CALL_REALLOC(key_oplist, M_F(name, _freelist_ct), h->data, h->freelist_cap, (size_t) 1+2+h->upper_limit); \ if (M_UNLIKELY_NOMEM (h->data == NULL) ) { \ M_MEMORY_FULL(M_F(name, _freelist_ct), 2+newSize); \ return ; \ } \ + h->freelist_cap = 1+2+h->upper_limit; \ } \ 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) ) { \ diff --git a/m-genint.h b/m-genint.h index 2c4e1f19..b19596c2 100644 --- a/m-genint.h +++ b/m-genint.h @@ -130,7 +130,7 @@ M_INLINE void m_genint_clear(m_genint_t s) { M_GEN1NT_CONTRACT(s); - M_MEMORY_FREE(atomic_ullong, s->data, s->alloc); + M_MEMORY_FREE(atomic_ullong, s->data, s->max+1); s->data = NULL; } diff --git a/m-snapshot.h b/m-snapshot.h index eb7dfd43..3ebfc379 100644 --- a/m-snapshot.h +++ b/m-snapshot.h @@ -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 (atomic_uint, s->cptTab, s->n_reader-M_SNAPSH0T_SPMC_EXTRA_BUFFER); + 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; diff --git a/m-tree.h b/m-tree.h index 7e4f0ca9..c767f118 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, struct M_F(name, _node_s), ptr, tree->capacity+1); \ + M_CALL_FREE(oplist, struct M_F(name, _node_s), ptr, (size_t)(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, tree->capacity+1, alloc+1); \ + ptr = M_CALL_REALLOC(oplist, struct M_F(name, _node_s), ptr, (size_t)(tree->capacity+1), alloc+1); \ if (M_UNLIKELY_NOMEM (ptr == NULL) ) { \ M_MEMORY_FULL(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, tree->capacity+1, alloc+1); \ + ptr = M_CALL_REALLOC(oplist, struct M_F(name, _node_s), ptr, (size_t)(tree->capacity+1), alloc+1); \ if (M_UNLIKELY_NOMEM (ptr == NULL) ) { \ M_MEMORY_FULL(struct M_F(name, _node_s), alloc); \ return M_TR33_NO_NODE; \