[sr-dev] git:tmp/valgrind:44f0830d: mem: add valgrind memcheck instrumentation

Camille Oudot camille.oudot at orange.com
Fri Feb 24 15:53:56 CET 2017


Module: kamailio
Branch: tmp/valgrind
Commit: 44f0830d43955791fc4ee8bca9c1a7f427e74e41
URL: https://github.com/kamailio/kamailio/commit/44f0830d43955791fc4ee8bca9c1a7f427e74e41

Author: Camille Oudot <camille.oudot at orange.com>
Committer: Camille Oudot <camille.oudot at orange.com>
Date: 2017-02-24T15:54:10+01:00

mem: add valgrind memcheck instrumentation

---

Modified: src/Makefile.defs
Modified: src/core/mem/q_malloc.c
Modified: src/core/mem/q_malloc.h

---

Diff:  https://github.com/kamailio/kamailio/commit/44f0830d43955791fc4ee8bca9c1a7f427e74e41.diff
Patch: https://github.com/kamailio/kamailio/commit/44f0830d43955791fc4ee8bca9c1a7f427e74e41.patch

---

diff --git a/src/Makefile.defs b/src/Makefile.defs
index 0226115..1eb4c8b 100644
--- a/src/Makefile.defs
+++ b/src/Makefile.defs
@@ -118,6 +118,10 @@ MEMMNG ?= 0
 # 0 - off (no-debug mode)
 # 1 - on (debug mode)
 MEMDBG ?= 1
+# Valgrind memcheck support
+# 0 - off
+# 1 - on
+MEMCHECK ?= 0
 
 SER_VER = $(shell expr $(VERSION) \* 1000000 + $(PATCHLEVEL) \* 1000 + \
 			$(SUBLEVEL) )
@@ -694,7 +698,13 @@ C_DEFS+= -DTLSF_MALLOC
 ifeq 	($(MEMDBG), 1)
 	C_DEFS+= -DDBG_SR_MEMORY
 endif
-
+# FIXME: does not seem to work
+ifeq 	($(MEMCHECK), 1)
+	C_DEFS+= -DVALGRIND_MEMCHECK
+endif
+####
+C_DEFS+= -DVALGRIND_MEMCHECK
+# /FIXME
 ifneq ($(PKG_MEM_SIZE),)
 	C_DEFS+= -DPKG_MEM_SIZE=$(PKG_MEM_SIZE)
 endif
diff --git a/src/core/mem/q_malloc.c b/src/core/mem/q_malloc.c
index 0c24ef2..db42e09 100644
--- a/src/core/mem/q_malloc.c
+++ b/src/core/mem/q_malloc.c
@@ -162,6 +162,9 @@ static inline void qm_insert_free(struct qm_block* qm, struct qm_frag* frag)
 			f=f->u.nxt_free){
 		if (frag->size <= f->size) break;
 	}
+#ifdef VALGRIND_MEMCHECK
+	VALGRIND_MEMPOOL_FREE(qm, (char*)frag + sizeof(struct qm_frag));
+#endif /* #ifdef VALGRIND_MEMCHECK */
 	/*insert it here*/
 	prev=FRAG_END(f)->prev_free;
 	prev->u.nxt_free=frag;
@@ -236,6 +239,15 @@ struct qm_block* qm_malloc_init(char* address, unsigned long size, int type)
 		qm->free_hash[h].tail.size=0;
 	}
 
+#ifdef VALGRIND_MEMCHECK
+	VALGRIND_CREATE_MEMPOOL(qm, 0 /*sizeof(unsigned long)*/, 0);
+	VALGRIND_MEMPOOL_ALLOC(qm, qm->first_frag, sizeof(struct qm_frag));
+	VALGRIND_MAKE_MEM_DEFINED(qm->first_frag, sizeof(struct qm_frag));
+	VALGRIND_MEMPOOL_ALLOC(qm, FRAG_END(qm->first_frag), sizeof(struct qm_frag_end));
+	VALGRIND_MAKE_MEM_DEFINED(FRAG_END(qm->first_frag), sizeof(struct qm_frag_end));
+	VALGRIND_MEMPOOL_ALLOC(qm, (char*)qm->first_frag + sizeof(struct qm_frag), qm->first_frag->size);
+#endif /* #ifdef VALGRIND_MEMCHECK */
+
 	/* link initial fragment into the free list*/
 
 	qm_insert_free(qm, qm->first_frag);
@@ -254,6 +266,9 @@ static inline void qm_detach_free(struct qm_block* qm, struct qm_frag* frag)
 {
 	struct qm_frag *prev;
 	struct qm_frag *next;
+#ifdef VALGRIND_MEMCHECK
+	VALGRIND_MEMPOOL_ALLOC(qm, (char*)frag + sizeof(struct qm_frag), frag->size);
+#endif /* #ifdef VALGRIND_MEMCHECK */
 
 	prev=FRAG_END(frag)->prev_free;
 	next=frag->u.nxt_free;
@@ -315,15 +330,28 @@ int split_frag(struct qm_block* qm, struct qm_frag* f, size_t new_size)
 #else
 	if (rest>(FRAG_OVERHEAD+MIN_FRAG_SIZE)){
 #endif
+#ifdef VALGRIND_MEMCHECK
+		VALGRIND_MEMPOOL_CHANGE(qm, (char*)f + sizeof(struct qm_frag), (char*)f + sizeof(struct qm_frag), new_size);
+#endif /* #ifdef VALGRIND_MEMCHECK */
 		f->size=new_size;
 		/*split the fragment*/
 		end=FRAG_END(f);
+#ifdef VALGRIND_MEMCHECK
+		VALGRIND_MEMPOOL_ALLOC(qm, end, sizeof(struct qm_frag_end));
+#endif /* #ifdef VALGRIND_MEMCHECK */
 		end->size=new_size;
 		n=(struct qm_frag*)((char*)end+sizeof(struct qm_frag_end));
+#ifdef VALGRIND_MEMCHECK
+		VALGRIND_MEMPOOL_ALLOC(qm, n, sizeof(struct qm_frag));
+#endif /* #ifdef VALGRIND_MEMCHECK */
 		n->size=rest-FRAG_OVERHEAD;
+#ifdef VALGRIND_MEMCHECK
+		VALGRIND_MEMPOOL_ALLOC(qm, (char *)n + sizeof(struct qm_frag), n->size);
+#endif /* #ifdef VALGRIND_MEMCHECK */
 		FRAG_END(n)->size=n->size;
 		FRAG_CLEAR_USED(n); /* never used */
 		qm->real_used+=FRAG_OVERHEAD;
+
 #ifdef DBG_QM_MALLOC
 		end->check1=END_CHECK_PATTERN1;
 		end->check2=END_CHECK_PATTERN2;
diff --git a/src/core/mem/q_malloc.h b/src/core/mem/q_malloc.h
index 5d43900..a9e7704 100644
--- a/src/core/mem/q_malloc.h
+++ b/src/core/mem/q_malloc.h
@@ -28,6 +28,9 @@
 #define q_malloc_h
 
 #include "meminfo.h"
+#ifdef VALGRIND_MEMCHECK
+#include <valgrind/memcheck.h>
+#endif
 
 #ifdef DBG_SR_MEMORY
 #define DBG_QM_MALLOC




More information about the sr-dev mailing list