Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | 62405a2 | 2014-12-23 13:51:28 +0100 | [diff] [blame] | 2 | * include/common/memory.h |
| 3 | * Memory management definitions.. |
| 4 | * |
| 5 | * Copyright (C) 2000-2014 Willy Tarreau - w@1wt.eu |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation, version 2.1 |
| 10 | * exclusively. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 22 | #ifndef _COMMON_MEMORY_H |
| 23 | #define _COMMON_MEMORY_H |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 24 | |
| 25 | #include <stdlib.h> |
Willy Tarreau | e430e77 | 2014-12-23 14:13:16 +0100 | [diff] [blame] | 26 | #include <string.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 27 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 28 | #include <common/config.h> |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 29 | #include <common/mini-clist.h> |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 30 | #include <common/hathreads.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 31 | |
Willy Tarreau | a84dcb8 | 2015-10-28 12:04:02 +0100 | [diff] [blame] | 32 | #ifndef DEBUG_DONT_SHARE_POOLS |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 33 | #define MEM_F_SHARED 0x1 |
Willy Tarreau | a84dcb8 | 2015-10-28 12:04:02 +0100 | [diff] [blame] | 34 | #else |
| 35 | #define MEM_F_SHARED 0 |
| 36 | #endif |
Willy Tarreau | 581bf81 | 2016-01-25 02:19:13 +0100 | [diff] [blame] | 37 | #define MEM_F_EXACT 0x2 |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 38 | |
Willy Tarreau | ac42111 | 2015-10-28 15:09:29 +0100 | [diff] [blame] | 39 | /* reserve an extra void* at the end of a pool for linking */ |
| 40 | #ifdef DEBUG_MEMORY_POOLS |
| 41 | #define POOL_EXTRA (sizeof(void *)) |
| 42 | #define POOL_LINK(pool, item) (void **)(((char *)item) + (pool->size)) |
| 43 | #else |
| 44 | #define POOL_EXTRA (0) |
| 45 | #define POOL_LINK(pool, item) ((void **)(item)) |
| 46 | #endif |
| 47 | |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 48 | struct pool_head { |
| 49 | void **free_list; |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 50 | __decl_hathreads(HA_SPINLOCK_T lock); /* the spin lock */ |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 51 | struct list list; /* list of all known pools */ |
| 52 | unsigned int used; /* how many chunks are currently in use */ |
| 53 | unsigned int allocated; /* how many chunks have been allocated */ |
| 54 | unsigned int limit; /* hard limit on the number of chunks */ |
| 55 | unsigned int minavail; /* how many chunks are expected to be used */ |
| 56 | unsigned int size; /* chunk size */ |
| 57 | unsigned int flags; /* MEM_F_* */ |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 58 | unsigned int users; /* number of pools sharing this zone */ |
Willy Tarreau | 58102cf | 2015-10-28 16:24:21 +0100 | [diff] [blame] | 59 | unsigned int failed; /* failed allocations */ |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 60 | char name[12]; /* name of the pool */ |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 61 | }; |
| 62 | |
Willy Tarreau | 067ac9f | 2015-10-08 14:12:13 +0200 | [diff] [blame] | 63 | /* poison each newly allocated area with this byte if >= 0 */ |
| 64 | extern int mem_poison_byte; |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 65 | |
Willy Tarreau | a885f6d | 2014-12-03 15:25:28 +0100 | [diff] [blame] | 66 | /* Allocates new entries for pool <pool> until there are at least <avail> + 1 |
| 67 | * available, then returns the last one for immediate use, so that at least |
| 68 | * <avail> are left available in the pool upon return. NULL is returned if the |
| 69 | * last entry could not be allocated. It's important to note that at least one |
| 70 | * allocation is always performed even if there are enough entries in the pool. |
| 71 | * A call to the garbage collector is performed at most once in case malloc() |
| 72 | * returns an error, before returning NULL. |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 73 | */ |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 74 | void *__pool_refill_alloc(struct pool_head *pool, unsigned int avail); |
Willy Tarreau | a885f6d | 2014-12-03 15:25:28 +0100 | [diff] [blame] | 75 | void *pool_refill_alloc(struct pool_head *pool, unsigned int avail); |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 76 | |
| 77 | /* Try to find an existing shared pool with the same characteristics and |
| 78 | * returns it, otherwise creates this one. NULL is returned if no memory |
| 79 | * is available for a new creation. |
| 80 | */ |
| 81 | struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags); |
| 82 | |
| 83 | /* Dump statistics on pools usage. |
| 84 | */ |
Willy Tarreau | 12833bb | 2014-01-28 16:49:56 +0100 | [diff] [blame] | 85 | void dump_pools_to_trash(); |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 86 | void dump_pools(void); |
Willy Tarreau | 58102cf | 2015-10-28 16:24:21 +0100 | [diff] [blame] | 87 | int pool_total_failures(); |
| 88 | unsigned long pool_total_allocated(); |
| 89 | unsigned long pool_total_used(); |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 90 | |
| 91 | /* |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 92 | * This function frees whatever can be freed in pool <pool>. |
| 93 | */ |
| 94 | void pool_flush2(struct pool_head *pool); |
| 95 | |
| 96 | /* |
| 97 | * This function frees whatever can be freed in all pools, but respecting |
| 98 | * the minimum thresholds imposed by owners. |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 99 | * |
| 100 | * <pool_ctx> is used when pool_gc2 is called to release resources to allocate |
| 101 | * an element in __pool_refill_alloc. It is important because <pool_ctx> is |
| 102 | * already locked, so we need to skip the lock here. |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 103 | */ |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 104 | void pool_gc2(struct pool_head *pool_ctx); |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 105 | |
| 106 | /* |
| 107 | * This function destroys a pull by freeing it completely. |
| 108 | * This should be called only under extreme circumstances. |
| 109 | */ |
Willy Tarreau | 4d2d098 | 2007-05-14 00:39:29 +0200 | [diff] [blame] | 110 | void *pool_destroy2(struct pool_head *pool); |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 111 | |
| 112 | /* |
Willy Tarreau | 0262241 | 2014-12-08 16:35:23 +0100 | [diff] [blame] | 113 | * Returns a pointer to type <type> taken from the pool <pool_type> if |
| 114 | * available, otherwise returns NULL. No malloc() is attempted, and poisonning |
| 115 | * is never performed. The purpose is to get the fastest possible allocation. |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 116 | */ |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 117 | static inline void *__pool_get_first(struct pool_head *pool) |
Willy Tarreau | e430e77 | 2014-12-23 14:13:16 +0100 | [diff] [blame] | 118 | { |
| 119 | void *p; |
| 120 | |
Willy Tarreau | 0262241 | 2014-12-08 16:35:23 +0100 | [diff] [blame] | 121 | if ((p = pool->free_list) != NULL) { |
Willy Tarreau | ac42111 | 2015-10-28 15:09:29 +0100 | [diff] [blame] | 122 | pool->free_list = *POOL_LINK(pool, p); |
Willy Tarreau | e430e77 | 2014-12-23 14:13:16 +0100 | [diff] [blame] | 123 | pool->used++; |
Willy Tarreau | de30a68 | 2015-10-28 15:23:51 +0100 | [diff] [blame] | 124 | #ifdef DEBUG_MEMORY_POOLS |
| 125 | /* keep track of where the element was allocated from */ |
| 126 | *POOL_LINK(pool, p) = (void *)pool; |
| 127 | #endif |
Willy Tarreau | e430e77 | 2014-12-23 14:13:16 +0100 | [diff] [blame] | 128 | } |
| 129 | return p; |
| 130 | } |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 131 | |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 132 | static inline void *pool_get_first(struct pool_head *pool) |
| 133 | { |
| 134 | void *ret; |
| 135 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 136 | HA_SPIN_LOCK(POOL_LOCK, &pool->lock); |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 137 | ret = __pool_get_first(pool); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 138 | HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock); |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 139 | return ret; |
| 140 | } |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 141 | /* |
Willy Tarreau | 0262241 | 2014-12-08 16:35:23 +0100 | [diff] [blame] | 142 | * Returns a pointer to type <type> taken from the pool <pool_type> or |
| 143 | * dynamically allocated. In the first case, <pool_type> is updated to point to |
| 144 | * the next element in the list. No memory poisonning is ever performed on the |
| 145 | * returned area. |
| 146 | */ |
| 147 | static inline void *pool_alloc_dirty(struct pool_head *pool) |
| 148 | { |
| 149 | void *p; |
| 150 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 151 | HA_SPIN_LOCK(POOL_LOCK, &pool->lock); |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 152 | if ((p = __pool_get_first(pool)) == NULL) |
| 153 | p = __pool_refill_alloc(pool, 0); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 154 | HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock); |
Willy Tarreau | 0262241 | 2014-12-08 16:35:23 +0100 | [diff] [blame] | 155 | return p; |
| 156 | } |
| 157 | |
Willy Tarreau | f13322e | 2017-11-22 10:50:54 +0100 | [diff] [blame^] | 158 | /* allocates an area of size <size> and returns it. The semantics are similar |
| 159 | * to those of malloc(). |
| 160 | */ |
| 161 | static inline void *pool_alloc_area(size_t size) |
| 162 | { |
| 163 | return malloc(size); |
| 164 | } |
| 165 | |
| 166 | /* frees an area <area> of size <size> allocated by pool_alloc_area(). The |
| 167 | * semantics are identical to free() except that the size is specified and |
| 168 | * may be ignored. |
| 169 | */ |
| 170 | static inline void pool_free_area(void *area, size_t __maybe_unused size) |
| 171 | { |
| 172 | free(area); |
| 173 | } |
| 174 | |
Willy Tarreau | 0262241 | 2014-12-08 16:35:23 +0100 | [diff] [blame] | 175 | /* |
| 176 | * Returns a pointer to type <type> taken from the pool <pool_type> or |
| 177 | * dynamically allocated. In the first case, <pool_type> is updated to point to |
| 178 | * the next element in the list. Memory poisonning is performed if enabled. |
| 179 | */ |
| 180 | static inline void *pool_alloc2(struct pool_head *pool) |
| 181 | { |
| 182 | void *p; |
| 183 | |
| 184 | p = pool_alloc_dirty(pool); |
Willy Tarreau | de30a68 | 2015-10-28 15:23:51 +0100 | [diff] [blame] | 185 | #ifdef DEBUG_MEMORY_POOLS |
| 186 | if (p) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 187 | HA_SPIN_LOCK(POOL_LOCK, &pool->lock); |
Willy Tarreau | de30a68 | 2015-10-28 15:23:51 +0100 | [diff] [blame] | 188 | /* keep track of where the element was allocated from */ |
| 189 | *POOL_LINK(pool, p) = (void *)pool; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 190 | HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock); |
Willy Tarreau | de30a68 | 2015-10-28 15:23:51 +0100 | [diff] [blame] | 191 | } |
| 192 | #endif |
| 193 | if (p && mem_poison_byte >= 0) { |
Willy Tarreau | 0262241 | 2014-12-08 16:35:23 +0100 | [diff] [blame] | 194 | memset(p, mem_poison_byte, pool->size); |
Willy Tarreau | de30a68 | 2015-10-28 15:23:51 +0100 | [diff] [blame] | 195 | } |
| 196 | |
Willy Tarreau | 0262241 | 2014-12-08 16:35:23 +0100 | [diff] [blame] | 197 | return p; |
| 198 | } |
| 199 | |
| 200 | /* |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 201 | * Puts a memory area back to the corresponding pool. |
| 202 | * Items are chained directly through a pointer that |
| 203 | * is written in the beginning of the memory area, so |
| 204 | * there's no need for any carrier cell. This implies |
| 205 | * that each memory area is at least as big as one |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 206 | * pointer. Just like with the libc's free(), nothing |
| 207 | * is done if <ptr> is NULL. |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 208 | */ |
Willy Tarreau | e430e77 | 2014-12-23 14:13:16 +0100 | [diff] [blame] | 209 | static inline void pool_free2(struct pool_head *pool, void *ptr) |
| 210 | { |
| 211 | if (likely(ptr != NULL)) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 212 | HA_SPIN_LOCK(POOL_LOCK, &pool->lock); |
Willy Tarreau | de30a68 | 2015-10-28 15:23:51 +0100 | [diff] [blame] | 213 | #ifdef DEBUG_MEMORY_POOLS |
| 214 | /* we'll get late corruption if we refill to the wrong pool or double-free */ |
| 215 | if (*POOL_LINK(pool, ptr) != (void *)pool) |
| 216 | *(int *)0 = 0; |
| 217 | #endif |
Willy Tarreau | ac42111 | 2015-10-28 15:09:29 +0100 | [diff] [blame] | 218 | *POOL_LINK(pool, ptr) = (void *)pool->free_list; |
Willy Tarreau | e430e77 | 2014-12-23 14:13:16 +0100 | [diff] [blame] | 219 | pool->free_list = (void *)ptr; |
| 220 | pool->used--; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 221 | HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock); |
Willy Tarreau | e430e77 | 2014-12-23 14:13:16 +0100 | [diff] [blame] | 222 | } |
| 223 | } |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 224 | #endif /* _COMMON_MEMORY_H */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 225 | |
| 226 | /* |
| 227 | * Local variables: |
| 228 | * c-indent-level: 8 |
| 229 | * c-basic-offset: 8 |
| 230 | * End: |
| 231 | */ |