[sr-dev] git:sr_3.0: mem: fix real_used stats for realloc

Andrei Pelinescu-Onciul andrei at iptel.org
Wed Mar 17 20:57:27 CET 2010


Module: sip-router
Branch: sr_3.0
Commit: e3dc76b5830f40c08251d6df21cb502ba39f442e
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=e3dc76b5830f40c08251d6df21cb502ba39f442e

Author: Andrei Pelinescu-Onciul <andrei at iptel.org>
Committer: Andrei Pelinescu-Onciul <andrei at iptel.org>
Date:   Fri Mar 12 12:16:16 2010 +0100

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).
(cherry picked from commit 75c1e9a735c693a6985a7a1786116b5fe4044fd9)

---

 mem/f_malloc.c |    4 +++-
 mem/q_malloc.c |    7 +++++--
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/mem/f_malloc.c b/mem/f_malloc.c
index 2c05fe6..49c4b7a 100644
--- a/mem/f_malloc.c
+++ b/mem/f_malloc.c
@@ -491,7 +491,9 @@ void* fm_realloc(struct fm_block* qm, void* p, unsigned long size)
 		fm_split_frag(qm, f, size);
 #endif
 #if defined(DBG_F_MALLOC) || defined(MALLOC_STATS)
-		qm->real_used-=(orig_size-f->size-FRAG_OVERHEAD);
+		/* fm_split frag already adds FRAG_OVERHEAD for the newly created
+		   free frag, so here we only need orig_size-f->size for real used */
+		qm->real_used-=(orig_size-f->size);
 		qm->used-=(orig_size-f->size);
 #endif
 	}else if (f->size<size){
diff --git a/mem/q_malloc.c b/mem/q_malloc.c
index 09cb5c7..04c6dc9 100644
--- a/mem/q_malloc.c
+++ b/mem/q_malloc.c
@@ -562,8 +562,11 @@ void* qm_realloc(struct qm_block* qm, void* p, unsigned long size)
 #else
 		if(split_frag(qm, f, size)!=0){
 #endif
-			/* update used sizes: freed the spitted frag */
-			qm->real_used-=(orig_size-f->size-FRAG_OVERHEAD);
+			/* update used sizes: freed the splited frag */
+			/* split frag already adds FRAG_OVERHEAD for the newly created
+			   free frag, so here we only need orig_size-f->size for real used
+			 */
+			qm->real_used-=(orig_size-f->size);
 			qm->used-=(orig_size-f->size);
 		}
 		




More information about the sr-dev mailing list