1 /* $NetBSD: pool.c,v 1.1.1.2 2009/12/02 00:26:09 haad Exp $ */
2
3 /*
4 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
5 * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
6 *
7 * This file is part of the device-mapper userspace tools.
8 *
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.
12 *
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
16 */
17
18 #include "dmlib.h"
19
20 /* FIXME: thread unsafe */
21 static DM_LIST_INIT(_dm_pools);
22 void dm_pools_check_leaks(void);
23
24 #ifdef DEBUG_POOL
25 #include "pool-debug.c"
26 #else
27 #include "pool-fast.c"
28 #endif
29
dm_pool_strdup(struct dm_pool * p,const char * str)30 char *dm_pool_strdup(struct dm_pool *p, const char *str)
31 {
32 char *ret = dm_pool_alloc(p, strlen(str) + 1);
33
34 if (ret)
35 strcpy(ret, str);
36
37 return ret;
38 }
39
dm_pool_strndup(struct dm_pool * p,const char * str,size_t n)40 char *dm_pool_strndup(struct dm_pool *p, const char *str, size_t n)
41 {
42 char *ret = dm_pool_alloc(p, n + 1);
43
44 if (ret) {
45 strncpy(ret, str, n);
46 ret[n] = '\0';
47 }
48
49 return ret;
50 }
51
dm_pool_zalloc(struct dm_pool * p,size_t s)52 void *dm_pool_zalloc(struct dm_pool *p, size_t s)
53 {
54 void *ptr = dm_pool_alloc(p, s);
55
56 if (ptr)
57 memset(ptr, 0, s);
58
59 return ptr;
60 }
61
dm_pools_check_leaks(void)62 void dm_pools_check_leaks(void)
63 {
64 struct dm_pool *p;
65
66 if (dm_list_empty(&_dm_pools))
67 return;
68
69 log_error("You have a memory leak (not released memory pool):");
70 dm_list_iterate_items(p, &_dm_pools) {
71 #ifdef DEBUG_POOL
72 log_error(" [%p] %s (%u bytes)",
73 p->orig_pool,
74 p->name, p->stats.bytes);
75 #else
76 log_error(" [%p]", p);
77 #endif
78 }
79 }
80