From e8cc756c132de678777f8439e13757890b9e90aa Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Wed, 26 Mar 2008 17:50:27 +0000 Subject: r19072@catbus: nickm | 2008-03-26 13:50:24 -0400 Add code to debug memory area size. Use results of this code to set a couple of area sizes more sanely. svn:r14201 --- src/common/memarea.c | 16 ++++++++++++++++ src/common/memarea.h | 2 ++ 2 files changed, 18 insertions(+) (limited to 'src/common') diff --git a/src/common/memarea.c b/src/common/memarea.c index 916010cfd1..743a451a8b 100644 --- a/src/common/memarea.c +++ b/src/common/memarea.c @@ -197,6 +197,22 @@ memarea_strndup(memarea_t *area, const char *s, size_t n) return result; } +/** Set allocated_out to the number of bytes allocated in area, + * and used_out to the number of bytes currently used. */ +void +memarea_get_stats(memarea_t *area, size_t *allocated_out, size_t *used_out) +{ + size_t a = 0, u = 0; + memarea_chunk_t *chunk; + for (chunk = area->first; chunk; chunk = chunk->next_chunk) { + a += CHUNK_HEADER_SIZE + chunk->mem_size; + tor_assert(chunk->next_mem >= chunk->u.mem); + u += CHUNK_HEADER_SIZE + (chunk->next_mem - chunk->u.mem); + } + *allocated_out = a; + *used_out = u; +} + /** Assert that area is okay. */ void memarea_assert_ok(memarea_t *area) diff --git a/src/common/memarea.h b/src/common/memarea.h index 76a3c3af81..17ab781a7f 100644 --- a/src/common/memarea.h +++ b/src/common/memarea.h @@ -17,6 +17,8 @@ void *memarea_alloc_zero(memarea_t *area, size_t sz); void *memarea_memdup(memarea_t *area, const void *s, size_t n); char *memarea_strdup(memarea_t *area, const char *s); char *memarea_strndup(memarea_t *area, const char *s, size_t n); +void memarea_get_stats(memarea_t *area, + size_t *allocated_out, size_t *used_out); void memarea_assert_ok(memarea_t *area); #endif -- cgit v1.2.3-54-g00ecf