blob: 820c1d98e18bbb66f2889771e53e7ccb5af7c16f [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
5 Copyright (C) 2000-2006 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*/
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 Tarreaubaaee002006-06-26 02:48:02 +020028
29#define sizeof_requri REQURI_LEN
30#define sizeof_capture CAPTURE_LEN
31/*
32 * Returns a pointer to an area of <__len> bytes taken from the pool <pool> or
33 * dynamically allocated. In the first case, <__pool> is updated to point to
34 * the next element in the list.
35 */
36#define pool_alloc_from(__pool, __len) \
37({ \
38 void *__p; \
39 if ((__p = (__pool)) == NULL) \
40 __p = malloc(((__len) >= sizeof (void *)) ? \
41 (__len) : sizeof(void *)); \
42 else { \
43 __pool = *(void **)(__pool); \
44 } \
45 __p; \
46})
47
48/*
49 * Puts a memory area back to the corresponding pool.
50 * Items are chained directly through a pointer that
51 * is written in the beginning of the memory area, so
52 * there's no need for any carrier cell. This implies
53 * that each memory area is at least as big as one
54 * pointer.
55 */
56#define pool_free_to(__pool, __ptr) \
57({ \
58 *(void **)(__ptr) = (void *)(__pool); \
59 __pool = (void *)(__ptr); \
60})
61
62
63#ifdef CONFIG_HAP_MEM_OPTIM
64/*
65 * Returns a pointer to type <type> taken from the
66 * pool <pool_type> or dynamically allocated. In the
67 * first case, <pool_type> is updated to point to the
68 * next element in the list.
69 */
70#define pool_alloc(type) \
71({ \
72 void *__p; \
73 if ((__p = pool_##type) == NULL) \
74 __p = malloc(sizeof_##type); \
75 else { \
76 pool_##type = *(void **)pool_##type; \
77 } \
78 __p; \
79})
80
81/*
82 * Puts a memory area back to the corresponding pool.
83 * Items are chained directly through a pointer that
84 * is written in the beginning of the memory area, so
85 * there's no need for any carrier cell. This implies
86 * that each memory area is at least as big as one
87 * pointer.
88 */
89#define pool_free(type, ptr) \
90({ \
91 *(void **)ptr = (void *)pool_##type; \
92 pool_##type = (void *)ptr; \
93})
94
95#else
96#define pool_alloc(type) (calloc(1,sizeof_##type))
97#define pool_free(type, ptr) (free(ptr))
98#endif /* CONFIG_HAP_MEM_OPTIM */
99
100/*
101 * This function destroys a pull by freeing it completely.
102 * This should be called only under extreme circumstances.
103 */
104static inline void pool_destroy(void **pool)
105{
106 void *temp, *next;
107 next = pool;
108 while (next) {
109 temp = next;
110 next = *(void **)temp;
111 free(temp);
112 }
113}
114
Willy Tarreau2dd0d472006-06-29 17:53:05 +0200115#endif /* _COMMON_MEMORY_H */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200116
117/*
118 * Local variables:
119 * c-indent-level: 8
120 * c-basic-offset: 8
121 * End:
122 */