commit 8567aef72aae66acb276553f862f13b4d09bb0a0
parent bc1547ffc9669d20f33a226454a01114238c6f78
Author: vaplv <vincent.forest@meso-star.com>
Date: Thu, 1 Dec 2016 16:01:14 +0100
Remove the local buffer of the dynamic array
Diffstat:
| M | src/dynamic_array.h | | | 83 | +++++++++++++++++++++++++++---------------------------------------------------- |
1 file changed, 28 insertions(+), 55 deletions(-)
diff --git a/src/dynamic_array.h b/src/dynamic_array.h
@@ -65,14 +65,6 @@
struct DARRAY_TYPE__ {
DARRAY_DATA* data;
- /* Avoids alloc on small arrays. The CL compiler does not support the use of
- * the ALIGN macro on structure fields and consequently this optimization is
- * `disabled' for this compiler */
-#ifdef COMPILER_CL
- char buf[1];
-#else
- char ALIGN(DARRAY_ALIGNMENT__) buf[16*sizeof(DARRAY_DATA)];
-#endif
size_t size;
size_t capacity;
struct mem_allocator* allocator;
@@ -123,13 +115,9 @@ DARRAY_FUNC__(init)
{
size_t i;
ASSERT(darray);
- darray->data = (DARRAY_DATA*)darray->buf;
+ darray->data = NULL;
darray->size = 0;
-#ifdef COMPILER_CL
darray->capacity = 0;
-#else
- darray->capacity = sizeof(darray->buf)/sizeof(DARRAY_DATA);
-#endif
FOR_EACH(i, 0, darray->capacity)
DARRAY_FUNCTOR_INIT(allocator, darray->data + i);
darray->allocator = allocator ? allocator : &mem_default_allocator;
@@ -150,8 +138,7 @@ DARRAY_FUNC__(release)(struct DARRAY_TYPE__* darray)
{
ASSERT(darray);
DARRAY_FUNC__(clear)(darray);
- if(darray->data != (DARRAY_DATA*)darray->buf)
- MEM_RM(darray->allocator, darray->data);
+ MEM_RM(darray->allocator, darray->data);
}
static INLINE res_T
@@ -165,6 +152,7 @@ DARRAY_FUNC__(reserve)(struct DARRAY_TYPE__* darray, const size_t sz)
return RES_OK;
sz_adjusted = round_up_pow2(sz);
+ printf("%i %i %i\n", (int)ALIGNOF(DARRAY_DATA), (int)16, (int)DARRAY_ALIGNMENT__);
data = (DARRAY_DATA*)MEM_ALLOC_ALIGNED
(darray->allocator,
sz_adjusted * sizeof(DARRAY_DATA),
@@ -184,8 +172,7 @@ DARRAY_FUNC__(reserve)(struct DARRAY_TYPE__* darray, const size_t sz)
}
}
}
- if(darray->data != (DARRAY_DATA*)darray->buf)
- MEM_RM(darray->allocator, darray->data);
+ MEM_RM(darray->allocator, darray->data);
darray->data = data;
darray->capacity = sz_adjusted;
@@ -203,10 +190,13 @@ DARRAY_FUNC__(resize)(struct DARRAY_TYPE__* darray, const size_t sz)
if(res != RES_OK)
return res;
- FOR_EACH(i, sz, darray->size)
- DARRAY_FUNCTOR_RELEASE(darray->data+i);
- FOR_EACH(i, darray->size, sz)
- DARRAY_FUNCTOR_INIT(darray->allocator, darray->data+i);
+ if(sz < darray->size) {
+ FOR_EACH(i, sz, darray->size)
+ DARRAY_FUNCTOR_RELEASE(darray->data+i);
+ } else if(darray->size < sz) {
+ FOR_EACH(i, darray->size, sz)
+ DARRAY_FUNCTOR_INIT(darray->allocator, darray->data+i);
+ }
darray->size = sz;
return RES_OK;
}
@@ -292,12 +282,10 @@ DARRAY_FUNC__(copy_and_clear)
return RES_OK;
}
- if(src->data != (DARRAY_DATA*)src->buf && src->allocator == dst->allocator) {
+ if(src->allocator == dst->allocator) {
/* Give the ownership of src->data to dst */
DARRAY_FUNC__(clear)(dst);
- if(dst->data != (DARRAY_DATA*)dst->buf) {
- MEM_RM(dst->allocator, dst->data);
- }
+ MEM_RM(dst->allocator, dst->data);
dst->data = src->data;
dst->capacity = src->capacity;
dst->size = src->size;
@@ -343,7 +331,6 @@ DARRAY_FUNC__(copy_and_release)
static INLINE res_T
DARRAY_FUNC__(swap)(struct DARRAY_TYPE__* a, struct DARRAY_TYPE__* b)
{
- int a_buf, b_buf;
res_T res = RES_OK;
ASSERT(a && b);
@@ -352,11 +339,7 @@ DARRAY_FUNC__(swap)(struct DARRAY_TYPE__* a, struct DARRAY_TYPE__* b)
SWAP(struct DARRAY_TYPE__*, a, b);
}
- /* Define if the data are stored in the local buffer */
- a_buf = (a->data == (DARRAY_DATA*)a->buf);
- b_buf = (b->data == (DARRAY_DATA*)b->buf);
-
- if(a->allocator != b->allocator || (a_buf && b_buf)) {
+ if(a->allocator != b->allocator) {
struct DARRAY_TYPE__ tmp;
DARRAY_FUNC__(init)(a->allocator, &tmp);
@@ -368,30 +351,20 @@ DARRAY_FUNC__(swap)(struct DARRAY_TYPE__* a, struct DARRAY_TYPE__* b)
if(res != RES_OK) return res;
} else {
- /* `b' cannot be buffered since at most one array is buffered and that `b'
- * is greater than `a' */
- ASSERT(!b_buf);
-
- if(!a_buf) { /* `a' is not buffered => swap data ownership */
- SWAP(DARRAY_DATA*, a->data, b->data);
- SWAP(size_t, a->capacity, b->capacity);
- SWAP(size_t, a->size, b->size);
- } else {
- /* Back-up the data of `b' */
- DARRAY_DATA* b_data = b->data;
- const size_t b_capacity = b->capacity;
- const size_t b_size = b->size;
-
- /* Reset `b' and copy `a' into `b' */
- DARRAY_FUNC__(init)(b->allocator, b);
- res = DARRAY_FUNC__(copy_and_clear)(b, a);
- ASSERT(res == RES_OK);
-
- /* Give the ownership of `b' data to `a' */
- a->data = b_data;
- a->capacity = b_capacity;
- a->size = b_size;
- }
+ /* Back-up the data of `b' */
+ DARRAY_DATA* b_data = b->data;
+ const size_t b_capacity = b->capacity;
+ const size_t b_size = b->size;
+
+ /* Reset `b' and copy `a' into `b' */
+ DARRAY_FUNC__(init)(b->allocator, b);
+ res = DARRAY_FUNC__(copy_and_clear)(b, a);
+ ASSERT(res == RES_OK);
+
+ /* Give the ownership of `b' data to `a' */
+ a->data = b_data;
+ a->capacity = b_capacity;
+ a->size = b_size;
}
return RES_OK;
}