From ee09c3627dee4eb2ec87fda9783be1bb15e132d9 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Sun, 21 Nov 2021 23:17:24 +0100 Subject: BREAKING CHANGE: Replaced c_new(T) with c_new(T, ...). This now is similar to the c++ new operator. The previous c_new(T) is renamed to c_alloc(T), and c_new_n(T,n) => c_alloc_n(T,n). Old usage of c_new() will fail as it requires additional argument. Sorry for the inconvenience. --- include/stc/alt/clist.h | 2 +- include/stc/alt/csmap.h | 4 ++-- include/stc/carr2.h | 4 ++-- include/stc/carr3.h | 4 ++-- include/stc/ccommon.h | 15 ++++++++------- include/stc/clist.h | 2 +- include/stc/cmap.h | 4 ++-- include/stc/csptr.h | 4 ++-- 8 files changed, 20 insertions(+), 19 deletions(-) (limited to 'include/stc') diff --git a/include/stc/alt/clist.h b/include/stc/alt/clist.h index ea1bb9a5..6695a26e 100644 --- a/include/stc/alt/clist.h +++ b/include/stc/alt/clist.h @@ -294,7 +294,7 @@ STC_API size_t _clist_count(const clist_VOID* self); #define _c_clist_insert_after(self, _cx_self, node, val) \ - _cx_node *entry = c_new (_cx_node); \ + _cx_node *entry = c_alloc (_cx_node); \ if (node) entry->next = node->next, node->next = entry; \ else entry->next = entry; \ entry->value = val diff --git a/include/stc/alt/csmap.h b/include/stc/alt/csmap.h index 699e18c1..0e1650ee 100644 --- a/include/stc/alt/csmap.h +++ b/include/stc/alt/csmap.h @@ -299,7 +299,7 @@ static csmap_SENTINEL_node _aatree_sentinel = {&_aatree_sentinel, &_aatree_senti if (!(c = i_cmp(&r, rkey))) {res->ref = &tx->value; return tn; } \ tx = tx->link[(dir = (c < 0))]; \ } \ - tn = c_new(_cx_node); \ + tn = c_alloc(_cx_node); \ res->ref = &tn->value, res->inserted = true; \ tn->link[0] = tn->link[1] = (_cx_node*) &_aatree_sentinel, tn->level = 1; \ if (top == 0) return tn; \ @@ -382,7 +382,7 @@ static csmap_SENTINEL_node _aatree_sentinel = {&_aatree_sentinel, &_aatree_senti STC_DEF _cx_node* \ _cx_memb(_clone_r_)(_cx_node *tn) { \ if (! tn->level) return tn; \ - _cx_node *cn = c_new(_cx_node); \ + _cx_node *cn = c_alloc(_cx_node); \ cn->link[0] = _cx_memb(_clone_r_)(tn->link[0]); \ cn->link[1] = _cx_memb(_clone_r_)(tn->link[1]); \ cn->level = tn->level; \ diff --git a/include/stc/carr2.h b/include/stc/carr2.h index cda5a3b0..439a5c2d 100644 --- a/include/stc/carr2.h +++ b/include/stc/carr2.h @@ -68,7 +68,7 @@ STC_API _cx_value* _cx_memb(_release)(_cx_self* self); STC_API void _cx_memb(_del)(_cx_self* self); STC_INLINE _cx_self _cx_memb(_init)(size_t xdim, size_t ydim) { - return _cx_memb(_with_storage)(xdim, ydim, c_new_n(_cx_value, xdim*ydim)); + return _cx_memb(_with_storage)(xdim, ydim, c_alloc_n(_cx_value, xdim*ydim)); } STC_INLINE size_t _cx_memb(_size)(_cx_self arr) { return arr.xdim*arr.ydim; } @@ -103,7 +103,7 @@ STC_INLINE void _cx_memb(_next)(_cx_iter* it) #if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION) || defined(i_imp) STC_DEF _cx_self _cx_memb(_with_storage)(size_t xdim, size_t ydim, _cx_value* block) { - _cx_self _arr = {c_new_n(_cx_value*, xdim), xdim, ydim}; + _cx_self _arr = {c_alloc_n(_cx_value*, xdim), xdim, ydim}; for (size_t x = 0; x < xdim; ++x, block += ydim) _arr.data[x] = block; return _arr; diff --git a/include/stc/carr3.h b/include/stc/carr3.h index d8c79c64..80e1ef0f 100644 --- a/include/stc/carr3.h +++ b/include/stc/carr3.h @@ -69,7 +69,7 @@ STC_API _cx_value* _cx_memb(_release)(_cx_self* self); STC_API void _cx_memb(_del)(_cx_self* self); STC_INLINE _cx_self _cx_memb(_init)(size_t xdim, size_t ydim, size_t zdim) { - return _cx_memb(_with_storage)(xdim, ydim, zdim, c_new_n(_cx_value, xdim*ydim*zdim)); + return _cx_memb(_with_storage)(xdim, ydim, zdim, c_alloc_n(_cx_value, xdim*ydim*zdim)); } STC_INLINE size_t _cx_memb(_size)(_cx_self arr) @@ -105,7 +105,7 @@ STC_INLINE void _cx_memb(_next)(_cx_iter* it) #if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION) || defined(i_imp) STC_DEF _cx_self _cx_memb(_with_storage)(size_t xdim, size_t ydim, size_t zdim, _cx_value* block) { - _cx_self _arr = {c_new_n(_cx_value**, xdim*(ydim + 1)), xdim, ydim, zdim}; + _cx_self _arr = {c_alloc_n(_cx_value**, xdim*(ydim + 1)), xdim, ydim, zdim}; _cx_value** p = (_cx_value**) &_arr.data[xdim]; for (size_t x = 0, y; x < xdim; ++x, p += ydim) for (_arr.data[x] = p, y = 0; y < ydim; ++y, block += zdim) diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index 4f97344c..df2cd438 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -74,17 +74,18 @@ ((type *)((char *)(ptr) - offsetof(type, member))) #ifndef __cplusplus -# define c_new(T) c_malloc(sizeof(T)) -# define c_new_n(T, n) c_malloc(sizeof(T)*(n)) +# define c_alloc(T) c_malloc(sizeof(T)) +# define c_alloc_n(T, n) c_malloc(sizeof(T)*(n)) # define c_make(T) (T) -# define c_make_ptr(T, ...) memcpy(c_new(T), (T[]){__VA_ARGS__}, sizeof(T)) +# define c_new(T, ...) memcpy(c_alloc(T), (T[]){__VA_ARGS__}, sizeof(T)) #else # include -# define c_new(T) static_cast(c_malloc(sizeof(T))) -# define c_new_n(T, n) static_cast(c_malloc(sizeof(T)*(n))) +# define c_alloc(T) static_cast(c_malloc(sizeof(T))) +# define c_alloc_n(T, n) static_cast(c_malloc(sizeof(T)*(n))) # define c_make(T) T -# define c_make_ptr(T, ...) new (c_new(T)) T{__VA_ARGS__} +# define c_new(T, ...) new (c_alloc(T)) T(__VA_ARGS__) #endif +#define c_delete(T, ptr) do { T* _p = ptr; T##_del(_p); c_free(_p); } while(0) #ifndef c_malloc # define c_malloc(sz) malloc(sz) # define c_calloc(n, sz) calloc(n, sz) @@ -169,7 +170,7 @@ STC_INLINE uint64_t c_default_hash(const void* key, size_t len) { #define c_autobuf(b, type, n) c_autobuf_N(b, type, n, 256) #define c_autobuf_N(b, type, n, BYTES) \ for (type _c_b[((BYTES) - 1) / sizeof(type) + 1], \ - *b = (n)*sizeof *b > (BYTES) ? c_new_n(type, n) : _c_b \ + *b = (n)*sizeof *b > (BYTES) ? c_alloc_n(type, n) : _c_b \ ; b; b != _c_b ? c_free(b) : (void)0, b = NULL) #define c_apply(CX, method, cx, ...) do { \ diff --git a/include/stc/clist.h b/include/stc/clist.h index 037d5104..247086a1 100644 --- a/include/stc/clist.h +++ b/include/stc/clist.h @@ -70,7 +70,7 @@ _c_clist_types(clist_VOID, int); _c_clist_complete_types(clist_VOID, dummy); #define _c_clist_insert_after(self, _cx_self, node, val) \ - _cx_node *entry = c_new (_cx_node); \ + _cx_node *entry = c_alloc(_cx_node); \ if (node) entry->next = node->next, node->next = entry; \ else entry->next = entry; \ entry->value = val diff --git a/include/stc/cmap.h b/include/stc/cmap.h index 56ea32a3..5feff0c3 100644 --- a/include/stc/cmap.h +++ b/include/stc/cmap.h @@ -308,7 +308,7 @@ _cx_memb(_insert_entry_)(_cx_self* self, i_keyraw rkey) { STC_DEF _cx_self _cx_memb(_clone)(_cx_self m) { _cx_self clone = { - c_new_n(_cx_value, m.bucket_count), + c_alloc_n(_cx_value, m.bucket_count), (uint8_t *) memcpy(c_malloc(m.bucket_count + 1), m._hashx, m.bucket_count + 1), m.size, m.bucket_count, m.max_load_factor @@ -325,7 +325,7 @@ _cx_memb(_reserve)(_cx_self* self, const size_t _newcap) { const _cx_size _oldbuckets = self->bucket_count; const _cx_size _nbuckets = ((_cx_size)(_newcap/self->max_load_factor) + 2) | 1; _cx_self _tmp = { - c_new_n(_cx_value, _nbuckets), + c_alloc_n(_cx_value, _nbuckets), (uint8_t *) c_calloc(_nbuckets + 1, sizeof(uint8_t)), self->size, (_cx_size) _nbuckets, self->max_load_factor diff --git a/include/stc/csptr.h b/include/stc/csptr.h index 5db626a4..6bcd8a56 100644 --- a/include/stc/csptr.h +++ b/include/stc/csptr.h @@ -102,13 +102,13 @@ _cx_memb(_use_count)(_cx_self ptr) { return ptr.use_count ? *ptr.use_count : 0; STC_INLINE _cx_self _cx_memb(_from)(_cx_value* p) { _cx_self ptr = {p}; - if (p) *(ptr.use_count = c_new(atomic_count_t)) = 1; + if (p) *(ptr.use_count = c_alloc(atomic_count_t)) = 1; return ptr; } STC_INLINE _cx_self _cx_memb(_make)(_cx_value val) { - _cx_self ptr; cx_csptr_rep *rep = c_new(cx_csptr_rep); + _cx_self ptr; cx_csptr_rep *rep = c_alloc(cx_csptr_rep); *(ptr.use_count = &rep->counter) = 1; *(ptr.get = &rep->value) = val; return ptr; -- cgit v1.2.3