From 3578edef98a47b53d9f47ef4e34070120debf3e2 Mon Sep 17 00:00:00 2001 From: Valentin Bartenev Date: Thu, 8 Dec 2016 19:29:40 +0300 Subject: [PATCH] NXT_DEBUG_MEMORY macro. It allows to turn off accumulation of small pool allocations into a big preallocated chunk of memory. This is useful for debugging memory access with sanitizer, since such accumulation can cover buffer overruns from being detected. --- nxt/nxt_mem_cache_pool.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/nxt/nxt_mem_cache_pool.c b/nxt/nxt_mem_cache_pool.c index fcf68280..f1cbd546 100644 --- a/nxt/nxt_mem_cache_pool.c +++ b/nxt/nxt_mem_cache_pool.c @@ -124,12 +124,14 @@ struct nxt_mem_cache_pool_s { static nxt_uint_t nxt_mem_cache_shift(nxt_uint_t n); +#if !(NXT_DEBUG_MEMORY) static void *nxt_mem_cache_alloc_small(nxt_mem_cache_pool_t *pool, size_t size); static nxt_uint_t nxt_mem_cache_alloc_chunk(u_char *map, nxt_uint_t size); static nxt_mem_cache_page_t * nxt_mem_cache_alloc_page(nxt_mem_cache_pool_t *pool); static nxt_mem_cache_block_t * nxt_mem_cache_alloc_cluster(nxt_mem_cache_pool_t *pool); +#endif static void *nxt_mem_cache_alloc_large(nxt_mem_cache_pool_t *pool, size_t alignment, size_t size); static intptr_t nxt_mem_cache_rbtree_compare(nxt_rbtree_node_t *node1, @@ -302,10 +304,14 @@ nxt_mem_cache_alloc(nxt_mem_cache_pool_t *pool, size_t size) pool->proto->trace(pool->trace, "mem cache alloc: %zd", size); } +#if !(NXT_DEBUG_MEMORY) + if (size <= pool->page_size) { return nxt_mem_cache_alloc_small(pool, size); } +#endif + return nxt_mem_cache_alloc_large(pool, NXT_MAX_ALIGNMENT, size); } @@ -337,6 +343,8 @@ nxt_mem_cache_align(nxt_mem_cache_pool_t *pool, size_t alignment, size_t size) if (nxt_fast_path((alignment - 1) & alignment) == 0) { +#if !(NXT_DEBUG_MEMORY) + if (size <= pool->page_size && alignment <= pool->page_alignment) { size = nxt_max(size, alignment); @@ -345,6 +353,8 @@ nxt_mem_cache_align(nxt_mem_cache_pool_t *pool, size_t alignment, size_t size) } } +#endif + return nxt_mem_cache_alloc_large(pool, alignment, size); } @@ -367,6 +377,8 @@ nxt_mem_cache_zalign(nxt_mem_cache_pool_t *pool, size_t alignment, size_t size) } +#if !(NXT_DEBUG_MEMORY) + static void * nxt_mem_cache_alloc_small(nxt_mem_cache_pool_t *pool, size_t size) { @@ -549,6 +561,8 @@ nxt_mem_cache_alloc_cluster(nxt_mem_cache_pool_t *pool) return cluster; } +#endif + static void * nxt_mem_cache_alloc_large(nxt_mem_cache_pool_t *pool, size_t alignment, -- 2.47.3