add option to disable realloc and free

This commit is contained in:
Tilen M 2024-10-06 13:20:40 +02:00
parent 05942ae576
commit 54ff6c1aee
5 changed files with 48 additions and 15 deletions

View File

@ -3,6 +3,7 @@
## Develop
- Rework library CMake with removed INTERFACE type
- Add `LWMEM_CFG_SUPPORT_REALLOC_AND_FREE` to disable realloc and free functions
## v2.1.0

View File

@ -110,11 +110,13 @@ typedef struct {
size_t lwmem_assignmem_ex(lwmem_t* lwobj, const lwmem_region_t* regions);
void* lwmem_malloc_ex(lwmem_t* lwobj, const lwmem_region_t* region, const size_t size);
void* lwmem_calloc_ex(lwmem_t* lwobj, const lwmem_region_t* region, const size_t nitems, const size_t size);
#if LWMEM_CFG_SUPPORT_REALLOC_AND_FREE || __DOXYGEN__
void* lwmem_realloc_ex(lwmem_t* lwobj, const lwmem_region_t* region, void* const ptr, const size_t size);
uint8_t lwmem_realloc_s_ex(lwmem_t* lwobj, const lwmem_region_t* region, void** const ptr, const size_t size);
void lwmem_free_ex(lwmem_t* lwobj, void* const ptr);
void lwmem_free_s_ex(lwmem_t* lwobj, void** const ptr);
size_t lwmem_get_size_ex(lwmem_t* lwobj, void* ptr);
#endif /* LWMEM_CFG_SUPPORT_REALLOC_AND_FREE || __DOXYGEN__ */
#if LWMEM_CFG_ENABLE_STATS || __DOXYGEN__
void lwmem_get_stats_ex(lwmem_t* lwobj, lwmem_stats_t* stats);
#endif /* LWMEM_CFG_ENABLE_STATS || __DOXYGEN__ */
@ -136,7 +138,7 @@ lwmem_region_t regions[] = {
\endcode
* \return `0` on failure, number of final regions used for memory manager on success
*/
#define lwmem_assignmem(regions) lwmem_assignmem_ex(NULL, (regions))
#define lwmem_assignmem(regions) lwmem_assignmem_ex(NULL, (regions))
/**
* \note This is a wrapper for \ref lwmem_malloc_ex function.
@ -145,7 +147,7 @@ lwmem_region_t regions[] = {
* \return Pointer to allocated memory on success, `NULL` otherwise
* \note This function is thread safe when \ref LWMEM_CFG_OS is enabled
*/
#define lwmem_malloc(size) lwmem_malloc_ex(NULL, NULL, (size))
#define lwmem_malloc(size) lwmem_malloc_ex(NULL, NULL, (size))
/**
* \note This is a wrapper for \ref lwmem_calloc_ex function.
@ -155,7 +157,9 @@ lwmem_region_t regions[] = {
* \return Pointer to allocated memory on success, `NULL` otherwise
* \note This function is thread safe when \ref LWMEM_CFG_OS is enabled
*/
#define lwmem_calloc(nitems, size) lwmem_calloc_ex(NULL, NULL, (nitems), (size))
#define lwmem_calloc(nitems, size) lwmem_calloc_ex(NULL, NULL, (nitems), (size))
#if LWMEM_CFG_SUPPORT_REALLOC_AND_FREE || __DOXYGEN__
/**
* \note This is a wrapper for \ref lwmem_realloc_ex function.
@ -205,12 +209,18 @@ lwmem_region_t regions[] = {
*/
#define lwmem_get_size(ptr) lwmem_get_size_ex(NULL, (ptr))
#endif /* LWMEM_CFG_SUPPORT_REALLOC_AND_FREE || __DOXYGEN__ */
#if LWMEM_CFG_ENABLE_STATS || __DOXYGEN__
/**
* \note This is a wrapper for \ref lwmem_get_stats_ex function.
* It operates in default LwMEM instance
* \param[in] ptr: Pointer to lwmem_stats_t to store result
*/
#define lwmem_get_stats(stats) lwmem_get_stats_ex(NULL, (stats))
#define lwmem_get_stats(stats) lwmem_get_stats_ex(NULL, (stats))
#endif /* LWMEM_CFG_ENABLE_STATS || __DOXYGEN__ */
#if defined(LWMEM_DEV) && !__DOXYGEN__
unsigned char lwmem_debug_create_regions(lwmem_region_t** regs_out, size_t count, size_t size);

View File

@ -93,6 +93,7 @@ class LwmemLight {
return lwmem_calloc_ex(&m_lw, nullptr, nitems, size);
}
#if LWMEM_CFG_SUPPORT_REALLOC_AND_FREE || __DOXYGEN__
/**
* \brief Reallocate block of memory
* \param ptr: Pointer to previously allocated memory block
@ -114,6 +115,7 @@ class LwmemLight {
free(void* ptr) {
lwmem_free_ex(&m_lw, ptr);
}
#endif /* LWMEM_CFG_SUPPORT_REALLOC_AND_FREE || __DOXYGEN__ */
private:
/* Delete unused constructors */

View File

@ -85,6 +85,21 @@ extern "C" {
#define LWMEM_CFG_ALIGN_NUM ((size_t)4)
#endif
/**
* \brief Enables `1` or disables `0` full memory management support.
*
* When enabled, library supports allocation, reallocation and freeing of the memory.
* When disabled, library only supports allocation, which is useful for
* power up memory initialization only.
*
* \note When disabled, \ref lwmem_get_size_ex is also not available,
* as it is assumed that user won't frequently ask for size of
* previously allocated block if realloc isn't being used
*/
#ifndef LWMEM_CFG_SUPPORT_REALLOC_AND_FREE
#define LWMEM_CFG_SUPPORT_REALLOC_AND_FREE 1
#endif
/**
* \brief Enables `1` or disables `0` memory cleanup on free operation (or realloc).
*

View File

@ -435,6 +435,8 @@ prv_alloc(lwmem_t* const lwobj, const lwmem_region_t* region, const size_t size)
return retval;
}
#if LWMEM_CFG_SUPPORT_REALLOC_AND_FREE
/**
* \brief Free input pointer
* \param[in] lwobj: LwMEM instance. Set to `NULL` to use default instance
@ -653,15 +655,12 @@ prv_realloc(lwmem_t* const lwobj, const lwmem_region_t* region, void* const ptr,
*/
LWMEM_MEMMOVE(new_data_ptr, old_data_ptr, block_size); /* Copy old buffer size to new location */
lwobj->mem_available_bytes -=
prev->size
+ prev->next
->size; /* Decrease effective available bytes for free blocks before and after input block */
/* Decrease effective available bytes for free blocks before and after input block */
lwobj->mem_available_bytes -= prev->size + prev->next->size;
LWMEM_UPDATE_MIN_FREE(lwobj);
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) */
/* Remove free block before current one and block after current one from linked list (remove 2) */
prevprev->next = prev->next->next;
block = prev; /* Previous block is now current */
prv_split_too_big_block(lwobj, block, final_size); /* Split block if it is too big */
@ -684,14 +683,16 @@ prv_realloc(lwmem_t* const lwobj, const lwmem_region_t* region, void* const ptr,
*/
retval = prv_alloc(lwobj, region, size); /* Try to allocate new block */
if (retval != NULL) {
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 */
prv_free(lwobj, ptr); /* Free input pointer */
/* Get application size from input pointer, then copy content to new block */
block_size = (block->size & ~LWMEM_ALLOC_BIT) - LWMEM_BLOCK_META_SIZE;
LWMEM_MEMCPY(retval, ptr, size > block_size ? block_size : size);
prv_free(lwobj, ptr); /* Free old block */
}
return retval;
}
#endif /* LWMEM_CFG_SUPPORT_REALLOC_AND_FREE */
/**
* \brief Initializes and assigns user regions for memory used by allocator algorithm
* \param[in] lwobj: LwMEM instance. Set to `NULL` to use default instance
@ -873,6 +874,8 @@ lwmem_calloc_ex(lwmem_t* lwobj, const lwmem_region_t* region, const size_t nitem
return ptr;
}
#if LWMEM_CFG_SUPPORT_REALLOC_AND_FREE || __DOXYGEN__
/**
* \brief Reallocates already allocated memory with new size in specific lwmem instance and region.
*
@ -1022,6 +1025,8 @@ lwmem_get_size_ex(lwmem_t* lwobj, void* ptr) {
return len;
}
#endif /* LWMEM_CFG_SUPPORT_REALLOC_AND_FREE || __DOXYGEN__ */
#if LWMEM_CFG_ENABLE_STATS || __DOXYGEN__
/**