Skip to content

Commit

Permalink
core: fix mem statistics when DBG_QM_MALLOC is on
Browse files Browse the repository at this point in the history
(cherry picked from commit ffb0cb6)

Conflicts:

	mem/q_malloc.c
  • Loading branch information
liviuchircu committed Aug 21, 2014
1 parent 7a5763e commit 9e8258b
Showing 1 changed file with 19 additions and 23 deletions.
42 changes: 19 additions & 23 deletions mem/q_malloc.c
Expand Up @@ -185,6 +185,9 @@ static inline void qm_insert_free(struct qm_block* qm, struct qm_frag* frag)
frag->u.nxt_free=f;
FRAG_END(f)->prev_free=frag;
qm->free_hash[hash].no++;

qm->real_used-=frag->size;
qm->used-=frag->size;
}


Expand Down Expand Up @@ -223,8 +226,9 @@ struct qm_block* qm_malloc_init(char* address, unsigned long size)
qm=(struct qm_block*)start;
memset(qm, 0, sizeof(struct qm_block));
qm->size=size;
qm->real_used=init_overhead;
qm->max_real_used=qm->real_used;
qm->used=size-init_overhead;
qm->real_used=size;
qm->max_real_used = 0;
size-=init_overhead;

qm->first_frag=(struct qm_frag*)(start+ROUNDUP(sizeof(struct qm_block)));
Expand Down Expand Up @@ -253,8 +257,7 @@ struct qm_block* qm_malloc_init(char* address, unsigned long size)
/*qm->first_frag->u.nxt_free=&(qm->free_lst);
qm->last_frag_end->prev_free=&(qm->free_lst);
*/



return qm;
}

Expand All @@ -269,7 +272,9 @@ static inline void qm_detach_free(struct qm_block* qm, struct qm_frag* frag)
next=frag->u.nxt_free;
prev->u.nxt_free=next;
FRAG_END(next)->prev_free=prev;


qm->real_used+=frag->size;
qm->used+=frag->size;
}


Expand Down Expand Up @@ -332,7 +337,7 @@ int split_frag(struct qm_block* qm, struct qm_frag* f, unsigned long new_size)
n->size=rest-FRAG_OVERHEAD;
FRAG_END(n)->size=n->size;
FRAG_CLEAR_USED(n); /* never used */
qm->real_used+=FRAG_OVERHEAD;
qm->used-=FRAG_OVERHEAD;
#ifdef DBG_QM_MALLOC
end->check1=END_CHECK_PATTERN1;
end->check2=END_CHECK_PATTERN2;
Expand Down Expand Up @@ -398,8 +403,6 @@ void* qm_malloc(struct qm_block* qm, unsigned long size)
#else
split_frag(qm, f, size);
#endif
qm->real_used+=f->size;
qm->used+=f->size;
if (qm->max_real_used<qm->real_used)
qm->max_real_used=qm->real_used;
#ifdef DBG_QM_MALLOC
Expand Down Expand Up @@ -433,8 +436,8 @@ void qm_free(struct qm_block* qm, void* p)
#ifdef QM_JOIN_FREE
struct qm_frag* prev;
struct qm_frag* next;
#endif
unsigned long size;
#endif

#ifdef DBG_QM_MALLOC
LM_GEN1( memlog, "params(%p, %p), called from %s: %s(%d)\n",
Expand All @@ -460,11 +463,9 @@ void qm_free(struct qm_block* qm, void* p)
LM_GEN1( memlog, "freeing frag. %p alloc'ed from %s: %s(%ld)\n",
f, f->file, f->func, f->line);
#endif
size=f->size;
qm->used-=size;
qm->real_used-=size;

#ifdef QM_JOIN_FREE
size=f->size;
/* join packets if possible*/
prev=next=0;
next=FRAG_NEXT(f);
Expand All @@ -475,7 +476,7 @@ void qm_free(struct qm_block* qm, void* p)
#endif
qm_detach_free(qm, next);
size+=next->size+FRAG_OVERHEAD;
qm->real_used-=FRAG_OVERHEAD;
qm->used+=FRAG_OVERHEAD;
qm->free_hash[GET_HASH(next->size)].no--; /* FIXME slow */
}

Expand All @@ -490,7 +491,7 @@ void qm_free(struct qm_block* qm, void* p)
/*join*/
qm_detach_free(qm, prev);
size+=prev->size+FRAG_OVERHEAD;
qm->real_used-=FRAG_OVERHEAD;
qm->used+=FRAG_OVERHEAD;
qm->free_hash[GET_HASH(prev->size)].no--; /* FIXME slow */
f=prev;
}
Expand Down Expand Up @@ -568,14 +569,11 @@ void* qm_realloc(struct qm_block* qm, void* p, unsigned long size)
LM_GEN1(memlog,"shrinking from %lu to %lu\n", f->size, size);
if(split_frag(qm, f, size, file, "fragm. from qm_realloc", line)!=0){
LM_GEN1(memlog,"shrinked successful\n");
}
#else
if(split_frag(qm, f, size)!=0){
split_frag(qm, f, size);
#endif
/* update used sizes: freed the spitted frag */
qm->real_used-=(orig_size-f->size-FRAG_OVERHEAD);
qm->used-=(orig_size-f->size);
}


}else if (f->size < size){
/* grow */
#ifdef DBG_QM_MALLOC
Expand All @@ -590,7 +588,7 @@ void* qm_realloc(struct qm_block* qm, void* p, unsigned long size)
qm_detach_free(qm, n);
qm->free_hash[GET_HASH(n->size)].no--; /*FIXME: slow*/
f->size+=n->size+FRAG_OVERHEAD;
qm->real_used-=FRAG_OVERHEAD;
qm->used+=FRAG_OVERHEAD;
FRAG_END(f)->size=f->size;
/* end checks should be ok */
/* split it if necessary */
Expand All @@ -602,8 +600,6 @@ void* qm_realloc(struct qm_block* qm, void* p, unsigned long size)
split_frag(qm, f, size);
#endif
}
qm->real_used+=(f->size-orig_size);
qm->used+=(f->size-orig_size);
}else{
/* could not join => realloc */
#ifdef DBG_QM_MALLOC
Expand Down

0 comments on commit 9e8258b

Please sign in to comment.