2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Memory Management
23 * \author Mark Spencer <markster@digium.com>
26 #ifdef __AST_DEBUG_MALLOC
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36 #include "asterisk/cli.h"
37 #include "asterisk/logger.h"
38 #include "asterisk/options.h"
39 #include "asterisk/lock.h"
40 #include "asterisk/strings.h"
41 #include "asterisk/unaligned.h"
43 #define SOME_PRIME 563
55 /* Undefine all our macros */
65 #define FENCE_MAGIC 0xdeadbeef
69 static struct ast_region {
70 struct ast_region *next;
77 unsigned char data[0];
78 } *regions[SOME_PRIME];
81 (((unsigned long)(a)) % SOME_PRIME)
83 AST_MUTEX_DEFINE_STATIC(reglock);
84 AST_MUTEX_DEFINE_STATIC(showmemorylock);
86 #define astmm_log(...) \
88 fprintf(stderr, __VA_ARGS__); \
90 fprintf(mmlog, __VA_ARGS__); \
95 static inline void *__ast_alloc_region(size_t size, const enum func_type which, const char *file, int lineno, const char *func)
97 struct ast_region *reg;
102 if (!(reg = malloc(size + sizeof(*reg) + sizeof(*fence)))) {
103 astmm_log("Memory Allocation Failure - '%d' bytes in function %s "
104 "at line %d of %s\n", (int) size, func, lineno, file);
107 ast_copy_string(reg->file, file, sizeof(reg->file));
108 ast_copy_string(reg->func, func, sizeof(reg->func));
109 reg->lineno = lineno;
114 reg->fence = FENCE_MAGIC;
115 fence = (ptr + reg->len);
116 put_unaligned_uint32(fence, FENCE_MAGIC);
118 ast_mutex_lock(®lock);
119 reg->next = regions[hash];
121 ast_mutex_unlock(®lock);
126 static inline size_t __ast_sizeof_region(void *ptr)
128 int hash = HASH(ptr);
129 struct ast_region *reg;
132 ast_mutex_lock(®lock);
133 for (reg = regions[hash]; reg; reg = reg->next) {
134 if (reg->data == ptr) {
139 ast_mutex_unlock(®lock);
144 static void __ast_free_region(void *ptr, const char *file, int lineno, const char *func)
146 int hash = HASH(ptr);
147 struct ast_region *reg, *prev = NULL;
150 ast_mutex_lock(®lock);
151 for (reg = regions[hash]; reg; reg = reg->next) {
152 if (reg->data == ptr) {
154 prev->next = reg->next;
156 regions[hash] = reg->next;
161 ast_mutex_unlock(®lock);
164 fence = (unsigned int *)(reg->data + reg->len);
165 if (reg->fence != FENCE_MAGIC) {
166 astmm_log("WARNING: Low fence violation at %p, in %s of %s, "
167 "line %d\n", reg->data, reg->func, reg->file, reg->lineno);
169 if (get_unaligned_uint32(fence) != FENCE_MAGIC) {
170 astmm_log("WARNING: High fence violation at %p, in %s of %s, "
171 "line %d\n", reg->data, reg->func, reg->file, reg->lineno);
175 astmm_log("WARNING: Freeing unused memory at %p, in %s of %s, line %d\n",
176 ptr, func, file, lineno);
180 void *__ast_calloc(size_t nmemb, size_t size, const char *file, int lineno, const char *func)
184 if ((ptr = __ast_alloc_region(size * nmemb, FUNC_CALLOC, file, lineno, func)))
185 memset(ptr, 0, size * nmemb);
190 void *__ast_malloc(size_t size, const char *file, int lineno, const char *func)
192 return __ast_alloc_region(size, FUNC_MALLOC, file, lineno, func);
195 void __ast_free(void *ptr, const char *file, int lineno, const char *func)
197 __ast_free_region(ptr, file, lineno, func);
200 void *__ast_realloc(void *ptr, size_t size, const char *file, int lineno, const char *func)
205 if (ptr && !(len = __ast_sizeof_region(ptr))) {
206 astmm_log("WARNING: Realloc of unalloced memory at %p, in %s of %s, "
207 "line %d\n", ptr, func, file, lineno);
211 if (!(tmp = __ast_alloc_region(size, FUNC_REALLOC, file, lineno, func)))
217 memcpy(tmp, ptr, len);
218 __ast_free_region(ptr, file, lineno, func);
224 char *__ast_strdup(const char *s, const char *file, int lineno, const char *func)
233 if ((ptr = __ast_alloc_region(len, FUNC_STRDUP, file, lineno, func)))
239 char *__ast_strndup(const char *s, size_t n, const char *file, int lineno, const char *func)
250 if ((ptr = __ast_alloc_region(len, FUNC_STRNDUP, file, lineno, func)))
256 int __ast_asprintf(const char *file, int lineno, const char *func, char **strp, const char *fmt, ...)
265 size = vsnprintf(&s, 1, fmt, ap2);
267 if (!(*strp = __ast_alloc_region(size + 1, FUNC_ASPRINTF, file, lineno, func))) {
271 vsnprintf(*strp, size + 1, fmt, ap);
277 int __ast_vasprintf(char **strp, const char *fmt, va_list ap, const char *file, int lineno, const char *func)
285 size = vsnprintf(&s, 1, fmt, ap2);
287 if (!(*strp = __ast_alloc_region(size + 1, FUNC_VASPRINTF, file, lineno, func))) {
291 vsnprintf(*strp, size + 1, fmt, ap);
296 static int handle_show_memory(int fd, int argc, char *argv[])
299 struct ast_region *reg;
301 unsigned int len = 0;
302 unsigned int count = 0;
308 ast_mutex_lock(&showmemorylock);
309 for (x = 0; x < SOME_PRIME; x++) {
310 for (reg = regions[x]; reg; reg = reg->next) {
311 if (!fn || !strcasecmp(fn, reg->file) || !strcasecmp(fn, "anomolies")) {
312 fence = (unsigned int *)(reg->data + reg->len);
313 if (reg->fence != FENCE_MAGIC) {
314 astmm_log("WARNING: Low fence violation at %p, "
315 "in %s of %s, line %d\n", reg->data,
316 reg->func, reg->file, reg->lineno);
318 if (get_unaligned_uint32(fence) != FENCE_MAGIC) {
319 astmm_log("WARNING: High fence violation at %p, in %s of %s, "
320 "line %d\n", reg->data, reg->func, reg->file, reg->lineno);
323 if (!fn || !strcasecmp(fn, reg->file)) {
324 ast_cli(fd, "%10d bytes allocated in %20s at line %5d of %s\n",
325 (int) reg->len, reg->func, reg->lineno, reg->file);
331 ast_mutex_unlock(&showmemorylock);
333 ast_cli(fd, "%d bytes allocated %d units total\n", len, count);
335 return RESULT_SUCCESS;
338 static int handle_show_memory_summary(int fd, int argc, char *argv[])
342 struct ast_region *reg;
343 unsigned int len = 0;
345 struct file_summary {
349 struct file_summary *next;
350 } *list = NULL, *cur;
355 ast_mutex_lock(®lock);
356 for (x = 0; x < SOME_PRIME; x++) {
357 for (reg = regions[x]; reg; reg = reg->next) {
358 if (fn && strcasecmp(fn, reg->file))
361 for (cur = list; cur; cur = cur->next) {
362 if ((!fn && !strcmp(cur->fn, reg->file)) || (fn && !strcmp(cur->fn, reg->func)))
366 cur = alloca(sizeof(*cur));
367 memset(cur, 0, sizeof(*cur));
368 ast_copy_string(cur->fn, fn ? reg->func : reg->file, sizeof(cur->fn));
373 cur->len += reg->len;
377 ast_mutex_unlock(®lock);
379 /* Dump the whole list */
380 for (cur = list; cur; cur = cur->next) {
382 count += list->count;
384 ast_cli(fd, "%10d bytes in %5d allocations in function '%s' of '%s'\n",
385 list->len, list->count, list->fn, fn);
387 ast_cli(fd, "%10d bytes in %5d allocations in file '%s'\n",
388 list->len, list->count, list->fn);
392 ast_cli(fd, "%d bytes allocated %d units total\n", len, count);
394 return RESULT_SUCCESS;
397 static char show_memory_help[] =
398 "Usage: memory show allocations [<file>]\n"
399 " Dumps a list of all segments of allocated memory, optionally\n"
400 "limited to those from a specific file\n";
402 static char show_memory_summary_help[] =
403 "Usage: memory show summary [<file>]\n"
404 " Summarizes heap memory allocations by file, or optionally\n"
405 "by function, if a file is specified\n";
407 static struct ast_cli_entry cli_show_memory_allocations_deprecated = {
408 { "show", "memory", "allocations", NULL },
409 handle_show_memory, NULL,
412 static struct ast_cli_entry cli_show_memory_summary_deprecated = {
413 { "show", "memory", "summary", NULL },
414 handle_show_memory_summary, NULL,
417 static struct ast_cli_entry cli_memory[] = {
418 { { "memory", "show", "allocations", NULL },
419 handle_show_memory, "Display outstanding memory allocations",
420 show_memory_help, NULL, &cli_show_memory_allocations },
422 { { "memory", "show", "summary", NULL },
423 handle_show_memory_summary, "Summarize outstanding memory allocations",
424 show_memory_summary_help, NULL, &cli_show_memory_summary },
427 void __ast_mm_init(void)
429 char filename[PATH_MAX];
431 ast_cli_register_multiple(cli_memory, sizeof(cli_memory) / sizeof(struct ast_cli_entry));
433 snprintf(filename, sizeof(filename), "%s/mmlog", (char *)ast_config_AST_LOG_DIR);
436 ast_verbose("Asterisk Malloc Debugger Started (see %s))\n", filename);
438 if ((mmlog = fopen(filename, "a+"))) {
439 fprintf(mmlog, "%ld - New session\n", time(NULL));