Apply clang-format

This commit is contained in:
Tilen Majerle 2022-09-01 19:05:40 +02:00
parent c16c2a3ef0
commit b268fbbb86
6 changed files with 199 additions and 183 deletions

View File

@ -1,8 +1,8 @@
#include "lwmem/lwmem.h" #include "lwmem/lwmem.h"
#include "lwmem/lwmem.hpp" #include "lwmem/lwmem.hpp"
#include <string.h>
#include <stdint.h>
#include <iostream> #include <iostream>
#include <stdint.h>
#include <string.h>
extern "C" void lwmem_test_run(void); extern "C" void lwmem_test_run(void);
extern "C" void lwmem_test_memory_structure(void); extern "C" void lwmem_test_memory_structure(void);

View File

@ -34,8 +34,8 @@
#ifndef LWMEM_HDR_H #ifndef LWMEM_HDR_H
#define LWMEM_HDR_H #define LWMEM_HDR_H
#include <stdint.h>
#include <limits.h> #include <limits.h>
#include <stdint.h>
#include "lwmem/lwmem_opt.h" #include "lwmem/lwmem_opt.h"
#ifdef __cplusplus #ifdef __cplusplus
@ -59,8 +59,8 @@ extern "C" {
* \brief Memory block structure * \brief Memory block structure
*/ */
typedef struct lwmem_block { typedef struct lwmem_block {
struct lwmem_block* next; /*!< Next free memory block on linked list. struct lwmem_block*
Set to \ref LWMEM_BLOCK_ALLOC_MARK when block is allocated and in use */ next; /*!< Next free memory block on linked list. Set to \ref LWMEM_BLOCK_ALLOC_MARK when block is allocated and in use */
size_t size; /*!< Size of block, including metadata part. size_t size; /*!< Size of block, including metadata part.
MSB bit is set to `1` when block is allocated and in use, MSB bit is set to `1` when block is allocated and in use,
or `0` when block is considered free */ or `0` when block is considered free */
@ -72,7 +72,8 @@ typedef struct lwmem_block {
typedef struct { typedef struct {
uint32_t mem_size_bytes; /*!< Total memory size of all regions combined */ uint32_t mem_size_bytes; /*!< Total memory size of all regions combined */
uint32_t mem_available_bytes; /*!< Free memory available for allocation */ uint32_t mem_available_bytes; /*!< Free memory available for allocation */
uint32_t minimum_ever_mem_available_bytes; /*!< Minimum amount of total free memory there has been in the heap since the system booted. */ uint32_t
minimum_ever_mem_available_bytes; /*!< Minimum amount of total free memory there has been in the heap since the system booted. */
uint32_t nr_alloc; /*!< Number of all allocated blocks in single instance */ uint32_t nr_alloc; /*!< Number of all allocated blocks in single instance */
uint32_t nr_free; /*!< Number of frees in the LwMEM instance */ uint32_t nr_free; /*!< Number of frees in the LwMEM instance */
} lwmem_stats_t; } lwmem_stats_t;

View File

@ -43,7 +43,6 @@
* \{ * \{
*/ */
namespace Lwmem { namespace Lwmem {
/** /**
@ -62,15 +61,12 @@ void* ptr = mngr.malloc(...);
mngr.free(ptr); mngr.free(ptr);
\endcode \endcode
*/ */
template<size_t LEN> template <size_t LEN>
class LwmemLight { class LwmemLight {
public: public:
LwmemLight() { LwmemLight() {
/* Simple region descriptor with one region */ /* Simple region descriptor with one region */
const lwmem_region_t regions[] = { const lwmem_region_t regions[] = {{m_reg_data, sizeof(m_reg_data)}, {NULL, 0}};
{m_reg_data, sizeof(m_reg_data)},
{NULL, 0}
};
lwmem_assignmem_ex(&m_lw, regions); lwmem_assignmem_ex(&m_lw, regions);
} }
@ -119,7 +115,7 @@ public:
lwmem_free_ex(&m_lw, ptr); lwmem_free_ex(&m_lw, ptr);
} }
private: private:
/* Delete unused constructors */ /* Delete unused constructors */
LwmemLight(const LwmemLight& other) = delete; LwmemLight(const LwmemLight& other) = delete;
/* Delete copy assignment operators */ /* Delete copy assignment operators */
@ -130,7 +126,7 @@ private:
uint8_t m_reg_data[LEN]; uint8_t m_reg_data[LEN];
}; };
}; }; // namespace Lwmem
/** /**
* \} * \}

View File

@ -69,7 +69,7 @@ extern "C" {
* definition before you define handle type * definition before you define handle type
*/ */
#ifndef LWMEM_CFG_OS_MUTEX_HANDLE #ifndef LWMEM_CFG_OS_MUTEX_HANDLE
#define LWMEM_CFG_OS_MUTEX_HANDLE void * #define LWMEM_CFG_OS_MUTEX_HANDLE void*
#endif #endif
/** /**

View File

@ -34,8 +34,8 @@
#ifndef LWMEM_HDR_SYS_H #ifndef LWMEM_HDR_SYS_H
#define LWMEM_HDR_SYS_H #define LWMEM_HDR_SYS_H
#include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include <stdint.h>
#include "lwmem/lwmem.h" #include "lwmem/lwmem.h"
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -31,9 +31,9 @@
* Author: Tilen MAJERLE <tilen@majerle.eu> * Author: Tilen MAJERLE <tilen@majerle.eu>
* Version: v2.0.0 * Version: v2.0.0
*/ */
#include "lwmem/lwmem.h"
#include <limits.h> #include <limits.h>
#include <string.h> #include <string.h>
#include "lwmem/lwmem.h"
#if LWMEM_CFG_OS #if LWMEM_CFG_OS
#include "system/lwmem_sys.h" #include "system/lwmem_sys.h"
@ -74,7 +74,7 @@
* \brief Cast input pointer to byte * \brief Cast input pointer to byte
* \param[in] p: Input pointer to cast to byte pointer * \param[in] p: Input pointer to cast to byte pointer
*/ */
#define LWMEM_TO_BYTE_PTR(p) ((uint8_t *)(p)) #define LWMEM_TO_BYTE_PTR(p) ((uint8_t*)(p))
/** /**
* \brief Bit indicating memory block is allocated * \brief Bit indicating memory block is allocated
@ -90,25 +90,34 @@
* \brief Set block as allocated * \brief Set block as allocated
* \param[in] block: Block to set as allocated * \param[in] block: Block to set as allocated
*/ */
#define LWMEM_BLOCK_SET_ALLOC(block) do { if ((block) != NULL) { (block)->size |= LWMEM_ALLOC_BIT; (block)->next = (void *)(LWMEM_TO_BYTE_PTR(0) + LWMEM_BLOCK_ALLOC_MARK); }} while (0) #define LWMEM_BLOCK_SET_ALLOC(block) \
do { \
if ((block) != NULL) { \
(block)->size |= LWMEM_ALLOC_BIT; \
(block)->next = (void*)(LWMEM_TO_BYTE_PTR(0) + LWMEM_BLOCK_ALLOC_MARK); \
} \
} while (0)
/** /**
* \brief Check if input block is properly allocated and valid * \brief Check if input block is properly allocated and valid
* \param[in] block: Block to check if properly set as allocated * \param[in] block: Block to check if properly set as allocated
*/ */
#define LWMEM_BLOCK_IS_ALLOC(block) ((block) != NULL && ((block)->size & LWMEM_ALLOC_BIT) && (block)->next == (void *)(LWMEM_TO_BYTE_PTR(0) + LWMEM_BLOCK_ALLOC_MARK)) #define LWMEM_BLOCK_IS_ALLOC(block) \
((block) != NULL && ((block)->size & LWMEM_ALLOC_BIT) \
&& (block)->next == (void*)(LWMEM_TO_BYTE_PTR(0) + LWMEM_BLOCK_ALLOC_MARK))
/** /**
* \brief Get block handle from application pointer * \brief Get block handle from application pointer
* \param[in] ptr: Input pointer to get block from * \param[in] ptr: Input pointer to get block from
*/ */
#define LWMEM_GET_BLOCK_FROM_PTR(ptr) (void *)((ptr) != NULL ? ((LWMEM_TO_BYTE_PTR(ptr)) - LWMEM_BLOCK_META_SIZE) : NULL) #define LWMEM_GET_BLOCK_FROM_PTR(ptr) (void*)((ptr) != NULL ? ((LWMEM_TO_BYTE_PTR(ptr)) - LWMEM_BLOCK_META_SIZE) : NULL)
/** /**
* \brief Get block handle from application pointer * \brief Get block handle from application pointer
* \param[in] block: Input pointer to get block from * \param[in] block: Input pointer to get block from
*/ */
#define LWMEM_GET_PTR_FROM_BLOCK(block) (void *)((block) != NULL ? ((LWMEM_TO_BYTE_PTR(block)) + LWMEM_BLOCK_META_SIZE) : NULL) #define LWMEM_GET_PTR_FROM_BLOCK(block) \
(void*)((block) != NULL ? ((LWMEM_TO_BYTE_PTR(block)) + LWMEM_BLOCK_META_SIZE) : NULL)
/** /**
* \brief Minimum amount of memory required to make new empty block * \brief Minimum amount of memory required to make new empty block
@ -130,11 +139,10 @@
* \param[in] in_pp: Previous previous of input block * \param[in] in_pp: Previous previous of input block
* \param[in] in_p: Previous of input block * \param[in] in_p: Previous of input block
*/ */
#define LWMEM_GET_PREV_CURR_OF_BLOCK(in_lw, in_b, in_pp, in_p) do { \ #define LWMEM_GET_PREV_CURR_OF_BLOCK(in_lw, in_b, in_pp, in_p) \
for ((in_pp) = NULL, (in_p) = &((in_lw)->start_block); \ do { \
(in_p) != NULL && (in_p)->next < (in_b); \ for ((in_pp) = NULL, (in_p) = &((in_lw)->start_block); (in_p) != NULL && (in_p)->next < (in_b); \
(in_pp) = (in_p), (in_p) = (in_p)->next \ (in_pp) = (in_p), (in_p) = (in_p)->next) {} \
) {} \
} while (0) } while (0)
#if LWMEM_CFG_OS #if LWMEM_CFG_OS
@ -148,7 +156,8 @@
/* Statistics part */ /* Statistics part */
#if LWMEM_CFG_ENABLE_STATS #if LWMEM_CFG_ENABLE_STATS
#define LWMEM_INC_STATS(field) (++(field)) #define LWMEM_INC_STATS(field) (++(field))
#define LWMEM_UPDATE_MIN_FREE(lw) do { \ #define LWMEM_UPDATE_MIN_FREE(lw) \
do { \
if ((lw)->mem_available_bytes < (lw)->stats.minimum_ever_mem_available_bytes) { \ if ((lw)->mem_available_bytes < (lw)->stats.minimum_ever_mem_available_bytes) { \
(lw)->stats.minimum_ever_mem_available_bytes = (lw)->mem_available_bytes; \ (lw)->stats.minimum_ever_mem_available_bytes = (lw)->mem_available_bytes; \
} \ } \
@ -182,7 +191,7 @@ prv_get_region_addr_size(const lwmem_region_t* region, uint8_t** msa, size_t* m
*ms = 0; *ms = 0;
/* Check region size and align it to config bits */ /* Check region size and align it to config bits */
mem_size = region->size & ~LWMEM_ALIGN_BITS;/* Size does not include lower bits */ mem_size = region->size & ~LWMEM_ALIGN_BITS; /* Size does not include lower bits */
if (mem_size < (2 * LWMEM_BLOCK_MIN_SIZE)) { if (mem_size < (2 * LWMEM_BLOCK_MIN_SIZE)) {
return 0; return 0;
} }
@ -273,10 +282,12 @@ prv_insert_free_block(lwmem_t* const lw, lwmem_block_t* nb) {
*/ */
if (prev->next != NULL && prev->next->size > 0 /* Do not remove "end of region" indicator in each region */ if (prev->next != NULL && prev->next->size > 0 /* Do not remove "end of region" indicator in each region */
&& (LWMEM_TO_BYTE_PTR(nb) + nb->size) == LWMEM_TO_BYTE_PTR(prev->next)) { && (LWMEM_TO_BYTE_PTR(nb) + nb->size) == LWMEM_TO_BYTE_PTR(prev->next)) {
if (prev->next == lw->end_block) {/* Does it points to the end? */ if (prev->next == lw->end_block) { /* Does it points to the end? */
nb->next = lw->end_block; /* Set end block pointer */ nb->next = lw->end_block; /* Set end block pointer */
} else { } else {
nb->size += prev->next->size; /* Expand of current block for size of next free block which is right behind new block */ nb->size +=
prev->next
->size; /* Expand of current block for size of next free block which is right behind new block */
nb->next = prev->next->next; /* Next free is pointed to the next one of previous next */ nb->next = prev->next->next; /* Next free is pointed to the next one of previous next */
} }
} else { } else {
@ -306,7 +317,7 @@ prv_split_too_big_block(lwmem_t* const lw, lwmem_block_t* block, size_t new_bloc
uint8_t success = 0; uint8_t success = 0;
is_alloc_bit = block->size & LWMEM_ALLOC_BIT; /* Check if allocation bit is set */ is_alloc_bit = block->size & LWMEM_ALLOC_BIT; /* Check if allocation bit is set */
block_size = block->size & ~LWMEM_ALLOC_BIT;/* Use size without allocated bit */ block_size = block->size & ~LWMEM_ALLOC_BIT; /* Use size without allocated bit */
/* /*
* If current block size is greater than requested size, * If current block size is greater than requested size,
@ -318,7 +329,7 @@ prv_split_too_big_block(lwmem_t* const lw, lwmem_block_t* block, size_t new_bloc
next->size = block_size - new_block_size; /* Modify block data */ next->size = block_size - new_block_size; /* Modify block data */
block->size = new_block_size; /* Current size is now smaller */ block->size = new_block_size; /* Current size is now smaller */
lw->mem_available_bytes += next->size;/* Increase available bytes by new block size */ lw->mem_available_bytes += next->size; /* Increase available bytes by new block size */
prv_insert_free_block(lw, next); /* Add new block to the free list */ prv_insert_free_block(lw, next); /* Add new block to the free list */
success = 1; success = 1;
@ -345,7 +356,7 @@ prv_split_too_big_block(lwmem_t* const lw, lwmem_block_t* block, size_t new_bloc
*/ */
static void* static void*
prv_alloc(lwmem_t* const lw, const lwmem_region_t* region, const size_t size) { prv_alloc(lwmem_t* const lw, const lwmem_region_t* region, const size_t size) {
lwmem_block_t* prev, *curr; lwmem_block_t *prev, *curr;
void* retval = NULL; void* retval = NULL;
/* Calculate final size including meta data size */ /* Calculate final size including meta data size */
@ -388,7 +399,7 @@ prv_alloc(lwmem_t* const lw, const lwmem_region_t* region, const size_t size) {
if ((uint8_t*)curr < (uint8_t*)region_start_addr) { /* Check if we reached region */ if ((uint8_t*)curr < (uint8_t*)region_start_addr) { /* Check if we reached region */
continue; continue;
} }
if ((uint8_t*)curr >= (uint8_t*)(region_start_addr + region_size)) {/* Check if we are out already */ if ((uint8_t*)curr >= (uint8_t*)(region_start_addr + region_size)) { /* Check if we are out already */
return NULL; return NULL;
} }
if (curr->size >= final_size) { if (curr->size >= final_size) {
@ -401,7 +412,7 @@ prv_alloc(lwmem_t* const lw, const lwmem_region_t* region, const size_t size) {
* Loop until size of current block is smaller than requested final size * Loop until size of current block is smaller than requested final size
*/ */
for (; curr != NULL && curr->size < final_size; prev = curr, curr = curr->next) { for (; curr != NULL && curr->size < final_size; prev = curr, curr = curr->next) {
if (curr->next == NULL || curr == lw->end_block) {/* If no more blocks available */ if (curr->next == NULL || curr == lw->end_block) { /* If no more blocks available */
return NULL; /* No sufficient memory available to allocate block of memory */ return NULL; /* No sufficient memory available to allocate block of memory */
} }
} }
@ -418,7 +429,7 @@ prv_alloc(lwmem_t* const lw, const lwmem_region_t* region, const size_t size) {
/* curr block is now removed from linked list */ /* curr block is now removed from linked list */
lw->mem_available_bytes -= curr->size;/* Decrease available bytes by allocated block size */ lw->mem_available_bytes -= curr->size; /* Decrease available bytes by allocated block size */
prv_split_too_big_block(lw, curr, final_size); /* Split block if it is too big */ prv_split_too_big_block(lw, curr, final_size); /* Split block if it is too big */
LWMEM_BLOCK_SET_ALLOC(curr); /* Set block as allocated */ LWMEM_BLOCK_SET_ALLOC(curr); /* Set block as allocated */
@ -466,9 +477,10 @@ prv_free(lwmem_t* const lw, void* const ptr) {
*/ */
static void* static void*
prv_realloc(lwmem_t* const lw, const lwmem_region_t* region, void* const ptr, const size_t size) { prv_realloc(lwmem_t* const lw, const lwmem_region_t* region, void* const ptr, const size_t size) {
lwmem_block_t* block, *prevprev, *prev; lwmem_block_t *block, *prevprev, *prev;
size_t block_size; /* Holds size of input block (ptr), including metadata size */ size_t block_size; /* Holds size of input block (ptr), including metadata size */
const size_t final_size = LWMEM_ALIGN(size) + LWMEM_BLOCK_META_SIZE;/* Holds size of new requested block size, including metadata size */ const size_t final_size =
LWMEM_ALIGN(size) + LWMEM_BLOCK_META_SIZE; /* Holds size of new requested block size, including metadata size */
void* retval; /* Return pointer, used with LWMEM_RETURN macro */ void* retval; /* Return pointer, used with LWMEM_RETURN macro */
/* Check optional input parameters */ /* Check optional input parameters */
@ -490,7 +502,7 @@ prv_realloc(lwmem_t* const lw, const lwmem_region_t* region, void* const ptr, co
/* Process existing block */ /* Process existing block */
block = LWMEM_GET_BLOCK_FROM_PTR(ptr); block = LWMEM_GET_BLOCK_FROM_PTR(ptr);
if (LWMEM_BLOCK_IS_ALLOC(block)) { if (LWMEM_BLOCK_IS_ALLOC(block)) {
block_size = block->size & ~LWMEM_ALLOC_BIT;/* Get actual block size, without memory allocation bit */ block_size = block->size & ~LWMEM_ALLOC_BIT; /* Get actual block size, without memory allocation bit */
/* Check current block size is the same as new requested size */ /* Check current block size is the same as new requested size */
if (block_size == final_size) { if (block_size == final_size) {
@ -540,7 +552,8 @@ prv_realloc(lwmem_t* const lw, const lwmem_region_t* region, void* const ptr, co
prev->next = (void*)(LWMEM_TO_BYTE_PTR(prev->next) - (block_size - final_size)); prev->next = (void*)(LWMEM_TO_BYTE_PTR(prev->next) - (block_size - final_size));
prev->next->size = tmp_size + (block_size - final_size); prev->next->size = tmp_size + (block_size - final_size);
prev->next->next = tmp_next; prev->next->next = tmp_next;
lw->mem_available_bytes += block_size - final_size; /* Increase available bytes by increase of free block */ lw->mem_available_bytes +=
block_size - final_size; /* Increase available bytes by increase of free block */
block->size = final_size; /* Block size is requested size */ block->size = final_size; /* Block size is requested size */
} }
@ -564,7 +577,8 @@ prv_realloc(lwmem_t* const lw, const lwmem_region_t* region, void* const ptr, co
/* Input block points to address somewhere between "prev" and "prev->next" pointers */ /* Input block points to address somewhere between "prev" and "prev->next" pointers */
/* Check if "block" and next free "prev->next" create contiguous memory with size of at least new requested size */ /* Check if "block" and next free "prev->next" create contiguous memory with size of at least new requested size */
if ((LWMEM_TO_BYTE_PTR(block) + block_size) == LWMEM_TO_BYTE_PTR(prev->next)/* Blocks create contiguous block */ if ((LWMEM_TO_BYTE_PTR(block) + block_size)
== LWMEM_TO_BYTE_PTR(prev->next) /* Blocks create contiguous block */
&& (block_size + prev->next->size) >= final_size) { /* Size is greater or equal to requested */ && (block_size + prev->next->size) >= final_size) { /* Size is greater or equal to requested */
/* /*
@ -573,8 +587,9 @@ prv_realloc(lwmem_t* const lw, const lwmem_region_t* region, void* const ptr, co
*/ */
lw->mem_available_bytes -= prev->next->size; /* For now decrease effective available bytes */ lw->mem_available_bytes -= prev->next->size; /* For now decrease effective available bytes */
LWMEM_UPDATE_MIN_FREE(lw); LWMEM_UPDATE_MIN_FREE(lw);
block->size = block_size + prev->next->size;/* Increase effective size of new block */ block->size = block_size + prev->next->size; /* Increase effective size of new block */
prev->next = prev->next->next; /* Set next to next's next, effectively remove expanded block from free list */ prev->next =
prev->next->next; /* Set next to next's next, effectively remove expanded block from free list */
prv_split_too_big_block(lw, block, final_size); /* Split block if it is too big */ prv_split_too_big_block(lw, block, final_size); /* Split block if it is too big */
LWMEM_BLOCK_SET_ALLOC(block); /* Set block as allocated */ LWMEM_BLOCK_SET_ALLOC(block); /* Set block as allocated */
@ -602,10 +617,11 @@ prv_realloc(lwmem_t* const lw, const lwmem_region_t* region, void* const ptr, co
*/ */
LWMEM_MEMMOVE(new_data_ptr, old_data_ptr, block_size); LWMEM_MEMMOVE(new_data_ptr, old_data_ptr, block_size);
lw->mem_available_bytes -= prev->size;/* For now decrease effective available bytes */ lw->mem_available_bytes -= prev->size; /* For now decrease effective available bytes */
LWMEM_UPDATE_MIN_FREE(lw); LWMEM_UPDATE_MIN_FREE(lw);
prev->size += block_size; /* Increase size of input block size */ prev->size += block_size; /* Increase size of input block size */
prevprev->next = prev->next; /* Remove prev from free list as it is now being used for allocation together with existing block */ prevprev->next =
prev->next; /* Remove prev from free list as it is now being used for allocation together with existing block */
block = prev; /* Move block pointer to previous one */ block = prev; /* Move block pointer to previous one */
prv_split_too_big_block(lw, block, final_size); /* Split block if it is too big */ prv_split_too_big_block(lw, block, final_size); /* Split block if it is too big */
@ -621,9 +637,11 @@ prv_realloc(lwmem_t* const lw, const lwmem_region_t* region, void* const ptr, co
* Last option is to check if previous free block "prev", input block "block" and next free block "prev->next" create contiguous block * Last option is to check if previous free block "prev", input block "block" and next free block "prev->next" create contiguous block
* and size of new block (from 3 contiguous blocks) together is big enough * and size of new block (from 3 contiguous blocks) together is big enough
*/ */
if ((LWMEM_TO_BYTE_PTR(prev) + prev->size) == LWMEM_TO_BYTE_PTR(block) /* Input block and free block before create contiguous block */ if ((LWMEM_TO_BYTE_PTR(prev) + prev->size)
&& (LWMEM_TO_BYTE_PTR(block) + block_size) == LWMEM_TO_BYTE_PTR(prev->next) /* Input block and free block after create contiguous block */ == LWMEM_TO_BYTE_PTR(block) /* Input block and free block before create contiguous block */
&& (prev->size + block_size + prev->next->size) >= final_size) {/* Size is greater or equal to requested */ && (LWMEM_TO_BYTE_PTR(block) + block_size)
== LWMEM_TO_BYTE_PTR(prev->next) /* Input block and free block after create contiguous block */
&& (prev->size + block_size + prev->next->size) >= final_size) { /* Size is greater or equal to requested */
/* Move memory from block to block previous to current */ /* Move memory from block to block previous to current */
void* const old_data_ptr = LWMEM_GET_PTR_FROM_BLOCK(block); void* const old_data_ptr = LWMEM_GET_PTR_FROM_BLOCK(block);
@ -639,10 +657,15 @@ prv_realloc(lwmem_t* const lw, const lwmem_region_t* region, void* const ptr, co
*/ */
LWMEM_MEMMOVE(new_data_ptr, old_data_ptr, block_size); /* Copy old buffer size to new location */ LWMEM_MEMMOVE(new_data_ptr, old_data_ptr, block_size); /* Copy old buffer size to new location */
lw->mem_available_bytes -= prev->size + prev->next->size; /* Decrease effective available bytes for free blocks before and after input block */ lw->mem_available_bytes -=
prev->size
+ prev->next
->size; /* Decrease effective available bytes for free blocks before and after input block */
LWMEM_UPDATE_MIN_FREE(lw); LWMEM_UPDATE_MIN_FREE(lw);
prev->size += block_size + prev->next->size;/* Increase size of new block by size of 2 free blocks */ prev->size += block_size + prev->next->size; /* Increase size of new block by size of 2 free blocks */
prevprev->next = prev->next->next; /* Remove free block before current one and block after current one from linked list (remove 2) */ prevprev->next =
prev->next
->next; /* Remove free block before current one and block after current one from linked list (remove 2) */
block = prev; /* Previous block is now current */ block = prev; /* Previous block is now current */
prv_split_too_big_block(lw, block, final_size); /* Split block if it is too big */ prv_split_too_big_block(lw, block, final_size); /* Split block if it is too big */
@ -665,7 +688,8 @@ prv_realloc(lwmem_t* const lw, const lwmem_region_t* region, void* const ptr, co
*/ */
retval = prv_alloc(lw, region, size); /* Try to allocate new block */ retval = prv_alloc(lw, region, size); /* Try to allocate new block */
if (retval != NULL) { if (retval != NULL) {
block_size = (block->size & ~LWMEM_ALLOC_BIT) - LWMEM_BLOCK_META_SIZE; /* Get application size from input pointer */ block_size =
(block->size & ~LWMEM_ALLOC_BIT) - LWMEM_BLOCK_META_SIZE; /* Get application size from input pointer */
LWMEM_MEMCPY(retval, ptr, size > block_size ? block_size : size); /* Copy content to new allocated block */ LWMEM_MEMCPY(retval, ptr, size > block_size ? block_size : size); /* Copy content to new allocated block */
prv_free(lw, ptr); /* Free input pointer */ prv_free(lw, ptr); /* Free input pointer */
} }
@ -695,12 +719,11 @@ size_t
lwmem_assignmem_ex(lwmem_t* lw, const lwmem_region_t* regions) { lwmem_assignmem_ex(lwmem_t* lw, const lwmem_region_t* regions) {
uint8_t* mem_start_addr; uint8_t* mem_start_addr;
size_t mem_size, len = 0; size_t mem_size, len = 0;
lwmem_block_t* first_block, *prev_end_block; lwmem_block_t *first_block, *prev_end_block;
lw = LWMEM_GET_LW(lw); lw = LWMEM_GET_LW(lw);
/* Check first things first */ /* Check first things first */
if (regions == NULL if (regions == NULL || lw->end_block != NULL /* Init function may only be called once per lwmem instance */
|| lw->end_block != NULL /* Init function may only be called once per lwmem instance */
|| (((size_t)LWMEM_CFG_ALIGN_NUM) & (((size_t)LWMEM_CFG_ALIGN_NUM) - 1)) > 0) { /* Must be power of 2 */ || (((size_t)LWMEM_CFG_ALIGN_NUM) & (((size_t)LWMEM_CFG_ALIGN_NUM) - 1)) > 0) { /* Must be power of 2 */
return 0; return 0;
} }
@ -780,7 +803,7 @@ lwmem_assignmem_ex(lwmem_t* lw, const lwmem_region_t* regions) {
* Actual maximal available size for application in the region is mem_size - 2 * MEM_BLOCK_META_SIZE * Actual maximal available size for application in the region is mem_size - 2 * MEM_BLOCK_META_SIZE
*/ */
first_block = (void*)mem_start_addr; first_block = (void*)mem_start_addr;
first_block->next = lw->end_block;/* Next block of first is last block */ first_block->next = lw->end_block; /* Next block of first is last block */
first_block->size = mem_size - LWMEM_BLOCK_META_SIZE; first_block->size = mem_size - LWMEM_BLOCK_META_SIZE;
/* Check if previous regions exist by checking previous end block state */ /* Check if previous regions exist by checking previous end block state */
@ -1082,11 +1105,7 @@ print_block(size_t i, lwmem_block_t* block) {
is_free = (block->size & LWMEM_ALLOC_BIT) == 0 && block != &lwmem_default.start_block_first_use && block->size > 0; is_free = (block->size & LWMEM_ALLOC_BIT) == 0 && block != &lwmem_default.start_block_first_use && block->size > 0;
block_size = block->size & ~LWMEM_ALLOC_BIT; block_size = block->size & ~LWMEM_ALLOC_BIT;
printf("| %5d | %16p | %6d | %4d | %16d |", printf("| %5d | %16p | %6d | %4d | %16d |", (int)i, (void*)block, (int)is_free, (int)block_size,
(int)i,
(void*)block,
(int)is_free,
(int)block_size,
(int)(is_free ? (block_size - LWMEM_BLOCK_META_SIZE) : 0)); (int)(is_free ? (block_size - LWMEM_BLOCK_META_SIZE) : 0));
if (block == &lwmem_default.start_block_first_use) { if (block == &lwmem_default.start_block_first_use) {
printf(" Start block "); printf(" Start block ");