mirror of
https://github.com/MaJerle/lwmem.git
synced 2025-01-13 21:42:53 +08:00
Add simple stats
This commit is contained in:
parent
fa54b7c34b
commit
8e8f637987
@ -67,6 +67,14 @@ typedef struct lwmem_block {
|
|||||||
or `0` when block is considered free */
|
or `0` when block is considered free */
|
||||||
} lwmem_block_t;
|
} 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
|
* \brief LwMEM main structure
|
||||||
*/
|
*/
|
||||||
@ -78,6 +86,9 @@ typedef struct lwmem {
|
|||||||
#if LWMEM_CFG_OS || __DOXYGEN__
|
#if LWMEM_CFG_OS || __DOXYGEN__
|
||||||
LWMEM_CFG_OS_MUTEX_HANDLE mutex; /*!< System mutex for OS */
|
LWMEM_CFG_OS_MUTEX_HANDLE mutex; /*!< System mutex for OS */
|
||||||
#endif /* LWMEM_CFG_OS || __DOXYGEN__ */
|
#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__
|
#if defined(LWMEM_DEV) && !__DOXYGEN__
|
||||||
lwmem_block_t start_block_first_use; /*!< Value of start block for very first time.
|
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 */
|
This is used only during validation process and is removed in final use */
|
||||||
|
@ -82,6 +82,14 @@ extern "C" {
|
|||||||
#define LWMEM_CFG_ALIGN_NUM ((size_t)4)
|
#define LWMEM_CFG_ALIGN_NUM ((size_t)4)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Enables `1` or disables `0` statistics in the library
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
#ifndef LWMEM_CFG_ENABLE_STATS
|
||||||
|
#define LWMEM_CFG_ENABLE_STATS 0
|
||||||
|
#endif
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \}
|
* \}
|
||||||
*/
|
*/
|
||||||
|
@ -144,6 +144,13 @@
|
|||||||
#define LWMEM_UNPROTECT(lw)
|
#define LWMEM_UNPROTECT(lw)
|
||||||
#endif /* !LWMEM_CFG_OS */
|
#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
|
* \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 */
|
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 */
|
||||||
|
|
||||||
|
LWMEM_INC_STATS(LWMEM_GET_LW(lw)->stats.nr_alloc);
|
||||||
|
|
||||||
return retval;
|
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 */
|
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 */
|
prv_insert_free_block(lw, block); /* Put block back to list of free block */
|
||||||
|
|
||||||
|
LWMEM_INC_STATS(LWMEM_GET_LW(lw)->stats.nr_free);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user