import source from lvm2 2.02.79
[external/device-mapper.git] / libdm / mm / dbg_malloc.c
1 /*
2  * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.  
3  * Copyright (C) 2004-2007 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 #include <assert.h>
19 #include <stdarg.h>
20
21 char *dm_strdup_aux(const char *str, const char *file, int line)
22 {
23         char *ret;
24
25         if (!str) {
26                 log_error(INTERNAL_ERROR "dm_strdup called with NULL pointer");
27                 return NULL;
28         }
29
30         if ((ret = dm_malloc_aux_debug(strlen(str) + 1, file, line)))
31                 strcpy(ret, str);
32
33         return ret;
34 }
35
36 struct memblock {
37         struct memblock *prev, *next;   /* All allocated blocks are linked */
38         size_t length;          /* Size of the requested block */
39         int id;                 /* Index of the block */
40         const char *file;       /* File that allocated */
41         int line;               /* Line that allocated */
42         void *magic;            /* Address of this block */
43 } __attribute__((aligned(8)));
44
45 static struct {
46         unsigned block_serialno;/* Non-decreasing serialno of block */
47         unsigned blocks_allocated; /* Current number of blocks allocated */
48         unsigned blocks_max;    /* Max no of concurrently-allocated blocks */
49         unsigned int bytes, mbytes;
50
51 } _mem_stats = {
52 0, 0, 0, 0, 0};
53
54 static struct memblock *_head = 0;
55 static struct memblock *_tail = 0;
56
57 void *dm_malloc_aux_debug(size_t s, const char *file, int line)
58 {
59         struct memblock *nb;
60         size_t tsize = s + sizeof(*nb) + sizeof(unsigned long);
61
62         if (s > 50000000) {
63                 log_error("Huge memory allocation (size %" PRIsize_t
64                           ") rejected - metadata corruption?", s);
65                 return 0;
66         }
67
68         if (!(nb = malloc(tsize))) {
69                 log_error("couldn't allocate any memory, size = %" PRIsize_t,
70                           s);
71                 return 0;
72         }
73
74         /* set up the file and line info */
75         nb->file = file;
76         nb->line = line;
77
78         dm_bounds_check();
79
80         /* setup fields */
81         nb->magic = nb + 1;
82         nb->length = s;
83         nb->id = ++_mem_stats.block_serialno;
84         nb->next = 0;
85
86         /* stomp a pretty pattern across the new memory
87            and fill in the boundary bytes */
88         {
89                 char *ptr = (char *) (nb + 1);
90                 size_t i;
91                 for (i = 0; i < s; i++)
92                         *ptr++ = i & 0x1 ? (char) 0xba : (char) 0xbe;
93
94                 for (i = 0; i < sizeof(unsigned long); i++)
95                         *ptr++ = (char) nb->id;
96         }
97
98         nb->prev = _tail;
99
100         /* link to tail of the list */
101         if (!_head)
102                 _head = _tail = nb;
103         else {
104                 _tail->next = nb;
105                 _tail = nb;
106         }
107
108         _mem_stats.blocks_allocated++;
109         if (_mem_stats.blocks_allocated > _mem_stats.blocks_max)
110                 _mem_stats.blocks_max = _mem_stats.blocks_allocated;
111
112         _mem_stats.bytes += s;
113         if (_mem_stats.bytes > _mem_stats.mbytes)
114                 _mem_stats.mbytes = _mem_stats.bytes;
115
116         /* log_debug("Allocated: %u %u %u", nb->id, _mem_stats.blocks_allocated,
117                   _mem_stats.bytes); */
118
119         return nb + 1;
120 }
121
122 void *dm_zalloc_aux_debug(size_t s, const char *file, int line)
123 {
124         void *ptr = dm_malloc_aux_debug(s, file, line);
125
126         if (ptr)
127                 memset(ptr, 0, s);
128
129         return ptr;
130 }
131
132 void dm_free_aux(void *p)
133 {
134         char *ptr;
135         size_t i;
136         struct memblock *mb = ((struct memblock *) p) - 1;
137         if (!p)
138                 return;
139
140         dm_bounds_check();
141
142         /* sanity check */
143         assert(mb->magic == p);
144
145         /* check data at the far boundary */
146         ptr = ((char *) mb) + sizeof(struct memblock) + mb->length;
147         for (i = 0; i < sizeof(unsigned long); i++)
148                 if (*ptr++ != (char) mb->id)
149                         assert(!"Damage at far end of block");
150
151         /* have we freed this before ? */
152         assert(mb->id != 0);
153
154         /* unlink */
155         if (mb->prev)
156                 mb->prev->next = mb->next;
157         else
158                 _head = mb->next;
159
160         if (mb->next)
161                 mb->next->prev = mb->prev;
162         else
163                 _tail = mb->prev;
164
165         mb->id = 0;
166
167         /* stomp a different pattern across the memory */
168         ptr = ((char *) mb) + sizeof(struct memblock);
169         for (i = 0; i < mb->length; i++)
170                 *ptr++ = i & 1 ? (char) 0xde : (char) 0xad;
171
172         assert(_mem_stats.blocks_allocated);
173         _mem_stats.blocks_allocated--;
174         _mem_stats.bytes -= mb->length;
175
176         /* free the memory */
177         free(mb);
178 }
179
180 void *dm_realloc_aux(void *p, unsigned int s, const char *file, int line)
181 {
182         void *r;
183         struct memblock *mb = ((struct memblock *) p) - 1;
184
185         r = dm_malloc_aux_debug(s, file, line);
186
187         if (p) {
188                 memcpy(r, p, mb->length);
189                 dm_free_aux(p);
190         }
191
192         return r;
193 }
194
195 int dm_dump_memory_debug(void)
196 {
197         unsigned long tot = 0;
198         struct memblock *mb;
199         char str[32];
200         size_t c;
201
202         if (_head)
203                 log_very_verbose("You have a memory leak:");
204
205         for (mb = _head; mb; mb = mb->next) {
206 #ifdef VALGRIND_POOL
207                 /*
208                  * We can't look at the memory in case it has had
209                  * VALGRIND_MAKE_MEM_NOACCESS called on it.
210                  */
211                 str[0] = '\0';
212 #else
213                 for (c = 0; c < sizeof(str) - 1; c++) {
214                         if (c >= mb->length)
215                                 str[c] = ' ';
216                         else if (((char *)mb->magic)[c] == '\0')
217                                 str[c] = '\0';
218                         else if (((char *)mb->magic)[c] < ' ')
219                                 str[c] = '?';
220                         else
221                                 str[c] = ((char *)mb->magic)[c];
222                 }
223                 str[sizeof(str) - 1] = '\0';
224 #endif
225
226                 LOG_MESG(_LOG_INFO, mb->file, mb->line, 0,
227                          "block %d at %p, size %" PRIsize_t "\t [%s]",
228                          mb->id, mb->magic, mb->length, str);
229                 tot += mb->length;
230         }
231
232         if (_head)
233                 log_very_verbose("%ld bytes leaked in total", tot);
234
235         return 1;
236 }
237
238 void dm_bounds_check_debug(void)
239 {
240         struct memblock *mb = _head;
241         while (mb) {
242                 size_t i;
243                 char *ptr = ((char *) (mb + 1)) + mb->length;
244                 for (i = 0; i < sizeof(unsigned long); i++)
245                         if (*ptr++ != (char) mb->id)
246                                 assert(!"Memory smash");
247
248                 mb = mb->next;
249         }
250 }
251
252 void *dm_malloc_aux(size_t s, const char *file __attribute__((unused)),
253                     int line __attribute__((unused)))
254 {
255         if (s > 50000000) {
256                 log_error("Huge memory allocation (size %" PRIsize_t
257                           ") rejected - metadata corruption?", s);
258                 return 0;
259         }
260
261         return malloc(s);
262 }
263
264 void *dm_zalloc_aux(size_t s, const char *file, int line)
265 {
266         void *ptr = dm_malloc_aux(s, file, line);
267
268         if (ptr)
269                 memset(ptr, 0, s);
270
271         return ptr;
272 }