summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2021-11-21 23:17:24 +0100
committerTyge Løvset <[email protected]>2021-11-21 23:17:24 +0100
commitee09c3627dee4eb2ec87fda9783be1bb15e132d9 (patch)
treeecc646a430e75f618e00c2a95ce2372e1021ba5d
parent879f7a1c5637a5f67d430d3583fc7ba34b687234 (diff)
downloadSTC-modified-ee09c3627dee4eb2ec87fda9783be1bb15e132d9.tar.gz
STC-modified-ee09c3627dee4eb2ec87fda9783be1bb15e132d9.zip
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.
-rw-r--r--docs/ccommon_api.md13
-rw-r--r--docs/cdeq_api.md2
-rw-r--r--docs/cvec_api.md4
-rw-r--r--include/stc/alt/clist.h2
-rw-r--r--include/stc/alt/csmap.h4
-rw-r--r--include/stc/carr2.h4
-rw-r--r--include/stc/carr3.h4
-rw-r--r--include/stc/ccommon.h15
-rw-r--r--include/stc/clist.h2
-rw-r--r--include/stc/cmap.h4
-rw-r--r--include/stc/csptr.h4
11 files changed, 32 insertions, 26 deletions
diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md
index 9a309835..db89120e 100644
--- a/docs/ccommon_api.md
+++ b/docs/ccommon_api.md
@@ -133,17 +133,22 @@ int arr[] = {1, 2, 3};
c_apply_n(cvec_i, push_back, &vec, arr, c_arraylen(arr));
```
-### c_new, c_new_n, c_del, c_make
+### c_new, c_alloc, c_alloc_n, c_del, c_make
| Usage | Meaning |
|:-------------------------------|:----------------------------------------|
-| `c_new (type)` | `(type *) c_malloc(sizeof(type))` |
-| `c_new_n (type, N)` | `(type *) c_malloc((N)*sizeof(type))` |
+| `c_new (type, value)` | Move value to a new object on the heap |
+| `c_alloc (type)` | `(type *) c_malloc(sizeof(type))` |
+| `c_alloc_n (type, N)` | `(type *) c_malloc((N)*sizeof(type))` |
| `c_del (ctype, &c1, ..., &cN)` | `ctype_del(&c1); ... ctype_del(&cN)` |
| `c_make(type){value...}` | `(type){value...}` // c++ compatability |
```c
-int* array = c_new_n (int, 100);
+struct Pnt { double x, y, z; };
+struct Pnt *pnt = c_new (struct Pnt, {1.2, 3.4, 5.6});
+c_free(pnt);
+
+int* array = c_alloc_n (int, 100);
c_free(array);
cstr a = cstr_from("Hello"), b = cstr_from("World");
diff --git a/docs/cdeq_api.md b/docs/cdeq_api.md
index 22487682..554b4c7b 100644
--- a/docs/cdeq_api.md
+++ b/docs/cdeq_api.md
@@ -28,7 +28,7 @@ cdeq_X cdeq_X_clone(cdeq_X deq);
void cdeq_X_clear(cdeq_X* self);
void cdeq_X_copy(cdeq_X* self, cdeq_X other);
-void cdeq_X_reserve(cdeq_X* self, size_t cap);
+bool cdeq_X_reserve(cdeq_X* self, size_t cap);
void cdeq_X_shrink_to_fit(cdeq_X* self);
void cdeq_X_swap(cdeq_X* a, cdeq_X* b);
void cdeq_X_del(cdeq_X* self); // destructor
diff --git a/docs/cvec_api.md b/docs/cvec_api.md
index 64ca724f..b3bc0f1b 100644
--- a/docs/cvec_api.md
+++ b/docs/cvec_api.md
@@ -35,8 +35,8 @@ cvec_X cvec_X_clone(cvec_X vec);
void cvec_X_clear(cvec_X* self);
void cvec_X_copy(cvec_X* self, cvec_X other);
-void cvec_X_reserve(cvec_X* self, size_t cap);
-void cvec_X_resize(cvec_X* self, size_t size, i_val fill);
+bool cvec_X_reserve(cvec_X* self, size_t cap);
+bool cvec_X_resize(cvec_X* self, size_t size, i_val fill);
void cvec_X_shrink_to_fit(cvec_X* self);
void cvec_X_swap(cvec_X* a, cvec_X* b);
void cvec_X_del(cvec_X* self); // destructor
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 <new>
-# define c_new(T) static_cast<T*>(c_malloc(sizeof(T)))
-# define c_new_n(T, n) static_cast<T*>(c_malloc(sizeof(T)*(n)))
+# define c_alloc(T) static_cast<T*>(c_malloc(sizeof(T)))
+# define c_alloc_n(T, n) static_cast<T*>(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;