]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: pools: split the OS-based allocator in two
authorWilly Tarreau <w@1wt.eu>
Sat, 17 Apr 2021 14:57:25 +0000 (16:57 +0200)
committerWilly Tarreau <w@1wt.eu>
Mon, 19 Apr 2021 13:24:33 +0000 (15:24 +0200)
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.

include/haproxy/pool.h
src/pool.c

index 0f76b9658338aa4d1c51e7a4f331f491a945b1df..19e596552dd98b1927e7218b8dee22152fc0c373 100644 (file)
@@ -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);
index 5939d961d192a5bc6cf51ac6dc331293e27ad974..f309b848799a8d6743417fd88cb94eec7f8a9d5c 100644 (file)
@@ -119,6 +119,26 @@ struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags)
        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_evict_from_local_caches()
  */
 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++;
-               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++;
+       ptr = pool_get_from_os(pool);
+       if (!ptr)
                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