Browse code

mem: fix real_used stats for realloc

A realloc that shrank an allocation accounted twice for the
fragment overhead. Basically each shrinking realloc would
introduce an error in the real_used mem stats, between 8 bytes
(f_malloc, no debugging, 32 bits) and up to 96 bytes (q_malloc
with debugging, 64 bits).
This bug concerns only the accounting part. It does not cause any
memory leak or any real runtime problem. It was introduced
in commit fb9d6e50 (2005).

Andrei Pelinescu-Onciul authored on 12/03/2010 11:16:16
Showing 2 changed files
... ...
@@ -491,7 +491,9 @@ void* fm_realloc(struct fm_block* qm, void* p, unsigned long size)
491 491
 		fm_split_frag(qm, f, size);
492 492
 #endif
493 493
 #if defined(DBG_F_MALLOC) || defined(MALLOC_STATS)
494
-		qm->real_used-=(orig_size-f->size-FRAG_OVERHEAD);
494
+		/* fm_split frag already adds FRAG_OVERHEAD for the newly created
495
+		   free frag, so here we only need orig_size-f->size for real used */
496
+		qm->real_used-=(orig_size-f->size);
495 497
 		qm->used-=(orig_size-f->size);
496 498
 #endif
497 499
 	}else if (f->size<size){
... ...
@@ -562,8 +562,11 @@ void* qm_realloc(struct qm_block* qm, void* p, unsigned long size)
562 562
 #else
563 563
 		if(split_frag(qm, f, size)!=0){
564 564
 #endif
565
-			/* update used sizes: freed the spitted frag */
566
-			qm->real_used-=(orig_size-f->size-FRAG_OVERHEAD);
565
+			/* update used sizes: freed the splited frag */
566
+			/* split frag already adds FRAG_OVERHEAD for the newly created
567
+			   free frag, so here we only need orig_size-f->size for real used
568
+			 */
569
+			qm->real_used-=(orig_size-f->size);
567 570
 			qm->used-=(orig_size-f->size);
568 571
 		}
569 572