blob: 999150d35ea085c888a0b4d0c77f55ec9bcd8966 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreau62405a22014-12-23 13:51:28 +01002 * 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 Tarreaubaaee002006-06-26 02:48:02 +020021
Willy Tarreau2dd0d472006-06-29 17:53:05 +020022#ifndef _COMMON_MEMORY_H
23#define _COMMON_MEMORY_H
Willy Tarreaubaaee002006-06-26 02:48:02 +020024
25#include <stdlib.h>
Willy Tarreaue430e772014-12-23 14:13:16 +010026#include <string.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020027
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/config.h>
Willy Tarreau50e608d2007-05-13 18:26:08 +020029#include <common/mini-clist.h>
Christopher Fauletb349e482017-08-29 09:52:38 +020030#include <common/hathreads.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020031
Willy Tarreaua84dcb82015-10-28 12:04:02 +010032#ifndef DEBUG_DONT_SHARE_POOLS
Willy Tarreau50e608d2007-05-13 18:26:08 +020033#define MEM_F_SHARED 0x1
Willy Tarreaua84dcb82015-10-28 12:04:02 +010034#else
35#define MEM_F_SHARED 0
36#endif
Willy Tarreau581bf812016-01-25 02:19:13 +010037#define MEM_F_EXACT 0x2
Willy Tarreau50e608d2007-05-13 18:26:08 +020038
Willy Tarreauac421112015-10-28 15:09:29 +010039/* 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 Tarreau50e608d2007-05-13 18:26:08 +020048struct pool_head {
49 void **free_list;
Christopher Fauletb349e482017-08-29 09:52:38 +020050#ifdef USE_THREAD
51 HA_SPINLOCK_T lock; /* the spin lock */
52#endif
Willy Tarreau50e608d2007-05-13 18:26:08 +020053 struct list list; /* list of all known pools */
54 unsigned int used; /* how many chunks are currently in use */
55 unsigned int allocated; /* how many chunks have been allocated */
56 unsigned int limit; /* hard limit on the number of chunks */
57 unsigned int minavail; /* how many chunks are expected to be used */
58 unsigned int size; /* chunk size */
59 unsigned int flags; /* MEM_F_* */
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020060 unsigned int users; /* number of pools sharing this zone */
Willy Tarreau58102cf2015-10-28 16:24:21 +010061 unsigned int failed; /* failed allocations */
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020062 char name[12]; /* name of the pool */
Willy Tarreau50e608d2007-05-13 18:26:08 +020063};
64
Willy Tarreau067ac9f2015-10-08 14:12:13 +020065/* poison each newly allocated area with this byte if >= 0 */
66extern int mem_poison_byte;
Willy Tarreau50e608d2007-05-13 18:26:08 +020067
Willy Tarreaua885f6d2014-12-03 15:25:28 +010068/* Allocates new entries for pool <pool> until there are at least <avail> + 1
69 * available, then returns the last one for immediate use, so that at least
70 * <avail> are left available in the pool upon return. NULL is returned if the
71 * last entry could not be allocated. It's important to note that at least one
72 * allocation is always performed even if there are enough entries in the pool.
73 * A call to the garbage collector is performed at most once in case malloc()
74 * returns an error, before returning NULL.
Willy Tarreau50e608d2007-05-13 18:26:08 +020075 */
Christopher Fauletb349e482017-08-29 09:52:38 +020076void *__pool_refill_alloc(struct pool_head *pool, unsigned int avail);
Willy Tarreaua885f6d2014-12-03 15:25:28 +010077void *pool_refill_alloc(struct pool_head *pool, unsigned int avail);
Willy Tarreau50e608d2007-05-13 18:26:08 +020078
79/* Try to find an existing shared pool with the same characteristics and
80 * returns it, otherwise creates this one. NULL is returned if no memory
81 * is available for a new creation.
82 */
83struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags);
84
85/* Dump statistics on pools usage.
86 */
Willy Tarreau12833bb2014-01-28 16:49:56 +010087void dump_pools_to_trash();
Willy Tarreau50e608d2007-05-13 18:26:08 +020088void dump_pools(void);
Willy Tarreau58102cf2015-10-28 16:24:21 +010089int pool_total_failures();
90unsigned long pool_total_allocated();
91unsigned long pool_total_used();
Willy Tarreau50e608d2007-05-13 18:26:08 +020092
93/*
Willy Tarreaue6ce59d2007-05-13 19:38:49 +020094 * This function frees whatever can be freed in pool <pool>.
95 */
96void pool_flush2(struct pool_head *pool);
97
98/*
99 * This function frees whatever can be freed in all pools, but respecting
100 * the minimum thresholds imposed by owners.
Christopher Fauletb349e482017-08-29 09:52:38 +0200101 *
102 * <pool_ctx> is used when pool_gc2 is called to release resources to allocate
103 * an element in __pool_refill_alloc. It is important because <pool_ctx> is
104 * already locked, so we need to skip the lock here.
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200105 */
Christopher Fauletb349e482017-08-29 09:52:38 +0200106void pool_gc2(struct pool_head *pool_ctx);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200107
108/*
109 * This function destroys a pull by freeing it completely.
110 * This should be called only under extreme circumstances.
111 */
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200112void *pool_destroy2(struct pool_head *pool);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200113
114/*
Willy Tarreau02622412014-12-08 16:35:23 +0100115 * Returns a pointer to type <type> taken from the pool <pool_type> if
116 * available, otherwise returns NULL. No malloc() is attempted, and poisonning
117 * is never performed. The purpose is to get the fastest possible allocation.
Willy Tarreau50e608d2007-05-13 18:26:08 +0200118 */
Christopher Fauletb349e482017-08-29 09:52:38 +0200119static inline void *__pool_get_first(struct pool_head *pool)
Willy Tarreaue430e772014-12-23 14:13:16 +0100120{
121 void *p;
122
Willy Tarreau02622412014-12-08 16:35:23 +0100123 if ((p = pool->free_list) != NULL) {
Willy Tarreauac421112015-10-28 15:09:29 +0100124 pool->free_list = *POOL_LINK(pool, p);
Willy Tarreaue430e772014-12-23 14:13:16 +0100125 pool->used++;
Willy Tarreaude30a682015-10-28 15:23:51 +0100126#ifdef DEBUG_MEMORY_POOLS
127 /* keep track of where the element was allocated from */
128 *POOL_LINK(pool, p) = (void *)pool;
129#endif
Willy Tarreaue430e772014-12-23 14:13:16 +0100130 }
131 return p;
132}
Willy Tarreau50e608d2007-05-13 18:26:08 +0200133
Christopher Fauletb349e482017-08-29 09:52:38 +0200134static inline void *pool_get_first(struct pool_head *pool)
135{
136 void *ret;
137
138 SPIN_LOCK(POOL_LOCK, &pool->lock);
139 ret = __pool_get_first(pool);
140 SPIN_UNLOCK(POOL_LOCK, &pool->lock);
141 return ret;
142}
Willy Tarreau50e608d2007-05-13 18:26:08 +0200143/*
Willy Tarreau02622412014-12-08 16:35:23 +0100144 * Returns a pointer to type <type> taken from the pool <pool_type> or
145 * dynamically allocated. In the first case, <pool_type> is updated to point to
146 * the next element in the list. No memory poisonning is ever performed on the
147 * returned area.
148 */
149static inline void *pool_alloc_dirty(struct pool_head *pool)
150{
151 void *p;
152
Christopher Fauletb349e482017-08-29 09:52:38 +0200153 SPIN_LOCK(POOL_LOCK, &pool->lock);
154 if ((p = __pool_get_first(pool)) == NULL)
155 p = __pool_refill_alloc(pool, 0);
156 SPIN_UNLOCK(POOL_LOCK, &pool->lock);
Willy Tarreau02622412014-12-08 16:35:23 +0100157 return p;
158}
159
160/*
161 * Returns a pointer to type <type> taken from the pool <pool_type> or
162 * dynamically allocated. In the first case, <pool_type> is updated to point to
163 * the next element in the list. Memory poisonning is performed if enabled.
164 */
165static inline void *pool_alloc2(struct pool_head *pool)
166{
167 void *p;
168
169 p = pool_alloc_dirty(pool);
Willy Tarreaude30a682015-10-28 15:23:51 +0100170#ifdef DEBUG_MEMORY_POOLS
171 if (p) {
Christopher Fauletb349e482017-08-29 09:52:38 +0200172 SPIN_LOCK(POOL_LOCK, &pool->lock);
Willy Tarreaude30a682015-10-28 15:23:51 +0100173 /* keep track of where the element was allocated from */
174 *POOL_LINK(pool, p) = (void *)pool;
Christopher Fauletb349e482017-08-29 09:52:38 +0200175 SPIN_UNLOCK(POOL_LOCK, &pool->lock);
Willy Tarreaude30a682015-10-28 15:23:51 +0100176 }
177#endif
178 if (p && mem_poison_byte >= 0) {
Willy Tarreau02622412014-12-08 16:35:23 +0100179 memset(p, mem_poison_byte, pool->size);
Willy Tarreaude30a682015-10-28 15:23:51 +0100180 }
181
Willy Tarreau02622412014-12-08 16:35:23 +0100182 return p;
183}
184
185/*
Willy Tarreau50e608d2007-05-13 18:26:08 +0200186 * Puts a memory area back to the corresponding pool.
187 * Items are chained directly through a pointer that
188 * is written in the beginning of the memory area, so
189 * there's no need for any carrier cell. This implies
190 * that each memory area is at least as big as one
Willy Tarreau48d63db2008-08-03 17:41:33 +0200191 * pointer. Just like with the libc's free(), nothing
192 * is done if <ptr> is NULL.
Willy Tarreau50e608d2007-05-13 18:26:08 +0200193 */
Willy Tarreaue430e772014-12-23 14:13:16 +0100194static inline void pool_free2(struct pool_head *pool, void *ptr)
195{
196 if (likely(ptr != NULL)) {
Christopher Fauletb349e482017-08-29 09:52:38 +0200197 SPIN_LOCK(POOL_LOCK, &pool->lock);
Willy Tarreaude30a682015-10-28 15:23:51 +0100198#ifdef DEBUG_MEMORY_POOLS
199 /* we'll get late corruption if we refill to the wrong pool or double-free */
200 if (*POOL_LINK(pool, ptr) != (void *)pool)
201 *(int *)0 = 0;
202#endif
Willy Tarreauac421112015-10-28 15:09:29 +0100203 *POOL_LINK(pool, ptr) = (void *)pool->free_list;
Willy Tarreaue430e772014-12-23 14:13:16 +0100204 pool->free_list = (void *)ptr;
205 pool->used--;
Christopher Fauletb349e482017-08-29 09:52:38 +0200206 SPIN_UNLOCK(POOL_LOCK, &pool->lock);
Willy Tarreaue430e772014-12-23 14:13:16 +0100207 }
208}
Willy Tarreau2dd0d472006-06-29 17:53:05 +0200209#endif /* _COMMON_MEMORY_H */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200210
211/*
212 * Local variables:
213 * c-indent-level: 8
214 * c-basic-offset: 8
215 * End:
216 */