blob: 5dbbdd91f7cd4684d12665ec43f0dd846c64c645 [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;
29 unsigned int align;
30
31 /* We need to store at least a (void *) in the chunks. Since we know
32 * that the malloc() function will never return such a small size,
33 * let's round the size up to something slightly bigger, in order to
34 * ease merging of entries. Note that the rounding is a power of two.
35 */
36
37 align = 4 * sizeof(void *);
38 size = (size + align - 1) & -align;
39
40 pool = NULL;
41 if (flags & MEM_F_SHARED) {
42 struct pool_head *entry;
43 list_for_each_entry(entry, &pools, list) {
44 if (!(entry->flags & MEM_F_SHARED))
45 continue;
46 if (entry->size == size) {
47 pool = entry;
48 break;
49 }
50 }
51 }
52
53 if (!pool) {
54 pool = CALLOC(1, sizeof(*pool));
55 if (!pool)
56 return NULL;
57 if (name)
58 strlcpy2(pool->name, name, sizeof(pool->name));
59 pool->size = size;
60 pool->flags = flags;
61 LIST_ADDQ(&pools, &pool->list);
62 }
63 return pool;
64}
65
66/* Allocate a new entry for pool <pool>, and return it for immediate use.
67 * NULL is returned if no memory is available for a new creation.
68 */
69void *refill_pool_alloc(struct pool_head *pool)
70{
71 void *ret;
72
73 if (pool->limit && (pool->allocated >= pool->limit))
74 return NULL;
75 ret = MALLOC(pool->size);
76 if (!ret)
77 return NULL;
78 pool->allocated++;
79 pool->used++;
80 return ret;
81}
82
83/* Dump statistics on pools usage.
84 */
85void dump_pools(void)
86{
87 struct pool_head *entry;
88 unsigned long allocated, used;
89 int nbpools;
90
91 allocated = used = nbpools = 0;
92 qfprintf(stderr, "Dumping pools usage.\n");
93 list_for_each_entry(entry, &pools, list) {
94 qfprintf(stderr, " - Pool %s (%d bytes) : %d allocated, %d used%s\n",
95 entry->name, entry->size, entry->allocated, entry->used,
96 (entry->flags & MEM_F_SHARED) ? " (SHARED)" : "");
97
98 allocated += entry->allocated * entry->size;
99 used += entry->used * entry->size;
100 nbpools++;
101 }
102 qfprintf(stderr, "Total: %d pools, %lu allocated, %lu used.\n",
103 nbpools, allocated, used);
104}
105
106/*
107 * Local variables:
108 * c-indent-level: 8
109 * c-basic-offset: 8
110 * End:
111 */