blob: 45ac7f962a81ad04c8b9681ea47de37f5f6da843 [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 Tarreau50e608d2007-05-13 18:26:08 +02005 Copyright (C) 2000-2007 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 { \
42 __pool = *(void **)(__pool); \
43 } \
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({ \
90 *(void **)ptr = (void *)pool_##type; \
91 pool_##type = (void *)ptr; \
92})
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_* */
128 char name[9]; /* name of the pool */
129};
130
131
132/* Allocate a new entry for pool <pool>, and return it for immediate use.
133 * NULL is returned if no memory is available for a new creation.
134 */
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200135void *pool_refill_alloc(struct pool_head *pool);
Willy Tarreau50e608d2007-05-13 18:26:08 +0200136
137/* Try to find an existing shared pool with the same characteristics and
138 * returns it, otherwise creates this one. NULL is returned if no memory
139 * is available for a new creation.
140 */
141struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags);
142
143/* Dump statistics on pools usage.
144 */
145void dump_pools(void);
146
147/*
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200148 * This function frees whatever can be freed in pool <pool>.
149 */
150void pool_flush2(struct pool_head *pool);
151
152/*
153 * This function frees whatever can be freed in all pools, but respecting
154 * the minimum thresholds imposed by owners.
155 */
156void pool_gc2();
157
158/*
159 * This function destroys a pull by freeing it completely.
160 * This should be called only under extreme circumstances.
161 */
162void pool_destroy2(struct pool_head *pool);
163
164/*
Willy Tarreau50e608d2007-05-13 18:26:08 +0200165 * Returns a pointer to type <type> taken from the
166 * pool <pool_type> or dynamically allocated. In the
167 * first case, <pool_type> is updated to point to the
168 * next element in the list.
169 */
170#define pool_alloc2(pool) \
171({ \
172 void *__p; \
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200173 if ((__p = pool->free_list) == NULL) \
174 __p = pool_refill_alloc(pool); \
Willy Tarreau50e608d2007-05-13 18:26:08 +0200175 else { \
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200176 pool->free_list = *(void **)pool->free_list; \
177 pool->used++; \
Willy Tarreau50e608d2007-05-13 18:26:08 +0200178 } \
179 __p; \
180})
181
182/*
183 * Puts a memory area back to the corresponding pool.
184 * Items are chained directly through a pointer that
185 * is written in the beginning of the memory area, so
186 * there's no need for any carrier cell. This implies
187 * that each memory area is at least as big as one
188 * pointer.
189 */
190#define pool_free2(pool, ptr) \
191({ \
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200192 *(void **)ptr = (void *)pool->free_list; \
193 pool->free_list = (void *)ptr; \
194 pool->used--; \
Willy Tarreau50e608d2007-05-13 18:26:08 +0200195})
196
197
Willy Tarreau2dd0d472006-06-29 17:53:05 +0200198#endif /* _COMMON_MEMORY_H */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200199
200/*
201 * Local variables:
202 * c-indent-level: 8
203 * c-basic-offset: 8
204 * End:
205 */