<p>Joshua Colp <strong>submitted</strong> this change.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/13169">View Change</a></p><div style="white-space:pre-wrap">Approvals:
Kevin Harwell: Looks good to me, but someone else must approve
Joshua Colp: Looks good to me, approved; Approved for Submit
</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">core: Improve MALLOC_DEBUG for frames.<br><br>* Pass caller information to frame allocation functions.<br>* Disable caching as it interfers with MALLOC_DEBUG reporting.<br>* Stop using ast_calloc_cache.<br><br>Change-Id: Id343cd80a3db941d2daefde2a060750fea8cd260<br>---<br>M channels/Makefile<br>M channels/iax2/parser.c<br>M include/asterisk/frame.h<br>M main/Makefile<br>M main/frame.c<br>5 files changed, 38 insertions(+), 26 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/channels/Makefile b/channels/Makefile</span><br><span>index 96421d8..b641173 100644</span><br><span>--- a/channels/Makefile</span><br><span>+++ b/channels/Makefile</span><br><span>@@ -24,6 +24,8 @@</span><br><span> endif</span><br><span> </span><br><span> $(call MOD_ADD_C,chan_iax2,$(wildcard iax2/*.c))</span><br><span style="color: hsl(120, 100%, 40%);">+iax2/parser.o: _ASTCFLAGS+=$(call get_menuselect_cflags,MALLOC_DEBUG)</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> $(call MOD_ADD_C,chan_sip,$(wildcard sip/*.c))</span><br><span> $(call MOD_ADD_C,chan_pjsip,$(wildcard pjsip/*.c))</span><br><span> $(call MOD_ADD_C,chan_dahdi,$(wildcard dahdi/*.c) sig_analog.c sig_pri.c sig_ss7.c)</span><br><span>diff --git a/channels/iax2/parser.c b/channels/iax2/parser.c</span><br><span>index b94d306..8683938 100644</span><br><span>--- a/channels/iax2/parser.c</span><br><span>+++ b/channels/iax2/parser.c</span><br><span>@@ -52,7 +52,11 @@</span><br><span> static int iframes = 0;</span><br><span> static int oframes = 0;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-#if !defined(LOW_MEMORY)</span><br><span style="color: hsl(120, 100%, 40%);">+#if (defined(LOW_MEMORY) || defined(MALLOC_DEBUG)) && !defined(NO_FRAME_CACHE)</span><br><span style="color: hsl(120, 100%, 40%);">+#define NO_FRAME_CACHE</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#if !defined(NO_FRAME_CACHE)</span><br><span> static void frame_cache_cleanup(void *data);</span><br><span> </span><br><span> /*! \brief A per-thread cache of iax_frame structures */</span><br><span>@@ -1215,7 +1219,7 @@</span><br><span> {</span><br><span> struct iax_frame *fr;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-#if !defined(LOW_MEMORY)</span><br><span style="color: hsl(120, 100%, 40%);">+#if !defined(NO_FRAME_CACHE)</span><br><span> if (cacheable) {</span><br><span> struct iax_frames *iax_frames;</span><br><span> struct iax_frame *smallest;</span><br><span>@@ -1243,13 +1247,13 @@</span><br><span> iax_frames->size--;</span><br><span> ast_free(smallest);</span><br><span> }</span><br><span style="color: hsl(0, 100%, 40%);">- if (!(fr = ast_calloc_cache(1, sizeof(*fr) + datalen))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!(fr = ast_calloc(1, sizeof(*fr) + datalen))) {</span><br><span> return NULL;</span><br><span> }</span><br><span> fr->afdatalen = datalen;</span><br><span> }</span><br><span> } else {</span><br><span style="color: hsl(0, 100%, 40%);">- if (!(fr = ast_calloc_cache(1, sizeof(*fr) + datalen))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!(fr = ast_calloc(1, sizeof(*fr) + datalen))) {</span><br><span> return NULL;</span><br><span> }</span><br><span> fr->afdatalen = datalen;</span><br><span>@@ -1280,7 +1284,7 @@</span><br><span> </span><br><span> void iax_frame_free(struct iax_frame *fr)</span><br><span> {</span><br><span style="color: hsl(0, 100%, 40%);">-#if !defined(LOW_MEMORY)</span><br><span style="color: hsl(120, 100%, 40%);">+#if !defined(NO_FRAME_CACHE)</span><br><span> struct iax_frames *iax_frames = NULL;</span><br><span> #endif</span><br><span> </span><br><span>@@ -1295,7 +1299,7 @@</span><br><span> }</span><br><span> ast_atomic_fetchadd_int(&frames, -1);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-#if !defined(LOW_MEMORY)</span><br><span style="color: hsl(120, 100%, 40%);">+#if !defined(NO_FRAME_CACHE)</span><br><span> if (!fr->cacheable</span><br><span> || !ast_opt_cache_media_frames</span><br><span> || !(iax_frames = ast_threadstorage_get(&frame_cache, sizeof(*iax_frames)))) {</span><br><span>@@ -1319,7 +1323,7 @@</span><br><span> ast_free(fr);</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-#if !defined(LOW_MEMORY)</span><br><span style="color: hsl(120, 100%, 40%);">+#if !defined(NO_FRAME_CACHE)</span><br><span> static void frame_cache_cleanup(void *data)</span><br><span> {</span><br><span> struct iax_frames *framelist = data;</span><br><span>diff --git a/include/asterisk/frame.h b/include/asterisk/frame.h</span><br><span>index b618d19..780cba3 100644</span><br><span>--- a/include/asterisk/frame.h</span><br><span>+++ b/include/asterisk/frame.h</span><br><span>@@ -560,14 +560,16 @@</span><br><span> * should be the last operation you do with that frame before freeing</span><br><span> * it (or exiting the block, if the frame is on the stack.)</span><br><span> */</span><br><span style="color: hsl(0, 100%, 40%);">-struct ast_frame *ast_frisolate(struct ast_frame *fr);</span><br><span style="color: hsl(120, 100%, 40%);">+#define ast_frisolate(fr) __ast_frisolate(fr, __FILE__, __LINE__, __PRETTY_FUNCTION__)</span><br><span style="color: hsl(120, 100%, 40%);">+struct ast_frame *__ast_frisolate(struct ast_frame *fr, const char *file, int line, const char *func);</span><br><span> </span><br><span> /*! \brief Copies a frame</span><br><span> * \param fr frame to copy</span><br><span> * Duplicates a frame -- should only rarely be used, typically frisolate is good enough</span><br><span> * \return Returns a frame on success, NULL on error</span><br><span> */</span><br><span style="color: hsl(0, 100%, 40%);">-struct ast_frame *ast_frdup(const struct ast_frame *fr);</span><br><span style="color: hsl(120, 100%, 40%);">+#define ast_frdup(fr) __ast_frdup(fr, __FILE__, __LINE__, __PRETTY_FUNCTION__)</span><br><span style="color: hsl(120, 100%, 40%);">+struct ast_frame *__ast_frdup(const struct ast_frame *fr, const char *file, int line, const char *func);</span><br><span> </span><br><span> void ast_swapcopy_samples(void *dst, const void *src, int samples);</span><br><span> </span><br><span>diff --git a/main/Makefile b/main/Makefile</span><br><span>index a22a1ad..299998c 100644</span><br><span>--- a/main/Makefile</span><br><span>+++ b/main/Makefile</span><br><span>@@ -159,6 +159,7 @@</span><br><span> crypt.o: _ASTCFLAGS+=$(CRYPT_INCLUDE)</span><br><span> db.o: _ASTCFLAGS+=$(SQLITE3_INCLUDE)</span><br><span> dsp.o: _ASTCFLAGS+=$(call get_menuselect_cflags,RADIO_RELAX)</span><br><span style="color: hsl(120, 100%, 40%);">+frame.o: _ASTCFLAGS+=$(call get_menuselect_cflags,MALLOC_DEBUG)</span><br><span> http.o: _ASTCFLAGS+=$(GMIMECFLAGS)</span><br><span> iostream.o: _ASTCFLAGS+=$(OPENSSL_INCLUDE)</span><br><span> json.o: _ASTCFLAGS+=$(JANSSON_INCLUDE)</span><br><span>diff --git a/main/frame.c b/main/frame.c</span><br><span>index 208c82d..f7a5222 100644</span><br><span>--- a/main/frame.c</span><br><span>+++ b/main/frame.c</span><br><span>@@ -43,7 +43,11 @@</span><br><span> #include "asterisk/dsp.h"</span><br><span> #include "asterisk/file.h"</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-#if !defined(LOW_MEMORY)</span><br><span style="color: hsl(120, 100%, 40%);">+#if (defined(LOW_MEMORY) || defined(MALLOC_DEBUG)) && !defined(NO_FRAME_CACHE)</span><br><span style="color: hsl(120, 100%, 40%);">+#define NO_FRAME_CACHE</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#if !defined(NO_FRAME_CACHE)</span><br><span> static void frame_cache_cleanup(void *data);</span><br><span> </span><br><span> /*! \brief A per-thread cache of frame headers */</span><br><span>@@ -72,11 +76,11 @@</span><br><span> </span><br><span> struct ast_frame ast_null_frame = { AST_FRAME_NULL, };</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-static struct ast_frame *ast_frame_header_new(void)</span><br><span style="color: hsl(120, 100%, 40%);">+static struct ast_frame *ast_frame_header_new(const char *file, int line, const char *func)</span><br><span> {</span><br><span> struct ast_frame *f;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-#if !defined(LOW_MEMORY)</span><br><span style="color: hsl(120, 100%, 40%);">+#if !defined(NO_FRAME_CACHE)</span><br><span> struct ast_frame_cache *frames;</span><br><span> </span><br><span> if ((frames = ast_threadstorage_get(&frame_cache, sizeof(*frames)))) {</span><br><span>@@ -89,19 +93,18 @@</span><br><span> return f;</span><br><span> }</span><br><span> }</span><br><span style="color: hsl(0, 100%, 40%);">- if (!(f = ast_calloc_cache(1, sizeof(*f))))</span><br><span style="color: hsl(0, 100%, 40%);">- return NULL;</span><br><span style="color: hsl(0, 100%, 40%);">-#else</span><br><span style="color: hsl(0, 100%, 40%);">- if (!(f = ast_calloc(1, sizeof(*f))))</span><br><span style="color: hsl(0, 100%, 40%);">- return NULL;</span><br><span> #endif</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+ if (!(f = __ast_calloc(1, sizeof(*f), file, line, func))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> f->mallocd_hdr_len = sizeof(*f);</span><br><span> </span><br><span> return f;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-#if !defined(LOW_MEMORY)</span><br><span style="color: hsl(120, 100%, 40%);">+#if !defined(NO_FRAME_CACHE)</span><br><span> static void frame_cache_cleanup(void *data)</span><br><span> {</span><br><span> struct ast_frame_cache *frames = data;</span><br><span>@@ -119,7 +122,7 @@</span><br><span> if (!fr->mallocd)</span><br><span> return;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-#if !defined(LOW_MEMORY)</span><br><span style="color: hsl(120, 100%, 40%);">+#if !defined(NO_FRAME_CACHE)</span><br><span> if (fr->mallocd == AST_MALLOCD_HDR</span><br><span> && cache</span><br><span> && ast_opt_cache_media_frames) {</span><br><span>@@ -185,7 +188,7 @@</span><br><span> * (header, src, data).</span><br><span> * On return all components are malloc'ed</span><br><span> */</span><br><span style="color: hsl(0, 100%, 40%);">-struct ast_frame *ast_frisolate(struct ast_frame *fr)</span><br><span style="color: hsl(120, 100%, 40%);">+struct ast_frame *__ast_frisolate(struct ast_frame *fr, const char *file, int line, const char *func)</span><br><span> {</span><br><span> struct ast_frame *out;</span><br><span> void *newdata;</span><br><span>@@ -194,7 +197,7 @@</span><br><span> since it is more efficient</span><br><span> */</span><br><span> if (fr->mallocd == 0) {</span><br><span style="color: hsl(0, 100%, 40%);">- return ast_frdup(fr);</span><br><span style="color: hsl(120, 100%, 40%);">+ return __ast_frdup(fr, file, line, func);</span><br><span> }</span><br><span> </span><br><span> /* if everything is already malloc'd, we are done */</span><br><span>@@ -205,7 +208,7 @@</span><br><span> </span><br><span> if (!(fr->mallocd & AST_MALLOCD_HDR)) {</span><br><span> /* Allocate a new header if needed */</span><br><span style="color: hsl(0, 100%, 40%);">- if (!(out = ast_frame_header_new())) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!(out = ast_frame_header_new(file, line, func))) {</span><br><span> return NULL;</span><br><span> }</span><br><span> out->frametype = fr->frametype;</span><br><span>@@ -291,13 +294,13 @@</span><br><span> return out;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-struct ast_frame *ast_frdup(const struct ast_frame *f)</span><br><span style="color: hsl(120, 100%, 40%);">+struct ast_frame *__ast_frdup(const struct ast_frame *f, const char *file, int line, const char *func)</span><br><span> {</span><br><span> struct ast_frame *out = NULL;</span><br><span> int len, srclen = 0;</span><br><span> void *buf = NULL;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-#if !defined(LOW_MEMORY)</span><br><span style="color: hsl(120, 100%, 40%);">+#if !defined(NO_FRAME_CACHE)</span><br><span> struct ast_frame_cache *frames;</span><br><span> #endif</span><br><span> </span><br><span>@@ -313,7 +316,7 @@</span><br><span> if (srclen > 0)</span><br><span> len += srclen + 1;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-#if !defined(LOW_MEMORY)</span><br><span style="color: hsl(120, 100%, 40%);">+#if !defined(NO_FRAME_CACHE)</span><br><span> if ((frames = ast_threadstorage_get(&frame_cache, sizeof(*frames)))) {</span><br><span> AST_LIST_TRAVERSE_SAFE_BEGIN(&frames->list, out, frame_list) {</span><br><span> if (out->mallocd_hdr_len >= len) {</span><br><span>@@ -332,7 +335,7 @@</span><br><span> #endif</span><br><span> </span><br><span> if (!buf) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (!(buf = ast_calloc_cache(1, len)))</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!(buf = __ast_calloc(1, len, file, line, func)))</span><br><span> return NULL;</span><br><span> out = buf;</span><br><span> out->mallocd_hdr_len = len;</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/13169">change 13169</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/c/asterisk/+/13169"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: Id343cd80a3db941d2daefde2a060750fea8cd260 </div>
<div style="display:none"> Gerrit-Change-Number: 13169 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Corey Farrell <git@cfware.com> </div>
<div style="display:none"> Gerrit-Reviewer: Friendly Automation </div>
<div style="display:none"> Gerrit-Reviewer: Joshua Colp <jcolp@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Kevin Harwell <kharwell@digium.com> </div>
<div style="display:none"> Gerrit-MessageType: merged </div>