Add simple stats

This commit is contained in:
Tilen Majerle 2020-11-24 08:51:37 +01:00
parent fa54b7c34b
commit 8e8f637987
3 changed files with 30 additions and 0 deletions

View File

@ -67,6 +67,14 @@ typedef struct lwmem_block {
or `0` when block is considered free */
} lwmem_block_t;
/**
* \brief Statistics structure
*/
typedef struct {
uint32_t nr_alloc; /*!< Number of all allocated blocks in single instance */
uint32_t nr_free; /*!< Number of frees in the LwMEM instance */
} lwmem_stats_t;
/**
* \brief LwMEM main structure
*/
@ -78,6 +86,9 @@ typedef struct lwmem {
#if LWMEM_CFG_OS || __DOXYGEN__
LWMEM_CFG_OS_MUTEX_HANDLE mutex; /*!< System mutex for OS */
#endif /* LWMEM_CFG_OS || __DOXYGEN__ */
#if LWMEM_CFG_ENABLE_STATS || __DOXYGEN__
lwmem_stats_t stats; /*!< Statistics */
#endif /* LWMEM_CFG_ENABLE_STATS || __DOXYGEN__ */
#if defined(LWMEM_DEV) && !__DOXYGEN__
lwmem_block_t start_block_first_use; /*!< Value of start block for very first time.
This is used only during validation process and is removed in final use */

View File

@ -82,6 +82,14 @@ extern "C" {
#define LWMEM_CFG_ALIGN_NUM ((size_t)4)
#endif
/**
* \brief Enables `1` or disables `0` statistics in the library
*
*/
#ifndef LWMEM_CFG_ENABLE_STATS
#define LWMEM_CFG_ENABLE_STATS 0
#endif
/**
* \}
*/

View File

@ -144,6 +144,13 @@
#define LWMEM_UNPROTECT(lw)
#endif /* !LWMEM_CFG_OS */
/* Statistics part */
#if LWMEM_CFG_ENABLE_STATS
#define LWMEM_INC_STATS(field) (++(field))
#else
#define LWMEM_INC_STATS(field)
#endif /* LWMEM_CFG_ENABLE_STATS */
/**
* \brief LwMEM default structure used by application
*/
@ -387,6 +394,8 @@ prv_alloc(lwmem_t* const lw, const lwmem_region_t* region, const size_t size) {
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_INC_STATS(LWMEM_GET_LW(lw)->stats.nr_alloc);
return retval;
}
@ -403,6 +412,8 @@ prv_free(lwmem_t* const lw, void* const ptr) {
LWMEM_GET_LW(lw)->mem_available_bytes += block->size; /* Increase available bytes */
prv_insert_free_block(lw, block); /* Put block back to list of free block */
LWMEM_INC_STATS(LWMEM_GET_LW(lw)->stats.nr_free);
}
}