mirror of
https://github.com/MaJerle/lwmem.git
synced 2025-01-26 06:02:54 +08:00
Add option to get user size
This commit is contained in:
parent
c0c2c5f133
commit
1a2aab4fb5
@ -99,9 +99,11 @@ void* lwmem_realloc_ex(lwmem_t* const lw, const lwmem_region_t* region
|
|||||||
unsigned char lwmem_realloc_s_ex(lwmem_t* const lw, const lwmem_region_t* region, void** const ptr, const size_t size);
|
unsigned char lwmem_realloc_s_ex(lwmem_t* const lw, const lwmem_region_t* region, void** const ptr, const size_t size);
|
||||||
void lwmem_free_ex(lwmem_t* const lw, void* const ptr);
|
void lwmem_free_ex(lwmem_t* const lw, void* const ptr);
|
||||||
void lwmem_free_s_ex(lwmem_t* const lw, void** const ptr);
|
void lwmem_free_s_ex(lwmem_t* const lw, void** const ptr);
|
||||||
|
size_t lwmem_get_size_ex(lwmem_t* const lw, void* ptr);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \note This is a wrapper for \ref lwmem_assignmem_ex function
|
* \note This is a wrapper for \ref lwmem_assignmem_ex function.
|
||||||
|
* It operates in default LwMEM instance and uses first available region for memory operations
|
||||||
* \param[in] regions: Array of regions with address and its size.
|
* \param[in] regions: Array of regions with address and its size.
|
||||||
* Regions must be in increasing order (start address) and must not overlap in-between
|
* Regions must be in increasing order (start address) and must not overlap in-between
|
||||||
* \param[in] len: Number of regions in array
|
* \param[in] len: Number of regions in array
|
||||||
@ -124,7 +126,8 @@ void lwmem_free_s_ex(lwmem_t* const lw, void** const ptr);
|
|||||||
#define lwmem_calloc(nitems, size) lwmem_calloc_ex(NULL, NULL, (nitems), (size))
|
#define lwmem_calloc(nitems, size) lwmem_calloc_ex(NULL, NULL, (nitems), (size))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \note This is a wrapper for \ref lwmem_realloc_ex function
|
* \note This is a wrapper for \ref lwmem_realloc_ex function.
|
||||||
|
* It operates in default LwMEM instance and uses first available region for memory operations
|
||||||
* \param[in] ptr: Memory block previously allocated with one of allocation functions.
|
* \param[in] ptr: Memory block previously allocated with one of allocation functions.
|
||||||
* It may be set to `NULL` to create new clean allocation
|
* It may be set to `NULL` to create new clean allocation
|
||||||
* \param[in] size: Size of new memory to reallocate
|
* \param[in] size: Size of new memory to reallocate
|
||||||
@ -132,7 +135,8 @@ void lwmem_free_s_ex(lwmem_t* const lw, void** const ptr);
|
|||||||
#define lwmem_realloc(ptr, size) lwmem_realloc_ex(NULL, NULL, (ptr), (size))
|
#define lwmem_realloc(ptr, size) lwmem_realloc_ex(NULL, NULL, (ptr), (size))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \note This is a wrapper for \ref lwmem_realloc_s_ex function
|
* \note This is a wrapper for \ref lwmem_realloc_s_ex function.
|
||||||
|
* It operates in default LwMEM instance and uses first available region for memory operations
|
||||||
* \param[in] ptrptr: Pointer to pointer to allocated memory. Must not be set to `NULL`.
|
* \param[in] ptrptr: Pointer to pointer to allocated memory. Must not be set to `NULL`.
|
||||||
* If reallocation is successful, it modified where pointer points to,
|
* If reallocation is successful, it modified where pointer points to,
|
||||||
* or sets it to `NULL` in case of `free` operation
|
* or sets it to `NULL` in case of `free` operation
|
||||||
@ -141,18 +145,28 @@ void lwmem_free_s_ex(lwmem_t* const lw, void** const ptr);
|
|||||||
#define lwmem_realloc_s(ptrptr, size) lwmem_realloc_s_ex(NULL, NULL, (ptrptr), (size))
|
#define lwmem_realloc_s(ptrptr, size) lwmem_realloc_s_ex(NULL, NULL, (ptrptr), (size))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \note This is a wrapper for \ref lwmem_free_ex function
|
* \note This is a wrapper for \ref lwmem_free_ex function.
|
||||||
|
* It operates in default LwMEM instance and uses first available region for memory operations
|
||||||
* \param[in] ptr: Memory to free. `NULL` pointer is valid input
|
* \param[in] ptr: Memory to free. `NULL` pointer is valid input
|
||||||
*/
|
*/
|
||||||
#define lwmem_free(ptr) lwmem_free_ex(NULL, (ptr))
|
#define lwmem_free(ptr) lwmem_free_ex(NULL, (ptr))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \note This is a wrapper for \ref lwmem_free_s_ex function
|
* \note This is a wrapper for \ref lwmem_free_s_ex function.
|
||||||
|
* It operates in default LwMEM instance and uses first available region for memory operations
|
||||||
* \param[in] ptrptr: Pointer to pointer to allocated memory.
|
* \param[in] ptrptr: Pointer to pointer to allocated memory.
|
||||||
* When set to non `NULL`, pointer is freed and set to `NULL`
|
* When set to non `NULL`, pointer is freed and set to `NULL`
|
||||||
*/
|
*/
|
||||||
#define lwmem_free_s(ptrptr) lwmem_free_s_ex(NULL, (ptrptr))
|
#define lwmem_free_s(ptrptr) lwmem_free_s_ex(NULL, (ptrptr))
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \note This is a wrapper for \ref lwmem_get_size_ex function.
|
||||||
|
* It operates in default LwMEM instance and uses first available region for memory operations
|
||||||
|
* \param[in] ptr: Pointer to allocated memory
|
||||||
|
* \return Block size for user in units of bytes
|
||||||
|
*/
|
||||||
|
#define lwmem_get_size(ptr) lwmem_get_size(NULL, (ptr))
|
||||||
|
|
||||||
#if defined(LWMEM_DEV) && !__DOXYGEN__
|
#if defined(LWMEM_DEV) && !__DOXYGEN__
|
||||||
unsigned char lwmem_debug_create_regions(lwmem_region_t** regs_out, size_t count, size_t size);
|
unsigned char lwmem_debug_create_regions(lwmem_region_t** regs_out, size_t count, size_t size);
|
||||||
void lwmem_debug_save_state(void);
|
void lwmem_debug_save_state(void);
|
||||||
|
@ -902,6 +902,29 @@ lwmem_free_s_ex(lwmem_t* const lw, void** const ptr) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Get user size of allocated memory
|
||||||
|
* \param[in] lw: LwMEM instance. Set to `NULL` to use default instance.
|
||||||
|
* Instance must be the same as used during allocation procedure
|
||||||
|
* \param[in] ptr: Pointer to allocated memory
|
||||||
|
* \return Block size for user in units of bytes
|
||||||
|
*/
|
||||||
|
size_t
|
||||||
|
lwmem_get_size_ex(lwmem_t* const lw, void* ptr) {
|
||||||
|
lwmem_block_t* block;
|
||||||
|
uint32_t len = 0;
|
||||||
|
|
||||||
|
if (ptr != NULL) {
|
||||||
|
LWMEM_PROTECT(lw);
|
||||||
|
block = LWMEM_GET_BLOCK_FROM_PTR(ptr);
|
||||||
|
if (LWMEM_BLOCK_IS_ALLOC(block)) {
|
||||||
|
len = (block->size & ~LWMEM_ALLOC_BIT) - LWMEM_BLOCK_META_SIZE;
|
||||||
|
}
|
||||||
|
LWMEM_UNPROTECT(lw);
|
||||||
|
}
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
/* Part of library used ONLY for LWMEM_DEV purposes */
|
/* Part of library used ONLY for LWMEM_DEV purposes */
|
||||||
/* To validate and test library */
|
/* To validate and test library */
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user