diff options
author | Yann Ylavic <ylavic@apache.org> | 2020-12-04 16:31:06 +0000 |
---|---|---|
committer | Yann Ylavic <ylavic@apache.org> | 2020-12-04 16:31:06 +0000 |
commit | 460ae3ed07e71c17d22300d0c3fe5bd53ab7681e (patch) | |
tree | 0b62b611c2df63ebb397da30d78131c81106fc20 | |
parent | a460d52961ce130fd61cbc49a42c9d51360db8c2 (diff) | |
download | apr-460ae3ed07e71c17d22300d0c3fe5bd53ab7681e.tar.gz |
Merge r1806296, r1883805 from trunk:
Factor out common code in apr_pools.c. No function changes intended.
* memory/unix/apr_pools.c
(allocator_lock, allocator_unlock): New helpers.
(apr_allocator_max_free_set, allocator_alloc, allocator_free): Use
allocator_lock() and allocator_unlock().
apr_pools: two more uses of allocator_[un]lock() helpers to simplify the code.
git-svn-id: https://svn.apache.org/repos/asf/apr/apr/branches/1.7.x@1884101 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r-- | memory/unix/apr_pools.c | 95 |
1 files changed, 32 insertions, 63 deletions
diff --git a/memory/unix/apr_pools.c b/memory/unix/apr_pools.c index 93cf3b1b7..6a5feba38 100644 --- a/memory/unix/apr_pools.c +++ b/memory/unix/apr_pools.c @@ -160,6 +160,24 @@ struct apr_allocator_t { * Allocator */ +static APR_INLINE +void allocator_lock(apr_allocator_t *allocator) +{ +#if APR_HAS_THREADS + if (allocator->mutex) + apr_thread_mutex_lock(allocator->mutex); +#endif /* APR_HAS_THREADS */ +} + +static APR_INLINE +void allocator_unlock(apr_allocator_t *allocator) +{ +#if APR_HAS_THREADS + if (allocator->mutex) + apr_thread_mutex_unlock(allocator->mutex); +#endif /* APR_HAS_THREADS */ +} + APR_DECLARE(apr_status_t) apr_allocator_create(apr_allocator_t **allocator) { apr_allocator_t *new_allocator; @@ -229,13 +247,7 @@ APR_DECLARE(void) apr_allocator_max_free_set(apr_allocator_t *allocator, apr_size_t max_free_index; apr_size_t size = in_size; -#if APR_HAS_THREADS - apr_thread_mutex_t *mutex; - - mutex = apr_allocator_mutex_get(allocator); - if (mutex != NULL) - apr_thread_mutex_lock(mutex); -#endif /* APR_HAS_THREADS */ + allocator_lock(allocator); max_free_index = APR_ALIGN(size, BOUNDARY_SIZE) >> BOUNDARY_INDEX; allocator->current_free_index += max_free_index; @@ -244,10 +256,7 @@ APR_DECLARE(void) apr_allocator_max_free_set(apr_allocator_t *allocator, if (allocator->current_free_index > max_free_index) allocator->current_free_index = max_free_index; -#if APR_HAS_THREADS - if (mutex != NULL) - apr_thread_mutex_unlock(mutex); -#endif + allocator_unlock(allocator); } static APR_INLINE @@ -304,10 +313,7 @@ apr_memnode_t *allocator_alloc(apr_allocator_t *allocator, apr_size_t in_size) * our node will fit into. */ if (index <= allocator->max_index) { -#if APR_HAS_THREADS - if (allocator->mutex) - apr_thread_mutex_lock(allocator->mutex); -#endif /* APR_HAS_THREADS */ + allocator_lock(allocator); /* Walk the free list to see if there are * any nodes on it of the requested size @@ -347,28 +353,19 @@ apr_memnode_t *allocator_alloc(apr_allocator_t *allocator, apr_size_t in_size) if (allocator->current_free_index > allocator->max_free_index) allocator->current_free_index = allocator->max_free_index; -#if APR_HAS_THREADS - if (allocator->mutex) - apr_thread_mutex_unlock(allocator->mutex); -#endif /* APR_HAS_THREADS */ + allocator_unlock(allocator); goto have_node; } -#if APR_HAS_THREADS - if (allocator->mutex) - apr_thread_mutex_unlock(allocator->mutex); -#endif /* APR_HAS_THREADS */ + allocator_unlock(allocator); } /* If we found nothing, seek the sink (at index 0), if * it is not empty. */ else if (allocator->free[0]) { -#if APR_HAS_THREADS - if (allocator->mutex) - apr_thread_mutex_lock(allocator->mutex); -#endif /* APR_HAS_THREADS */ + allocator_lock(allocator); /* Walk the free list to see if there are * any nodes on it of the requested size @@ -384,18 +381,12 @@ apr_memnode_t *allocator_alloc(apr_allocator_t *allocator, apr_size_t in_size) if (allocator->current_free_index > allocator->max_free_index) allocator->current_free_index = allocator->max_free_index; -#if APR_HAS_THREADS - if (allocator->mutex) - apr_thread_mutex_unlock(allocator->mutex); -#endif /* APR_HAS_THREADS */ + allocator_unlock(allocator); goto have_node; } -#if APR_HAS_THREADS - if (allocator->mutex) - apr_thread_mutex_unlock(allocator->mutex); -#endif /* APR_HAS_THREADS */ + allocator_unlock(allocator); } /* If we haven't got a suitable node, malloc a new one @@ -438,10 +429,7 @@ void allocator_free(apr_allocator_t *allocator, apr_memnode_t *node) apr_size_t index, max_index; apr_size_t max_free_index, current_free_index; -#if APR_HAS_THREADS - if (allocator->mutex) - apr_thread_mutex_lock(allocator->mutex); -#endif /* APR_HAS_THREADS */ + allocator_lock(allocator); max_index = allocator->max_index; max_free_index = allocator->max_free_index; @@ -492,10 +480,7 @@ void allocator_free(apr_allocator_t *allocator, apr_memnode_t *node) allocator->max_index = max_index; allocator->current_free_index = current_free_index; -#if APR_HAS_THREADS - if (allocator->mutex) - apr_thread_mutex_unlock(allocator->mutex); -#endif /* APR_HAS_THREADS */ + allocator_unlock(allocator); while (freelist != NULL) { node = freelist; @@ -1011,20 +996,12 @@ APR_DECLARE(void) apr_pool_destroy(apr_pool_t *pool) /* Remove the pool from the parents child list */ if (pool->parent) { -#if APR_HAS_THREADS - apr_thread_mutex_t *mutex; - - if ((mutex = apr_allocator_mutex_get(pool->parent->allocator)) != NULL) - apr_thread_mutex_lock(mutex); -#endif /* APR_HAS_THREADS */ + allocator_lock(pool->parent->allocator); if ((*pool->ref = pool->sibling) != NULL) pool->sibling->ref = pool->ref; -#if APR_HAS_THREADS - if (mutex) - apr_thread_mutex_unlock(mutex); -#endif /* APR_HAS_THREADS */ + allocator_unlock(pool->parent->allocator); } /* Find the block attached to the pool structure. Save a copy of the @@ -1127,12 +1104,7 @@ APR_DECLARE(apr_status_t) apr_pool_create_ex(apr_pool_t **newpool, #endif /* defined(NETWARE) */ if ((pool->parent = parent) != NULL) { -#if APR_HAS_THREADS - apr_thread_mutex_t *mutex; - - if ((mutex = apr_allocator_mutex_get(parent->allocator)) != NULL) - apr_thread_mutex_lock(mutex); -#endif /* APR_HAS_THREADS */ + allocator_lock(parent->allocator); if ((pool->sibling = parent->child) != NULL) pool->sibling->ref = &pool->sibling; @@ -1140,10 +1112,7 @@ APR_DECLARE(apr_status_t) apr_pool_create_ex(apr_pool_t **newpool, parent->child = pool; pool->ref = &parent->child; -#if APR_HAS_THREADS - if (mutex) - apr_thread_mutex_unlock(mutex); -#endif /* APR_HAS_THREADS */ + allocator_unlock(parent->allocator); } else { pool->sibling = NULL; |