1 /* $NetBSD: pool-debug.c,v 1.1.1.2 2009/12/02 00:26:09 haad Exp $ */
4 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
5 * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
7 * This file is part of the device-mapper userspace tools.
9 * This copyrighted material is made available to anyone wishing to use,
10 * modify, copy, or redistribute it subject to the terms and conditions
11 * of the GNU Lesser General Public License v.2.1.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software Foundation,
15 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 unsigned block_serialno; /* Non-decreasing serialno of block */
29 unsigned blocks_allocated; /* Current number of blocks allocated */
30 unsigned blocks_max; /* Max no of concurrently-allocated blocks */
31 unsigned int bytes, maxbytes;
37 void *orig_pool; /* to pair it with first allocation call */
48 /* by default things come out aligned for doubles */
49 #define DEFAULT_ALIGNMENT __alignof__ (double)
51 struct dm_pool *dm_pool_create(const char *name, size_t chunk_hint)
53 struct dm_pool *mem = dm_malloc(sizeof(*mem));
56 log_error("Couldn't create memory pool %s (size %"
57 PRIsize_t ")", name, sizeof(*mem));
64 mem->blocks = mem->tail = NULL;
66 mem->stats.block_serialno = 0;
67 mem->stats.blocks_allocated = 0;
68 mem->stats.blocks_max = 0;
70 mem->stats.maxbytes = 0;
75 log_debug("Created mempool %s", name);
78 dm_list_add(&_dm_pools, &mem->list);
82 static void _free_blocks(struct dm_pool *p, struct block *b)
87 p->stats.bytes -= b->size;
88 p->stats.blocks_allocated--;
97 static void _pool_stats(struct dm_pool *p, const char *action)
100 log_debug("%s mempool %s: %u/%u bytes, %u/%u blocks, "
101 "%u allocations)", action, p->name, p->stats.bytes,
102 p->stats.maxbytes, p->stats.blocks_allocated,
103 p->stats.blocks_max, p->stats.block_serialno);
109 void dm_pool_destroy(struct dm_pool *p)
111 _pool_stats(p, "Destroying");
112 _free_blocks(p, p->blocks);
113 dm_list_del(&p->list);
117 void *dm_pool_alloc(struct dm_pool *p, size_t s)
119 return dm_pool_alloc_aligned(p, s, DEFAULT_ALIGNMENT);
122 static void _append_block(struct dm_pool *p, struct block *b)
128 p->blocks = p->tail = b;
130 p->stats.block_serialno++;
131 p->stats.blocks_allocated++;
132 if (p->stats.blocks_allocated > p->stats.blocks_max)
133 p->stats.blocks_max = p->stats.blocks_allocated;
135 p->stats.bytes += b->size;
136 if (p->stats.bytes > p->stats.maxbytes)
137 p->stats.maxbytes = p->stats.bytes;
140 static struct block *_new_block(size_t s, unsigned alignment)
142 /* FIXME: I'm currently ignoring the alignment arg. */
143 size_t len = sizeof(struct block) + s;
144 struct block *b = dm_malloc(len);
147 * Too lazy to implement alignment for debug version, and
148 * I don't think LVM will use anything but default
151 assert(alignment == DEFAULT_ALIGNMENT);
154 log_error("Out of memory");
158 if (!(b->data = dm_malloc(s))) {
159 log_error("Out of memory");
170 void *dm_pool_alloc_aligned(struct dm_pool *p, size_t s, unsigned alignment)
172 struct block *b = _new_block(s, alignment);
182 void dm_pool_empty(struct dm_pool *p)
184 _pool_stats(p, "Emptying");
185 _free_blocks(p, p->blocks);
186 p->blocks = p->tail = NULL;
189 void dm_pool_free(struct dm_pool *p, void *ptr)
191 struct block *b, *prev = NULL;
193 _pool_stats(p, "Freeing (before)");
195 for (b = p->blocks; b; b = b->next) {
202 * If this fires then you tried to free a
203 * pointer that either wasn't from this
204 * pool, or isn't the start of a block.
214 p->blocks = p->tail = NULL;
216 _pool_stats(p, "Freeing (after)");
219 int dm_pool_begin_object(struct dm_pool *p, size_t init_size)
226 int dm_pool_grow_object(struct dm_pool *p, const void *extra, size_t delta)
232 delta = strlen(extra);
237 new_size = delta + p->object->size;
241 if (!(new = _new_block(new_size, DEFAULT_ALIGNMENT))) {
242 log_error("Couldn't extend object.");
247 memcpy(new->data, p->object->data, p->object->size);
248 dm_free(p->object->data);
253 memcpy(new->data + new_size - delta, extra, delta);
258 void *dm_pool_end_object(struct dm_pool *p)
261 _append_block(p, p->object);
265 return p->tail->data;
268 void dm_pool_abandon_object(struct dm_pool *p)