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> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 30 | |
Willy Tarreau | a84dcb8 | 2015-10-28 12:04:02 +0100 | [diff] [blame] | 31 | #ifndef DEBUG_DONT_SHARE_POOLS |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 32 | #define MEM_F_SHARED 0x1 |
Willy Tarreau | a84dcb8 | 2015-10-28 12:04:02 +0100 | [diff] [blame] | 33 | #else |
| 34 | #define MEM_F_SHARED 0 |
| 35 | #endif |
Willy Tarreau | 581bf81 | 2016-01-25 02:19:13 +0100 | [diff] [blame] | 36 | #define MEM_F_EXACT 0x2 |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 37 | |
Willy Tarreau | ac42111 | 2015-10-28 15:09:29 +0100 | [diff] [blame] | 38 | /* reserve an extra void* at the end of a pool for linking */ |
| 39 | #ifdef DEBUG_MEMORY_POOLS |
| 40 | #define POOL_EXTRA (sizeof(void *)) |
| 41 | #define POOL_LINK(pool, item) (void **)(((char *)item) + (pool->size)) |
| 42 | #else |
| 43 | #define POOL_EXTRA (0) |
| 44 | #define POOL_LINK(pool, item) ((void **)(item)) |
| 45 | #endif |
| 46 | |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 47 | struct pool_head { |
| 48 | void **free_list; |
| 49 | struct list list; /* list of all known pools */ |
| 50 | unsigned int used; /* how many chunks are currently in use */ |
| 51 | unsigned int allocated; /* how many chunks have been allocated */ |
| 52 | unsigned int limit; /* hard limit on the number of chunks */ |
| 53 | unsigned int minavail; /* how many chunks are expected to be used */ |
| 54 | unsigned int size; /* chunk size */ |
| 55 | unsigned int flags; /* MEM_F_* */ |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 56 | unsigned int users; /* number of pools sharing this zone */ |
Willy Tarreau | 58102cf | 2015-10-28 16:24:21 +0100 | [diff] [blame] | 57 | unsigned int failed; /* failed allocations */ |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 58 | char name[12]; /* name of the pool */ |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 59 | }; |
| 60 | |
Willy Tarreau | 067ac9f | 2015-10-08 14:12:13 +0200 | [diff] [blame] | 61 | /* poison each newly allocated area with this byte if >= 0 */ |
| 62 | extern int mem_poison_byte; |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 63 | |
Willy Tarreau | 62405a2 | 2014-12-23 13:51:28 +0100 | [diff] [blame] | 64 | /* |
| 65 | * This function destroys a pull by freeing it completely. |
| 66 | * This should be called only under extreme circumstances. |
| 67 | */ |
| 68 | static inline void pool_destroy(void **pool) |
| 69 | { |
| 70 | void *temp, *next; |
| 71 | next = pool; |
| 72 | while (next) { |
| 73 | temp = next; |
| 74 | next = *(void **)temp; |
| 75 | free(temp); |
| 76 | } |
| 77 | } |
| 78 | |
Willy Tarreau | a885f6d | 2014-12-03 15:25:28 +0100 | [diff] [blame] | 79 | /* Allocates new entries for pool <pool> until there are at least <avail> + 1 |
| 80 | * available, then returns the last one for immediate use, so that at least |
| 81 | * <avail> are left available in the pool upon return. NULL is returned if the |
| 82 | * last entry could not be allocated. It's important to note that at least one |
| 83 | * allocation is always performed even if there are enough entries in the pool. |
| 84 | * A call to the garbage collector is performed at most once in case malloc() |
| 85 | * returns an error, before returning NULL. |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 86 | */ |
Willy Tarreau | a885f6d | 2014-12-03 15:25:28 +0100 | [diff] [blame] | 87 | void *pool_refill_alloc(struct pool_head *pool, unsigned int avail); |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 88 | |
| 89 | /* Try to find an existing shared pool with the same characteristics and |
| 90 | * returns it, otherwise creates this one. NULL is returned if no memory |
| 91 | * is available for a new creation. |
| 92 | */ |
| 93 | struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags); |
| 94 | |
| 95 | /* Dump statistics on pools usage. |
| 96 | */ |
Willy Tarreau | 12833bb | 2014-01-28 16:49:56 +0100 | [diff] [blame] | 97 | void dump_pools_to_trash(); |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 98 | void dump_pools(void); |
Willy Tarreau | 58102cf | 2015-10-28 16:24:21 +0100 | [diff] [blame] | 99 | int pool_total_failures(); |
| 100 | unsigned long pool_total_allocated(); |
| 101 | unsigned long pool_total_used(); |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 102 | |
| 103 | /* |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 104 | * This function frees whatever can be freed in pool <pool>. |
| 105 | */ |
| 106 | void pool_flush2(struct pool_head *pool); |
| 107 | |
| 108 | /* |
| 109 | * This function frees whatever can be freed in all pools, but respecting |
| 110 | * the minimum thresholds imposed by owners. |
| 111 | */ |
| 112 | void pool_gc2(); |
| 113 | |
| 114 | /* |
| 115 | * This function destroys a pull by freeing it completely. |
| 116 | * This should be called only under extreme circumstances. |
| 117 | */ |
Willy Tarreau | 4d2d098 | 2007-05-14 00:39:29 +0200 | [diff] [blame] | 118 | void *pool_destroy2(struct pool_head *pool); |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 119 | |
| 120 | /* |
Willy Tarreau | 0262241 | 2014-12-08 16:35:23 +0100 | [diff] [blame] | 121 | * Returns a pointer to type <type> taken from the pool <pool_type> if |
| 122 | * available, otherwise returns NULL. No malloc() is attempted, and poisonning |
| 123 | * is never performed. The purpose is to get the fastest possible allocation. |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 124 | */ |
Willy Tarreau | 0262241 | 2014-12-08 16:35:23 +0100 | [diff] [blame] | 125 | static inline void *pool_get_first(struct pool_head *pool) |
Willy Tarreau | e430e77 | 2014-12-23 14:13:16 +0100 | [diff] [blame] | 126 | { |
| 127 | void *p; |
| 128 | |
Willy Tarreau | 0262241 | 2014-12-08 16:35:23 +0100 | [diff] [blame] | 129 | if ((p = pool->free_list) != NULL) { |
Willy Tarreau | ac42111 | 2015-10-28 15:09:29 +0100 | [diff] [blame] | 130 | pool->free_list = *POOL_LINK(pool, p); |
Willy Tarreau | e430e77 | 2014-12-23 14:13:16 +0100 | [diff] [blame] | 131 | pool->used++; |
Willy Tarreau | de30a68 | 2015-10-28 15:23:51 +0100 | [diff] [blame] | 132 | #ifdef DEBUG_MEMORY_POOLS |
| 133 | /* keep track of where the element was allocated from */ |
| 134 | *POOL_LINK(pool, p) = (void *)pool; |
| 135 | #endif |
Willy Tarreau | e430e77 | 2014-12-23 14:13:16 +0100 | [diff] [blame] | 136 | } |
| 137 | return p; |
| 138 | } |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 139 | |
| 140 | /* |
Willy Tarreau | 0262241 | 2014-12-08 16:35:23 +0100 | [diff] [blame] | 141 | * Returns a pointer to type <type> taken from the pool <pool_type> or |
| 142 | * dynamically allocated. In the first case, <pool_type> is updated to point to |
| 143 | * the next element in the list. No memory poisonning is ever performed on the |
| 144 | * returned area. |
| 145 | */ |
| 146 | static inline void *pool_alloc_dirty(struct pool_head *pool) |
| 147 | { |
| 148 | void *p; |
| 149 | |
| 150 | if ((p = pool_get_first(pool)) == NULL) |
Willy Tarreau | a885f6d | 2014-12-03 15:25:28 +0100 | [diff] [blame] | 151 | p = pool_refill_alloc(pool, 0); |
Willy Tarreau | 0262241 | 2014-12-08 16:35:23 +0100 | [diff] [blame] | 152 | |
| 153 | return p; |
| 154 | } |
| 155 | |
| 156 | /* |
| 157 | * Returns a pointer to type <type> taken from the pool <pool_type> or |
| 158 | * dynamically allocated. In the first case, <pool_type> is updated to point to |
| 159 | * the next element in the list. Memory poisonning is performed if enabled. |
| 160 | */ |
| 161 | static inline void *pool_alloc2(struct pool_head *pool) |
| 162 | { |
| 163 | void *p; |
| 164 | |
| 165 | p = pool_alloc_dirty(pool); |
Willy Tarreau | de30a68 | 2015-10-28 15:23:51 +0100 | [diff] [blame] | 166 | #ifdef DEBUG_MEMORY_POOLS |
| 167 | if (p) { |
| 168 | /* keep track of where the element was allocated from */ |
| 169 | *POOL_LINK(pool, p) = (void *)pool; |
| 170 | } |
| 171 | #endif |
| 172 | if (p && mem_poison_byte >= 0) { |
Willy Tarreau | 0262241 | 2014-12-08 16:35:23 +0100 | [diff] [blame] | 173 | memset(p, mem_poison_byte, pool->size); |
Willy Tarreau | de30a68 | 2015-10-28 15:23:51 +0100 | [diff] [blame] | 174 | } |
| 175 | |
Willy Tarreau | 0262241 | 2014-12-08 16:35:23 +0100 | [diff] [blame] | 176 | return p; |
| 177 | } |
| 178 | |
| 179 | /* |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 180 | * Puts a memory area back to the corresponding pool. |
| 181 | * Items are chained directly through a pointer that |
| 182 | * is written in the beginning of the memory area, so |
| 183 | * there's no need for any carrier cell. This implies |
| 184 | * that each memory area is at least as big as one |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 185 | * pointer. Just like with the libc's free(), nothing |
| 186 | * is done if <ptr> is NULL. |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 187 | */ |
Willy Tarreau | e430e77 | 2014-12-23 14:13:16 +0100 | [diff] [blame] | 188 | static inline void pool_free2(struct pool_head *pool, void *ptr) |
| 189 | { |
| 190 | if (likely(ptr != NULL)) { |
Willy Tarreau | de30a68 | 2015-10-28 15:23:51 +0100 | [diff] [blame] | 191 | #ifdef DEBUG_MEMORY_POOLS |
| 192 | /* we'll get late corruption if we refill to the wrong pool or double-free */ |
| 193 | if (*POOL_LINK(pool, ptr) != (void *)pool) |
| 194 | *(int *)0 = 0; |
| 195 | #endif |
Willy Tarreau | ac42111 | 2015-10-28 15:09:29 +0100 | [diff] [blame] | 196 | *POOL_LINK(pool, ptr) = (void *)pool->free_list; |
Willy Tarreau | e430e77 | 2014-12-23 14:13:16 +0100 | [diff] [blame] | 197 | pool->free_list = (void *)ptr; |
| 198 | pool->used--; |
| 199 | } |
| 200 | } |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 201 | |
| 202 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 203 | #endif /* _COMMON_MEMORY_H */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 204 | |
| 205 | /* |
| 206 | * Local variables: |
| 207 | * c-indent-level: 8 |
| 208 | * c-basic-offset: 8 |
| 209 | * End: |
| 210 | */ |