MaJerle/lwmem

Problem of user option LWMEM_CFG_ENABLE_STATS and macro lwmem_get_stats

SuppenSeebi opened this issue · 1 comments

Problem
When LWMEM_CFG_ENABLE_STATS is defined as 0, then the statistics function lwmem_get_stats_ex is removed from header file (see line 116-118 in lwmem.h):

116: #if LWMEM_CFG_ENABLE_STATS || __DOXYGEN__
117: void lwmem_get_stats_ex(lwmem_t* lw, lwmem_stats_t* stats);
118: #endif /* LWMEM_CFG_ENABLE_STATS || __DOXYGEN__ */

When user now uses macro lwmem_get_stats in his code and compiles it, there will be an error, that no reference to the function lwmem_get_stats_ex is defined.

211: #define lwmem_get_stats(stats) lwmem_get_stats_ex(NULL, (stats))

Proposal for fix:
I don't know if this is intended behavior, since this only happens when the statistics function is called while the statistics themselves are turned off. If not i propose the following solutions:

  • If no statisics enabled hide the macro. This will result in a linker error, that the macro is undefined, and not the nested error below.
    #if LWMEM_CFG_ENABLE_STATS || __DOXYGEN__
    #define lwmem_get_stats(stats) lwmem_get_stats_ex(NULL, (stats))
    #endif
  • If no statistics enabled set status input to default values. This will compile and run and will not result in an error or crash. Statistics have no real content though.
    #if LWMEM_CFG_ENABLE_STATS || __DOXYGEN__
    #define lwmem_get_stats(stats) lwmem_get_stats_ex(NULL, (stats))
    #else
    #define lwmem_get_stats(stats) (*stats = (lwmem_stats_t){.mem_size_bytes = 0, .mem_available_bytes = 0, .minimum_ever_mem_available_bytes = 0, .nr_alloc = 0, .nr_free = 0})
    #endif

Thanks to report this - this is expected behavior since if user disabled LWMEM_CFG_ENABLE_STATS then there is no sense to call lwmem_get_stats in the user application.

This is an architectural choice. If user wants to have possibility to enable/disable the feature on the fly (debug/release), it can encapsulate everything to:

#if LWMEM_CFG_ENABLE_STATS
/* your code in your module */
#endif

If you don't do that - you will anyway have a warning about unused variable - for instance.