Skip to content

Commit

Permalink
OPS-6: Refactor QM_MALLOC into Q_MALLOC
Browse files Browse the repository at this point in the history
  • Loading branch information
liviuchircu committed Mar 15, 2019
1 parent 243fcee commit 8b054a1
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 86 deletions.
2 changes: 1 addition & 1 deletion Makefile.conf.template
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ DEFS+= -DSTATISTICS #Enables the statistics manager
DEFS+= -DHAVE_RESOLV_RES #Support for changing some of the resolver parameters
# Including exactly 1 allocator will cause it to be inlined (fastest)
DEFS+= -DF_MALLOC #Fast memory allocator with minimal runtime overhead
DEFS+= -DQM_MALLOC #Quality assurance memory allocator with runtime safety checks
DEFS+= -DQ_MALLOC #Quality assurance memory allocator with runtime safety checks
DEFS+= -DHP_MALLOC #High performance allocator with fine-grained locking
DEFS+= -DDBG_MALLOC #Include additional, debug-enabled allocator flavors
#DEFS+= -DNO_DEBUG #Turns off all debug messages
Expand Down
21 changes: 11 additions & 10 deletions Makefile.defs
Original file line number Diff line number Diff line change
Expand Up @@ -567,17 +567,18 @@ MKTAGS=ctags -R .
# use mmap instead of SYSV shared memory
# -DPKG_MALLOC
# uses a faster malloc
# -DDBG_MALLOC
# inserts debug code, will cause pkg_malloc and shm_malloc
# to keep and display lot of debuging information: file name,
# function, line number of malloc/free call for each block,
# extra error checking (trying to free the same pointer
# twice, trying to free a pointer alloc'ed with a different
# malloc etc.)
# -DQM_MALLOC
# memory allocator recommended for debugging
# -DF_MALLOC
# an even faster malloc, not recommended for debugging
# fast allocator with minimal overhead, but no fine-grained locking
# -DQ_MALLOC
# decently fast allocator with extra sanity checks + safety buffers
# -DHP_MALLOC
# high performance allocator optimized for shared memory multiprocessing
# -DDBG_MALLOC
# will cause each of the enabled allocators to also include a "debugging"
# flavor of itself; by running a debugging version of an allocator, you
# are able to print a summary of the entire used memory pool (summed up
# used fragments, along with the file/func/line which allocated them).
# Especially useful in quickly troubleshooting memory leaks.
# -DFAST_LOCK
# uses fast architecture specific locking (see the arch. specific section)
# -DUSE_FUTEX
Expand Down
6 changes: 3 additions & 3 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,14 +321,14 @@ void cleanup(int show_status)
some process crashed and let it locked; this will
allow an almost gracious shutdown */
if (0
#if defined F_MALLOC || defined QM_MALLOC
#if defined F_MALLOC || defined Q_MALLOC
|| mem_lock
#endif
#ifdef HP_MALLOC
|| mem_locks
#endif
) {
#if defined HP_MALLOC && (defined F_MALLOC || defined QM_MALLOC)
#if defined HP_MALLOC && (defined F_MALLOC || defined Q_MALLOC)
if (mem_allocator_shm == MM_HP_MALLOC ||
mem_allocator_shm == MM_HP_MALLOC_DBG) {
int i;
Expand Down Expand Up @@ -368,7 +368,7 @@ void cleanup(int show_status)
cleanup_log_level();

if (pt && (0
#if defined F_MALLOC || defined QM_MALLOC
#if defined F_MALLOC || defined Q_MALLOC
|| mem_lock
#endif
#ifdef HP_MALLOC
Expand Down
16 changes: 8 additions & 8 deletions mem/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

#include "meminfo.h"

#if !defined(F_MALLOC) && !defined(QM_MALLOC) && !defined(HP_MALLOC)
#if !defined(F_MALLOC) && !defined(Q_MALLOC) && !defined(HP_MALLOC)
#error "no memory allocator selected"
#endif

Expand All @@ -37,20 +37,20 @@ extern enum osips_mm mem_allocator;
enum osips_mm {
MM_NONE,
MM_F_MALLOC,
MM_QM_MALLOC,
MM_Q_MALLOC,
MM_HP_MALLOC,
MM_F_MALLOC_DBG,
MM_QM_MALLOC_DBG,
MM_Q_MALLOC_DBG,
MM_HP_MALLOC_DBG,
};

#define mm_str(mm) \
((mm) == MM_NONE ? "NONE" : \
(mm) == MM_F_MALLOC ? "F_MALLOC" : \
(mm) == MM_QM_MALLOC ? "QM_MALLOC" : \
(mm) == MM_Q_MALLOC ? "Q_MALLOC" : \
(mm) == MM_HP_MALLOC ? "HP_MALLOC" : \
(mm) == MM_F_MALLOC_DBG ? "F_MALLOC_DBG" : \
(mm) == MM_QM_MALLOC_DBG ? "QM_MALLOC_DBG" : \
(mm) == MM_Q_MALLOC_DBG ? "Q_MALLOC_DBG" : \
(mm) == MM_HP_MALLOC_DBG ? "HP_MALLOC_DBG" : "unknown")

extern void *mem_block;
Expand Down Expand Up @@ -78,7 +78,7 @@ typedef void (*osips_shm_stats_init_f) (void *block, int core_index);
#include "f_malloc.h"
#endif

#if defined QM_MALLOC
#if defined Q_MALLOC
#include "q_malloc.h"
#endif

Expand All @@ -95,9 +95,9 @@ typedef void (*osips_shm_stats_init_f) (void *block, int core_index);
"command line parameter!\n"

/* if exactly an allocator was selected, let's inline it! */
#if ((!defined QM_MALLOC && !defined HP_MALLOC) || \
#if ((!defined Q_MALLOC && !defined HP_MALLOC) || \
(!defined F_MALLOC && !defined HP_MALLOC) || \
(!defined F_MALLOC && !defined QM_MALLOC))
(!defined F_MALLOC && !defined Q_MALLOC))
#define INLINE_ALLOC
#endif

Expand Down
10 changes: 5 additions & 5 deletions mem/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ int init_pkg_mallocs(void)
#ifdef INLINE_ALLOC
#if defined F_MALLOC
mem_block = fm_malloc_init(mem_pool, pkg_mem_size, "pkg");
#elif defined QM_MALLOC
#elif defined Q_MALLOC
mem_block = qm_malloc_init(mem_pool, pkg_mem_size, "pkg");
#elif defined HP_MALLOC
mem_block = hp_pkg_malloc_init(mem_pool, pkg_mem_size, "pkg");
Expand All @@ -96,8 +96,8 @@ int init_pkg_mallocs(void)
gen_pkg_get_frags = (osips_get_mmstat_f)fm_get_frags;
break;
#endif
#ifdef QM_MALLOC
case MM_QM_MALLOC:
#ifdef Q_MALLOC
case MM_Q_MALLOC:
mem_block = qm_malloc_init(mem_pool, pkg_mem_size, "pkg");
gen_pkg_malloc = (osips_malloc_f)qm_malloc;
gen_pkg_realloc = (osips_realloc_f)qm_realloc;
Expand Down Expand Up @@ -145,8 +145,8 @@ int init_pkg_mallocs(void)
gen_pkg_get_frags = (osips_get_mmstat_f)fm_get_frags;
break;
#endif
#ifdef QM_MALLOC
case MM_QM_MALLOC_DBG:
#ifdef Q_MALLOC
case MM_Q_MALLOC_DBG:
mem_block = qm_malloc_init(mem_pool, pkg_mem_size, "pkg");
gen_pkg_malloc = (osips_malloc_f)qm_malloc_dbg;
gen_pkg_realloc = (osips_realloc_f)qm_realloc_dbg;
Expand Down
8 changes: 4 additions & 4 deletions mem/mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ extern unsigned long (*gen_pkg_get_frags)(void *blk);
#define PKG_GET_MUSED() fm_get_max_real_used(mem_block)
#define PKG_GET_FREE() fm_get_free(mem_block)
#define PKG_GET_FRAGS() fm_get_frags(mem_block)
#elif defined QM_MALLOC
#elif defined Q_MALLOC
#define PKG_MALLOC_ qm_malloc
#define PKG_REALLOC qm_realloc
#define PKG_FREE qm_free
Expand Down Expand Up @@ -129,7 +129,7 @@ extern unsigned long (*gen_pkg_get_frags)(void *blk);
#ifdef __SUNPRO_C
#define __FUNCTION__ "" /* gcc specific */
#endif
# if defined F_MALLOC || defined QM_MALLOC || defined HP_MALLOC
# if defined F_MALLOC || defined Q_MALLOC || defined HP_MALLOC
# define pkg_malloc(s) \
PKG_MALLOC_(mem_block, (s),__FILE__, __FUNCTION__, __LINE__)
# define pkg_free(p) \
Expand All @@ -141,7 +141,7 @@ extern unsigned long (*gen_pkg_get_frags)(void *blk);
# error "no memory allocator selected"
# endif
# else
# if defined F_MALLOC || defined QM_MALLOC || defined HP_MALLOC
# if defined F_MALLOC || defined Q_MALLOC || defined HP_MALLOC
# define pkg_malloc(s) PKG_MALLOC_(mem_block, (s))
# define pkg_realloc(p, s) PKG_REALLOC(mem_block, (p), (s))
# define pkg_free(p) PKG_FREE(mem_block, (p))
Expand All @@ -151,7 +151,7 @@ extern unsigned long (*gen_pkg_get_frags)(void *blk);
# endif
# endif

# if defined F_MALLOC || defined HP_MALLOC || defined QM_MALLOC
# if defined F_MALLOC || defined HP_MALLOC || defined Q_MALLOC
# define pkg_status() PKG_STATUS(mem_block)
# else
# error "no memory allocator selected"
Expand Down
20 changes: 10 additions & 10 deletions mem/q_malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifdef QM_MALLOC
#ifdef Q_MALLOC

#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -66,15 +66,15 @@


/* finds the hash value for s, s=QM_ROUNDTO multiple*/
#define GET_HASH(s) ( ((unsigned long)(s)<=QM_MALLOC_OPTIMIZE)?\
#define GET_HASH(s) ( ((unsigned long)(s)<=Q_MALLOC_OPTIMIZE)?\
(unsigned long)(s)/QM_ROUNDTO: \
QM_MALLOC_OPTIMIZE/QM_ROUNDTO+big_hash_idx((s))- \
QM_MALLOC_OPTIMIZE_FACTOR+1 )
Q_MALLOC_OPTIMIZE/QM_ROUNDTO+big_hash_idx((s))- \
Q_MALLOC_OPTIMIZE_FACTOR+1 )

#define UN_HASH(h) ( ((unsigned long)(h)<=(QM_MALLOC_OPTIMIZE/QM_ROUNDTO))?\
#define UN_HASH(h) ( ((unsigned long)(h)<=(Q_MALLOC_OPTIMIZE/QM_ROUNDTO))?\
(unsigned long)(h)*QM_ROUNDTO: \
1UL<<((h)-QM_MALLOC_OPTIMIZE/QM_ROUNDTO+\
QM_MALLOC_OPTIMIZE_FACTOR-1)\
1UL<<((h)-Q_MALLOC_OPTIMIZE/QM_ROUNDTO+\
Q_MALLOC_OPTIMIZE_FACTOR-1)\
)

/* mark/test used/unused frags */
Expand Down Expand Up @@ -194,7 +194,7 @@ struct qm_block* qm_malloc_init(char* address, unsigned long size, char *name)
/* make address and size multiple of 8*/
start=(char*)ROUNDUP((unsigned long) address);
LM_DBG("QM_OPTIMIZE=%lu, /ROUNDTO=%lu\n",
QM_MALLOC_OPTIMIZE, QM_MALLOC_OPTIMIZE/QM_ROUNDTO);
Q_MALLOC_OPTIMIZE, Q_MALLOC_OPTIMIZE/QM_ROUNDTO);
LM_DBG("QM_HASH_SIZE=%lu, qm_block size=%lu\n",
QM_HASH_SIZE, (long)sizeof(struct qm_block));
LM_DBG("params (%p, %lu), start=%p\n", address, size, start);
Expand Down Expand Up @@ -302,11 +302,11 @@ static inline struct qm_frag* qm_find_free(struct qm_block* qm,
return 0;
}

#include "qm_malloc_dyn.h"
#include "q_malloc_dyn.h"

#if !defined INLINE_ALLOC && defined DBG_MALLOC
#undef DBG_MALLOC
#include "qm_malloc_dyn.h"
#include "q_malloc_dyn.h"
#define DBG_MALLOC
#endif

Expand Down
10 changes: 5 additions & 5 deletions mem/q_malloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,20 @@
*/
#endif

#define QM_MALLOC_OPTIMIZE_FACTOR 14UL /*used below */
#define QM_MALLOC_OPTIMIZE ((unsigned long)(1UL<<QM_MALLOC_OPTIMIZE_FACTOR))
#define Q_MALLOC_OPTIMIZE_FACTOR 14UL /*used below */
#define Q_MALLOC_OPTIMIZE ((unsigned long)(1UL<<Q_MALLOC_OPTIMIZE_FACTOR))
/* size to optimize for,
(most allocs <= this size),
must be 2^k */

#define QM_HASH_SIZE ((unsigned long)(QM_MALLOC_OPTIMIZE/QM_ROUNDTO + \
(sizeof(long)*8-QM_MALLOC_OPTIMIZE_FACTOR)+1))
#define QM_HASH_SIZE ((unsigned long)(Q_MALLOC_OPTIMIZE/QM_ROUNDTO + \
(sizeof(long)*8-Q_MALLOC_OPTIMIZE_FACTOR)+1))

#define QM_FRAG(p) \
((struct qm_frag *)((char *)(p) - sizeof(struct qm_frag)))

/* hash structure:
* 0 .... QM_MALLOC_OPTIMIZE/QM_ROUNDTO - small buckets, size increases with
* 0 .... Q_MALLOC_OPTIMIZE/QM_ROUNDTO - small buckets, size increases with
* QM_ROUNDTO from bucket to bucket
* +1 .... end - size = 2^k, big buckets */

Expand Down
Loading

0 comments on commit 8b054a1

Please sign in to comment.