1 : /*
2 : * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3 : * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
4 : *
5 : * This file is part of the device-mapper userspace tools.
6 : *
7 : * This copyrighted material is made available to anyone wishing to use,
8 : * modify, copy, or redistribute it subject to the terms and conditions
9 : * of the GNU Lesser General Public License v.2.1.
10 : *
11 : * You should have received a copy of the GNU Lesser General Public License
12 : * along with this program; if not, write to the Free Software Foundation,
13 : * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 : */
15 :
16 : #include "dmlib.h"
17 :
18 : /* FIXME: thread unsafe */
19 : static DM_LIST_INIT(_dm_pools);
20 : void dm_pools_check_leaks(void);
21 :
22 : #ifdef DEBUG_POOL
23 : #include "pool-debug.c"
24 : #else
25 : #include "pool-fast.c"
26 : #endif
27 :
28 1363 : char *dm_pool_strdup(struct dm_pool *p, const char *str)
29 : {
30 1363 : char *ret = dm_pool_alloc(p, strlen(str) + 1);
31 :
32 1363 : if (ret)
33 1363 : strcpy(ret, str);
34 :
35 1363 : return ret;
36 : }
37 :
38 0 : char *dm_pool_strndup(struct dm_pool *p, const char *str, size_t n)
39 : {
40 0 : char *ret = dm_pool_alloc(p, n + 1);
41 :
42 0 : if (ret) {
43 0 : strncpy(ret, str, n);
44 0 : ret[n] = '\0';
45 : }
46 :
47 0 : return ret;
48 : }
49 :
50 85757 : void *dm_pool_zalloc(struct dm_pool *p, size_t s)
51 : {
52 85757 : void *ptr = dm_pool_alloc(p, s);
53 :
54 85757 : if (ptr)
55 85757 : memset(ptr, 0, s);
56 :
57 85757 : return ptr;
58 : }
59 :
60 1 : void dm_pools_check_leaks(void)
61 : {
62 : struct dm_pool *p;
63 :
64 1 : if (dm_list_empty(&_dm_pools))
65 1 : return;
66 :
67 0 : log_error("You have a memory leak (not released memory pool):");
68 0 : dm_list_iterate_items(p, &_dm_pools) {
69 : #ifdef DEBUG_POOL
70 : log_error(" [%p] %s (%u bytes)",
71 : p->orig_pool,
72 : p->name, p->stats.bytes);
73 : #else
74 0 : log_error(" [%p]", p);
75 : #endif
76 : }
77 : }
|