blob: dd2b21f4e8693c486ea664e3dee4c6d82d270592 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreau2dd0d472006-06-29 17:53:05 +02002 include/common/memory.h
Willy Tarreaubaaee002006-06-26 02:48:02 +02003 Memory management definitions..
4
Willy Tarreau48d63db2008-08-03 17:41:33 +02005 Copyright (C) 2000-2008 Willy Tarreau - w@1wt.eu
Willy Tarreaubaaee002006-06-26 02:48:02 +02006
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 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>
26
Willy Tarreau2dd0d472006-06-29 17:53:05 +020027#include <common/config.h>
Willy Tarreau50e608d2007-05-13 18:26:08 +020028#include <common/mini-clist.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020029
Willy Tarreaubaaee002006-06-26 02:48:02 +020030/*
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 { \
Willy Tarreauf8f33282010-06-06 12:07:32 +020042 (__pool) = *(void **)(__pool); \
Willy Tarreaubaaee002006-06-26 02:48:02 +020043 } \
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({ \
Willy Tarreauf8f33282010-06-06 12:07:32 +020090 *(void **)(ptr) = (void *)pool_##type; \
91 pool_##type = (void *)(ptr); \
Willy Tarreaubaaee002006-06-26 02:48:02 +020092})
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 */
103static 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 Tarreau50e608d2007-05-13 18:26:08 +0200114
115/******* pools version 2 ********/
116
117#define MEM_F_SHARED 0x1
118
119struct 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 Tarreau7dcd46d2007-05-14 00:16:13 +0200128 unsigned int users; /* number of pools sharing this zone */
129 char name[12]; /* name of the pool */
Willy Tarreau50e608d2007-05-13 18:26:08 +0200130};
131
Willy Tarreau6e064432012-05-08 15:40:42 +0200132/* poison each newly allocated area with this byte if not null */
133extern char mem_poison_byte;
Willy Tarreau50e608d2007-05-13 18:26:08 +0200134
135/* Allocate a new entry for pool <pool>, and return it for immediate use.
136 * NULL is returned if no memory is available for a new creation.
137 */
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200138void *pool_refill_alloc(struct pool_head *pool);
Willy Tarreau50e608d2007-05-13 18:26:08 +0200139
140/* Try to find an existing shared pool with the same characteristics and
141 * returns it, otherwise creates this one. NULL is returned if no memory
142 * is available for a new creation.
143 */
144struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags);
145
146/* Dump statistics on pools usage.
147 */
Willy Tarreau12833bb2014-01-28 16:49:56 +0100148void dump_pools_to_trash();
Willy Tarreau50e608d2007-05-13 18:26:08 +0200149void dump_pools(void);
150
151/*
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200152 * This function frees whatever can be freed in pool <pool>.
153 */
154void pool_flush2(struct pool_head *pool);
155
156/*
157 * This function frees whatever can be freed in all pools, but respecting
158 * the minimum thresholds imposed by owners.
159 */
160void pool_gc2();
161
162/*
163 * This function destroys a pull by freeing it completely.
164 * This should be called only under extreme circumstances.
165 */
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200166void *pool_destroy2(struct pool_head *pool);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200167
168/*
Willy Tarreau50e608d2007-05-13 18:26:08 +0200169 * Returns a pointer to type <type> taken from the
170 * pool <pool_type> or dynamically allocated. In the
171 * first case, <pool_type> is updated to point to the
172 * next element in the list.
173 */
174#define pool_alloc2(pool) \
175({ \
176 void *__p; \
Willy Tarreauf8f33282010-06-06 12:07:32 +0200177 if ((__p = (pool)->free_list) == NULL) \
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200178 __p = pool_refill_alloc(pool); \
Willy Tarreau50e608d2007-05-13 18:26:08 +0200179 else { \
Willy Tarreauf8f33282010-06-06 12:07:32 +0200180 (pool)->free_list = *(void **)(pool)->free_list;\
181 (pool)->used++; \
Willy Tarreau50e608d2007-05-13 18:26:08 +0200182 } \
183 __p; \
184})
185
186/*
187 * Puts a memory area back to the corresponding pool.
188 * Items are chained directly through a pointer that
189 * is written in the beginning of the memory area, so
190 * there's no need for any carrier cell. This implies
191 * that each memory area is at least as big as one
Willy Tarreau48d63db2008-08-03 17:41:33 +0200192 * pointer. Just like with the libc's free(), nothing
193 * is done if <ptr> is NULL.
Willy Tarreau50e608d2007-05-13 18:26:08 +0200194 */
195#define pool_free2(pool, ptr) \
196({ \
Willy Tarreau48d63db2008-08-03 17:41:33 +0200197 if (likely((ptr) != NULL)) { \
Willy Tarreauf8f33282010-06-06 12:07:32 +0200198 *(void **)(ptr) = (void *)(pool)->free_list; \
199 (pool)->free_list = (void *)(ptr); \
200 (pool)->used--; \
Willy Tarreau48d63db2008-08-03 17:41:33 +0200201 } \
Willy Tarreau50e608d2007-05-13 18:26:08 +0200202})
203
204
Willy Tarreau2dd0d472006-06-29 17:53:05 +0200205#endif /* _COMMON_MEMORY_H */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200206
207/*
208 * Local variables:
209 * c-indent-level: 8
210 * c-basic-offset: 8
211 * End:
212 */