]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: pools: partially uninline pool_free()
authorWilly Tarreau <w@1wt.eu>
Mon, 24 Jan 2022 10:51:43 +0000 (11:51 +0100)
committerWilly Tarreau <w@1wt.eu>
Mon, 24 Jan 2022 15:40:48 +0000 (16:40 +0100)
The pool_free() function has become a bit big over time due to the
extra consistency checks. It used to remain inline only to deal
cleanly with the NULL pointer free that's quite present on some
structures (e.g. in stream_free()).

Here we're splitting the function in two:
  - __pool_free() does the inner block without the pointer test and
    becomes a function ;

  - pool_free() is now a macro that only checks the pointer and calls
    __pool_free() if needed.

The use of a macro versus an inline function is only motivated by an
easier intrumentation of the code later.

With this change, the code size reduces by ~1%, which means that at
this point all pool_free() call places used to represent more than
1% of the total code size.

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

index 61428a884f55a6c27c8bc089fcc1fcc7f41fb459..d3074084f403e937e8167310e23cf7a8888d3e58 100644 (file)
@@ -97,6 +97,7 @@ void create_pool_callback(struct pool_head **ptr, char *name, unsigned int size)
 void *pool_destroy(struct pool_head *pool);
 void pool_destroy_all(void);
 int mem_should_fail(const struct pool_head *pool);
+void __pool_free(struct pool_head *pool, void *ptr);
 
 
 #ifdef CONFIG_HAP_POOLS
@@ -345,26 +346,15 @@ static inline void *pool_zalloc(struct pool_head *pool)
 }
 
 /*
- * Puts a memory area back to the corresponding pool.
- * Items are chained directly through a pointer that
- * is written in the beginning of the memory area, so
- * there's no need for any carrier cell. This implies
- * that each memory area is at least as big as one
- * pointer. Just like with the libc's free(), nothing
- * is done if <ptr> is NULL.
+ * Puts a memory area back to the corresponding pool. Just like with the libc's
+ * free(), <ptr> may be NULL.
  */
-static inline void pool_free(struct pool_head *pool, void *ptr)
-{
-        if (likely(ptr != NULL)) {
-               /* we'll get late corruption if we refill to the wrong pool or double-free */
-               POOL_DEBUG_CHECK_MARK(pool, ptr);
-
-               if (unlikely(mem_poison_byte >= 0))
-                       memset(ptr, mem_poison_byte, pool->size);
-
-               pool_put_to_cache(pool, ptr);
-       }
-}
+#define pool_free(pool, ptr)                           \
+       do {                                            \
+               typeof(ptr) __ptr = (ptr);              \
+               if (likely((__ptr) != NULL))            \
+                       __pool_free(pool, __ptr);       \
+       } while (0)
 
 #endif /* _HAPROXY_POOL_H */
 
index 06089c3c5972c481af9e315701f39c010b9da2a5..21b48af4d038aa9354038b750d36d6297c3b81dd 100644 (file)
@@ -588,6 +588,21 @@ void pool_gc(struct pool_head *pool_ctx)
 
 #endif /* CONFIG_HAP_POOLS */
 
+/*
+ * Puts a memory area back to the corresponding pool. <ptr> be valid. Using
+ * pool_free() is preferred.
+ */
+void __pool_free(struct pool_head *pool, void *ptr)
+{
+       /* we'll get late corruption if we refill to the wrong pool or double-free */
+       POOL_DEBUG_CHECK_MARK(pool, ptr);
+
+       if (unlikely(mem_poison_byte >= 0))
+               memset(ptr, mem_poison_byte, pool->size);
+
+       pool_put_to_cache(pool, ptr);
+}
+
 
 #ifdef DEBUG_UAF