1.新增内存管理。

This commit is contained in:
MacRsh
2023-11-21 18:16:25 +08:00
parent f624c2369d
commit 2e2db79854
3 changed files with 160 additions and 2 deletions

View File

@@ -20,6 +20,7 @@ extern "C" {
* @addtogroup Memory.
* @{
*/
int mr_heap_init(void);
void *mr_malloc(size_t size);
void mr_free(void *memory);
/** @} */

View File

@@ -123,6 +123,20 @@ extern "C" {
*/
#define mr_min(a, b) ({ __typeof__ (a) _a = (a); __typeof__ (b) _b = (b); _a < _b ? _a : _b; })
/**
* @brief This macro function aligns the size up to a multiple of 4.
*
* @param size The size to align.
*/
#define mr_align4_up(size) (((size) + 3) & (~3))
/**
* @brief This macro function aligns a size down to a multiple of 4.
*
* @param size The size to align.
*/
#define mr_align4_down(size) ((size) & (~3))
/**
* @brief This macro function checks if a list is empty.
*

View File

@@ -52,6 +52,89 @@ MR_WEAK void mr_interrupt_enable(void)
}
/**
* @brief Heap memory.
*/
#ifndef MR_CFG_HEAP_SIZE
#define MR_CFG_HEAP_SIZE (4 * 1024) /* If not defined, use 4KB */
#endif /* MR_CFG_HEAP_SIZE */
static uint8_t heap_mem[MR_CFG_HEAP_SIZE] = {0};
#define MR_HEAP_BLOCK_FREE (0)
#define MR_HEAP_BLOCK_ALLOCATED (1)
#define MR_HEAP_BLOCK_MIN_SIZE (sizeof(struct mr_heap_block) << 1)
/**
* @brief Heap block structure.
*/
static struct mr_heap_block
{
struct mr_heap_block *next;
uint32_t size: 31;
uint32_t allocated: 1;
} heap_start = {MR_NULL, 0, MR_HEAP_BLOCK_FREE};
/**
* @brief This function initialize the heap.
*
* @return MR_ERR_OK on success, otherwise an error code.
*/
int mr_heap_init(void)
{
struct mr_heap_block *first_block = (struct mr_heap_block *)&heap_mem;
/* Initialize the first block */
first_block->next = MR_NULL;
first_block->size = sizeof(heap_mem) - sizeof(struct mr_heap_block);
first_block->allocated = MR_HEAP_BLOCK_FREE;
/* Initialize the heap */
heap_start.next = first_block;
return MR_EOK;
}
MR_INIT_BOARD_EXPORT(mr_heap_init);
static void heap_insert_block(struct mr_heap_block *block)
{
struct mr_heap_block *block_prev = &heap_start;
/* Search for the previous block */
while (((block_prev->next != MR_NULL) && ((uint32_t)block_prev->next < (uint32_t)block)))
{
block_prev = block_prev->next;
}
/* Insert the block */
if (block_prev->next != MR_NULL)
{
/* Merge with the previous block */
if ((void *)(((uint8_t *)block_prev) + sizeof(struct mr_heap_block) + block_prev->size) == (void *)block)
{
block_prev->size += block->size + sizeof(struct mr_heap_block);
block = block_prev;
}
/* Merge with the next block */
if ((void *)(((uint8_t *)block) + sizeof(struct mr_heap_block) + block->size) == (void *)block_prev->next)
{
block->size += block_prev->next->size + sizeof(struct mr_heap_block);
block->next = block_prev->next->next;
if (block != block_prev)
{
block_prev->next = block;
block = block_prev;
}
}
}
/* Insert the block */
if (block != block_prev)
{
block->next = block_prev->next;
block_prev->next = block;
}
}
/**
* @brief This function allocate memory.
*
@@ -61,7 +144,55 @@ MR_WEAK void mr_interrupt_enable(void)
*/
MR_WEAK void *mr_malloc(size_t size)
{
return malloc(size);
struct mr_heap_block *block_prev = &heap_start;
struct mr_heap_block *block = block_prev->next;
void *memory = MR_NULL;
size_t residual = 0;
/* Check size and residual memory */
if ((size == 0) || (size > (UINT32_MAX >> 1) || (block == MR_NULL)))
{
return MR_NULL;
}
/* Align the size up 4 bytes */
size = mr_align4_up(size);
/* Search for and take blocks that match the criteria */
while (block->size < size)
{
if (block->next == MR_NULL)
{
return MR_NULL;
}
block_prev = block;
block = block->next;
}
block_prev->next = block->next;
/* Allocate memory */
memory = (void *)((uint8_t *)block) + sizeof(struct mr_heap_block);
residual = block->size - size;
/* Set the block information */
block->size = size;
block->next = MR_NULL;
block->allocated = MR_HEAP_BLOCK_ALLOCATED;
/* Check if we need to allocate a new block */
if (residual > MR_HEAP_BLOCK_MIN_SIZE)
{
struct mr_heap_block *new_block = (struct mr_heap_block *)(((uint8_t *)memory) + size);
/* Set the new block information */
new_block->size = residual - sizeof(struct mr_heap_block);
new_block->next = MR_NULL;
new_block->allocated = MR_HEAP_BLOCK_FREE;
/* Insert the new block */
heap_insert_block(new_block);
}
return memory;
}
/**
@@ -71,7 +202,19 @@ MR_WEAK void *mr_malloc(size_t size)
*/
MR_WEAK void mr_free(void *memory)
{
free(memory);
if (memory != MR_NULL)
{
struct mr_heap_block *block = (struct mr_heap_block *)((uint8_t *)memory - sizeof(struct mr_heap_block));
/* Check the block */
if (block->allocated == MR_HEAP_BLOCK_ALLOCATED && block->size != 0)
{
block->allocated = MR_HEAP_BLOCK_FREE;
/* Insert the free block */
heap_insert_block(block);
}
}
}
/**