From b8deb32e55fd4777a50d6c667969dbc64da52e3c Mon Sep 17 00:00:00 2001 From: Yann Ylavic Date: Thu, 23 Mar 2017 21:34:25 +0000 Subject: apr_allocator: Provide apr_allocator_align() to get the true size that would be allocated for the given size (including the header and alignment). git-svn-id: https://svn.apache.org/repos/asf/apr/apr/trunk@1788334 13f79535-47bb-0310-9956-ffa450edef68 --- memory/unix/apr_pools.c | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) (limited to 'memory') diff --git a/memory/unix/apr_pools.c b/memory/unix/apr_pools.c index 138180b77..784280f2c 100644 --- a/memory/unix/apr_pools.c +++ b/memory/unix/apr_pools.c @@ -235,6 +235,30 @@ APR_DECLARE(void) apr_allocator_max_free_set(apr_allocator_t *allocator, #endif } +static APR_INLINE +apr_size_t allocator_align(apr_size_t in_size) +{ + apr_size_t size = in_size; + + /* Round up the block size to the next boundary, but always + * allocate at least a certain size (MIN_ALLOC). + */ + size = APR_ALIGN(size + APR_MEMNODE_T_SIZE, BOUNDARY_SIZE); + if (size < in_size) { + return 0; + } + if (size < MIN_ALLOC) { + size = MIN_ALLOC; + } + + return size; +} + +APR_DECLARE(apr_size_t) apr_allocator_align(apr_size_t size) +{ + return allocator_align(size); +} + static APR_INLINE apr_memnode_t *allocator_alloc(apr_allocator_t *allocator, apr_size_t in_size) { @@ -245,12 +269,10 @@ apr_memnode_t *allocator_alloc(apr_allocator_t *allocator, apr_size_t in_size) /* Round up the block size to the next boundary, but always * allocate at least a certain size (MIN_ALLOC). */ - size = APR_ALIGN(in_size + APR_MEMNODE_T_SIZE, BOUNDARY_SIZE); - if (size < in_size) { + size = allocator_align(in_size); + if (!size) { return NULL; } - if (size < MIN_ALLOC) - size = MIN_ALLOC; /* Find the index for this node size by * dividing its size by the boundary size -- cgit v1.2.1