]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: memory: improve pool_refill_alloc() to pass a refill count
authorWilly Tarreau <w@1wt.eu>
Wed, 3 Dec 2014 14:25:28 +0000 (15:25 +0100)
committerWilly Tarreau <w@1wt.eu>
Wed, 24 Dec 2014 22:47:31 +0000 (23:47 +0100)
Till now this function would only allocate one entry at a time. But with
dynamic buffers we'll like to allocate the number of missing entries to
properly refill the pool.

Let's modify it to take a minimum amount of available entries. This means
that when we know we need at least a number of available entries, we can
ask to allocate all of them at once. It also ensures that we don't move
the pointers back and forth between the caller and the pool, and that we
don't call pool_gc2() for each failed malloc. Instead, it's called only
once and the malloc is only allowed to fail once.

include/common/memory.h
src/memory.c

index 3a0eaf6955ef212f421a1fa5fe8d3b24dbc3d620..965dfc4886e16479d52e14818f840ad6ce504e79 100644 (file)
@@ -61,10 +61,15 @@ static inline void pool_destroy(void **pool)
        }
 }
 
-/* Allocate a new entry for pool <pool>, and return it for immediate use.
- * NULL is returned if no memory is available for a new creation.
+/* Allocates new entries for pool <pool> until there are at least <avail> + 1
+ * available, then returns the last one for immediate use, so that at least
+ * <avail> are left available in the pool upon return. NULL is returned if the
+ * last entry could not be allocated. It's important to note that at least one
+ * allocation is always performed even if there are enough entries in the pool.
+ * A call to the garbage collector is performed at most once in case malloc()
+ * returns an error, before returning NULL.
  */
-void *pool_refill_alloc(struct pool_head *pool);
+void *pool_refill_alloc(struct pool_head *pool, unsigned int avail);
 
 /* Try to find an existing shared pool with the same characteristics and
  * returns it, otherwise creates this one. NULL is returned if no memory
@@ -121,7 +126,7 @@ static inline void *pool_alloc_dirty(struct pool_head *pool)
        void *p;
 
        if ((p = pool_get_first(pool)) == NULL)
-               p = pool_refill_alloc(pool);
+               p = pool_refill_alloc(pool, 0);
 
        return p;
 }
index 3fbc52059413b9d6a956eeb4062be1f336c86641..61c150bfd996afb509da615b58cd78649b353952 100644 (file)
@@ -79,26 +79,42 @@ struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags)
        return pool;
 }
 
-/* Allocate a new entry for pool <pool>, and return it for immediate use.
- * NULL is returned if no memory is available for a new creation. A call
- * to the garbage collector is performed before returning NULL.
+/* Allocates new entries for pool <pool> until there are at least <avail> + 1
+ * available, then returns the last one for immediate use, so that at least
+ * <avail> are left available in the pool upon return. NULL is returned if the
+ * last entry could not be allocated. It's important to note that at least one
+ * allocation is always performed even if there are enough entries in the pool.
+ * A call to the garbage collector is performed at most once in case malloc()
+ * returns an error, before returning NULL.
  */
-void *pool_refill_alloc(struct pool_head *pool)
+void *pool_refill_alloc(struct pool_head *pool, unsigned int avail)
 {
-       void *ret;
-
-       if (pool->limit && (pool->allocated >= pool->limit))
-               return NULL;
-       ret = CALLOC(1, pool->size);
-       if (!ret) {
-               pool_gc2();
-               ret = CALLOC(1, pool->size);
-               if (!ret)
+       void *ptr = NULL;
+       int failed = 0;
+
+       /* stop point */
+       avail += pool->used;
+
+       while (1) {
+               if (pool->limit && pool->allocated >= pool->limit)
                        return NULL;
+
+               ptr = MALLOC(pool->size);
+               if (!ptr) {
+                       if (failed)
+                               return NULL;
+                       failed++;
+                       pool_gc2();
+                       continue;
+               }
+               if (++pool->allocated > avail)
+                       break;
+
+               *(void **)ptr = (void *)pool->free_list;
+               pool->free_list = ptr;
        }
-       pool->allocated++;
        pool->used++;
-       return ret;
+       return ptr;
 }
 
 /*