MINOR: pools: split the OS-based allocator in two
Now there's one part dealing with the allocation itself and keeping
counters up to date, and another one on top of it to return such an
allocated pointer to the user and update the use count and stats.
This is in anticipation for being able to group cache-related parts.
The release code is still done at once.
diff --git a/include/haproxy/pool.h b/include/haproxy/pool.h
index 0f76b96..19e5965 100644
--- a/include/haproxy/pool.h
+++ b/include/haproxy/pool.h
@@ -48,6 +48,7 @@
/* poison each newly allocated area with this byte if >= 0 */
extern int mem_poison_byte;
+void *pool_get_from_os(struct pool_head *pool);
void *pool_alloc_nocache(struct pool_head *pool);
void dump_pools_to_trash();
void dump_pools(void);
diff --git a/src/pool.c b/src/pool.c
index 5939d96..f309b84 100644
--- a/src/pool.c
+++ b/src/pool.c
@@ -119,6 +119,26 @@
return pool;
}
+/* Tries to allocate an object for the pool <pool> using the system's allocator
+ * and directly returns it. The pool's allocated counter is checked and updated,
+ * but no other checks are performed. The pool's lock is not used and is not a
+ * problem either.
+ */
+void *pool_get_from_os(struct pool_head *pool)
+{
+ if (!pool->limit || pool->allocated < pool->limit) {
+ void *ptr = pool_alloc_area(pool->size + POOL_EXTRA);
+ if (ptr) {
+ _HA_ATOMIC_INC(&pool->allocated);
+ return ptr;
+ }
+ _HA_ATOMIC_INC(&pool->failed);
+ }
+ activity[tid].pool_fail++;
+ return NULL;
+
+}
+
#ifdef CONFIG_HAP_POOLS
/* Evicts some of the oldest objects from the local cache, pushing them to the
* global pool.
@@ -154,25 +174,13 @@
*/
void *pool_alloc_nocache(struct pool_head *pool)
{
- int allocated = pool->allocated;
- int limit = pool->limit;
void *ptr = NULL;
- if (limit && allocated >= limit) {
- activity[tid].pool_fail++;
+ ptr = pool_get_from_os(pool);
+ if (!ptr)
return NULL;
- }
-
- swrate_add_scaled(&pool->needed_avg, POOL_AVG_SAMPLES, pool->allocated, POOL_AVG_SAMPLES/4);
-
- ptr = pool_alloc_area(pool->size + POOL_EXTRA);
- if (!ptr) {
- _HA_ATOMIC_INC(&pool->failed);
- activity[tid].pool_fail++;
- return NULL;
- }
- _HA_ATOMIC_INC(&pool->allocated);
+ swrate_add_scaled(&pool->needed_avg, POOL_AVG_SAMPLES, pool->used, POOL_AVG_SAMPLES/4);
_HA_ATOMIC_INC(&pool->used);
#ifdef DEBUG_MEMORY_POOLS