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$")
32 #include "asterisk/paths.h" /* use ast_config_AST_LOG_DIR */
35 #include "asterisk/cli.h"
36 #include "asterisk/lock.h"
37 #include "asterisk/strings.h"
38 #include "asterisk/unaligned.h"
40 #define SOME_PRIME 563
52 /* Undefine all our macros */
62 #define FENCE_MAGIC 0xdeadbeef
66 static struct ast_region {
67 struct ast_region *next;
72 unsigned int cache; /* region was allocated as part of a cache pool */
75 unsigned char data[0];
76 } *regions[SOME_PRIME];
79 (((unsigned long)(a)) % SOME_PRIME)
81 /*! Tracking this mutex will cause infinite recursion, as the mutex tracking
82 * code allocates memory */
83 AST_MUTEX_DEFINE_STATIC_NOTRACKING(reglock);
85 #define astmm_log(...) \
87 fprintf(stderr, __VA_ARGS__); \
89 fprintf(mmlog, __VA_ARGS__); \
94 static inline void *__ast_alloc_region(size_t size, const enum func_type which, const char *file, int lineno, const char *func, unsigned int cache)
96 struct ast_region *reg;
101 if (!(reg = malloc(size + sizeof(*reg) + sizeof(*fence)))) {
102 astmm_log("Memory Allocation Failure - '%d' bytes in function %s "
103 "at line %d of %s\n", (int) size, func, lineno, file);
106 ast_copy_string(reg->file, file, sizeof(reg->file));
107 ast_copy_string(reg->func, func, sizeof(reg->func));
108 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, 0)))
185 memset(ptr, 0, size * nmemb);
190 void *__ast_calloc_cache(size_t nmemb, size_t size, const char *file, int lineno, const char *func)
194 if ((ptr = __ast_alloc_region(size * nmemb, FUNC_CALLOC, file, lineno, func, 1)))
195 memset(ptr, 0, size * nmemb);
200 void *__ast_malloc(size_t size, const char *file, int lineno, const char *func)
202 return __ast_alloc_region(size, FUNC_MALLOC, file, lineno, func, 0);
205 void __ast_free(void *ptr, const char *file, int lineno, const char *func)
207 __ast_free_region(ptr, file, lineno, func);
210 void *__ast_realloc(void *ptr, size_t size, const char *file, int lineno, const char *func)
215 if (ptr && !(len = __ast_sizeof_region(ptr))) {
216 astmm_log("WARNING: Realloc of unalloced memory at %p, in %s of %s, "
217 "line %d\n", ptr, func, file, lineno);
221 if (!(tmp = __ast_alloc_region(size, FUNC_REALLOC, file, lineno, func, 0)))
227 memcpy(tmp, ptr, len);
228 __ast_free_region(ptr, file, lineno, func);
234 char *__ast_strdup(const char *s, const char *file, int lineno, const char *func)
243 if ((ptr = __ast_alloc_region(len, FUNC_STRDUP, file, lineno, func, 0)))
249 char *__ast_strndup(const char *s, size_t n, const char *file, int lineno, const char *func)
260 if ((ptr = __ast_alloc_region(len, FUNC_STRNDUP, file, lineno, func, 0)))
266 int __ast_asprintf(const char *file, int lineno, const char *func, char **strp, const char *fmt, ...)
275 size = vsnprintf(&s, 1, fmt, ap2);
277 if (!(*strp = __ast_alloc_region(size + 1, FUNC_ASPRINTF, file, lineno, func, 0))) {
281 vsnprintf(*strp, size + 1, fmt, ap);
287 int __ast_vasprintf(char **strp, const char *fmt, va_list ap, const char *file, int lineno, const char *func)
295 size = vsnprintf(&s, 1, fmt, ap2);
297 if (!(*strp = __ast_alloc_region(size + 1, FUNC_VASPRINTF, file, lineno, func, 0))) {
301 vsnprintf(*strp, size + 1, fmt, ap);
306 static char *handle_memory_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
309 struct ast_region *reg;
311 unsigned int len = 0;
312 unsigned int cache_len = 0;
313 unsigned int count = 0;
318 e->command = "memory show allocations";
320 "Usage: memory show allocations [<file>]\n"
321 " Dumps a list of all segments of allocated memory, optionally\n"
322 " limited to those from a specific file\n";
332 ast_mutex_lock(®lock);
333 for (x = 0; x < SOME_PRIME; x++) {
334 for (reg = regions[x]; reg; reg = reg->next) {
335 if (!fn || !strcasecmp(fn, reg->file) || !strcasecmp(fn, "anomolies")) {
336 fence = (unsigned int *)(reg->data + reg->len);
337 if (reg->fence != FENCE_MAGIC) {
338 astmm_log("WARNING: Low fence violation at %p, "
339 "in %s of %s, line %d\n", reg->data,
340 reg->func, reg->file, reg->lineno);
342 if (get_unaligned_uint32(fence) != FENCE_MAGIC) {
343 astmm_log("WARNING: High fence violation at %p, in %s of %s, "
344 "line %d\n", reg->data, reg->func, reg->file, reg->lineno);
347 if (!fn || !strcasecmp(fn, reg->file)) {
348 ast_cli(a->fd, "%10d bytes allocated%s in %20s at line %5d of %s\n",
349 (int) reg->len, reg->cache ? " (cache)" : "",
350 reg->func, reg->lineno, reg->file);
353 cache_len += reg->len;
358 ast_mutex_unlock(®lock);
361 ast_cli(a->fd, "%d bytes allocated (%d in caches) in %d allocations\n", len, cache_len, count);
363 ast_cli(a->fd, "%d bytes allocated in %d allocations\n", len, count);
368 static char *handle_memory_show_summary(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
372 struct ast_region *reg;
373 unsigned int len = 0;
374 unsigned int cache_len = 0;
376 struct file_summary {
381 struct file_summary *next;
382 } *list = NULL, *cur;
386 e->command = "memory show summary";
388 "Usage: memory show summary [<file>]\n"
389 " Summarizes heap memory allocations by file, or optionally\n"
390 "by function, if a file is specified\n";
399 ast_mutex_lock(®lock);
400 for (x = 0; x < SOME_PRIME; x++) {
401 for (reg = regions[x]; reg; reg = reg->next) {
402 if (fn && strcasecmp(fn, reg->file))
405 for (cur = list; cur; cur = cur->next) {
406 if ((!fn && !strcmp(cur->fn, reg->file)) || (fn && !strcmp(cur->fn, reg->func)))
410 cur = alloca(sizeof(*cur));
411 memset(cur, 0, sizeof(*cur));
412 ast_copy_string(cur->fn, fn ? reg->func : reg->file, sizeof(cur->fn));
417 cur->len += reg->len;
419 cur->cache_len += reg->len;
423 ast_mutex_unlock(®lock);
425 /* Dump the whole list */
426 for (cur = list; cur; cur = cur->next) {
428 cache_len += cur->cache_len;
430 if (cur->cache_len) {
432 ast_cli(a->fd, "%10d bytes (%10d cache) in %d allocations in function '%s' of '%s'\n",
433 cur->len, cur->cache_len, cur->count, cur->fn, fn);
435 ast_cli(a->fd, "%10d bytes (%10d cache) in %d allocations in file '%s'\n",
436 cur->len, cur->cache_len, cur->count, cur->fn);
440 ast_cli(a->fd, "%10d bytes in %d allocations in function '%s' of '%s'\n",
441 cur->len, cur->count, cur->fn, fn);
443 ast_cli(a->fd, "%10d bytes in %d allocations in file '%s'\n",
444 cur->len, cur->count, cur->fn);
450 ast_cli(a->fd, "%d bytes allocated (%d in caches) in %d allocations\n", len, cache_len, count);
452 ast_cli(a->fd, "%d bytes allocated in %d allocations\n", len, count);
457 static struct ast_cli_entry cli_memory[] = {
458 AST_CLI_DEFINE(handle_memory_show, "Display outstanding memory allocations"),
459 AST_CLI_DEFINE(handle_memory_show_summary, "Summarize outstanding memory allocations"),
462 void __ast_mm_init(void)
464 char filename[PATH_MAX];
466 ast_cli_register_multiple(cli_memory, sizeof(cli_memory) / sizeof(struct ast_cli_entry));
468 snprintf(filename, sizeof(filename), "%s/mmlog", ast_config_AST_LOG_DIR);
471 ast_verbose("Asterisk Malloc Debugger Started (see %s))\n", filename);
473 if ((mmlog = fopen(filename, "a+"))) {
474 fprintf(mmlog, "%ld - New session\n", (long)time(NULL));