diff options
author | Yann Ylavic <ylavic@apache.org> | 2017-03-23 21:34:25 +0000 |
---|---|---|
committer | Yann Ylavic <ylavic@apache.org> | 2017-03-23 21:34:25 +0000 |
commit | b8deb32e55fd4777a50d6c667969dbc64da52e3c (patch) | |
tree | b4dde15377f8035de11b584372a53d50caac1a79 | |
parent | 0e96cf8c2d256c9d5a25ac1541f1a6e9803aa093 (diff) | |
download | apr-b8deb32e55fd4777a50d6c667969dbc64da52e3c.tar.gz |
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
-rw-r--r-- | CHANGES | 4 | ||||
-rw-r--r-- | include/apr_allocator.h | 8 | ||||
-rw-r--r-- | memory/unix/apr_pools.c | 30 |
3 files changed, 38 insertions, 4 deletions
@@ -1,6 +1,10 @@ -*- coding: utf-8 -*- Changes for APR 2.0.0 + *) apr_allocator: Provide apr_allocator_align() to get the true size that + would be allocated for the given size (including the header and + alignment). [Yann Ylavic] + *) apr_crypto: avoid excessive iteration in bcrypt hash. [Hanno Böck <hanno hboeck.de>] diff --git a/include/apr_allocator.h b/include/apr_allocator.h index 70d31efa0..88d9e4d15 100644 --- a/include/apr_allocator.h +++ b/include/apr_allocator.h @@ -103,6 +103,14 @@ APR_DECLARE(apr_memnode_t *) apr_allocator_alloc(apr_allocator_t *allocator, APR_DECLARE(void) apr_allocator_free(apr_allocator_t *allocator, apr_memnode_t *memnode) __attribute__((nonnull(1,2))); + +/** + * Return the aligned (round up) size that an allocator would use for + * the given size. + * @param size The size to align + * @return The aligned size (or zero on apr_size_t overflow) + */ +APR_DECLARE(apr_size_t) apr_allocator_align(apr_size_t size); #include "apr_pools.h" 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 @@ -236,6 +236,30 @@ APR_DECLARE(void) apr_allocator_max_free_set(apr_allocator_t *allocator, } 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) { apr_memnode_t *node, **ref; @@ -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 |