diff --git a/sys/include/memarray.h b/sys/include/memarray.h new file mode 100644 index 0000000000..c5294aade2 --- /dev/null +++ b/sys/include/memarray.h @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2018 Tobias Heider + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ +/** + * @defgroup sys_memarray memory array allocator + * @ingroup sys + * @brief memory array allocator + * @{ + * + * @brief pseudo dynamic allocation in static memory arrays + * @author Tobias Heider + */ + +#ifndef MEMARRAY_H +#define MEMARRAY_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Memory pool + */ +typedef struct { + void *free_data; /**< memory pool data / head of the free list */ + size_t size; /**< size of single list element */ + size_t num; /**< max number of elements in list */ +} memarray_t; + +/** + * @brief Initialize memarray pool with free list + * + * @pre `mem != NULL` + * @pre `data != NULL` + * @pre `size >= sizeof(void*)` + * @pre `num != 0` + * + * @param[in,out] mem memarray pool to initialize + * @param[in] data pointer to user-allocated data + * @param[in] size size of a single element in data + * @param[in] num number of elements in data + */ +void memarray_init(memarray_t *mem, void *data, size_t size, size_t num); + +/** + * @brief Allocate memory chunk in memarray pool + * + * @pre `mem != NULL` + * + * @param[in,out] mem memarray pool to allocate block in + * + * @return pointer to allocated structure, if enough memory was available + * @return NULL, on failure + */ +void *memarray_alloc(memarray_t *mem); + +/** + * @brief Free memory chunk in memarray pool + * + * @pre `mem != NULL` + * @pre `ptr != NULL` + * + * @param[in,out] mem memarray pool to free block in + * @param[in] ptr pointer to memarray chunk + */ +void memarray_free(memarray_t *mem, void *ptr); + +#ifdef __cplusplus +} +#endif + +#endif /* MEMARRAY_H */ + +/** + * @} + */ diff --git a/sys/memarray/Makefile b/sys/memarray/Makefile new file mode 100644 index 0000000000..48422e909a --- /dev/null +++ b/sys/memarray/Makefile @@ -0,0 +1 @@ +include $(RIOTBASE)/Makefile.base diff --git a/sys/memarray/memarray.c b/sys/memarray/memarray.c new file mode 100644 index 0000000000..f48d227b07 --- /dev/null +++ b/sys/memarray/memarray.c @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2018 Tobias Heider + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +#include +#include "memarray.h" + +#define ENABLE_DEBUG (0) +#include "debug.h" + +void memarray_init(memarray_t *mem, void *data, size_t size, size_t num) +{ + assert((mem != NULL) && (data != NULL) && (size >= sizeof(void *)) && + (num != 0)); + + DEBUG("memarray: Initialize memarray of %u times %u Bytes at %p\n", + (unsigned)num, (unsigned)size, data); + + mem->free_data = data; + mem->size = size; + mem->num = num; + + for (size_t i = 0; i < (mem->num - 1); i++) { + void *next = ((char *)mem->free_data) + ((i + 1) * mem->size); + memcpy(((char *)mem->free_data) + (i * mem->size), &next, sizeof(void *)); + } +} + +void *memarray_alloc(memarray_t *mem) +{ + assert(mem != NULL); + + if (mem->free_data == NULL) { + return NULL; + } + void *free = mem->free_data; + mem->free_data = *((void **)mem->free_data); + DEBUG("memarray: Allocate %u Bytes at %p\n", (unsigned)mem->size, free); + return free; +} + +void memarray_free(memarray_t *mem, void *ptr) +{ + assert((mem != NULL) && (ptr != NULL)); + + memcpy(ptr, &mem->free_data, sizeof(void *)); + mem->free_data = ptr; + DEBUG("memarray: Free %u Bytes at %p\n", (unsigned)mem->size, ptr); +}