blob: 80fa586cb6932458ff63ac90d74a1b3d27765281 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreau62405a22014-12-23 13:51:28 +01002 * include/common/memory.h
3 * Memory management definitions..
4 *
5 * Copyright (C) 2000-2014 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 */
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
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 Tarreau50e608d2007-05-13 18:26:08 +020030#define MEM_F_SHARED 0x1
31
32struct pool_head {
33 void **free_list;
34 struct list list; /* list of all known pools */
35 unsigned int used; /* how many chunks are currently in use */
36 unsigned int allocated; /* how many chunks have been allocated */
37 unsigned int limit; /* hard limit on the number of chunks */
38 unsigned int minavail; /* how many chunks are expected to be used */
39 unsigned int size; /* chunk size */
40 unsigned int flags; /* MEM_F_* */
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020041 unsigned int users; /* number of pools sharing this zone */
42 char name[12]; /* name of the pool */
Willy Tarreau50e608d2007-05-13 18:26:08 +020043};
44
Willy Tarreau6e064432012-05-08 15:40:42 +020045/* poison each newly allocated area with this byte if not null */
46extern char mem_poison_byte;
Willy Tarreau50e608d2007-05-13 18:26:08 +020047
Willy Tarreau62405a22014-12-23 13:51:28 +010048/*
49 * This function destroys a pull by freeing it completely.
50 * This should be called only under extreme circumstances.
51 */
52static inline void pool_destroy(void **pool)
53{
54 void *temp, *next;
55 next = pool;
56 while (next) {
57 temp = next;
58 next = *(void **)temp;
59 free(temp);
60 }
61}
62
Willy Tarreau50e608d2007-05-13 18:26:08 +020063/* Allocate a new entry for pool <pool>, and return it for immediate use.
64 * NULL is returned if no memory is available for a new creation.
65 */
Willy Tarreaue6ce59d2007-05-13 19:38:49 +020066void *pool_refill_alloc(struct pool_head *pool);
Willy Tarreau50e608d2007-05-13 18:26:08 +020067
68/* Try to find an existing shared pool with the same characteristics and
69 * returns it, otherwise creates this one. NULL is returned if no memory
70 * is available for a new creation.
71 */
72struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags);
73
74/* Dump statistics on pools usage.
75 */
Willy Tarreau12833bb2014-01-28 16:49:56 +010076void dump_pools_to_trash();
Willy Tarreau50e608d2007-05-13 18:26:08 +020077void dump_pools(void);
78
79/*
Willy Tarreaue6ce59d2007-05-13 19:38:49 +020080 * This function frees whatever can be freed in pool <pool>.
81 */
82void pool_flush2(struct pool_head *pool);
83
84/*
85 * This function frees whatever can be freed in all pools, but respecting
86 * the minimum thresholds imposed by owners.
87 */
88void pool_gc2();
89
90/*
91 * This function destroys a pull by freeing it completely.
92 * This should be called only under extreme circumstances.
93 */
Willy Tarreau4d2d0982007-05-14 00:39:29 +020094void *pool_destroy2(struct pool_head *pool);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +020095
96/*
Willy Tarreau50e608d2007-05-13 18:26:08 +020097 * Returns a pointer to type <type> taken from the
98 * pool <pool_type> or dynamically allocated. In the
99 * first case, <pool_type> is updated to point to the
100 * next element in the list.
101 */
102#define pool_alloc2(pool) \
103({ \
104 void *__p; \
Willy Tarreauf8f33282010-06-06 12:07:32 +0200105 if ((__p = (pool)->free_list) == NULL) \
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200106 __p = pool_refill_alloc(pool); \
Willy Tarreau50e608d2007-05-13 18:26:08 +0200107 else { \
Willy Tarreauf8f33282010-06-06 12:07:32 +0200108 (pool)->free_list = *(void **)(pool)->free_list;\
109 (pool)->used++; \
Willy Tarreau23a5c392014-11-25 13:45:16 +0100110 if (unlikely(mem_poison_byte)) \
111 memset(__p, mem_poison_byte, (pool)->size); \
Willy Tarreau50e608d2007-05-13 18:26:08 +0200112 } \
113 __p; \
114})
115
116/*
117 * Puts a memory area back to the corresponding pool.
118 * Items are chained directly through a pointer that
119 * is written in the beginning of the memory area, so
120 * there's no need for any carrier cell. This implies
121 * that each memory area is at least as big as one
Willy Tarreau48d63db2008-08-03 17:41:33 +0200122 * pointer. Just like with the libc's free(), nothing
123 * is done if <ptr> is NULL.
Willy Tarreau50e608d2007-05-13 18:26:08 +0200124 */
125#define pool_free2(pool, ptr) \
126({ \
Willy Tarreau48d63db2008-08-03 17:41:33 +0200127 if (likely((ptr) != NULL)) { \
Willy Tarreauf8f33282010-06-06 12:07:32 +0200128 *(void **)(ptr) = (void *)(pool)->free_list; \
129 (pool)->free_list = (void *)(ptr); \
130 (pool)->used--; \
Willy Tarreau48d63db2008-08-03 17:41:33 +0200131 } \
Willy Tarreau50e608d2007-05-13 18:26:08 +0200132})
133
134
Willy Tarreau2dd0d472006-06-29 17:53:05 +0200135#endif /* _COMMON_MEMORY_H */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200136
137/*
138 * Local variables:
139 * c-indent-level: 8
140 * c-basic-offset: 8
141 * End:
142 */