blob: 3a0eaf6955ef212f421a1fa5fe8d3b24dbc3d620 [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>
Willy Tarreaue430e772014-12-23 14:13:16 +010026#include <string.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020027
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/config.h>
Willy Tarreau50e608d2007-05-13 18:26:08 +020029#include <common/mini-clist.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020030
Willy Tarreau50e608d2007-05-13 18:26:08 +020031#define MEM_F_SHARED 0x1
32
33struct pool_head {
34 void **free_list;
35 struct list list; /* list of all known pools */
36 unsigned int used; /* how many chunks are currently in use */
37 unsigned int allocated; /* how many chunks have been allocated */
38 unsigned int limit; /* hard limit on the number of chunks */
39 unsigned int minavail; /* how many chunks are expected to be used */
40 unsigned int size; /* chunk size */
41 unsigned int flags; /* MEM_F_* */
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020042 unsigned int users; /* number of pools sharing this zone */
43 char name[12]; /* name of the pool */
Willy Tarreau50e608d2007-05-13 18:26:08 +020044};
45
Willy Tarreau6e064432012-05-08 15:40:42 +020046/* poison each newly allocated area with this byte if not null */
47extern char mem_poison_byte;
Willy Tarreau50e608d2007-05-13 18:26:08 +020048
Willy Tarreau62405a22014-12-23 13:51:28 +010049/*
50 * This function destroys a pull by freeing it completely.
51 * This should be called only under extreme circumstances.
52 */
53static inline void pool_destroy(void **pool)
54{
55 void *temp, *next;
56 next = pool;
57 while (next) {
58 temp = next;
59 next = *(void **)temp;
60 free(temp);
61 }
62}
63
Willy Tarreau50e608d2007-05-13 18:26:08 +020064/* Allocate a new entry for pool <pool>, and return it for immediate use.
65 * NULL is returned if no memory is available for a new creation.
66 */
Willy Tarreaue6ce59d2007-05-13 19:38:49 +020067void *pool_refill_alloc(struct pool_head *pool);
Willy Tarreau50e608d2007-05-13 18:26:08 +020068
69/* Try to find an existing shared pool with the same characteristics and
70 * returns it, otherwise creates this one. NULL is returned if no memory
71 * is available for a new creation.
72 */
73struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags);
74
75/* Dump statistics on pools usage.
76 */
Willy Tarreau12833bb2014-01-28 16:49:56 +010077void dump_pools_to_trash();
Willy Tarreau50e608d2007-05-13 18:26:08 +020078void dump_pools(void);
79
80/*
Willy Tarreaue6ce59d2007-05-13 19:38:49 +020081 * This function frees whatever can be freed in pool <pool>.
82 */
83void pool_flush2(struct pool_head *pool);
84
85/*
86 * This function frees whatever can be freed in all pools, but respecting
87 * the minimum thresholds imposed by owners.
88 */
89void pool_gc2();
90
91/*
92 * This function destroys a pull by freeing it completely.
93 * This should be called only under extreme circumstances.
94 */
Willy Tarreau4d2d0982007-05-14 00:39:29 +020095void *pool_destroy2(struct pool_head *pool);
Willy Tarreaue6ce59d2007-05-13 19:38:49 +020096
97/*
Willy Tarreau02622412014-12-08 16:35:23 +010098 * Returns a pointer to type <type> taken from the pool <pool_type> if
99 * available, otherwise returns NULL. No malloc() is attempted, and poisonning
100 * is never performed. The purpose is to get the fastest possible allocation.
Willy Tarreau50e608d2007-05-13 18:26:08 +0200101 */
Willy Tarreau02622412014-12-08 16:35:23 +0100102static inline void *pool_get_first(struct pool_head *pool)
Willy Tarreaue430e772014-12-23 14:13:16 +0100103{
104 void *p;
105
Willy Tarreau02622412014-12-08 16:35:23 +0100106 if ((p = pool->free_list) != NULL) {
Willy Tarreaue430e772014-12-23 14:13:16 +0100107 pool->free_list = *(void **)pool->free_list;
108 pool->used++;
Willy Tarreaue430e772014-12-23 14:13:16 +0100109 }
110 return p;
111}
Willy Tarreau50e608d2007-05-13 18:26:08 +0200112
113/*
Willy Tarreau02622412014-12-08 16:35:23 +0100114 * Returns a pointer to type <type> taken from the pool <pool_type> or
115 * dynamically allocated. In the first case, <pool_type> is updated to point to
116 * the next element in the list. No memory poisonning is ever performed on the
117 * returned area.
118 */
119static inline void *pool_alloc_dirty(struct pool_head *pool)
120{
121 void *p;
122
123 if ((p = pool_get_first(pool)) == NULL)
124 p = pool_refill_alloc(pool);
125
126 return p;
127}
128
129/*
130 * Returns a pointer to type <type> taken from the pool <pool_type> or
131 * dynamically allocated. In the first case, <pool_type> is updated to point to
132 * the next element in the list. Memory poisonning is performed if enabled.
133 */
134static inline void *pool_alloc2(struct pool_head *pool)
135{
136 void *p;
137
138 p = pool_alloc_dirty(pool);
139 if (p && mem_poison_byte)
140 memset(p, mem_poison_byte, pool->size);
141 return p;
142}
143
144/*
Willy Tarreau50e608d2007-05-13 18:26:08 +0200145 * Puts a memory area back to the corresponding pool.
146 * Items are chained directly through a pointer that
147 * is written in the beginning of the memory area, so
148 * there's no need for any carrier cell. This implies
149 * that each memory area is at least as big as one
Willy Tarreau48d63db2008-08-03 17:41:33 +0200150 * pointer. Just like with the libc's free(), nothing
151 * is done if <ptr> is NULL.
Willy Tarreau50e608d2007-05-13 18:26:08 +0200152 */
Willy Tarreaue430e772014-12-23 14:13:16 +0100153static inline void pool_free2(struct pool_head *pool, void *ptr)
154{
155 if (likely(ptr != NULL)) {
156 *(void **)ptr= (void *)pool->free_list;
157 pool->free_list = (void *)ptr;
158 pool->used--;
159 }
160}
Willy Tarreau50e608d2007-05-13 18:26:08 +0200161
162
Willy Tarreau2dd0d472006-06-29 17:53:05 +0200163#endif /* _COMMON_MEMORY_H */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200164
165/*
166 * Local variables:
167 * c-indent-level: 8
168 * c-basic-offset: 8
169 * End:
170 */