mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2025-12-25 22:43:50 +01:00
Merge pull request #15683 from bergzand/pr/memarray/extender
memarray: Add functions to extend and reduce the pool with new memory areas
This commit is contained in:
commit
e046e01254
@ -1,5 +1,6 @@
|
||||
/*
|
||||
* Copyright (C) 2018 Tobias Heider <heidert@nm.ifi.lmu.de>
|
||||
* 2020 Koen Zandberg <koen@bergzand.net>
|
||||
*
|
||||
* 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
|
||||
@ -13,6 +14,7 @@
|
||||
*
|
||||
* @brief pseudo dynamic allocation in static memory arrays
|
||||
* @author Tobias Heider <heidert@nm.ifi.lmu.de>
|
||||
* @author Koen Zandberg <koen@bergzand.net>
|
||||
*/
|
||||
|
||||
#ifndef MEMARRAY_H
|
||||
@ -20,7 +22,7 @@
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
@ -33,9 +35,19 @@ extern "C" {
|
||||
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 Memory pool element
|
||||
*
|
||||
* Internal memarray element struct to increase code readability
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
typedef struct memarray_element {
|
||||
struct memarray_element *next; /**< Pointer to the next element */
|
||||
} memarray_element_t;
|
||||
|
||||
/**
|
||||
* @brief Initialize memarray pool with free list
|
||||
*
|
||||
@ -47,7 +59,7 @@ typedef struct {
|
||||
* @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
|
||||
* @param[in] num number of elements in @p data
|
||||
*/
|
||||
void memarray_init(memarray_t *mem, void *data, size_t size, size_t num);
|
||||
|
||||
@ -110,6 +122,44 @@ static inline void memarray_free(memarray_t *mem, void *ptr)
|
||||
mem->free_data = ptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Extend the memarray with a new memory region
|
||||
*
|
||||
* This function extends the memarray pool with a new memory region. The region
|
||||
* must be able to fit the supplied number of elements of the size used when
|
||||
* initializing this memarray.
|
||||
*
|
||||
* @pre `mem != NULL`
|
||||
* @pre `data != NULL`
|
||||
* @pre `num != 0`
|
||||
*
|
||||
* @param[in,out] mem memarray pool to extend
|
||||
* @param[in] data pointer to user-allocated data
|
||||
* @param[in] num number of elements in @p data
|
||||
*/
|
||||
void memarray_extend(memarray_t *mem, void *data, size_t num);
|
||||
|
||||
/**
|
||||
* @brief Reduce the memarray space, subtracting the memory pool
|
||||
*
|
||||
* It is up to the user to free all chunks in the reduced pool. The function
|
||||
* will check if all elements in the pool are freed.
|
||||
*
|
||||
* @param[in,out] mem memarray pool to reduce
|
||||
* @param[in] data pointer to the user-allocated data to reduce
|
||||
* @param[in] num number of elements to reduce the data pool with
|
||||
*/
|
||||
int memarray_reduce(memarray_t *mem, void *data, size_t num);
|
||||
|
||||
/**
|
||||
* @brief Returns the number of blocks available
|
||||
*
|
||||
* @param[in] mem memarray pool
|
||||
*
|
||||
* @returns Number of elements available in the memarray pool
|
||||
*/
|
||||
size_t memarray_available(memarray_t *mem);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
/*
|
||||
* Copyright (C) 2018 Tobias Heider <heidert@nm.ifi.lmu.de>
|
||||
* 2020 Koen Zandberg <koen@bergzand.net>
|
||||
*
|
||||
* 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
|
||||
@ -8,6 +9,7 @@
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "memarray.h"
|
||||
|
||||
#define ENABLE_DEBUG 0
|
||||
@ -21,13 +23,90 @@ void memarray_init(memarray_t *mem, void *data, size_t size, size_t num)
|
||||
DEBUG("memarray: Initialize memarray of %u times %u Bytes at %p\n",
|
||||
(unsigned)num, (unsigned)size, data);
|
||||
|
||||
mem->free_data = data;
|
||||
mem->free_data = NULL;
|
||||
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 *));
|
||||
}
|
||||
memset(((char *)mem->free_data) + ((mem->num - 1) * (mem->size)), 0, sizeof(void *));
|
||||
memarray_extend(mem, data, num);
|
||||
}
|
||||
|
||||
void memarray_extend(memarray_t *mem, void *data, size_t num)
|
||||
{
|
||||
for (uint8_t *element = data;
|
||||
element < (uint8_t*)data + (num * mem->size);
|
||||
element += mem->size) {
|
||||
memarray_free(mem, element);
|
||||
}
|
||||
}
|
||||
|
||||
static bool _in_pool(const memarray_t *mem, const void *data, size_t num,
|
||||
const void *element)
|
||||
{
|
||||
return element >= data &&
|
||||
(uint8_t*)element < ((uint8_t*)data + (mem->size * num));
|
||||
}
|
||||
|
||||
int memarray_reduce(memarray_t *mem, void *data, size_t num)
|
||||
{
|
||||
/* Number of free chunks found inside the pool to be freed */
|
||||
size_t remaining = num;
|
||||
|
||||
/* Keep a clist around of found elements in case we need to restore */
|
||||
memarray_element_t *found_elements = NULL;
|
||||
/* Cast it to memarray_element_t, makes things easier to read */
|
||||
memarray_element_t **element_ptr = (memarray_element_t**)&mem->free_data;
|
||||
|
||||
while (*element_ptr) {
|
||||
DEBUG("memarray: At element %p -> %p\n",
|
||||
(void*)*element_ptr, (void*)(*element_ptr)->next);
|
||||
if (_in_pool(mem, data, num, *element_ptr)) {
|
||||
|
||||
/* Save the element */
|
||||
memarray_element_t *found_element = *element_ptr;
|
||||
DEBUG("memarray: Found %p in %p, at %u\n",
|
||||
(void*)found_element, data, (unsigned)remaining);
|
||||
|
||||
/* Copy pointer over to previous element remove it from the pool
|
||||
* free list */
|
||||
memcpy(element_ptr, (*element_ptr), sizeof(void*));
|
||||
|
||||
if (found_elements) {
|
||||
found_element->next = found_elements->next;
|
||||
found_elements->next = found_element;
|
||||
}
|
||||
else {
|
||||
found_element->next = found_element;
|
||||
}
|
||||
found_elements = found_element;
|
||||
|
||||
remaining--;
|
||||
if (remaining == 0) {
|
||||
/* Early return if all elements are removed */
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else {
|
||||
element_ptr = &(*element_ptr)->next;
|
||||
}
|
||||
}
|
||||
|
||||
/* Not all elements found in free list, re-add them to the memarray */
|
||||
DEBUG("memarray: Missing elements, restoring");
|
||||
if (found_elements) {
|
||||
void *swap = mem->free_data;
|
||||
mem->free_data = found_elements->next;
|
||||
found_elements->next = swap;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
size_t memarray_available(memarray_t *mem)
|
||||
{
|
||||
size_t num = 0;
|
||||
void **element = &mem->free_data;
|
||||
while (*element) {
|
||||
element = (void**)*element;
|
||||
num++;
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
@ -1,3 +1,7 @@
|
||||
BOARD_INSUFFICIENT_MEMORY := \
|
||||
arduino-duemilanove \
|
||||
arduino-nano \
|
||||
arduino-uno \
|
||||
atmega328p \
|
||||
nucleo-l011k4 \
|
||||
#
|
||||
|
||||
@ -32,11 +32,11 @@
|
||||
#endif
|
||||
|
||||
#ifndef NUMBER_OF_TESTS
|
||||
#define NUMBER_OF_TESTS (12)
|
||||
#define NUMBER_OF_TESTS (1)
|
||||
#endif
|
||||
|
||||
#ifndef NUMBER_OF_LOOPS
|
||||
#define NUMBER_OF_LOOPS (2)
|
||||
#define NUMBER_OF_LOOPS (1)
|
||||
#endif
|
||||
|
||||
|
||||
@ -50,6 +50,7 @@ struct block_t {
|
||||
};
|
||||
|
||||
struct block_t block_storage_data[MAX_NUMBER_BLOCKS];
|
||||
struct block_t block_storage_data_extend[MAX_NUMBER_BLOCKS];
|
||||
memarray_t block_storage;
|
||||
|
||||
int total = 0;
|
||||
@ -141,6 +142,48 @@ int main(void)
|
||||
count++;
|
||||
}
|
||||
|
||||
puts("Extend and reduce tests");
|
||||
|
||||
printf("Memarray available: %u\n",
|
||||
(unsigned)memarray_available(&block_storage));
|
||||
|
||||
/* Extend with second block */
|
||||
memarray_extend(&block_storage, block_storage_data_extend,
|
||||
MAX_NUMBER_BLOCKS);
|
||||
printf("Memarray available: %u\n",
|
||||
(unsigned)memarray_available(&block_storage));
|
||||
|
||||
/* remove the original block */
|
||||
int res = memarray_reduce(&block_storage, block_storage_data,
|
||||
MAX_NUMBER_BLOCKS);
|
||||
printf("Memarray reduction: %d available: %u\n",
|
||||
res, (unsigned)memarray_available(&block_storage));
|
||||
|
||||
/* try to remove original block a second time */
|
||||
res = memarray_reduce(&block_storage, block_storage_data,
|
||||
MAX_NUMBER_BLOCKS);
|
||||
printf("Memarray reduction: %d available: %u\n",
|
||||
res, (unsigned)memarray_available(&block_storage));
|
||||
|
||||
/* remove the extension block */
|
||||
res = memarray_reduce(&block_storage, block_storage_data_extend,
|
||||
MAX_NUMBER_BLOCKS);
|
||||
printf("Memarray reduction: %d available: %u\n",
|
||||
res, (unsigned)memarray_available(&block_storage));
|
||||
|
||||
/* extend again with the original block */
|
||||
memarray_extend(&block_storage, block_storage_data, MAX_NUMBER_BLOCKS);
|
||||
|
||||
/* remove one element */
|
||||
memarray_alloc(&block_storage);
|
||||
printf("Memarray available: %u\n",
|
||||
(unsigned)memarray_available(&block_storage));
|
||||
|
||||
/* try to reduce with a missing element */
|
||||
res = memarray_reduce(&block_storage, block_storage_data, MAX_NUMBER_BLOCKS);
|
||||
printf("Memarray reduction: %d available: %u\n",
|
||||
res, (unsigned)memarray_available(&block_storage));
|
||||
|
||||
printf("Finishing\n");
|
||||
_ps_handler(0, NULL);
|
||||
|
||||
|
||||
@ -27,6 +27,20 @@ def testfunc(child):
|
||||
for i in range(max_number_blocks):
|
||||
child.expect(r'Free \({}\) \d+ Bytes at 0x[a-z0-9]+,'
|
||||
' total [0-9]+\r\n'.format(i))
|
||||
|
||||
child.expect_exact("Extend and reduce tests")
|
||||
|
||||
child.expect_exact("Memarray available: {}".format(max_number_blocks))
|
||||
child.expect_exact("Memarray available: {}".format(2 * max_number_blocks))
|
||||
child.expect_exact("Memarray reduction: 0 available: {}"
|
||||
"".format(max_number_blocks))
|
||||
child.expect_exact("Memarray reduction: -1 available: {}"
|
||||
"".format(max_number_blocks))
|
||||
child.expect_exact("Memarray reduction: 0 available: 0")
|
||||
child.expect_exact("Memarray available: {}".format(max_number_blocks - 1))
|
||||
child.expect_exact("Memarray reduction: -1 available: {}"
|
||||
"".format(max_number_blocks - 1))
|
||||
|
||||
child.expect_exact("Finishing")
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user