blob: 2b7121b420b5830c87fb11ce4439bce5957ad872 [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>
Willy Tarreaubaaee002006-06-26 02:48:02 +020030
Willy Tarreaua84dcb82015-10-28 12:04:02 +010031#ifndef DEBUG_DONT_SHARE_POOLS
Willy Tarreau50e608d2007-05-13 18:26:08 +020032#define MEM_F_SHARED 0x1
Willy Tarreaua84dcb82015-10-28 12:04:02 +010033#else
34#define MEM_F_SHARED 0
35#endif
Willy Tarreau50e608d2007-05-13 18:26:08 +020036
Willy Tarreauac421112015-10-28 15:09:29 +010037/* reserve an extra void* at the end of a pool for linking */
38#ifdef DEBUG_MEMORY_POOLS
39#define POOL_EXTRA (sizeof(void *))
40#define POOL_LINK(pool, item) (void **)(((char *)item) + (pool->size))
41#else
42#define POOL_EXTRA (0)
43#define POOL_LINK(pool, item) ((void **)(item))
44#endif
45
Willy Tarreau50e608d2007-05-13 18:26:08 +020046struct pool_head {
47 void **free_list;
48 struct list list; /* list of all known pools */
49 unsigned int used; /* how many chunks are currently in use */
50 unsigned int allocated; /* how many chunks have been allocated */
51 unsigned int limit; /* hard limit on the number of chunks */
52 unsigned int minavail; /* how many chunks are expected to be used */
53 unsigned int size; /* chunk size */
54 unsigned int flags; /* MEM_F_* */
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020055 unsigned int users; /* number of pools sharing this zone */
Willy Tarreau58102cf2015-10-28 16:24:21 +010056 unsigned int failed; /* failed allocations */
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020057 char name[12]; /* name of the pool */
Willy Tarreau50e608d2007-05-13 18:26:08 +020058};
59
Willy Tarreau067ac9f2015-10-08 14:12:13 +020060/* poison each newly allocated area with this byte if >= 0 */
61extern int mem_poison_byte;
Willy Tarreau50e608d2007-05-13 18:26:08 +020062
Willy Tarreau62405a22014-12-23 13:51:28 +010063/*
64 * This function destroys a pull by freeing it completely.
65 * This should be called only under extreme circumstances.
66 */
67static inline void pool_destroy(void **pool)
68{
69 void *temp, *next;
70 next = pool;
71 while (next) {
72 temp = next;
73 next = *(void **)temp;
74 free(temp);
75 }
76}
77
Willy Tarreaua885f6d2014-12-03 15:25:28 +010078/* Allocates new entries for pool <pool> until there are at least <avail> + 1
79 * available, then returns the last one for immediate use, so that at least
80 * <avail> are left available in the pool upon return. NULL is returned if the
81 * last entry could not be allocated. It's important to note that at least one
82 * allocation is always performed even if there are enough entries in the pool.
83 * A call to the garbage collector is performed at most once in case malloc()
84 * returns an error, before returning NULL.
Willy Tarreau50e608d2007-05-13 18:26:08 +020085 */
Willy Tarreaua885f6d2014-12-03 15:25:28 +010086void *pool_refill_alloc(struct pool_head *pool, unsigned int avail);
Willy Tarreau50e608d2007-05-13 18:26:08 +020087
88/* Try to find an existing shared pool with the same characteristics and
89 * returns it, otherwise creates this one. NULL is returned if no memory
90 * is available for a new creation.
91 */
92struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags);
93
94/* Dump statistics on pools usage.
95 */
Willy Tarreau12833bb2014-01-28 16:49:56 +010096void dump_pools_to_trash();
Willy Tarreau50e608d2007-05-13 18:26:08 +020097void dump_pools(void);
Willy Tarreau58102cf2015-10-28 16:24:21 +010098int pool_total_failures();
99unsigned long pool_total_allocated();
100unsigned long pool_total_used();
Willy Tarreau50e608d2007-05-13 18:26:08 +0200101
102/*
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200103 * This function frees whatever can be freed in pool <pool>.
104 */
105void pool_flush2(struct pool_head *pool);
106
107/*
108 * This function frees whatever can be freed in all pools, but respecting
109 * the minimum thresholds imposed by owners.
110 */
111void pool_gc2();
112
113/*
114 * This function destroys a pull by freeing it completely.
115 * This should be called only under extreme circumstances.
116 */
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200117void *pool_destroy2(struct pool_head *pool);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200118
119/*
Willy Tarreau02622412014-12-08 16:35:23 +0100120 * Returns a pointer to type <type> taken from the pool <pool_type> if
121 * available, otherwise returns NULL. No malloc() is attempted, and poisonning
122 * is never performed. The purpose is to get the fastest possible allocation.
Willy Tarreau50e608d2007-05-13 18:26:08 +0200123 */
Willy Tarreau02622412014-12-08 16:35:23 +0100124static inline void *pool_get_first(struct pool_head *pool)
Willy Tarreaue430e772014-12-23 14:13:16 +0100125{
126 void *p;
127
Willy Tarreau02622412014-12-08 16:35:23 +0100128 if ((p = pool->free_list) != NULL) {
Willy Tarreauac421112015-10-28 15:09:29 +0100129 pool->free_list = *POOL_LINK(pool, p);
Willy Tarreaue430e772014-12-23 14:13:16 +0100130 pool->used++;
Willy Tarreaude30a682015-10-28 15:23:51 +0100131#ifdef DEBUG_MEMORY_POOLS
132 /* keep track of where the element was allocated from */
133 *POOL_LINK(pool, p) = (void *)pool;
134#endif
Willy Tarreaue430e772014-12-23 14:13:16 +0100135 }
136 return p;
137}
Willy Tarreau50e608d2007-05-13 18:26:08 +0200138
139/*
Willy Tarreau02622412014-12-08 16:35:23 +0100140 * Returns a pointer to type <type> taken from the pool <pool_type> or
141 * dynamically allocated. In the first case, <pool_type> is updated to point to
142 * the next element in the list. No memory poisonning is ever performed on the
143 * returned area.
144 */
145static inline void *pool_alloc_dirty(struct pool_head *pool)
146{
147 void *p;
148
149 if ((p = pool_get_first(pool)) == NULL)
Willy Tarreaua885f6d2014-12-03 15:25:28 +0100150 p = pool_refill_alloc(pool, 0);
Willy Tarreau02622412014-12-08 16:35:23 +0100151
152 return p;
153}
154
155/*
156 * Returns a pointer to type <type> taken from the pool <pool_type> or
157 * dynamically allocated. In the first case, <pool_type> is updated to point to
158 * the next element in the list. Memory poisonning is performed if enabled.
159 */
160static inline void *pool_alloc2(struct pool_head *pool)
161{
162 void *p;
163
164 p = pool_alloc_dirty(pool);
Willy Tarreaude30a682015-10-28 15:23:51 +0100165#ifdef DEBUG_MEMORY_POOLS
166 if (p) {
167 /* keep track of where the element was allocated from */
168 *POOL_LINK(pool, p) = (void *)pool;
169 }
170#endif
171 if (p && mem_poison_byte >= 0) {
Willy Tarreau02622412014-12-08 16:35:23 +0100172 memset(p, mem_poison_byte, pool->size);
Willy Tarreaude30a682015-10-28 15:23:51 +0100173 }
174
Willy Tarreau02622412014-12-08 16:35:23 +0100175 return p;
176}
177
178/*
Willy Tarreau50e608d2007-05-13 18:26:08 +0200179 * Puts a memory area back to the corresponding pool.
180 * Items are chained directly through a pointer that
181 * is written in the beginning of the memory area, so
182 * there's no need for any carrier cell. This implies
183 * that each memory area is at least as big as one
Willy Tarreau48d63db2008-08-03 17:41:33 +0200184 * pointer. Just like with the libc's free(), nothing
185 * is done if <ptr> is NULL.
Willy Tarreau50e608d2007-05-13 18:26:08 +0200186 */
Willy Tarreaue430e772014-12-23 14:13:16 +0100187static inline void pool_free2(struct pool_head *pool, void *ptr)
188{
189 if (likely(ptr != NULL)) {
Willy Tarreaude30a682015-10-28 15:23:51 +0100190#ifdef DEBUG_MEMORY_POOLS
191 /* we'll get late corruption if we refill to the wrong pool or double-free */
192 if (*POOL_LINK(pool, ptr) != (void *)pool)
193 *(int *)0 = 0;
194#endif
Willy Tarreauac421112015-10-28 15:09:29 +0100195 *POOL_LINK(pool, ptr) = (void *)pool->free_list;
Willy Tarreaue430e772014-12-23 14:13:16 +0100196 pool->free_list = (void *)ptr;
197 pool->used--;
198 }
199}
Willy Tarreau50e608d2007-05-13 18:26:08 +0200200
201
Willy Tarreau2dd0d472006-06-29 17:53:05 +0200202#endif /* _COMMON_MEMORY_H */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200203
204/*
205 * Local variables:
206 * c-indent-level: 8
207 * c-basic-offset: 8
208 * End:
209 */