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