aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkpfleming <kpfleming@f38db490-d61c-443f-a65b-d21fe96a405b>2006-12-27 18:29:13 +0000
committerkpfleming <kpfleming@f38db490-d61c-443f-a65b-d21fe96a405b>2006-12-27 18:29:13 +0000
commit1690223ee5606043a8f768529d35eb52da1b5ea4 (patch)
tree26aeb8f817503c38e44f77aa2ab8ee05d2caa403
parentb0241a8bab4f7dbeba8ca3b824c2e5933e8842eb (diff)
allow 'show memory' and 'show memory summary' to distinguish memory allocations that were done for caching purposes, so they don't look like memory leaks
git-svn-id: http://svn.digium.com/svn/asterisk/branches/1.4@48987 f38db490-d61c-443f-a65b-d21fe96a405b
-rw-r--r--channels/iax2-parser.c2
-rw-r--r--include/asterisk/astmm.h4
-rw-r--r--include/asterisk/utils.h13
-rw-r--r--main/astmm.c73
-rw-r--r--main/frame.c4
5 files changed, 75 insertions, 21 deletions
diff --git a/channels/iax2-parser.c b/channels/iax2-parser.c
index 0a524e253..bb030d3d2 100644
--- a/channels/iax2-parser.c
+++ b/channels/iax2-parser.c
@@ -957,7 +957,7 @@ struct iax_frame *iax_frame_new(int direction, int datalen)
}
if (!fr) {
- if (!(fr = ast_calloc(1, sizeof(*fr) + datalen)))
+ if (!(fr = ast_calloc_cache(1, sizeof(*fr) + datalen)))
return NULL;
fr->mallocd_datalen = datalen;
}
diff --git a/include/asterisk/astmm.h b/include/asterisk/astmm.h
index 7e987b099..f1f838ef0 100644
--- a/include/asterisk/astmm.h
+++ b/include/asterisk/astmm.h
@@ -44,6 +44,7 @@
#undef vasprintf
void *__ast_calloc(size_t nmemb, size_t size, const char *file, int lineno, const char *func);
+void *__ast_calloc_cache(size_t nmemb, size_t size, const char *file, int lineno, const char *func);
void *__ast_malloc(size_t size, const char *file, int lineno, const char *func);
void __ast_free(void *ptr, const char *file, int lineno, const char *func);
void *__ast_realloc(void *ptr, size_t size, const char *file, int lineno, const char *func);
@@ -59,6 +60,9 @@ void __ast_mm_init(void);
#define calloc(a,b) \
__ast_calloc(a,b,__FILE__, __LINE__, __PRETTY_FUNCTION__)
+#define ast_calloc_cache(a,b) \
+ __ast_calloc_cache(a,b,__FILE__, __LINE__, __PRETTY_FUNCTION__)
+
#define malloc(a) \
__ast_malloc(a,__FILE__, __LINE__, __PRETTY_FUNCTION__)
diff --git a/include/asterisk/utils.h b/include/asterisk/utils.h
index 3fb817980..0322e506b 100644
--- a/include/asterisk/utils.h
+++ b/include/asterisk/utils.h
@@ -362,6 +362,19 @@ void * attribute_malloc _ast_calloc(size_t num, size_t len, const char *file, in
)
/*!
+ * \brief A wrapper for calloc() for use in cache pools
+ *
+ * ast_calloc_cache() is a wrapper for calloc() that will generate an Asterisk log
+ * message in the case that the allocation fails. When memory debugging is in use,
+ * the memory allocated by this function will be marked as 'cache' so it can be
+ * distinguished from normal memory allocations.
+ *
+ * The arguments and return value are the same as calloc()
+ */
+#define ast_calloc_cache(num, len) \
+ _ast_calloc((num), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
+
+/*!
* \brief A wrapper for realloc()
*
* ast_realloc() is a wrapper for realloc() that will generate an Asterisk log
diff --git a/main/astmm.c b/main/astmm.c
index 55c80f618..40d7502cd 100644
--- a/main/astmm.c
+++ b/main/astmm.c
@@ -72,6 +72,7 @@ static struct ast_region {
char func[40];
unsigned int lineno;
enum func_type which;
+ unsigned int cache; /* region was allocated as part of a cache pool */
size_t len;
unsigned int fence;
unsigned char data[0];
@@ -92,7 +93,7 @@ AST_MUTEX_DEFINE_STATIC(showmemorylock);
} \
} while (0)
-static inline void *__ast_alloc_region(size_t size, const enum func_type which, const char *file, int lineno, const char *func)
+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)
{
struct ast_region *reg;
void *ptr = NULL;
@@ -101,7 +102,7 @@ static inline void *__ast_alloc_region(size_t size, const enum func_type which,
if (!(reg = malloc(size + sizeof(*reg) + sizeof(*fence)))) {
astmm_log("Memory Allocation Failure - '%d' bytes in function %s "
- "at line %d of %s\n", (int) size, func, lineno, file);
+ "at line %d of %s\n", (int) size, func, lineno, file);
}
ast_copy_string(reg->file, file, sizeof(reg->file));
@@ -109,6 +110,7 @@ static inline void *__ast_alloc_region(size_t size, const enum func_type which,
reg->lineno = lineno;
reg->len = size;
reg->which = which;
+ reg->cache = cache;
ptr = reg->data;
hash = HASH(ptr);
reg->fence = FENCE_MAGIC;
@@ -181,7 +183,17 @@ void *__ast_calloc(size_t nmemb, size_t size, const char *file, int lineno, cons
{
void *ptr;
- if ((ptr = __ast_alloc_region(size * nmemb, FUNC_CALLOC, file, lineno, func)))
+ if ((ptr = __ast_alloc_region(size * nmemb, FUNC_CALLOC, file, lineno, func, 0)))
+ memset(ptr, 0, size * nmemb);
+
+ return ptr;
+}
+
+void *__ast_calloc_cache(size_t nmemb, size_t size, const char *file, int lineno, const char *func)
+{
+ void *ptr;
+
+ if ((ptr = __ast_alloc_region(size * nmemb, FUNC_CALLOC, file, lineno, func, 1)))
memset(ptr, 0, size * nmemb);
return ptr;
@@ -189,7 +201,7 @@ void *__ast_calloc(size_t nmemb, size_t size, const char *file, int lineno, cons
void *__ast_malloc(size_t size, const char *file, int lineno, const char *func)
{
- return __ast_alloc_region(size, FUNC_MALLOC, file, lineno, func);
+ return __ast_alloc_region(size, FUNC_MALLOC, file, lineno, func, 0);
}
void __ast_free(void *ptr, const char *file, int lineno, const char *func)
@@ -208,7 +220,7 @@ void *__ast_realloc(void *ptr, size_t size, const char *file, int lineno, const
return NULL;
}
- if (!(tmp = __ast_alloc_region(size, FUNC_REALLOC, file, lineno, func)))
+ if (!(tmp = __ast_alloc_region(size, FUNC_REALLOC, file, lineno, func, 0)))
return NULL;
if (len > size)
@@ -230,7 +242,7 @@ char *__ast_strdup(const char *s, const char *file, int lineno, const char *func
return NULL;
len = strlen(s) + 1;
- if ((ptr = __ast_alloc_region(len, FUNC_STRDUP, file, lineno, func)))
+ if ((ptr = __ast_alloc_region(len, FUNC_STRDUP, file, lineno, func, 0)))
strcpy(ptr, s);
return ptr;
@@ -247,7 +259,7 @@ char *__ast_strndup(const char *s, size_t n, const char *file, int lineno, const
len = strlen(s) + 1;
if (len > n)
len = n;
- if ((ptr = __ast_alloc_region(len, FUNC_STRNDUP, file, lineno, func)))
+ if ((ptr = __ast_alloc_region(len, FUNC_STRNDUP, file, lineno, func, 0)))
strcpy(ptr, s);
return ptr;
@@ -264,7 +276,7 @@ int __ast_asprintf(const char *file, int lineno, const char *func, char **strp,
va_copy(ap2, ap);
size = vsnprintf(&s, 1, fmt, ap2);
va_end(ap2);
- if (!(*strp = __ast_alloc_region(size + 1, FUNC_ASPRINTF, file, lineno, func))) {
+ if (!(*strp = __ast_alloc_region(size + 1, FUNC_ASPRINTF, file, lineno, func, 0))) {
va_end(ap);
return -1;
}
@@ -284,7 +296,7 @@ int __ast_vasprintf(char **strp, const char *fmt, va_list ap, const char *file,
va_copy(ap2, ap);
size = vsnprintf(&s, 1, fmt, ap2);
va_end(ap2);
- if (!(*strp = __ast_alloc_region(size + 1, FUNC_VASPRINTF, file, lineno, func))) {
+ if (!(*strp = __ast_alloc_region(size + 1, FUNC_VASPRINTF, file, lineno, func, 0))) {
va_end(ap);
return -1;
}
@@ -299,6 +311,7 @@ static int handle_show_memory(int fd, int argc, char *argv[])
struct ast_region *reg;
unsigned int x;
unsigned int len = 0;
+ unsigned int cache_len = 0;
unsigned int count = 0;
unsigned int *fence;
@@ -321,16 +334,22 @@ static int handle_show_memory(int fd, int argc, char *argv[])
}
}
if (!fn || !strcasecmp(fn, reg->file)) {
- ast_cli(fd, "%10d bytes allocated in %20s at line %5d of %s\n",
- (int) reg->len, reg->func, reg->lineno, reg->file);
+ ast_cli(fd, "%10d bytes allocated%s in %20s at line %5d of %s\n",
+ (int) reg->len, reg->cache ? " (cache)" : "",
+ reg->func, reg->lineno, reg->file);
len += reg->len;
+ if (reg->cache)
+ cache_len += reg->len;
count++;
}
}
}
ast_mutex_unlock(&showmemorylock);
- ast_cli(fd, "%d bytes allocated %d units total\n", len, count);
+ if (cache_len)
+ ast_cli(fd, "%d bytes allocated (%d in caches) in %d allocations", len, cache_len, count);
+ else
+ ast_cli(fd, "%d bytes allocated in %d allocations\n", len, count);
return RESULT_SUCCESS;
}
@@ -341,10 +360,12 @@ static int handle_show_memory_summary(int fd, int argc, char *argv[])
int x;
struct ast_region *reg;
unsigned int len = 0;
+ unsigned int cache_len = 0;
int count = 0;
struct file_summary {
char fn[80];
int len;
+ int cache_len;
int count;
struct file_summary *next;
} *list = NULL, *cur;
@@ -371,6 +392,8 @@ static int handle_show_memory_summary(int fd, int argc, char *argv[])
}
cur->len += reg->len;
+ if (reg->cache)
+ cur->cache_len += reg->len;
cur->count++;
}
}
@@ -379,17 +402,31 @@ static int handle_show_memory_summary(int fd, int argc, char *argv[])
/* Dump the whole list */
for (cur = list; cur; cur = cur->next) {
len += cur->len;
+ cache_len += cur->cache_len;
count += cur->count;
- if (fn) {
- ast_cli(fd, "%10d bytes in %5d allocations in function '%s' of '%s'\n",
- cur->len, cur->count, cur->fn, fn);
+ if (cur->cache_len) {
+ if (fn) {
+ ast_cli(fd, "%10d bytes (%10d cache) in %d allocations in function '%s' of '%s'\n",
+ cur->len, cur->cache_len, cur->count, cur->fn, fn);
+ } else {
+ ast_cli(fd, "%10d bytes (%10d cache) in %d allocations in file '%s'\n",
+ cur->len, cur->cache_len, cur->count, cur->fn);
+ }
} else {
- ast_cli(fd, "%10d bytes in %5d allocations in file '%s'\n",
- cur->len, cur->count, cur->fn);
+ if (fn) {
+ ast_cli(fd, "%10d bytes in %d allocations in function '%s' of '%s'\n",
+ cur->len, cur->count, cur->fn, fn);
+ } else {
+ ast_cli(fd, "%10d bytes in %d allocations in file '%s'\n",
+ cur->len, cur->count, cur->fn);
+ }
}
}
- ast_cli(fd, "%d bytes allocated %d units total\n", len, count);
+ if (cache_len)
+ ast_cli(fd, "%d bytes allocated (%d in caches) in %d allocations", len, cache_len, count);
+ else
+ ast_cli(fd, "%d bytes allocated in %d allocations\n", len, count);
return RESULT_SUCCESS;
}
diff --git a/main/frame.c b/main/frame.c
index beb141d52..a86e291ea 100644
--- a/main/frame.c
+++ b/main/frame.c
@@ -297,7 +297,7 @@ static struct ast_frame *ast_frame_header_new(void)
}
}
- if (!(f = ast_calloc(1, sizeof(*f))))
+ if (!(f = ast_calloc_cache(1, sizeof(*f))))
return NULL;
f->mallocd_hdr_len = sizeof(*f);
@@ -454,7 +454,7 @@ struct ast_frame *ast_frdup(const struct ast_frame *f)
AST_LIST_TRAVERSE_SAFE_END
}
if (!buf) {
- if (!(buf = ast_calloc(1, len)))
+ if (!(buf = ast_calloc_cache(1, len)))
return NULL;
out = buf;
out->mallocd_hdr_len = len;