From 2ae6b220cc639ecc9cdfdabfe7be36aa2ed98b15 Mon Sep 17 00:00:00 2001 From: Tylo Date: Tue, 23 Jun 2020 17:45:43 +0200 Subject: Improved representation storage for CArray. Stores "owned" as highest bit in xdim. --- stc/carray.h | 62 ++++++++++++++++++++++++++++++++++++------------------------ stc/cdefs.h | 29 ++++++++++++++-------------- stc/chash.h | 2 +- stc/clist.h | 2 +- 4 files changed, 53 insertions(+), 42 deletions(-) (limited to 'stc') diff --git a/stc/carray.h b/stc/carray.h index a5c9571c..214bc976 100644 --- a/stc/carray.h +++ b/stc/carray.h @@ -49,12 +49,23 @@ int main() } */ -#define carray_xdim(a) ((a).xdim) -#define carray_ydim(a) ((a)._yxdim / (a).xdim) +#define carray_xdim(a) ((a)._xdim & ~_carray_own) +#define carray_ydim(a) _carray_ydim(&(a)._yxdim) #define carray_zdim(a) ((a)._zdim) -#define carray1_size(a) ((a).xdim) +#define carray1_size(a) carray_xdim(a) #define carray2_size(a) ((a)._yxdim) -#define carray3_size(a) ((a)._zdim * (a)._yxdim) +#define carray3_size(a) _carray3_size(&(a)._zdim) + + +enum {_carray_own = 1ull << ((sizeof(size_t) << 3) - 1)}; + +static inline size_t _carray_ydim(const size_t* yxdim) { + return yxdim[0] / (yxdim[-1] & ~_carray_own); +} +static inline size_t _carray3_size(const size_t* zdim) { + return zdim[0] * zdim[-1]; +} + #define declare_CArray(...) c_MACRO_OVERLOAD(declare_CArray, __VA_ARGS__) @@ -64,90 +75,91 @@ int main() #define declare_CArray_3(tag, Value, valueDestroy) \ typedef struct { \ - Value *data; uint32_t owned, xdim; \ + Value *data; \ + size_t _xdim; \ } CArray1_##tag; \ \ typedef struct { \ - Value *data; uint32_t owned, xdim; \ - size_t _yxdim; \ + Value *data; \ + size_t _xdim, _yxdim; \ } CArray2_##tag; \ \ typedef struct { \ - Value *data; uint32_t owned, xdim; \ - size_t _yxdim; uint32_t _zdim; \ + Value *data; \ + size_t _xdim, _yxdim, _zdim; \ } CArray3_##tag; \ \ static inline void \ carray1_##tag##_destroy(CArray1_##tag* self) { \ const size_t n = carray1_size(*self); Value* a = self->data; \ - if (self->owned) {for (size_t i=0; i_xdim & _carray_own) {for (size_t i=0; idata; \ - if (self->owned) {for (size_t i=0; i_xdim & _carray_own) {for (size_t i=0; idata; \ - if (self->owned) {for (size_t i=0; i_xdim & _carray_own) {for (size_t i=0; i= newcap * self->maxLoadPercent * 0.01) return oldcap; \ CHash_##tag tmp = { \ - c_new_2(CHashEntry_##tag, newcap), \ + c_new_N(CHashEntry_##tag, newcap), \ (uint8_t *) calloc(newcap, sizeof(uint8_t)), \ self->_size, (uint32_t) newcap, \ self->maxLoadPercent, self->shrinkLimitPercent \ diff --git a/stc/clist.h b/stc/clist.h index 81af6e53..4dcd6e26 100644 --- a/stc/clist.h +++ b/stc/clist.h @@ -227,7 +227,7 @@ } #define _clist_insertAfter(self, tag, node, val) \ - CListNode_##tag *entry = c_new_1(CListNode_##tag), \ + CListNode_##tag *entry = c_new (CListNode_##tag), \ *next = self->last ? node->next : entry; \ entry->value = val; \ entry->next = next; \ -- cgit v1.2.3