blob: 589d1d2706471068bab7f85a486fca9baef7bd3f [file] [log] [blame]
Willy Tarreau50e608d2007-05-13 18:26:08 +02001/*
2 * Memory management functions.
3 *
4 * Copyright 2000-2007 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <common/config.h>
14#include <common/memory.h>
15#include <common/mini-clist.h>
16#include <common/standard.h>
17
18#include <proto/log.h>
19
20static struct list pools = LIST_HEAD_INIT(pools);
21
22/* Try to find an existing shared pool with the same characteristics and
23 * returns it, otherwise creates this one. NULL is returned if no memory
24 * is available for a new creation.
25 */
26struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags)
27{
28 struct pool_head *pool;
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020029 struct pool_head *entry;
30 struct list *start;
Willy Tarreau50e608d2007-05-13 18:26:08 +020031 unsigned int align;
32
33 /* We need to store at least a (void *) in the chunks. Since we know
34 * that the malloc() function will never return such a small size,
35 * let's round the size up to something slightly bigger, in order to
36 * ease merging of entries. Note that the rounding is a power of two.
37 */
38
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020039 align = 16;
Willy Tarreau50e608d2007-05-13 18:26:08 +020040 size = (size + align - 1) & -align;
41
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020042 start = &pools;
Willy Tarreau50e608d2007-05-13 18:26:08 +020043 pool = NULL;
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020044
45 list_for_each_entry(entry, &pools, list) {
46 if (entry->size == size) {
47 /* either we can share this place and we take it, or
48 * we look for a sharable one or for the next position
49 * before which we will insert a new one.
50 */
51 if (flags & entry->flags & MEM_F_SHARED) {
52 /* we can share this one */
Willy Tarreau50e608d2007-05-13 18:26:08 +020053 pool = entry;
54 break;
55 }
56 }
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020057 else if (entry->size > size) {
58 /* insert before this one */
59 start = &entry->list;
60 break;
61 }
Willy Tarreau50e608d2007-05-13 18:26:08 +020062 }
63
64 if (!pool) {
65 pool = CALLOC(1, sizeof(*pool));
66 if (!pool)
67 return NULL;
68 if (name)
69 strlcpy2(pool->name, name, sizeof(pool->name));
70 pool->size = size;
71 pool->flags = flags;
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020072 LIST_ADDQ(start, &pool->list);
Willy Tarreau50e608d2007-05-13 18:26:08 +020073 }
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020074 pool->users++;
Willy Tarreau50e608d2007-05-13 18:26:08 +020075 return pool;
76}
77
78/* Allocate a new entry for pool <pool>, and return it for immediate use.
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020079 * NULL is returned if no memory is available for a new creation. A call
80 * to the garbage collector is performed before returning NULL.
Willy Tarreau50e608d2007-05-13 18:26:08 +020081 */
Willy Tarreaue6ce59d2007-05-13 19:38:49 +020082void *pool_refill_alloc(struct pool_head *pool)
Willy Tarreau50e608d2007-05-13 18:26:08 +020083{
84 void *ret;
85
86 if (pool->limit && (pool->allocated >= pool->limit))
87 return NULL;
88 ret = MALLOC(pool->size);
Willy Tarreau7dcd46d2007-05-14 00:16:13 +020089 if (!ret) {
90 pool_gc2();
91 ret = MALLOC(pool->size);
92 if (!ret)
93 return NULL;
94 }
Willy Tarreau50e608d2007-05-13 18:26:08 +020095 pool->allocated++;
96 pool->used++;
97 return ret;
98}
99
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200100/*
101 * This function frees whatever can be freed in pool <pool>.
102 */
103void pool_flush2(struct pool_head *pool)
104{
105 void *temp, *next;
106 next = pool->free_list;
107 while (next) {
108 temp = next;
109 next = *(void **)temp;
110 pool->allocated--;
111 FREE(temp);
112 }
113 pool->free_list = next;
114
115 /* here, we should have pool->allocate == pool->used */
116}
117
118/*
119 * This function frees whatever can be freed in all pools, but respecting
120 * the minimum thresholds imposed by owners.
121 */
122void pool_gc2()
123{
124 struct pool_head *entry;
125 list_for_each_entry(entry, &pools, list) {
126 void *temp, *next;
127 //qfprintf(stderr, "Flushing pool %s\n", entry->name);
128 next = entry->free_list;
129 while (next &&
130 entry->allocated > entry->minavail &&
131 entry->allocated > entry->used) {
132 temp = next;
133 next = *(void **)temp;
134 entry->allocated--;
135 FREE(temp);
136 }
137 entry->free_list = next;
138 }
139}
140
141/*
142 * This function destroys a pull by freeing it completely.
143 * This should be called only under extreme circumstances.
144 */
145void pool_destroy2(struct pool_head *pool)
146{
147 pool_flush2(pool);
148 FREE(pool);
149}
150
Willy Tarreau50e608d2007-05-13 18:26:08 +0200151/* Dump statistics on pools usage.
152 */
153void dump_pools(void)
154{
155 struct pool_head *entry;
156 unsigned long allocated, used;
157 int nbpools;
158
159 allocated = used = nbpools = 0;
160 qfprintf(stderr, "Dumping pools usage.\n");
161 list_for_each_entry(entry, &pools, list) {
Willy Tarreau7dcd46d2007-05-14 00:16:13 +0200162 qfprintf(stderr, " - Pool %s (%d bytes) : %d allocated (%lu bytes), %d used, %d users%s\n",
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200163 entry->name, entry->size, entry->allocated,
164 entry->size * entry->allocated, entry->used,
Willy Tarreau7dcd46d2007-05-14 00:16:13 +0200165 entry->users, (entry->flags & MEM_F_SHARED) ? " [SHARED]" : "");
Willy Tarreau50e608d2007-05-13 18:26:08 +0200166
167 allocated += entry->allocated * entry->size;
168 used += entry->used * entry->size;
169 nbpools++;
170 }
Willy Tarreaue6ce59d2007-05-13 19:38:49 +0200171 qfprintf(stderr, "Total: %d pools, %lu bytes allocated, %lu used.\n",
Willy Tarreau50e608d2007-05-13 18:26:08 +0200172 nbpools, allocated, used);
173}
174
175/*
176 * Local variables:
177 * c-indent-level: 8
178 * c-basic-offset: 8
179 * End:
180 */