]> git.ipfire.org Git - thirdparty/unbound.git/commitdiff
total allocation tracking.
authorWouter Wijngaards <wouter@nlnetlabs.nl>
Fri, 10 Aug 2007 09:07:19 +0000 (09:07 +0000)
committerWouter Wijngaards <wouter@nlnetlabs.nl>
Fri, 10 Aug 2007 09:07:19 +0000 (09:07 +0000)
git-svn-id: file:///svn/unbound/trunk@507 be551aaa-1e26-0410-a405-d3ace91eadb9

configure.ac
daemon/worker.c
doc/Changelog
doc/TODO
util/alloc.c

index 93580da7171026860310e3555cc720ff5800288e..f13c9017b7039964db178b65a9d27f03510e4127 100644 (file)
@@ -631,6 +631,31 @@ struct sockaddr_storage;
 
 #include "ldns/ldns.h"
 
+/* use statistics for allocs and frees, for debug use */
+#define UNBOUND_ALLOC_STATS
+#ifdef UNBOUND_ALLOC_STATS
+#  ifdef malloc
+#    undef malloc
+#  endif
+#  ifdef realloc
+#    undef realloc
+#  endif
+#  ifdef calloc
+#    undef calloc
+#  endif
+#  ifdef free
+#    undef free
+#  endif
+#  define malloc unbound_stat_malloc
+#  define calloc unbound_stat_calloc
+#  define free unbound_stat_free
+#  define realloc unbound_stat_realloc
+void *unbound_stat_malloc(size_t size);
+void *unbound_stat_calloc(size_t nmemb, size_t size);
+void unbound_stat_free(void *ptr);
+void *unbound_stat_realloc(void *ptr, size_t size);
+#endif /* UNBOUND_ALLOC_STATS */
+
 /** default port for DNS traffic. */
 #define UNBOUND_DNS_PORT 53
 ])
index e5d118057dabea3db3c3111d1febba7f31e2f255..1773a375642afc25f254edbff317691d190b999e 100644 (file)
@@ -74,9 +74,12 @@ static void
 debug_total_mem()
 {
        extern void* unbound_start_brk;
+       extern size_t unbound_mem_alloc, unbound_mem_freed;
        void* cur = sbrk(0);
        int total = cur-unbound_start_brk;
-       log_info("Total heap memory estimate: %u", (unsigned)total);
+       log_info("Total heap memory estimate: %u  total-alloc: %u  "
+               "total-free: %u", (unsigned)total, 
+               (unsigned)unbound_mem_alloc, (unsigned)unbound_mem_freed);
 }
 
 /** Report on memory usage by this thread and global */
index 56295e5a4bd6f30e27613b9c5497c5e83ee512d0..03db89e185866edf5e1ba6ee9d62105bd20069e9 100644 (file)
@@ -1,3 +1,7 @@
+10 August 2007: Wouter
+       - malloc and free overrides that track total allocation and frees.
+         for memory debugging.
+
 9 August 2007: Wouter
        - canonicalization, signature checks
        - dname signature label count and unit test.
index d25c95b76db55ba31523bc1e8142fdefbbb345a5..da4f025d2792549c3f3f163311b4e1c89ef4a3f7 100644 (file)
--- a/doc/TODO
+++ b/doc/TODO
@@ -29,3 +29,5 @@ o (option) to not send replies to clients after a timeout of (say 5 secs) has
 o private TTL feature
 o pretend-dnssec-unaware, and pretend-edns-unaware modes for debug/workshops.
 o delegpt use rbtree for ns-list, to avoid slowdown for very large NS sets.
+o be able to have different listen and query-to addresses to bind to,
+  so you can listen to localhost and query-to to the internet.
index fcdf68871c71e55fc6b890d4e7eec90906de4342..97cd1eebcf98d711a90f93d0e8e17523148a8237 100644 (file)
@@ -252,3 +252,96 @@ size_t alloc_get_mem(struct alloc_cache* alloc)
        }
        return s;
 }
+
+/** global debug value to keep track of total memory mallocs */
+size_t unbound_mem_alloc = 0;
+/** global debug value to keep track of total memory frees */
+size_t unbound_mem_freed = 0;
+#ifdef UNBOUND_ALLOC_STATS
+/** special value to know if the memory is being tracked */
+uint64_t mem_special = (uint64_t)0xfeed43327766abcdLL;
+#ifdef malloc
+#undef malloc
+#endif
+/** malloc with stats */
+void *unbound_stat_malloc(size_t size)
+{
+       void* res;
+       if(size == 0) size = 1;
+       res = malloc(size+16);
+       if(!res) return NULL;
+       unbound_mem_alloc += size;
+       memcpy(res, &size, sizeof(size));
+       memcpy(res+8, &mem_special, sizeof(mem_special));
+       return res+16;
+}
+#ifdef calloc
+#undef calloc
+#endif
+/** calloc with stats */
+void *unbound_stat_calloc(size_t nmemb, size_t size)
+{
+       size_t s = (nmemb*size==0)?(size_t)1:nmemb*size;
+       void* res = calloc(1, s+16);
+       if(!res) return NULL;
+       unbound_mem_alloc += s;
+       memcpy(res, &s, sizeof(s));
+       memcpy(res+8, &mem_special, sizeof(mem_special));
+       return res+16;
+}
+#ifdef free
+#undef free
+#endif
+/** free with stats */
+void unbound_stat_free(void *ptr)
+{
+       size_t s;
+       if(!ptr) return;
+       if(memcmp(ptr-8, &mem_special, sizeof(mem_special)) != 0) {
+               free(ptr);
+               return;
+       }
+       ptr-=16;
+       memcpy(&s, ptr, sizeof(s));
+       memset(ptr+8, 0, 8);
+       unbound_mem_freed += s;
+       free(ptr);
+}
+#ifdef realloc
+#undef realloc
+#endif
+/** realloc with stats */
+void *unbound_stat_realloc(void *ptr, size_t size)
+{
+       size_t cursz;
+       void* res;
+       if(!ptr) return unbound_stat_malloc(size);
+       if(memcmp(ptr-8, &mem_special, sizeof(mem_special)) != 0) {
+               return realloc(ptr, size);
+       }
+       if(size==0) {
+               unbound_stat_free(ptr);
+               return NULL;
+       }
+       ptr -= 16;
+       memcpy(&cursz, ptr, sizeof(cursz));
+       if(cursz == size) {
+               /* nothing changes */
+               return ptr;
+       }
+       res = malloc(size+16);
+       if(!res) return NULL;
+       unbound_mem_alloc += size;
+       unbound_mem_freed += cursz;
+       if(cursz > size) {
+               memcpy(res+16, ptr+16, size);
+       } else if(size > cursz) {
+               memcpy(res+16, ptr+16, cursz);
+       }
+       memset(ptr+8, 0, 8);
+       free(ptr);
+       memcpy(res, &size, sizeof(size));
+       memcpy(res+8, &mem_special, sizeof(mem_special));
+       return res+16;
+}
+#endif /* UNBOUND_ALLOC_STATS */