summaryrefslogtreecommitdiffhomepage
path: root/include/stc
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-02-07 12:30:39 +0100
committerTyge Løvset <[email protected]>2023-02-07 12:30:39 +0100
commitca54204557669fb54f43959594ee92109fcc75b6 (patch)
tree9880ad3d7e3dca7e309e0b96f5c88ebbb0552853 /include/stc
parent3cb564e7274b7749531742fa7c50fd928fdbe2d9 (diff)
downloadSTC-modified-ca54204557669fb54f43959594ee92109fcc75b6.tar.gz
STC-modified-ca54204557669fb54f43959594ee92109fcc75b6.zip
Added custom allocator per container type.
Diffstat (limited to 'include/stc')
-rw-r--r--include/stc/carc.h8
-rw-r--r--include/stc/cbits.h2
-rw-r--r--include/stc/cbox.h6
-rw-r--r--include/stc/ccommon.h25
-rw-r--r--include/stc/cdeq.h6
-rw-r--r--include/stc/clist.h4
-rw-r--r--include/stc/cmap.h18
-rw-r--r--include/stc/cpque.h4
-rw-r--r--include/stc/csmap.h4
-rw-r--r--include/stc/cspan.h4
-rw-r--r--include/stc/cstack.h10
-rw-r--r--include/stc/cvec.h4
-rw-r--r--include/stc/priv/altnames.h5
-rw-r--r--include/stc/priv/template.h16
14 files changed, 66 insertions, 50 deletions
diff --git a/include/stc/carc.h b/include/stc/carc.h
index 5bf4ebc2..82f8835d 100644
--- a/include/stc/carc.h
+++ b/include/stc/carc.h
@@ -101,14 +101,14 @@ STC_INLINE long _cx_memb(_use_count)(const _cx_self* self)
STC_INLINE _cx_self _cx_memb(_from_ptr)(_cx_value* p) {
_cx_self ptr = {p};
if (p)
- *(ptr.use_count = c_ALLOC(catomic_long)) = 1;
+ *(ptr.use_count = _i_alloc(catomic_long)) = 1;
return ptr;
}
// c++: std::make_shared<_cx_value>(val)
STC_INLINE _cx_self _cx_memb(_make)(_cx_value val) {
_cx_self ptr;
- struct _cx_memb(_rep_)* rep = c_ALLOC(struct _cx_memb(_rep_));
+ struct _cx_memb(_rep_)* rep = _i_alloc(struct _cx_memb(_rep_));
*(ptr.use_count = &rep->counter) = 1;
*(ptr.get = &rep->value) = val;
return ptr;
@@ -127,8 +127,8 @@ STC_INLINE void _cx_memb(_drop)(_cx_self* self) {
if (self->use_count && _i_atomic_dec_and_test(self->use_count)) {
i_keydrop(self->get);
if ((char *)self->get != (char *)self->use_count + offsetof(struct _cx_memb(_rep_), value))
- c_free(self->get);
- c_free((long*)self->use_count);
+ i_free(self->get);
+ i_free((long*)self->use_count);
}
}
diff --git a/include/stc/cbits.h b/include/stc/cbits.h
index 577ecb13..d72be230 100644
--- a/include/stc/cbits.h
+++ b/include/stc/cbits.h
@@ -198,7 +198,7 @@ struct { uint64_t data64[(i_capacity - 1)/64 + 1]; } typedef i_type;
STC_INLINE i_type _i_memb(_init)(void) { return c_LITERAL(i_type){0}; }
STC_INLINE void _i_memb(_create)(i_type* self) {}
STC_INLINE void _i_memb(_drop)(i_type* self) {}
-STC_INLINE intptr_t _i_memb(_size)(const i_type* self) { return i_capacity; }
+STC_INLINE intptr_t _i_memb(_size)(const i_type* self) { return i_capacity; }
STC_INLINE i_type _i_memb(_move)(i_type* self) { return *self; }
STC_INLINE i_type* _i_memb(_take)(i_type* self, i_type other)
diff --git a/include/stc/cbox.h b/include/stc/cbox.h
index 4f0a2976..c914152c 100644
--- a/include/stc/cbox.h
+++ b/include/stc/cbox.h
@@ -89,7 +89,7 @@ STC_INLINE _cx_self _cx_memb(_from_ptr)(_cx_value* p)
// c++: std::make_unique<i_key>(val)
STC_INLINE _cx_self _cx_memb(_make)(_cx_value val) {
- _cx_self ptr = {c_ALLOC(_cx_value)};
+ _cx_self ptr = {_i_alloc(_cx_value)};
*ptr.get = val; return ptr;
}
@@ -100,7 +100,7 @@ STC_INLINE _cx_raw _cx_memb(_toraw)(const _cx_self* self)
STC_INLINE void _cx_memb(_drop)(_cx_self* self) {
if (self->get) {
i_keydrop(self->get);
- c_free(self->get);
+ i_free(self->get);
}
}
@@ -136,7 +136,7 @@ STC_INLINE _cx_self _cx_memb(_from)(_cx_value val)
STC_INLINE _cx_self _cx_memb(_clone)(_cx_self other) {
if (!other.get)
return other;
- _cx_self out = {c_ALLOC(i_key)};
+ _cx_self out = {_i_alloc(i_key)};
*out.get = i_keyclone((*other.get));
return out;
}
diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h
index d04aaacd..ddfb41b9 100644
--- a/include/stc/ccommon.h
+++ b/include/stc/ccommon.h
@@ -64,24 +64,24 @@
#ifdef __cplusplus
#include <new>
- #define c_ALLOC(T) static_cast<T*>(c_malloc(c_sizeof(T)))
- #define c_NEW(T, ...) new (c_ALLOC(T)) T(__VA_ARGS__)
+ #define _i_alloc(T) static_cast<T*>(i_malloc(c_sizeof(T)))
+ #define _i_new(T, ...) new (_i_alloc(T)) T(__VA_ARGS__)
+ #define c_new(T, ...) new (malloc(sizeof(T))) T(__VA_ARGS__)
#define c_LITERAL(T) T
#else
- #define c_ALLOC(T) ((T*)c_malloc(c_sizeof(T)))
- #define c_NEW(T, ...) ((T*)memcpy(c_ALLOC(T), (T[]){__VA_ARGS__}, sizeof(T)))
+ #define _i_alloc(T) ((T*)i_malloc(c_sizeof(T)))
+ #define _i_new(T, ...) ((T*)memcpy(_i_alloc(T), (T[]){__VA_ARGS__}, sizeof(T)))
+ #define c_new(T, ...) ((T*)memcpy(malloc(sizeof(T)), (T[]){__VA_ARGS__}, sizeof(T)))
#define c_LITERAL(T) (T)
#endif
+#define c_malloc(sz) malloc(c_i2u(sz))
+#define c_calloc(n, sz) calloc(c_i2u(n), c_i2u(sz))
+#define c_realloc(p, sz) realloc(p, c_i2u(sz))
+#define c_free(p) free(p)
-#ifndef c_malloc
- #define c_malloc(sz) malloc(c_i2u(sz))
- #define c_calloc(n, sz) calloc(c_i2u(n), c_i2u(sz))
- #define c_realloc(p, sz) realloc(p, c_i2u(sz))
- #define c_free(p) free(p)
-#endif
#define c_static_assert(b) ((int)(0*sizeof(int[(b) ? 1 : -1])))
#define c_container_of(p, T, m) ((T*)((char*)(p) + 0*sizeof((p) == &((T*)0)->m) - offsetof(T, m)))
-#define c_delete(T, ptr) do { T *_tp = ptr; T##_drop(_tp); c_free(_tp); } while (0)
+#define c_delete(T, ptr) do { T *_tp = ptr; T##_drop(_tp); i_free(_tp); } while (0)
#define c_swap(T, xp, yp) do { T *_xp = xp, *_yp = yp, \
_tv = *_xp; *_xp = *_yp; *_yp = _tv; } while (0)
#define c_sizeof (intptr_t)sizeof
@@ -121,13 +121,14 @@
#define c_make(C, ...) \
C##_from_n((C##_raw[])__VA_ARGS__, c_sizeof((C##_raw[])__VA_ARGS__)/c_sizeof(C##_raw))
+#define c_arraylen(a) \
+ (intptr_t)(sizeof(a)/sizeof 0[a])
typedef const char* crawstr;
#define crawstr_cmp(xp, yp) strcmp(*(xp), *(yp))
#define crawstr_hash(p) cstrhash(*(p))
#define crawstr_len(literal) (c_sizeof("" literal) - 1)
-#define c_ARRAYLEN(a) (intptr_t)(sizeof(a)/sizeof 0[a])
#define c_SV(...) c_MACRO_OVERLOAD(c_SV, __VA_ARGS__)
#define c_SV_1(lit) c_SV_2(lit, crawstr_len(lit))
#define c_SV_2(str, n) (c_LITERAL(csview){str, n})
diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h
index 56c0e867..fd508bf6 100644
--- a/include/stc/cdeq.h
+++ b/include/stc/cdeq.h
@@ -229,7 +229,7 @@ STC_DEF void
_cx_memb(_shrink_to_fit)(_cx_self *self) {
if (self->_len != self->_cap) {
c_memmove(self->_base, self->data, self->_len*c_sizeof(i_key));
- _cx_value* d = (_cx_value*)c_realloc(self->_base, self->_len*c_sizeof(i_key));
+ _cx_value* d = (_cx_value*)i_realloc(self->_base, self->_len*c_sizeof(i_key));
if (d) {
self->_base = d;
self->_cap = self->_len;
@@ -242,7 +242,7 @@ STC_DEF void
_cx_memb(_drop)(_cx_self* self) {
if (self->_base) {
_cx_memb(_clear)(self);
- c_free(self->_base);
+ i_free(self->_base);
}
}
@@ -250,7 +250,7 @@ static intptr_t
_cx_memb(_realloc_)(_cx_self* self, const intptr_t n) {
const intptr_t cap = (intptr_t)((float)self->_len*1.7f) + n + 7;
const intptr_t nfront = _cdeq_nfront(self);
- _cx_value* d = (_cx_value*)c_realloc(self->_base, cap*c_sizeof(i_key));
+ _cx_value* d = (_cx_value*)i_realloc(self->_base, cap*c_sizeof(i_key));
if (!d)
return 0;
self->_cap = cap;
diff --git a/include/stc/clist.h b/include/stc/clist.h
index 81443b59..de874927 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_entry_after(ref, val) \
- _cx_node *entry = (_cx_node *)c_malloc(c_sizeof *entry); entry->value = val; \
+ _cx_node *entry = (_cx_node *)i_malloc(c_sizeof *entry); entry->value = val; \
_c_clist_insert_node_after(ref, entry)
#define _c_clist_insert_node_after(ref, entry) \
@@ -348,7 +348,7 @@ STC_DEF void
_cx_memb(_erase_node_after)(_cx_self* self, _cx_node* ref) {
_cx_node* node = _cx_memb(_unlink_node_after)(self, ref);
i_keydrop((&node->value));
- c_free(node);
+ i_free(node);
}
STC_DEF _cx_node*
diff --git a/include/stc/cmap.h b/include/stc/cmap.h
index f5547125..f3c82469 100644
--- a/include/stc/cmap.h
+++ b/include/stc/cmap.h
@@ -318,8 +318,8 @@ STC_INLINE void _cx_memb(_wipe_)(_cx_self* self) {
STC_DEF void _cx_memb(_drop)(_cx_self* self) {
_cx_memb(_wipe_)(self);
- c_free(self->_hashx);
- c_free((void *) self->table);
+ i_free(self->_hashx);
+ i_free((void *) self->table);
}
STC_DEF void _cx_memb(_clear)(_cx_self* self) {
@@ -395,11 +395,11 @@ _cx_memb(_insert_entry_)(_cx_self* self, _cx_rawkey rkey) {
STC_DEF _cx_self
_cx_memb(_clone)(_cx_self m) {
if (m.table) {
- _cx_value *t = (_cx_value *)c_malloc(c_sizeof(_cx_value)*m.bucket_count),
+ _cx_value *t = (_cx_value *)i_malloc(c_sizeof(_cx_value)*m.bucket_count),
*dst = t, *m_end = m.table + m.bucket_count;
- uint8_t *h = (uint8_t *)c_memcpy(c_malloc(m.bucket_count + 1), m._hashx, m.bucket_count + 1);
+ uint8_t *h = (uint8_t *)c_memcpy(i_malloc(m.bucket_count + 1), m._hashx, m.bucket_count + 1);
if (!(t && h))
- { c_free(t), c_free(h), t = 0, h = 0, m.bucket_count = 0; }
+ { i_free(t), i_free(h), t = 0, h = 0, m.bucket_count = 0; }
else
for (; m.table != m_end; ++m.table, ++m._hashx, ++dst)
if (*m._hashx)
@@ -422,8 +422,8 @@ _cx_memb(_reserve)(_cx_self* self, const int64_t newcap) {
_nbuckets |= 1;
#endif
_cx_self m = {
- (_cx_value *)c_malloc(c_sizeof(_cx_value)*_nbuckets),
- (uint8_t *)c_calloc(_nbuckets + 1, 1),
+ (_cx_value *)i_malloc(c_sizeof(_cx_value)*_nbuckets),
+ (uint8_t *)i_calloc(_nbuckets + 1, 1),
self->size, _nbuckets,
};
bool ok = m.table && m._hashx;
@@ -439,8 +439,8 @@ _cx_memb(_reserve)(_cx_self* self, const int64_t newcap) {
}
c_swap(_cx_self, self, &m);
}
- c_free(m._hashx);
- c_free(m.table);
+ i_free(m._hashx);
+ i_free(m.table);
return ok;
}
diff --git a/include/stc/cpque.h b/include/stc/cpque.h
index 55df70ef..4955f2e0 100644
--- a/include/stc/cpque.h
+++ b/include/stc/cpque.h
@@ -55,7 +55,7 @@ STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, intptr_t n)
STC_INLINE bool _cx_memb(_reserve)(_cx_self* self, const intptr_t cap) {
if (cap != self->_len && cap <= self->_cap) return true;
- _cx_value *d = (_cx_value *)c_realloc(self->data, cap*c_sizeof *d);
+ _cx_value *d = (_cx_value *)i_realloc(self->data, cap*c_sizeof *d);
return d ? (self->data = d, self->_cap = cap, true) : false;
}
@@ -79,7 +79,7 @@ STC_INLINE void _cx_memb(_clear)(_cx_self* self) {
}
STC_INLINE void _cx_memb(_drop)(_cx_self* self)
- { _cx_memb(_clear)(self); c_free(self->data); }
+ { _cx_memb(_clear)(self); i_free(self->data); }
STC_INLINE intptr_t _cx_memb(_size)(const _cx_self* q)
{ return q->_len; }
diff --git a/include/stc/csmap.h b/include/stc/csmap.h
index 0f21f954..6a1899b9 100644
--- a/include/stc/csmap.h
+++ b/include/stc/csmap.h
@@ -231,7 +231,7 @@ STC_DEF bool
_cx_memb(_reserve)(_cx_self* self, const int64_t cap) {
if ((i_size)cap <= self->cap)
return false;
- _cx_node* nodes = (_cx_node*)c_realloc(self->nodes, (cap + 1)*c_sizeof(_cx_node));
+ _cx_node* nodes = (_cx_node*)i_realloc(self->nodes, (cap + 1)*c_sizeof(_cx_node));
if (!nodes)
return false;
nodes[0] = c_LITERAL(_cx_node){{0, 0}, 0};
@@ -581,7 +581,7 @@ STC_DEF void
_cx_memb(_drop)(_cx_self* self) {
if (self->cap) {
_cx_memb(_drop_r_)(self->nodes, self->root);
- c_free(self->nodes);
+ i_free(self->nodes);
}
}
diff --git a/include/stc/cspan.h b/include/stc/cspan.h
index cc3d03b3..6c7bb273 100644
--- a/include/stc/cspan.h
+++ b/include/stc/cspan.h
@@ -124,10 +124,10 @@ typedef struct { int32_t d[6]; } cspan_idx6;
{.data=(container)->data, .shape={(int32_t)(container)->_len}}
#define cspan_from_array(array) \
- {.data=(array) + c_static_assert(sizeof(array) != sizeof(void*)), .shape={c_ARRAYLEN(array)}}
+ {.data=(array) + c_static_assert(sizeof(array) != sizeof(void*)), .shape={c_arraylen(array)}}
#define cspan_size(self) _cspan_size((self)->shape, cspan_rank(self))
-#define cspan_rank(self) c_ARRAYLEN((self)->shape)
+#define cspan_rank(self) c_arraylen((self)->shape)
#define cspan_index(self, ...) c_PASTE(cspan_idx_, c_NUMARGS(__VA_ARGS__))(self, __VA_ARGS__)
#define cspan_idx_1 cspan_idx_4
diff --git a/include/stc/cstack.h b/include/stc/cstack.h
index dd4686e9..54bf7850 100644
--- a/include/stc/cstack.h
+++ b/include/stc/cstack.h
@@ -59,12 +59,12 @@ STC_INLINE void _cx_memb(_create)(_cx_self* self)
{ self->_len = 0; self->_cap = 0; self->data = NULL; }
STC_INLINE _cx_self _cx_memb(_with_capacity)(intptr_t cap) {
- _cx_self out = {(_cx_value *) c_malloc(cap*c_sizeof(i_key)), 0, cap};
+ _cx_self out = {(_cx_value *) i_malloc(cap*c_sizeof(i_key)), 0, cap};
return out;
}
STC_INLINE _cx_self _cx_memb(_with_size)(intptr_t size, i_key null) {
- _cx_self out = {(_cx_value *) c_malloc(size*c_sizeof null), size, size};
+ _cx_self out = {(_cx_value *) i_malloc(size*c_sizeof null), size, size};
while (size) out.data[--size] = null;
return out;
}
@@ -79,7 +79,7 @@ STC_INLINE void _cx_memb(_clear)(_cx_self* self) {
STC_INLINE void _cx_memb(_drop)(_cx_self* self) {
_cx_memb(_clear)(self);
#ifndef i_capacity
- c_free(self->data);
+ i_free(self->data);
#endif
}
@@ -100,7 +100,7 @@ STC_INLINE intptr_t _cx_memb(_capacity)(const _cx_self* self) {
STC_INLINE bool _cx_memb(_reserve)(_cx_self* self, intptr_t n) {
if (n < self->_len) return true;
#ifndef i_capacity
- _cx_value *t = (_cx_value *)c_realloc(self->data, n*c_sizeof *t);
+ _cx_value *t = (_cx_value *)i_realloc(self->data, n*c_sizeof *t);
if (t) { self->_cap = n, self->data = t; return true; }
#endif
return false;
@@ -154,7 +154,7 @@ STC_INLINE _cx_value* _cx_memb(_emplace)(_cx_self* self, _cx_raw raw)
#if !defined i_no_clone
STC_INLINE _cx_self _cx_memb(_clone)(_cx_self v) {
- _cx_self out = {(_cx_value *)c_malloc(v._len*c_sizeof(_cx_value)), v._len, v._len};
+ _cx_self out = {(_cx_value *)i_malloc(v._len*c_sizeof(_cx_value)), v._len, v._len};
if (!out.data) out._cap = 0;
else for (intptr_t i = 0; i < v._len; ++v.data)
out.data[i++] = i_keyclone((*v.data));
diff --git a/include/stc/cvec.h b/include/stc/cvec.h
index 06664b4d..84c91228 100644
--- a/include/stc/cvec.h
+++ b/include/stc/cvec.h
@@ -277,13 +277,13 @@ _cx_memb(_drop)(_cx_self* self) {
if (self->_cap == 0)
return;
_cx_memb(_clear)(self);
- c_free(self->data);
+ i_free(self->data);
}
STC_DEF bool
_cx_memb(_reserve)(_cx_self* self, const intptr_t cap) {
if (cap > self->_cap || (cap && cap == self->_len)) {
- _cx_value* d = (_cx_value*)c_realloc(self->data, cap*c_sizeof(i_key));
+ _cx_value* d = (_cx_value*)i_realloc(self->data, cap*c_sizeof(i_key));
if (!d)
return false;
self->data = d;
diff --git a/include/stc/priv/altnames.h b/include/stc/priv/altnames.h
index e3ce03a0..0b01d251 100644
--- a/include/stc/priv/altnames.h
+++ b/include/stc/priv/altnames.h
@@ -20,9 +20,6 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
-#define c_alloc c_ALLOC
-#define c_new c_NEW
-#define c_arraylen c_ARRAYLEN
#define c_forlist c_FORLIST
#define c_forrange c_FORRANGE
#define c_foreach c_FOREACH
@@ -38,3 +35,5 @@
#define c_defer c_DEFER
#define c_sv c_SV
#define c_ARGSV c_SVARG
+#define c_NEW c_new
+#define c_ARRAYLEN c_arraylen
diff --git a/include/stc/priv/template.h b/include/stc/priv/template.h
index 6eb82bcb..79627ff3 100644
--- a/include/stc/priv/template.h
+++ b/include/stc/priv/template.h
@@ -48,6 +48,16 @@
#define i_size intptr_t
#endif
+#ifndef i_allocator
+ #define i_allocator c_
+#endif
+#ifndef i_malloc
+ #define i_malloc c_PASTE(i_allocator, malloc)
+ #define i_calloc c_PASTE(i_allocator, calloc)
+ #define i_realloc c_PASTE(i_allocator, realloc)
+ #define i_free c_PASTE(i_allocator, free)
+#endif
+
#if !(defined i_key || defined i_key_str || defined i_key_ssv || \
defined i_keyclass || defined i_keyboxed)
#if defined _i_ismap
@@ -317,6 +327,12 @@
#undef i_static
#undef i_extern
+#undef i_allocator
+#undef i_malloc
+#undef i_calloc
+#undef i_realloc
+#undef i_free
+
#undef i_no_cmp
#undef i_no_eq
#undef i_no_hash