]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: pools: store the allocated size for each pool
authorWilly Tarreau <w@1wt.eu>
Wed, 23 Feb 2022 07:57:59 +0000 (08:57 +0100)
committerWilly Tarreau <w@1wt.eu>
Wed, 23 Feb 2022 16:11:33 +0000 (17:11 +0100)
The allocated size is the visible size plus the extra storage. Since
for now we can store up to two extra elements (mark and tracer), it's
convenient because now we know that the mark is always stored at
->size, and the tracer is always before ->alloc_sz.

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

index 0a8f9dd02a65feaeb3655e2fcc632525684250b0..a7b3dda783223a1c33b1b1b13b1d446160db174d 100644 (file)
@@ -113,7 +113,7 @@ struct pool_head {
        unsigned int flags;     /* MEM_F_* */
        unsigned int users;     /* number of pools sharing this zone */
        unsigned int failed;    /* failed allocations */
-       /* 32-bit hole here */
+       unsigned int alloc_sz;  /* allocated size (includes hidden fields) */
        struct list list;       /* list of all known pools */
        char name[12];          /* name of the pool */
        struct pool_cache_head cache[MAX_THREADS]; /* pool caches */
index acab81d97bd3e44c705aeb2d73a7c47067686bbe..90d928aded527214b6937e342e68d14eb4573679 100644 (file)
@@ -87,7 +87,8 @@
 #endif // DEBUG_MEMORY_POOLS
 
 /* It's possible to trace callers of pool_free() by placing their pointer
- * after the end of the area and the optional mark above.
+ * after the end of the area and the optional mark above, which means the
+ * end of the allocated array.
  */
 #if defined(DEBUG_POOL_TRACING)
 # define POOL_EXTRA_CALLER (sizeof(void *))
@@ -96,7 +97,7 @@
                typeof(pool) __p = (pool);                              \
                typeof(item) __i = (item);                              \
                typeof(caller) __c = (caller);                          \
-               *(typeof(caller)*)(((char *)__i) + __p->size + POOL_EXTRA_MARK) = __c; \
+               *(typeof(caller)*)(((char *)__i) + __p->alloc_sz - sizeof(void*)) = __c; \
        } while (0)
 
 #else // DEBUG_POOL_TRACING
index cb1f76891ab93993dcef93c2d3468da994c1f27e..f203397b06dbb313d076450fd1d86eb46d425499 100644 (file)
@@ -252,6 +252,7 @@ struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags)
                        return NULL;
                if (name)
                        strlcpy2(pool->name, name, sizeof(pool->name));
+               pool->alloc_sz = size + POOL_EXTRA;
                pool->size = size;
                pool->flags = flags;
                LIST_APPEND(start, &pool->list);
@@ -276,7 +277,7 @@ struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags)
 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);
+               void *ptr = pool_alloc_area(pool->alloc_sz);
                if (ptr) {
                        _HA_ATOMIC_INC(&pool->allocated);
                        return ptr;
@@ -301,7 +302,7 @@ void pool_put_to_os(struct pool_head *pool, void *ptr)
        *(uint32_t *)ptr = 0xDEADADD4;
 #endif /* DEBUG_UAF */
 
-       pool_free_area(ptr, pool->size + POOL_EXTRA);
+       pool_free_area(ptr, pool->alloc_sz);
        _HA_ATOMIC_DEC(&pool->allocated);
 }