add cmake option WITH_MEM_VALGRIND, helps to track down errors with mempool use which...
authorCampbell Barton <ideasman42@gmail.com>
Thu, 3 Oct 2013 12:22:44 +0000 (12:22 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Thu, 3 Oct 2013 12:22:44 +0000 (12:22 +0000)
CMakeLists.txt
source/blender/blenlib/CMakeLists.txt
source/blender/blenlib/intern/BLI_mempool.c

index eed1d64459f353006841fa5c768f7b5a25140c20..91003a1964ac2cda9cb78773fe31ea2e77f6b697 100644 (file)
@@ -278,6 +278,10 @@ mark_as_advanced(LLVM_STATIC)
 option(WITH_MEM_JEMALLOC   "Enable malloc replacement (http://www.canonware.com/jemalloc)" OFF)
 mark_as_advanced(WITH_MEM_JEMALLOC)
 
+# currently only used for BLI_mempool
+option(WITH_MEM_VALGRIND "Enable extended valgrind support for better reporting" OFF)
+mark_as_advanced(WITH_MEM_VALGRIND)
+
 # Debug
 option(WITH_CXX_GUARDEDALLOC "Enable GuardedAlloc for C++ memory allocation tracking (only enable for development)" OFF)
 mark_as_advanced(WITH_CXX_GUARDEDALLOC)
index 31125fd315326c0581a1728a68fcfe3966ef5f45..65ba545ef13fa5fe6bc67b943805fd6284d5e7c3 100644 (file)
@@ -178,6 +178,10 @@ if(WITH_BINRELOC)
        add_definitions(-DWITH_BINRELOC)
 endif()
 
+if(WITH_MEM_VALGRIND)
+       add_definitions(-DWITH_MEM_VALGRIND)
+endif()
+
 if(WIN32)
        list(APPEND INC
                ../../../intern/utfconv
index febf09745fb583baaaf5556c74ab8160c0c480c8..8301a7df35b6469eb02e52bbe71b707bf80706bd 100644 (file)
@@ -45,6 +45,9 @@
 
 #include "BLI_strict_flags.h"  /* keep last */
 
+#ifdef WITH_MEM_VALGRIND
+#  include "valgrind/memcheck.h"
+#endif
 
 /* note: copied from BLO_blend_defs.h, don't use here because we're in BLI */
 #ifdef __BIG_ENDIAN__
@@ -275,6 +278,10 @@ BLI_mempool *BLI_mempool_create(unsigned int esize, unsigned int totelem,
                lasttail = mempool_chunk_add(pool, mpchunk, lasttail);
        }
 
+#ifdef WITH_MEM_VALGRIND
+       VALGRIND_CREATE_MEMPOOL(pool, 0, false);
+#endif
+
        return pool;
 }
 
@@ -297,6 +304,11 @@ void *BLI_mempool_alloc(BLI_mempool *pool)
        }
 
        pool->free = pool->free->next;
+
+#ifdef WITH_MEM_VALGRIND
+       VALGRIND_MEMPOOL_ALLOC(pool, retval, pool->esize);
+#endif
+
        return retval;
 }
 
@@ -367,6 +379,10 @@ void BLI_mempool_free(BLI_mempool *pool, void *addr)
                }
                curnode->next = NULL; /* terminate the list */
        }
+
+#ifdef WITH_MEM_VALGRIND
+       VALGRIND_MEMPOOL_FREE(pool, addr);
+#endif
 }
 
 int BLI_mempool_count(BLI_mempool *pool)
@@ -582,6 +598,10 @@ void BLI_mempool_destroy(BLI_mempool *pool)
 {
        mempool_chunk_free_all(&pool->chunks, pool->flag);
 
+#ifdef WITH_MEM_VALGRIND
+       VALGRIND_DESTROY_MEMPOOL(pool);
+#endif
+
        if (pool->flag & BLI_MEMPOOL_SYSMALLOC) {
                free(pool);
        }