blob: a5c0ec49ef794a7f9c43de70426babce738c1c03 [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 Faulet9dcf9b62017-11-13 10:34:01 +010050 __decl_hathreads(HA_SPINLOCK_T lock); /* the spin lock */
Willy Tarreau50e608d2007-05-13 18:26:08 +020051 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 Tarreau7dcd46d2007-05-14 00:16:13 +020058 unsigned int users; /* number of pools sharing this zone */
Willy Tarreau58102cf2015-10-28 16:24:21 +010059 unsigned int failed; /* failed allocations */
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020060 char name[12]; /* name of the pool */
Willy Tarreau50e608d2007-05-13 18:26:08 +020061};
62
Willy Tarreau067ac9f2015-10-08 14:12:13 +020063/* poison each newly allocated area with this byte if >= 0 */
64extern int mem_poison_byte;
Willy Tarreau50e608d2007-05-13 18:26:08 +020065
Willy Tarreaua885f6d2014-12-03 15:25:28 +010066/* 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 Tarreau50e608d2007-05-13 18:26:08 +020073 */
Christopher Fauletb349e482017-08-29 09:52:38 +020074void *__pool_refill_alloc(struct pool_head *pool, unsigned int avail);
Willy Tarreaua885f6d2014-12-03 15:25:28 +010075void *pool_refill_alloc(struct pool_head *pool, unsigned int avail);
Willy Tarreau50e608d2007-05-13 18:26:08 +020076
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 */
81struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags);
82
83/* Dump statistics on pools usage.
84 */
Willy Tarreau12833bb2014-01-28 16:49:56 +010085void dump_pools_to_trash();
Willy Tarreau50e608d2007-05-13 18:26:08 +020086void dump_pools(void);
Willy Tarreau58102cf2015-10-28 16:24:21 +010087int pool_total_failures();
88unsigned long pool_total_allocated();
89unsigned long pool_total_used();
Willy Tarreau50e608d2007-05-13 18:26:08 +020090
91/*
Willy Tarreaue6ce59d2007-05-13 19:38:49 +020092 * This function frees whatever can be freed in pool <pool>.
93 */
94void 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 Fauletb349e482017-08-29 09:52:38 +020099 *
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 Tarreaue6ce59d2007-05-13 19:38:49 +0200103 */
Christopher Fauletb349e482017-08-29 09:52:38 +0200104void pool_gc2(struct pool_head *pool_ctx);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200105
106/*
107 * This function destroys a pull by freeing it completely.
108 * This should be called only under extreme circumstances.
109 */
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200110void *pool_destroy2(struct pool_head *pool);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200111
112/*
Willy Tarreau02622412014-12-08 16:35:23 +0100113 * 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 Tarreau50e608d2007-05-13 18:26:08 +0200116 */
Christopher Fauletb349e482017-08-29 09:52:38 +0200117static inline void *__pool_get_first(struct pool_head *pool)
Willy Tarreaue430e772014-12-23 14:13:16 +0100118{
119 void *p;
120
Willy Tarreau02622412014-12-08 16:35:23 +0100121 if ((p = pool->free_list) != NULL) {
Willy Tarreauac421112015-10-28 15:09:29 +0100122 pool->free_list = *POOL_LINK(pool, p);
Willy Tarreaue430e772014-12-23 14:13:16 +0100123 pool->used++;
Willy Tarreaude30a682015-10-28 15:23:51 +0100124#ifdef DEBUG_MEMORY_POOLS
125 /* keep track of where the element was allocated from */
126 *POOL_LINK(pool, p) = (void *)pool;
127#endif
Willy Tarreaue430e772014-12-23 14:13:16 +0100128 }
129 return p;
130}
Willy Tarreau50e608d2007-05-13 18:26:08 +0200131
Christopher Fauletb349e482017-08-29 09:52:38 +0200132static inline void *pool_get_first(struct pool_head *pool)
133{
134 void *ret;
135
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100136 HA_SPIN_LOCK(POOL_LOCK, &pool->lock);
Christopher Fauletb349e482017-08-29 09:52:38 +0200137 ret = __pool_get_first(pool);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100138 HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock);
Christopher Fauletb349e482017-08-29 09:52:38 +0200139 return ret;
140}
Willy Tarreau50e608d2007-05-13 18:26:08 +0200141/*
Willy Tarreau02622412014-12-08 16:35:23 +0100142 * 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 */
147static inline void *pool_alloc_dirty(struct pool_head *pool)
148{
149 void *p;
150
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100151 HA_SPIN_LOCK(POOL_LOCK, &pool->lock);
Christopher Fauletb349e482017-08-29 09:52:38 +0200152 if ((p = __pool_get_first(pool)) == NULL)
153 p = __pool_refill_alloc(pool, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100154 HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock);
Willy Tarreau02622412014-12-08 16:35:23 +0100155 return p;
156}
157
158/*
159 * Returns a pointer to type <type> taken from the pool <pool_type> or
160 * dynamically allocated. In the first case, <pool_type> is updated to point to
161 * the next element in the list. Memory poisonning is performed if enabled.
162 */
163static inline void *pool_alloc2(struct pool_head *pool)
164{
165 void *p;
166
167 p = pool_alloc_dirty(pool);
Willy Tarreaude30a682015-10-28 15:23:51 +0100168#ifdef DEBUG_MEMORY_POOLS
169 if (p) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100170 HA_SPIN_LOCK(POOL_LOCK, &pool->lock);
Willy Tarreaude30a682015-10-28 15:23:51 +0100171 /* keep track of where the element was allocated from */
172 *POOL_LINK(pool, p) = (void *)pool;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100173 HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock);
Willy Tarreaude30a682015-10-28 15:23:51 +0100174 }
175#endif
176 if (p && mem_poison_byte >= 0) {
Willy Tarreau02622412014-12-08 16:35:23 +0100177 memset(p, mem_poison_byte, pool->size);
Willy Tarreaude30a682015-10-28 15:23:51 +0100178 }
179
Willy Tarreau02622412014-12-08 16:35:23 +0100180 return p;
181}
182
183/*
Willy Tarreau50e608d2007-05-13 18:26:08 +0200184 * 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
Willy Tarreau48d63db2008-08-03 17:41:33 +0200189 * pointer. Just like with the libc's free(), nothing
190 * is done if <ptr> is NULL.
Willy Tarreau50e608d2007-05-13 18:26:08 +0200191 */
Willy Tarreaue430e772014-12-23 14:13:16 +0100192static inline void pool_free2(struct pool_head *pool, void *ptr)
193{
194 if (likely(ptr != NULL)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100195 HA_SPIN_LOCK(POOL_LOCK, &pool->lock);
Willy Tarreaude30a682015-10-28 15:23:51 +0100196#ifdef DEBUG_MEMORY_POOLS
197 /* we'll get late corruption if we refill to the wrong pool or double-free */
198 if (*POOL_LINK(pool, ptr) != (void *)pool)
199 *(int *)0 = 0;
200#endif
Willy Tarreauac421112015-10-28 15:09:29 +0100201 *POOL_LINK(pool, ptr) = (void *)pool->free_list;
Willy Tarreaue430e772014-12-23 14:13:16 +0100202 pool->free_list = (void *)ptr;
203 pool->used--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100204 HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock);
Willy Tarreaue430e772014-12-23 14:13:16 +0100205 }
206}
Willy Tarreau2dd0d472006-06-29 17:53:05 +0200207#endif /* _COMMON_MEMORY_H */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200208
209/*
210 * Local variables:
211 * c-indent-level: 8
212 * c-basic-offset: 8
213 * End:
214 */