Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 2 | include/common/memory.h |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3 | Memory management definitions.. |
| 4 | |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 5 | Copyright (C) 2000-2007 Willy Tarreau - w@1wt.eu |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 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 | */ |
| 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> |
| 26 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 27 | #include <common/config.h> |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 28 | #include <common/mini-clist.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 29 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 30 | /* |
| 31 | * Returns a pointer to an area of <__len> bytes taken from the pool <pool> or |
| 32 | * dynamically allocated. In the first case, <__pool> is updated to point to |
| 33 | * the next element in the list. |
| 34 | */ |
| 35 | #define pool_alloc_from(__pool, __len) \ |
| 36 | ({ \ |
| 37 | void *__p; \ |
| 38 | if ((__p = (__pool)) == NULL) \ |
| 39 | __p = malloc(((__len) >= sizeof (void *)) ? \ |
| 40 | (__len) : sizeof(void *)); \ |
| 41 | else { \ |
| 42 | __pool = *(void **)(__pool); \ |
| 43 | } \ |
| 44 | __p; \ |
| 45 | }) |
| 46 | |
| 47 | /* |
| 48 | * Puts a memory area back to the corresponding pool. |
| 49 | * Items are chained directly through a pointer that |
| 50 | * is written in the beginning of the memory area, so |
| 51 | * there's no need for any carrier cell. This implies |
| 52 | * that each memory area is at least as big as one |
| 53 | * pointer. |
| 54 | */ |
| 55 | #define pool_free_to(__pool, __ptr) \ |
| 56 | ({ \ |
| 57 | *(void **)(__ptr) = (void *)(__pool); \ |
| 58 | __pool = (void *)(__ptr); \ |
| 59 | }) |
| 60 | |
| 61 | |
| 62 | #ifdef CONFIG_HAP_MEM_OPTIM |
| 63 | /* |
| 64 | * Returns a pointer to type <type> taken from the |
| 65 | * pool <pool_type> or dynamically allocated. In the |
| 66 | * first case, <pool_type> is updated to point to the |
| 67 | * next element in the list. |
| 68 | */ |
| 69 | #define pool_alloc(type) \ |
| 70 | ({ \ |
| 71 | void *__p; \ |
| 72 | if ((__p = pool_##type) == NULL) \ |
| 73 | __p = malloc(sizeof_##type); \ |
| 74 | else { \ |
| 75 | pool_##type = *(void **)pool_##type; \ |
| 76 | } \ |
| 77 | __p; \ |
| 78 | }) |
| 79 | |
| 80 | /* |
| 81 | * Puts a memory area back to the corresponding pool. |
| 82 | * Items are chained directly through a pointer that |
| 83 | * is written in the beginning of the memory area, so |
| 84 | * there's no need for any carrier cell. This implies |
| 85 | * that each memory area is at least as big as one |
| 86 | * pointer. |
| 87 | */ |
| 88 | #define pool_free(type, ptr) \ |
| 89 | ({ \ |
| 90 | *(void **)ptr = (void *)pool_##type; \ |
| 91 | pool_##type = (void *)ptr; \ |
| 92 | }) |
| 93 | |
| 94 | #else |
| 95 | #define pool_alloc(type) (calloc(1,sizeof_##type)) |
| 96 | #define pool_free(type, ptr) (free(ptr)) |
| 97 | #endif /* CONFIG_HAP_MEM_OPTIM */ |
| 98 | |
| 99 | /* |
| 100 | * This function destroys a pull by freeing it completely. |
| 101 | * This should be called only under extreme circumstances. |
| 102 | */ |
| 103 | static inline void pool_destroy(void **pool) |
| 104 | { |
| 105 | void *temp, *next; |
| 106 | next = pool; |
| 107 | while (next) { |
| 108 | temp = next; |
| 109 | next = *(void **)temp; |
| 110 | free(temp); |
| 111 | } |
| 112 | } |
| 113 | |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 114 | |
| 115 | /******* pools version 2 ********/ |
| 116 | |
| 117 | #define MEM_F_SHARED 0x1 |
| 118 | |
| 119 | struct pool_head { |
| 120 | void **free_list; |
| 121 | struct list list; /* list of all known pools */ |
| 122 | unsigned int used; /* how many chunks are currently in use */ |
| 123 | unsigned int allocated; /* how many chunks have been allocated */ |
| 124 | unsigned int limit; /* hard limit on the number of chunks */ |
| 125 | unsigned int minavail; /* how many chunks are expected to be used */ |
| 126 | unsigned int size; /* chunk size */ |
| 127 | unsigned int flags; /* MEM_F_* */ |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 128 | unsigned int users; /* number of pools sharing this zone */ |
| 129 | char name[12]; /* name of the pool */ |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | |
| 133 | /* Allocate a new entry for pool <pool>, and return it for immediate use. |
| 134 | * NULL is returned if no memory is available for a new creation. |
| 135 | */ |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 136 | void *pool_refill_alloc(struct pool_head *pool); |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 137 | |
| 138 | /* Try to find an existing shared pool with the same characteristics and |
| 139 | * returns it, otherwise creates this one. NULL is returned if no memory |
| 140 | * is available for a new creation. |
| 141 | */ |
| 142 | struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags); |
| 143 | |
| 144 | /* Dump statistics on pools usage. |
| 145 | */ |
| 146 | void dump_pools(void); |
| 147 | |
| 148 | /* |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 149 | * This function frees whatever can be freed in pool <pool>. |
| 150 | */ |
| 151 | void pool_flush2(struct pool_head *pool); |
| 152 | |
| 153 | /* |
| 154 | * This function frees whatever can be freed in all pools, but respecting |
| 155 | * the minimum thresholds imposed by owners. |
| 156 | */ |
| 157 | void pool_gc2(); |
| 158 | |
| 159 | /* |
| 160 | * This function destroys a pull by freeing it completely. |
| 161 | * This should be called only under extreme circumstances. |
| 162 | */ |
Willy Tarreau | 4d2d098 | 2007-05-14 00:39:29 +0200 | [diff] [blame] | 163 | void *pool_destroy2(struct pool_head *pool); |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 164 | |
| 165 | /* |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 166 | * Returns a pointer to type <type> taken from the |
| 167 | * pool <pool_type> or dynamically allocated. In the |
| 168 | * first case, <pool_type> is updated to point to the |
| 169 | * next element in the list. |
| 170 | */ |
| 171 | #define pool_alloc2(pool) \ |
| 172 | ({ \ |
| 173 | void *__p; \ |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 174 | if ((__p = pool->free_list) == NULL) \ |
| 175 | __p = pool_refill_alloc(pool); \ |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 176 | else { \ |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 177 | pool->free_list = *(void **)pool->free_list; \ |
| 178 | pool->used++; \ |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 179 | } \ |
| 180 | __p; \ |
| 181 | }) |
| 182 | |
| 183 | /* |
| 184 | * Puts a memory area back to the corresponding pool. |
| 185 | * Items are chained directly through a pointer that |
| 186 | * is written in the beginning of the memory area, so |
| 187 | * there's no need for any carrier cell. This implies |
| 188 | * that each memory area is at least as big as one |
| 189 | * pointer. |
| 190 | */ |
| 191 | #define pool_free2(pool, ptr) \ |
| 192 | ({ \ |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 193 | *(void **)ptr = (void *)pool->free_list; \ |
| 194 | pool->free_list = (void *)ptr; \ |
| 195 | pool->used--; \ |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 196 | }) |
| 197 | |
| 198 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 199 | #endif /* _COMMON_MEMORY_H */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 200 | |
| 201 | /* |
| 202 | * Local variables: |
| 203 | * c-indent-level: 8 |
| 204 | * c-basic-offset: 8 |
| 205 | * End: |
| 206 | */ |