rsys

Basic data structures and low-level features
git clone git://git.meso-star.fr/rsys.git
Log | Files | Refs | README | LICENSE

commit 5a943ee2dd62682506a9cc64c679e1b08207c001
parent e16715fa253857c3273c5c289d77491c9587169a
Author: vaplv <vaplv@free.fr>
Date:   Sun, 16 Feb 2014 21:25:40 +0100

Begin the implementation of a simple dynamic array

Diffstat:
Mcmake/CMakeLists.txt | 2++
Asrc/dynamic_array.h | 119+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/free_list.h | 2+-
Asrc/test_dynamic_array.c | 14++++++++++++++
4 files changed, 136 insertions(+), 1 deletion(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -58,6 +58,7 @@ set(RSYS_FILES_SRC pthread/pthread_mutex.c) set(RSYS_FILES_INC_COMMON clock_time.h + dynamic_array.h free_list.h image.h library.h @@ -100,6 +101,7 @@ macro(new_test _name) endmacro(new_test) new_test(test_atomic) +new_test(test_dynamic_array) new_test(test_free_list rsys) new_test(test_library rsys) new_test(test_list rsys) diff --git a/src/dynamic_array.h b/src/dynamic_array.h @@ -0,0 +1,119 @@ +#if !defined(DARRAY_NAME) || !defined(DARRAY_DATA_TYPE) +#ifndef DYNAMIC_ARRAY_H +#define DYNAMIC_ARRAY_H + +#include "mem_allocator.h" +#include <string.h> + +#endif /* DYNAMIC_ARRAY_H */ +#else + +#ifndef DARRAY_NAME + #error "Missing the DARRAY_NAME macro defining the structure name" +#endif + +#ifndef DARRAY_DATA_TYPE + #error "Missing the DARRAY_DATA_TYPE macro defining the array data type" +#endif + +/* + * Dynamic array API generated with respect the DARRAY_<NAME|DATA_TYPE> macros. + */ +#define DARRAY_FUNC__(Func) CONCAT(CONCAT(CONCAT(darray_, DARRAY_NAME),_), Func) +#define DARRAY_TYPE__ CONCAT(darray_, DARRAY_NAME) + +struct DARRAY_TYPE__ { + DARRAY_DATA_TYPE* data; + size_t size; + size_t capacity; + struct mem_allocator* allocator; +}; + +static FINLINE void +DARRAY_FUNC__(init) + (struct mem_allocator* allocator, /* May be NULL <=> use default allocator */ + struct DARRAY_TYPE__* darray) +{ + ASSERT(darray); + darray->data = NULL; + darray->size = 0; + darray->capacity = 0; + darray->allocator = allocator ? allocator : &mem_default_allocator; +} + +static FINLINE void +DARRAY_FUNC__(release)(struct DARRAY_TYPE__* darray) +{ + ASSERT(darray); + MEM_FREE(darray->allocator, darray->data); +} + +static FINLINE void +DARRAY_FUNC__(clear)(struct DARRAY_TYPE__* darray) +{ + ASSERT(darray); + darray->size = 0; +} + +static FINLINE char +DARRAY_FUNC__(reserve)(struct DARRAY_TYPE__* darray, const size_t sz) +{ + DARRAY_DATA_TYPE* data = NULL; + ASSERT(darray); + if(sz <= darray->capacity) + return 0; + + data = MEM_ALLOC_ALIGNED + (darray->allocator, + sz * sizeof(DARRAY_DATA_TYPE), + ALIGNOF(DARRAY_DATA_TYPE)); + if(!data) + return -1; + + if(darray->data) { + memcpy(data, darray->data, darray->size * sizeof(DARRAY_DATA_TYPE)); + MEM_FREE(darray->allocator, darray->data); + } + darray->data = data; + darray->capacity = sz; + return 0; +} + +static FINLINE char +DARRAY_FUNC__(push_back) + (struct DARRAY_TYPE__* darray, + DARRAY_DATA_TYPE* const data) +{ + size_t i = 0; + ASSERT(darray && data); + i = darray->size + 1; + if(DARRAY_FUNC__(reserve)(darray, i)) + return -1; + darray->data[i] = *data; + darray->size = i; + return 0; +} + +static FINLINE size_t +DARRAY_FUNC__(size_get)(const struct DARRAY_TYPE__* darray) +{ + ASSERT(darray); + return darray->size; +} + +static FINLINE DARRAY_DATA_TYPE* +DARRAY_FUNC__(data_get)(struct DARRAY_TYPE__* darray) +{ + ASSERT(darray); + return darray->data; +} + +static FINLINE DARRAY_DATA_TYPE const* +DARRAY_FUNC__(cdata_get)(const struct DARRAY_TYPE__* darray) +{ + ASSERT(darray); + return darray->data; +} + +#endif /* !DARRAY_NAME || !DARRAY_TYPE */ + diff --git a/src/free_list.h b/src/free_list.h @@ -32,7 +32,7 @@ static const struct fid FID_NULL = { UINT32_MAX, UINT32_MAX }; * The name of the generated free list type is: * struct flist_<FITEM_TYPE> * - * while each generated `function' respect the following naming convention: + * while each generated `function' respect the following naming convention: * flist_<FITEM_TYPE>_<function> */ #define FLIST_FUNC__(Func) CONCAT(CONCAT(CONCAT(flist_, FITEM_TYPE), _), Func) diff --git a/src/test_dynamic_array.c b/src/test_dynamic_array.c @@ -0,0 +1,14 @@ +#include "rsys.h" +#include "dynamic_array.h" + +#define DARRAY_NAME Str +#define DARRAY_DATA_TYPE const char* +#include "dynamic_array.h" + +int +main(int argc, char** argv) +{ + (void)argc, (void)argv; + return 0; +} +