Skip to content

Commit

Permalink
mem: fix computing of frag size used for debugging in shm free
Browse files Browse the repository at this point in the history
This also fixes compilation when Q_MALLOC is not defined.
  • Loading branch information
rvlad-patrascu committed Feb 7, 2023
1 parent 3d6a4bb commit 8ac57f6
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 13 deletions.
2 changes: 1 addition & 1 deletion mem/f_malloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ void fm_status_dbg(struct fm_block *);
#endif
void fm_info(struct fm_block *, struct mem_info *);

#ifdef SHM_EXTRA_STATS
static inline unsigned long fm_frag_size(void *p)
{
if (!p)
Expand All @@ -137,6 +136,7 @@ static inline unsigned long fm_frag_size(void *p)
return FM_FRAG(p)->size;
}

#ifdef SHM_EXTRA_STATS
void fm_stats_core_init(struct fm_block *fm, int core_index);
unsigned long fm_stats_get_index(void *ptr);
void fm_stats_set_index(void *ptr, unsigned long idx);
Expand Down
2 changes: 1 addition & 1 deletion mem/hp_malloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,6 @@ void *hp_rpm_realloc(struct hp_block *, void *p, unsigned long size);
void *hp_rpm_realloc_unsafe(struct hp_block *, void *p, unsigned long size);
#endif

#ifdef SHM_EXTRA_STATS
static inline unsigned long hp_frag_size(void *p)
{
if (!p)
Expand All @@ -252,6 +251,7 @@ static inline unsigned long hp_frag_size(void *p)
return HP_FRAG(p)->size;
}

#ifdef SHM_EXTRA_STATS
void hp_stats_core_init(struct hp_block *hp, int core_index);
unsigned long hp_stats_get_index(void *ptr);
void hp_stats_set_index(void *ptr, unsigned long idx);
Expand Down
2 changes: 1 addition & 1 deletion mem/q_malloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ void qm_info(struct qm_block*, struct mem_info*);
*/
int qm_mem_check(struct qm_block *qm);

#ifdef SHM_EXTRA_STATS
static inline unsigned long qm_frag_size(void *p)
{
if (!p)
Expand All @@ -193,6 +192,7 @@ static inline unsigned long qm_frag_size(void *p)
return QM_FRAG(p)->size;
}

#ifdef SHM_EXTRA_STATS
void qm_stats_core_init(struct qm_block *qm, int core_index);
unsigned long qm_stats_get_index(void *ptr);
void qm_stats_set_index(void *ptr, unsigned long idx);
Expand Down
33 changes: 29 additions & 4 deletions mem/shm_mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,16 @@ void (*shm_stats_core_init)(void *blk, int core_index);
unsigned long (*shm_stats_get_index)(void *ptr);
void (*shm_stats_set_index)(void *ptr, unsigned long idx);
int shm_frag_overhead;
unsigned long (*shm_frag_size)(void *p);
const char *(*shm_frag_file)(void *p);
const char *(*shm_frag_func)(void *p);
unsigned long (*shm_frag_line)(void *p);
#endif
#endif

#ifndef INLINE_ALLOC
unsigned long (*shm_frag_size)(void *p);
#endif

static str shm_usage_str = { "usage", 5 };
static str shm_threshold_str = { "threshold", 9 };
static str shm_used_str = { "used", 4 };
Expand Down Expand Up @@ -308,7 +311,6 @@ int shm_mem_init_mallocs(void* mempool, unsigned long pool_size)
shm_stats_get_index = fm_stats_get_index;
shm_stats_set_index = fm_stats_set_index;
shm_frag_overhead = FM_FRAG_OVERHEAD;
shm_frag_size = fm_frag_size;
shm_frag_file = fm_frag_file;
shm_frag_func = fm_frag_func;
shm_frag_line = fm_frag_line;
Expand All @@ -321,7 +323,6 @@ int shm_mem_init_mallocs(void* mempool, unsigned long pool_size)
shm_stats_get_index = qm_stats_get_index;
shm_stats_set_index = qm_stats_set_index;
shm_frag_overhead = QM_FRAG_OVERHEAD;
shm_frag_size = qm_frag_size;
shm_frag_file = qm_frag_file;
shm_frag_func = qm_frag_func;
shm_frag_line = qm_frag_line;
Expand All @@ -334,7 +335,6 @@ int shm_mem_init_mallocs(void* mempool, unsigned long pool_size)
shm_stats_get_index = hp_stats_get_index;
shm_stats_set_index = hp_stats_set_index;
shm_frag_overhead = HP_FRAG_OVERHEAD;
shm_frag_size = hp_frag_size;
shm_frag_file = hp_frag_file;
shm_frag_func = hp_frag_func;
shm_frag_line = hp_frag_line;
Expand All @@ -347,6 +347,31 @@ int shm_mem_init_mallocs(void* mempool, unsigned long pool_size)
}
#endif

switch (mem_allocator_shm) {
#ifdef F_MALLOC
case MM_F_MALLOC:
case MM_F_MALLOC_DBG:
shm_frag_size = fm_frag_size;
break;
#endif
#ifdef Q_MALLOC
case MM_Q_MALLOC:
case MM_Q_MALLOC_DBG:
shm_frag_size = qm_frag_size;
break;
#endif
#ifdef HP_MALLOC
case MM_HP_MALLOC:
case MM_HP_MALLOC_DBG:
shm_frag_size = hp_frag_size;
break;
#endif
default:
LM_ERR("current build does not include support for "
"selected allocator (%s)\n", mm_str(mem_allocator_shm));
return -1;
}

switch (mem_allocator_shm) {
#ifdef F_MALLOC
case MM_F_MALLOC:
Expand Down
20 changes: 14 additions & 6 deletions mem/shm_mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
#define shm_stats_get_index fm_stats_get_index
#define shm_stats_set_index fm_stats_set_index
#define shm_frag_overhead FM_FRAG_OVERHEAD
#define shm_frag_size fm_frag_size
#define shm_frag_file fm_frag_file
#define shm_frag_func fm_frag_func
#define shm_frag_line fm_frag_line
Expand All @@ -72,7 +71,6 @@
#define shm_stats_get_index qm_stats_get_index
#define shm_stats_set_index qm_stats_set_index
#define shm_frag_overhead QM_FRAG_OVERHEAD
#define shm_frag_size qm_frag_size
#define shm_frag_file qm_frag_file
#define shm_frag_func qm_frag_func
#define shm_frag_line qm_frag_line
Expand All @@ -81,7 +79,6 @@
#define shm_stats_get_index hp_stats_get_index
#define shm_stats_set_index hp_stats_set_index
#define shm_frag_overhead HP_FRAG_OVERHEAD
#define shm_frag_size hp_frag_size
#define shm_frag_file hp_frag_file
#define shm_frag_func hp_frag_func
#define shm_frag_line hp_frag_line
Expand All @@ -91,13 +88,24 @@ extern void (*shm_stats_core_init)(void *blk, int core_index);
extern unsigned long (*shm_stats_get_index)(void *ptr);
extern void (*shm_stats_set_index)(void *ptr, unsigned long idx);
extern int shm_frag_overhead;
extern unsigned long (*shm_frag_size)(void *p);
extern const char *(*shm_frag_file)(void *p);
extern const char *(*shm_frag_func)(void *p);
extern unsigned long (*shm_frag_line)(void *p);
#endif
#endif

#ifdef INLINE_ALLOC
#ifdef F_MALLOC
#define shm_frag_size fm_frag_size
#elif defined Q_MALLOC
#define shm_frag_size qm_frag_size
#elif defined HP_MALLOC
#define shm_frag_size hp_frag_size
#endif
#else
extern unsigned long (*shm_frag_size)(void *p);
#endif

int set_shm_mm(const char *mm_name);

#ifndef INLINE_ALLOC
Expand Down Expand Up @@ -536,7 +544,7 @@ inline static void _shm_free(void *ptr,
#endif

if (ptr)
size = ((struct qm_frag *)((char *)ptr - sizeof (struct qm_frag)))->size;
size = shm_frag_size(ptr);

SHM_FREE(shm_block, ptr, file, function, line);
shm_threshold_check();
Expand Down Expand Up @@ -581,7 +589,7 @@ inline static void _shm_free_bulk(void *ptr,
#endif

if (ptr)
size = ((struct qm_frag *)((char *)ptr - sizeof (struct qm_frag)))->size;
size = shm_frag_size(ptr);

SHM_FREE(shm_block, ptr, file, function, line);
shm_threshold_check();
Expand Down

0 comments on commit 8ac57f6

Please sign in to comment.