Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 20 | static 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 | */ |
| 26 | struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags) |
| 27 | { |
| 28 | struct pool_head *pool; |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 29 | struct pool_head *entry; |
| 30 | struct list *start; |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 31 | 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 Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 39 | align = 16; |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 40 | size = (size + align - 1) & -align; |
| 41 | |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 42 | start = &pools; |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 43 | pool = NULL; |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 44 | |
| 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 Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 53 | pool = entry; |
| 54 | break; |
| 55 | } |
| 56 | } |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 57 | else if (entry->size > size) { |
| 58 | /* insert before this one */ |
| 59 | start = &entry->list; |
| 60 | break; |
| 61 | } |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 62 | } |
| 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 Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 72 | LIST_ADDQ(start, &pool->list); |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 73 | } |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 74 | pool->users++; |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 75 | return pool; |
| 76 | } |
| 77 | |
| 78 | /* Allocate a new entry for pool <pool>, and return it for immediate use. |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 79 | * 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 Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 81 | */ |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 82 | void *pool_refill_alloc(struct pool_head *pool) |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 83 | { |
| 84 | void *ret; |
| 85 | |
| 86 | if (pool->limit && (pool->allocated >= pool->limit)) |
| 87 | return NULL; |
| 88 | ret = MALLOC(pool->size); |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 89 | if (!ret) { |
| 90 | pool_gc2(); |
| 91 | ret = MALLOC(pool->size); |
| 92 | if (!ret) |
| 93 | return NULL; |
| 94 | } |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 95 | pool->allocated++; |
| 96 | pool->used++; |
| 97 | return ret; |
| 98 | } |
| 99 | |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 100 | /* |
| 101 | * This function frees whatever can be freed in pool <pool>. |
| 102 | */ |
| 103 | void pool_flush2(struct pool_head *pool) |
| 104 | { |
| 105 | void *temp, *next; |
Willy Tarreau | 4d2d098 | 2007-05-14 00:39:29 +0200 | [diff] [blame] | 106 | if (!pool) |
| 107 | return; |
| 108 | |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 109 | next = pool->free_list; |
| 110 | while (next) { |
| 111 | temp = next; |
| 112 | next = *(void **)temp; |
| 113 | pool->allocated--; |
| 114 | FREE(temp); |
| 115 | } |
| 116 | pool->free_list = next; |
| 117 | |
| 118 | /* here, we should have pool->allocate == pool->used */ |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * This function frees whatever can be freed in all pools, but respecting |
| 123 | * the minimum thresholds imposed by owners. |
| 124 | */ |
| 125 | void pool_gc2() |
| 126 | { |
| 127 | struct pool_head *entry; |
| 128 | list_for_each_entry(entry, &pools, list) { |
| 129 | void *temp, *next; |
| 130 | //qfprintf(stderr, "Flushing pool %s\n", entry->name); |
| 131 | next = entry->free_list; |
| 132 | while (next && |
| 133 | entry->allocated > entry->minavail && |
| 134 | entry->allocated > entry->used) { |
| 135 | temp = next; |
| 136 | next = *(void **)temp; |
| 137 | entry->allocated--; |
| 138 | FREE(temp); |
| 139 | } |
| 140 | entry->free_list = next; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * This function destroys a pull by freeing it completely. |
| 146 | * This should be called only under extreme circumstances. |
Willy Tarreau | 4d2d098 | 2007-05-14 00:39:29 +0200 | [diff] [blame] | 147 | * It always returns NULL, easing the clearing of the old pointer. |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 148 | */ |
Willy Tarreau | 4d2d098 | 2007-05-14 00:39:29 +0200 | [diff] [blame] | 149 | void *pool_destroy2(struct pool_head *pool) |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 150 | { |
Willy Tarreau | 4d2d098 | 2007-05-14 00:39:29 +0200 | [diff] [blame] | 151 | if (pool) { |
| 152 | pool_flush2(pool); |
| 153 | FREE(pool); |
| 154 | } |
| 155 | return NULL; |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 156 | } |
| 157 | |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 158 | /* Dump statistics on pools usage. |
| 159 | */ |
| 160 | void dump_pools(void) |
| 161 | { |
| 162 | struct pool_head *entry; |
| 163 | unsigned long allocated, used; |
| 164 | int nbpools; |
| 165 | |
| 166 | allocated = used = nbpools = 0; |
| 167 | qfprintf(stderr, "Dumping pools usage.\n"); |
| 168 | list_for_each_entry(entry, &pools, list) { |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 169 | qfprintf(stderr, " - Pool %s (%d bytes) : %d allocated (%lu bytes), %d used, %d users%s\n", |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 170 | entry->name, entry->size, entry->allocated, |
| 171 | entry->size * entry->allocated, entry->used, |
Willy Tarreau | 7dcd46d | 2007-05-14 00:16:13 +0200 | [diff] [blame] | 172 | entry->users, (entry->flags & MEM_F_SHARED) ? " [SHARED]" : ""); |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 173 | |
| 174 | allocated += entry->allocated * entry->size; |
| 175 | used += entry->used * entry->size; |
| 176 | nbpools++; |
| 177 | } |
Willy Tarreau | e6ce59d | 2007-05-13 19:38:49 +0200 | [diff] [blame] | 178 | qfprintf(stderr, "Total: %d pools, %lu bytes allocated, %lu used.\n", |
Willy Tarreau | 50e608d | 2007-05-13 18:26:08 +0200 | [diff] [blame] | 179 | nbpools, allocated, used); |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * Local variables: |
| 184 | * c-indent-level: 8 |
| 185 | * c-basic-offset: 8 |
| 186 | * End: |
| 187 | */ |