blob: ae483ec73c5bd6819bd9d095e4c208ace23af787 [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
132
133/* Allocate a new entry for pool <pool>, and return it for immediate use.
134 * NULL is returned if no memory is available for a new creation.
135 */
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200136void *pool_refill_alloc(struct pool_head *pool);
Willy Tarreau50e608d2007-05-13 18:26:08 +0200137
138/* Try to find an existing shared pool with the same characteristics and
139 * returns it, otherwise creates this one. NULL is returned if no memory
140 * is available for a new creation.
141 */
142struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags);
143
144/* Dump statistics on pools usage.
145 */
146void dump_pools(void);
147
148/*
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200149 * This function frees whatever can be freed in pool <pool>.
150 */
151void pool_flush2(struct pool_head *pool);
152
153/*
154 * This function frees whatever can be freed in all pools, but respecting
155 * the minimum thresholds imposed by owners.
156 */
157void pool_gc2();
158
159/*
160 * This function destroys a pull by freeing it completely.
161 * This should be called only under extreme circumstances.
162 */
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200163void *pool_destroy2(struct pool_head *pool);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200164
165/*
Willy Tarreau50e608d2007-05-13 18:26:08 +0200166 * Returns a pointer to type <type> taken from the
167 * pool <pool_type> or dynamically allocated. In the
168 * first case, <pool_type> is updated to point to the
169 * next element in the list.
170 */
171#define pool_alloc2(pool) \
172({ \
173 void *__p; \
Willy Tarreauf8f33282010-06-06 12:07:32 +0200174 if ((__p = (pool)->free_list) == NULL) \
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200175 __p = pool_refill_alloc(pool); \
Willy Tarreau50e608d2007-05-13 18:26:08 +0200176 else { \
Willy Tarreauf8f33282010-06-06 12:07:32 +0200177 (pool)->free_list = *(void **)(pool)->free_list;\
178 (pool)->used++; \
Willy Tarreau50e608d2007-05-13 18:26:08 +0200179 } \
180 __p; \
181})
182
183/*
184 * 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 */
192#define pool_free2(pool, ptr) \
193({ \
Willy Tarreau48d63db2008-08-03 17:41:33 +0200194 if (likely((ptr) != NULL)) { \
Willy Tarreauf8f33282010-06-06 12:07:32 +0200195 *(void **)(ptr) = (void *)(pool)->free_list; \
196 (pool)->free_list = (void *)(ptr); \
197 (pool)->used--; \
Willy Tarreau48d63db2008-08-03 17:41:33 +0200198 } \
Willy Tarreau50e608d2007-05-13 18:26:08 +0200199})
200
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 */