2
include/common/memory.h
3
Memory management definitions..
5
Copyright (C) 2000-2007 Willy Tarreau - w@1wt.eu
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
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.
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
22
#ifndef _COMMON_MEMORY_H
23
#define _COMMON_MEMORY_H
27
#include <common/config.h>
28
#include <common/mini-clist.h>
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.
35
#define pool_alloc_from(__pool, __len) \
38
if ((__p = (__pool)) == NULL) \
39
__p = malloc(((__len) >= sizeof (void *)) ? \
40
(__len) : sizeof(void *)); \
42
__pool = *(void **)(__pool); \
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
55
#define pool_free_to(__pool, __ptr) \
57
*(void **)(__ptr) = (void *)(__pool); \
58
__pool = (void *)(__ptr); \
62
#ifdef CONFIG_HAP_MEM_OPTIM
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.
69
#define pool_alloc(type) \
72
if ((__p = pool_##type) == NULL) \
73
__p = malloc(sizeof_##type); \
75
pool_##type = *(void **)pool_##type; \
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
88
#define pool_free(type, ptr) \
90
*(void **)ptr = (void *)pool_##type; \
91
pool_##type = (void *)ptr; \
95
#define pool_alloc(type) (calloc(1,sizeof_##type))
96
#define pool_free(type, ptr) (free(ptr))
97
#endif /* CONFIG_HAP_MEM_OPTIM */
100
* This function destroys a pull by freeing it completely.
101
* This should be called only under extreme circumstances.
103
static inline void pool_destroy(void **pool)
109
next = *(void **)temp;
115
/******* pools version 2 ********/
117
#define MEM_F_SHARED 0x1
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
unsigned int users; /* number of pools sharing this zone */
129
char name[12]; /* name of the pool */
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.
136
void *pool_refill_alloc(struct pool_head *pool);
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.
142
struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags);
144
/* Dump statistics on pools usage.
146
void dump_pools(void);
149
* This function frees whatever can be freed in pool <pool>.
151
void pool_flush2(struct pool_head *pool);
154
* This function frees whatever can be freed in all pools, but respecting
155
* the minimum thresholds imposed by owners.
160
* This function destroys a pull by freeing it completely.
161
* This should be called only under extreme circumstances.
163
void *pool_destroy2(struct pool_head *pool);
166
* 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.
171
#define pool_alloc2(pool) \
174
if ((__p = pool->free_list) == NULL) \
175
__p = pool_refill_alloc(pool); \
177
pool->free_list = *(void **)pool->free_list; \
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
191
#define pool_free2(pool, ptr) \
193
*(void **)ptr = (void *)pool->free_list; \
194
pool->free_list = (void *)ptr; \
199
#endif /* _COMMON_MEMORY_H */