diff options
| author | Tyge Løvset <[email protected]> | 2020-09-20 13:37:27 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2020-09-20 13:37:27 +0200 |
| commit | 84c1716de710d2d9067c9f8a50487e3ab2d6fa42 (patch) | |
| tree | ae5be1a90482b4940a6555ed1ff44c2e518dbe0d | |
| parent | 927a1b015b7b934220edf9b5c758eed540f3e510 (diff) | |
| download | STC-modified-84c1716de710d2d9067c9f8a50487e3ab2d6fa42.tar.gz STC-modified-84c1716de710d2d9067c9f8a50487e3ab2d6fa42.zip | |
Possible to redefine STC memory allocator globally.
| -rw-r--r-- | README.md | 4 | ||||
| -rw-r--r-- | examples/inits.c | 15 | ||||
| -rw-r--r-- | examples/queue.c | 4 | ||||
| -rw-r--r-- | stc/carray.h | 8 | ||||
| -rw-r--r-- | stc/cbitset.h | 8 | ||||
| -rw-r--r-- | stc/cdefs.h | 12 | ||||
| -rw-r--r-- | stc/clist.h | 4 | ||||
| -rw-r--r-- | stc/cmap.h | 12 | ||||
| -rw-r--r-- | stc/cstr.h | 12 | ||||
| -rw-r--r-- | stc/cvec.h | 8 |
10 files changed, 47 insertions, 40 deletions
@@ -173,7 +173,7 @@ int main() { cvec_ix_pop_back(&bignums); // erase the last
uint64_t value;
- c_forrange (i, cvec_size(bignums))
+ c_forrange (i, cvec_ix_size(bignums))
value = bignums.data[i];
cvec_ix_del(&bignums);
}
@@ -191,7 +191,7 @@ int main() { cvec_str_emplace_back(&names, "Joe");
cstr_assign(&names.data[1], "Jake"); // replace "Joe".
// Use push_back() to add a new cstr_t object to be moved into the vector, e.g.:
- cvec_str_push_back(&names, cstr_from("%d elements so far", cvec_size(names)));
+ cvec_str_push_back(&names, cstr_from("%d elements so far", cvec_str_size(names)));
printf("%s\n", names.data[1].str); // Access the string char*
c_foreach (i, cvec_str, names)
diff --git a/examples/inits.c b/examples/inits.c index 14fe8a64..27ee3c9b 100644 --- a/examples/inits.c +++ b/examples/inits.c @@ -10,18 +10,17 @@ using_cmap_strkey(cnt, int); typedef struct {int x, y;} ipair_t;
inline static int ipair_compare(const ipair_t* a, const ipair_t* b) {
- int c = c_default_compare(&a->x, &b->x);
- return c != 0 ? c : c_default_compare(&a->y, &b->y);
+ int cx = c_default_compare(&a->x, &b->x);
+ return cx == 0 ? c_default_compare(&a->y, &b->y) : cx;
}
+
using_cvec(ip, ipair_t, c_default_del, ipair_compare);
using_clist(ip, ipair_t, c_default_del, ipair_compare);
-
using_cvec(f, float);
using_cpqueue(f, cvec_f, >);
-
-int main(void) {
-
+int main(void)
+{
// CVEC FLOAT / PRIORITY QUEUE
cvec_f floats = cvec_INIT;
@@ -32,11 +31,11 @@ int main(void) { // CVEC PRIORITY QUEUE
- cpqueue_f_build(&floats); // reorganise vec
+ cpqueue_f_build(&floats); // make heap
c_push_items(&floats, cpqueue_f, {40.0f, 20.0f, 50.0f, 30.0f, 10.0f});
// sorted:
- while (cvec_size(floats) > 0) {
+ while (! cpqueue_f_empty(floats)) {
printf("%.1f ", *cpqueue_f_top(&floats));
cpqueue_f_pop(&floats);
}
diff --git a/examples/queue.c b/examples/queue.c index e4426884..c56214a7 100644 --- a/examples/queue.c +++ b/examples/queue.c @@ -14,12 +14,12 @@ int main() { cqueue_i queue = cqueue_i_init();
// Push ten million random numbers onto the queue.
- c_forrange (i, int, 0, n)
+ c_forrange (n)
cqueue_i_push(&queue, crand_uniform_i32(&rng, &dist));
// Push or pop on the queue ten million times
printf("%d\n", n);
- c_forrange (i, int, n, 0, -1) {
+ c_forrange (n) { // range uses initial n only.
int r = crand_uniform_i32(&rng, &dist);
if (r & 1)
++n, cqueue_i_push(&queue, r);
diff --git a/stc/carray.h b/stc/carray.h index 5a525d66..4e164f42 100644 --- a/stc/carray.h +++ b/stc/carray.h @@ -87,7 +87,7 @@ STC_INLINE size_t _carray3_size(const size_t* zdim) { if (self->_xdim & _carray_OWN) { \
c_foreach_3 (i, carray##D##X, *self) \
valueDestroy(i.get); \
- free(self->data); \
+ c_free(self->data); \
} \
}
@@ -122,7 +122,7 @@ STC_INLINE size_t _carray3_size(const size_t* zdim) { \
STC_INLINE carray1##X \
carray1##X##_make(size_t xdim, Value val) { \
- Value* m = c_new_n(Value, xdim); \
+ Value* m = c_new_2(Value, xdim); \
for (size_t i=0; i<xdim; ++i) m[i] = val; \
carray1##X a = {m, xdim | _carray_OWN}; \
return a; \
@@ -130,7 +130,7 @@ STC_INLINE size_t _carray3_size(const size_t* zdim) { STC_INLINE carray2##X \
carray2##X##_make(size_t ydim, size_t xdim, Value val) { \
const size_t n = ydim * xdim; \
- Value* m = c_new_n(Value, n); \
+ Value* m = c_new_2(Value, n); \
for (size_t i=0; i<n; ++i) m[i] = val; \
carray2##X a = {m, xdim | _carray_OWN, ydim * xdim}; \
return a; \
@@ -138,7 +138,7 @@ STC_INLINE size_t _carray3_size(const size_t* zdim) { STC_INLINE carray3##X \
carray3##X##_make(size_t zdim, size_t ydim, size_t xdim, Value val) { \
const size_t n = zdim * ydim * xdim; \
- Value* m = c_new_n(Value, n); \
+ Value* m = c_new_2(Value, n); \
for (size_t i=0; i<n; ++i) m[i] = val; \
carray3##X a = {m, xdim | _carray_OWN, ydim * xdim, zdim}; \
return a; \
diff --git a/stc/cbitset.h b/stc/cbitset.h index 7801423f..e2fb9db2 100644 --- a/stc/cbitset.h +++ b/stc/cbitset.h @@ -84,7 +84,7 @@ STC_INLINE void cbitset_flip_all(cbitset_t *self) { }
STC_INLINE cbitset_t cbitset_with_size(size_t size, bool value) {
- cbitset_t set = {(uint64_t *) malloc(((size + 63) >> 6) * 8), size};
+ cbitset_t set = {(uint64_t *) c_malloc(((size + 63) >> 6) * 8), size};
cbitset_set_all(&set, value);
return set;
}
@@ -101,11 +101,11 @@ STC_INLINE cstr_t cbitset_to_str(cbitset_t set) { }
STC_INLINE cbitset_t cbitset_clone(cbitset_t other) {
size_t bytes = ((other.size + 63) >> 6) * 8;
- cbitset_t set = {(uint64_t *) memcpy(malloc(bytes), other._arr, bytes), other.size};
+ cbitset_t set = {(uint64_t *) memcpy(c_malloc(bytes), other._arr, bytes), other.size};
return set;
}
STC_INLINE void cbitset_del(cbitset_t* self) {
- free(self->_arr);
+ c_free(self->_arr);
}
STC_INLINE size_t cbitset_size(cbitset_t set) {return set.size;}
@@ -170,7 +170,7 @@ STC_INLINE bool cbitset_itval(cbitset_iter_t it) { STC_API void cbitset_resize(cbitset_t* self, size_t size, bool value) {
size_t new_n = (size + 63) >> 6, osize = self->size, old_n = (osize + 63) >> 6;
- self->_arr = (uint64_t *) realloc(self->_arr, new_n * 8);
+ self->_arr = (uint64_t *) c_realloc(self->_arr, new_n * 8);
self->size = size;
if (new_n >= old_n) {
memset(self->_arr + old_n, value ? 0xff : 0x0, (new_n - old_n) * 8);
diff --git a/stc/cdefs.h b/stc/cdefs.h index ebf24738..7b06f74d 100644 --- a/stc/cdefs.h +++ b/stc/cdefs.h @@ -57,8 +57,16 @@ enum {_c_max_buffer = 512}; #define c_static_assert(cond, msg) typedef char static_assert_##msg[(cond) ? 1 : -1]
#define c_container_of(ptr, type, member) \
((type *)((char *)(ptr) - offsetof(type, member)))
-#define c_new(T) ((T *) malloc(sizeof(T)))
-#define c_new_n(T, n) ((T *) malloc(sizeof(T) * (n)))
+
+#define c_new(...) c_MACRO_OVERLOAD(c_new, __VA_ARGS__)
+#define c_new_1(T) ((T *) c_malloc(sizeof(T)))
+#define c_new_2(T, n) ((T *) c_malloc(sizeof(T) * (n)))
+#ifndef c_malloc
+#define c_malloc(sz) malloc(sz)
+#define c_calloc(n, sz) calloc(n, sz)
+#define c_realloc(p, sz) realloc(p, sz)
+#define c_free(p) free(p)
+#endif
#define c_swap(T, x, y) do { T __t = x; x = y; y = __t; } while (0)
#define c_no_compare(x, y) (0)
#define c_mem_equals(x, y) (memcmp(x, y, sizeof(*(y))) == 0)
diff --git a/stc/clist.h b/stc/clist.h index 9eb8f7b6..9aa83437 100644 --- a/stc/clist.h +++ b/stc/clist.h @@ -276,7 +276,7 @@ STC_API size_t _clist_size(const clist_void* self); node->next = next; \
if (del == next) self->last = node = NULL; \
else if (self->last == del) self->last = node, node = NULL; \
- valueDestroy(&del->value); free(del); \
+ valueDestroy(&del->value); c_free(del); \
return node; \
} \
\
@@ -323,7 +323,7 @@ STC_API size_t _clist_size(const clist_void* self); #define _c_clist_insert_after(self, X, node, val) \
- clist_##X##_node_t *entry = c_new (clist_##X##_node_t), \
+ clist_##X##_node_t *entry = c_new_1 (clist_##X##_node_t), \
*next = self->last ? node->next : entry; \
entry->value = val; \
entry->next = next; \
@@ -337,8 +337,8 @@ typedef struct {size_t idx; uint32_t hx;} cmap_bucket_t, cset_bucket_t; \
STC_API void ctype##_##X##_del(ctype##_##X* self) { \
ctype##_##X##_wipe_(self); \
- free(self->_hashx); \
- free(self->table); \
+ c_free(self->_hashx); \
+ c_free(self->table); \
} \
\
STC_API void ctype##_##X##_clear(ctype##_##X* self) { \
@@ -398,8 +398,8 @@ typedef struct {size_t idx; uint32_t hx;} cmap_bucket_t, cset_bucket_t; if (self->size > newcap) return; \
newcap = (size_t) (newcap / self->max_load_factor) | 1; \
ctype##_##X tmp = { \
- c_new_n(ctype##_##X##_value_t, newcap), \
- (uint8_t *) calloc(newcap + 1, sizeof(uint8_t)), \
+ c_new_2 (ctype##_##X##_value_t, newcap), \
+ (uint8_t *) c_calloc(newcap + 1, sizeof(uint8_t)), \
self->size, (uint32_t) newcap, \
self->max_load_factor, self->shrink_limit_factor \
}; \
@@ -414,8 +414,8 @@ typedef struct {size_t idx; uint32_t hx;} cmap_bucket_t, cset_bucket_t; slot[b.idx] = *e, \
hashx[b.idx] = (uint8_t) b.hx; \
} \
- free(tmp._hashx); \
- free(tmp.table); \
+ c_free(tmp._hashx); \
+ c_free(tmp.table); \
} \
\
STC_API void \
@@ -73,7 +73,7 @@ cstr_init() {return cstr_INIT;} STC_INLINE void
cstr_del(cstr_t* self) {
if (cstr_capacity(*self))
- free(_cstr_rep(self));
+ c_free(_cstr_rep(self));
}
STC_INLINE cstr_t
@@ -125,7 +125,7 @@ cstr_assign(cstr_t* self, const char* str) { STC_INLINE cstr_t*
cstr_take(cstr_t* self, cstr_t s) {
if (self->str != s.str && cstr_capacity(*self))
- free(_cstr_rep(self));
+ c_free(_cstr_rep(self));
self->str = s.str;
return self;
}
@@ -212,7 +212,7 @@ STC_API void cstr_reserve(cstr_t* self, size_t cap) {
size_t len = cstr_size(*self), oldcap = cstr_capacity(*self);
if (cap > oldcap) {
- size_t* rep = (size_t *) realloc(oldcap ? _cstr_rep(self) : NULL, _cstr_mem(cap));
+ size_t* rep = (size_t *) c_realloc(oldcap ? _cstr_rep(self) : NULL, _cstr_mem(cap));
self->str = (char *) (rep + 2);
self->str[rep[0] = len] = '\0';
rep[1] = _cstr_cap(cap);
@@ -230,7 +230,7 @@ cstr_resize(cstr_t* self, size_t len, char fill) { STC_API cstr_t
cstr_n(const char* str, size_t len) {
if (len == 0) return cstr_INIT;
- size_t *rep = (size_t *) malloc(_cstr_mem(len));
+ size_t *rep = (size_t *) c_malloc(_cstr_mem(len));
cstr_t s = {(char *) memcpy(rep + 2, str, len)};
s.str[rep[0] = len] = '\0';
rep[1] = _cstr_cap(len);
@@ -305,10 +305,10 @@ STC_INLINE void _cstr_internal_move(cstr_t* self, size_t pos1, size_t pos2) { STC_API void
cstr_replace_n(cstr_t* self, size_t pos, size_t len, const char* str, size_t n) {
char buf[_c_max_buffer];
- char* xstr = (char *) memcpy(n > _c_max_buffer ? malloc(n) : buf, str, n);
+ char* xstr = (char *) memcpy(n > _c_max_buffer ? c_malloc(n) : buf, str, n);
_cstr_internal_move(self, pos + len, pos + n);
memcpy(&self->str[pos], xstr, n);
- if (n > _c_max_buffer) free(xstr);
+ if (n > _c_max_buffer) c_free(xstr);
}
STC_API void
@@ -210,14 +210,14 @@ STC_API void \
cvec_##X##_del(cvec_##X* self) { \
cvec_##X##_clear(self); \
- if (self->data) free(_cvec_alloced(self->data)); \
+ if (self->data) c_free(_cvec_alloced(self->data)); \
} \
\
STC_API void \
cvec_##X##_reserve(cvec_##X* self, size_t cap) { \
size_t len = cvec_size(*self); \
if (cap >= len) { \
- size_t* rep = (size_t *) realloc(_cvec_alloced(self->data), 2 * sizeof(size_t) + cap * sizeof(Value)); \
+ size_t* rep = (size_t *) c_realloc(_cvec_alloced(self->data), 2 * sizeof(size_t) + cap * sizeof(Value)); \
self->data = (Value *) (rep + 2); \
rep[0] = len; \
rep[1] = cap; \
@@ -243,7 +243,7 @@ enum {max_buf = _c_max_buffer / sizeof(Value) + 1}; \
Value buf[max_buf]; \
size_t len = finish - first, idx = pos - self->data, size = cvec_size(*self); \
- cvec_##X##_value_t* xbuf = (len > max_buf ? c_new_n(cvec_##X##_value_t, len) : &buf[0]); \
+ cvec_##X##_value_t* xbuf = (len > max_buf ? c_new_2(cvec_##X##_value_t, len) : &buf[0]); \
for (size_t i=0; i<len; ++i, ++first) \
xbuf[i] = valueFromRaw(valueToRaw(first)); \
if (size + len > cvec_capacity(*self)) \
@@ -252,7 +252,7 @@ memmove(pos + len, pos, (size - idx) * sizeof(Value)); \
memcpy(pos, xbuf, len * sizeof(Value)); \
_cvec_size(self) += len; \
- if (len > max_buf) free(xbuf); \
+ if (len > max_buf) c_free(xbuf); \
cvec_##X##_iter_t it = {pos}; return it; \
} \
\
|
