Skip to content

Commit

Permalink
core: mem - qmalloc - cast size to unsigned long in debug messages
Browse files Browse the repository at this point in the history
- better for compatibility with C sepcs older than C99
- newer specs allow %zu, but 'z' specifier is not in older
  specs, %lu being the safer approach
  • Loading branch information
miconda committed Jun 8, 2016
1 parent 7e8b679 commit f17ac87
Showing 1 changed file with 28 additions and 16 deletions.
44 changes: 28 additions & 16 deletions mem/q_malloc.c
Expand Up @@ -188,21 +188,24 @@ struct qm_block* qm_malloc_init(char* address, unsigned long size, int type)
DBG("qm_malloc_init: QM_OPTIMIZE=%lu, /ROUNDTO=%lu\n",
QM_MALLOC_OPTIMIZE, QM_MALLOC_OPTIMIZE/ROUNDTO);
DBG("qm_malloc_init: QM_HASH_SIZE=%lu, qm_block size=%lu\n",
QM_HASH_SIZE, (long)sizeof(struct qm_block));
DBG("qm_malloc_init(%p, %lu), start=%p\n", address, size, start);
QM_HASH_SIZE, (unsigned long)sizeof(struct qm_block));
DBG("qm_malloc_init(%p, %lu), start=%p\n", address,
(unsigned long)size, start);
if (size<start-address) return 0;
size-=(start-address);
if (size <(MIN_FRAG_SIZE+FRAG_OVERHEAD)) return 0;
size=ROUNDDOWN(size);

init_overhead=ROUNDUP(sizeof(struct qm_block))+sizeof(struct qm_frag)+
sizeof(struct qm_frag_end);
DBG("qm_malloc_init: size= %lu, init_overhead=%lu\n", size, init_overhead);
DBG("qm_malloc_init: size= %lu, init_overhead=%lu\n",
(unsigned long)size, init_overhead);

if (size < init_overhead)
{
/* not enough mem to create our control structures !!!*/
LOG(L_ERR, "qm_malloc_init(%lu); No memory left to create control structures!\n", size);
LOG(L_ERR, "qm_malloc_init(%lu); No memory left to create control structures!\n",
(unsigned long)size);
return 0;
}
end=start+size;
Expand Down Expand Up @@ -286,7 +289,8 @@ static inline struct qm_frag* qm_find_free(struct qm_block* qm,
/*try in a bigger bucket*/
}
/* not found */
LOG(L_ERR, "qm_find_free(%p, %lu); Free fragment not found!\n", qm, size);
LOG(L_ERR, "qm_find_free(%p, %lu); Free fragment not found!\n",
qm, (unsigned long)size);
return 0;
}

Expand Down Expand Up @@ -361,8 +365,8 @@ void* qm_malloc(void* qmp, size_t size)

#ifdef DBG_QM_MALLOC
list_cntr = 0;
MDBG("qm_malloc(%p, %lu) called from %s: %s(%d)\n", qm, size, file, func,
line);
MDBG("qm_malloc(%p, %lu) called from %s: %s(%d)\n",
qm, (unsigned long)size, file, func, line);
#endif
/*malloc(0) should return a valid pointer according to specs*/
if(unlikely(size==0)) size=4;
Expand Down Expand Up @@ -406,7 +410,8 @@ void* qm_malloc(void* qmp, size_t size)
FRAG_END(f)->check2=END_CHECK_PATTERN2;*/
MDBG("qm_malloc(%p, %lu) returns address %p frag. %p (size=%lu) on %d"
" -th hit\n",
qm, size, (char*)f+sizeof(struct qm_frag), f, f->size, list_cntr );
qm, (unsigned long)size, (char*)f+sizeof(struct qm_frag), f,
f->size, list_cntr);
#endif
#ifdef MALLOC_STATS
if(qm->type==MEM_TYPE_PKG) {
Expand All @@ -417,9 +422,11 @@ void* qm_malloc(void* qmp, size_t size)
}

#ifdef DBG_QM_MALLOC
LOG(L_ERR, "qm_malloc(%p, %lu) called from %s: %s(%d), module: %s; Free fragment not found!\n", qm, size, file, func, line, mname);
LOG(L_ERR, "qm_malloc(%p, %lu) called from %s: %s(%d), module: %s; Free fragment not found!\n",
qm, (unsigned long)size, file, func, line, mname);
#else
LOG(L_ERR, "qm_malloc(%p, %lu); Free fragment not found!\n", qm, size);
LOG(L_ERR, "qm_malloc(%p, %lu); Free fragment not found!\n",
qm, (unsigned long)size);
#endif

return 0;
Expand Down Expand Up @@ -568,7 +575,8 @@ void* qm_realloc(void* qmp, void* p, size_t size)
qm = (struct qm_block*)qmp;

#ifdef DBG_QM_MALLOC
MDBG("qm_realloc(%p, %p, %lu) called from %s: %s(%d)\n", qm, p, size,
MDBG("qm_realloc(%p, %p, %lu) called from %s: %s(%d)\n",
qm, p, (unsigned long)size,
file, func, line);
if ((p)&&(p>(void*)qm->last_frag_end || p<(void*)qm->first_frag)){
LOG(L_CRIT, "BUG: qm_free: bad pointer %p (out of memory block!) - "
Expand Down Expand Up @@ -609,7 +617,8 @@ void* qm_realloc(void* qmp, void* p, size_t size)
orig_size=f->size;
/* shrink */
#ifdef DBG_QM_MALLOC
MDBG("qm_realloc: shrinking from %lu to %lu\n", f->size, size);
MDBG("qm_realloc: shrinking from %lu to %lu\n",
f->size, (unsigned long)size);
if(split_frag(qm, f, size, file, "fragm. from qm_realloc", line, mname)!=0){
MDBG("qm_realloc : shrinked successful\n");
#else
Expand All @@ -626,7 +635,8 @@ void* qm_realloc(void* qmp, void* p, size_t size)
}else if (f->size < size){
/* grow */
#ifdef DBG_QM_MALLOC
MDBG("qm_realloc: growing from %lu to %lu\n", f->size, size);
MDBG("qm_realloc: growing from %lu to %lu\n",
f->size, (unsigned long)size);
#endif
orig_size=f->size;
diff=size-f->size;
Expand Down Expand Up @@ -664,9 +674,11 @@ void* qm_realloc(void* qmp, void* p, size_t size)
memcpy(ptr, p, orig_size);
} else {
#ifdef DBG_QM_MALLOC
LOG(L_ERR, "qm_realloc(%p, %lu) called from %s: %s(%d), module: %s; qm_malloc() failed!\n", qm, size, file, func, line, mname);
LOG(L_ERR, "qm_realloc(%p, %lu) called from %s: %s(%d), module: %s; qm_malloc() failed!\n",
qm, (unsigned long)size, file, func, line, mname);
#else
LOG(L_ERR, "qm_realloc(%p, %lu); qm_malloc() failed!\n", qm, size);
LOG(L_ERR, "qm_realloc(%p, %lu); qm_malloc() failed!\n",
qm, (unsigned long)size);
#endif
}
#ifdef DBG_QM_MALLOC
Expand All @@ -680,7 +692,7 @@ void* qm_realloc(void* qmp, void* p, size_t size)
/* do nothing */
#ifdef DBG_QM_MALLOC
MDBG("qm_realloc: doing nothing, same size: %lu - %lu\n",
f->size, size);
f->size, (unsigned long)size);
#endif
}
#ifdef DBG_QM_MALLOC
Expand Down

0 comments on commit f17ac87

Please sign in to comment.