[Asterisk-code-review] pjproject bundled: Add MALLOC DEBUG capability (asterisk[13])

George Joseph asteriskteam at digium.com
Wed Oct 5 15:11:47 CDT 2016


George Joseph has uploaded a new change for review.

  https://gerrit.asterisk.org/4030

Change subject: pjproject_bundled:  Add MALLOC_DEBUG capability
......................................................................

pjproject_bundled:  Add MALLOC_DEBUG capability

pjproject_bundled will now use the asterisk memory debugging APIs
if MALLOC_DEBUG is turned on in menuselect.

Change-Id: Icc5e3d658fbfb00e0a46b44c66dcc2522d5171b0
---
M third-party/pjproject/Makefile
A third-party/pjproject/patches/asterisk_malloc_debug.h
M third-party/pjproject/patches/config_site.h
3 files changed, 152 insertions(+), 6 deletions(-)


  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/30/4030/1

diff --git a/third-party/pjproject/Makefile b/third-party/pjproject/Makefile
index 7349db6..3147a9c 100644
--- a/third-party/pjproject/Makefile
+++ b/third-party/pjproject/Makefile
@@ -43,8 +43,6 @@
         include build.mak
         CF := $(filter-out -W%,$(CC_CFLAGS))
         CF := $(filter-out -I%,$(CF))
-        export CFLAGS += $(CF)
-        export LDFLAGS += $(CC_LDFLAGS)
         TARGETS := pjproject.symbols
         ifeq ($(findstring TEST_FRAMEWORK,$(MENUSELECT_CFLAGS)),TEST_FRAMEWORK)
             TARGETS += source/pjsip-apps/bin/pjsua-$(TARGET_NAME)
@@ -52,12 +50,18 @@
                 TARGETS += source/pjsip-apps/src/python/build/_pjsua.so
             endif
         endif
+        ifeq ($(findstring MALLOC_DEBUG,$(MENUSELECT_CFLAGS)),MALLOC_DEBUG)
+            CF += -DMALLOC_DEBUG
+        endif
+        export CFLAGS += $(CF)
+        export LDFLAGS += $(CC_LDFLAGS)
     else
         all install:
     endif
 endif
 
 ECHO_PREFIX := $(ECHO_PREFIX) echo '[pjproject] '
+MULDEFS=-Wl,-z,muldefs
 
 ifndef $(TMPDIR)
     ifneq ($(wildcard /tmp),)
@@ -84,11 +88,11 @@
 	$(ECHO_PREFIX) Applying user.mak
 	$(CMD_PREFIX) cp -f ./patches/user.mak ./source/
 
-source/pjlib/include/pj/config_site.h: source/.unpacked ./patches/config_site.h
-	$(ECHO_PREFIX) Applying config_site.h
-	$(CMD_PREFIX) cp -f ./patches/config_site.h ./source/pjlib/include/pj/
+source/pjlib/include/pj/%.h : ./patches/%.h
+	$(ECHO_PREFIX) Applying custom include file $<
+	$(CMD_PREFIX) cp -f $< ./source/pjlib/include/pj/
 
-build.mak: source/.unpacked source/pjlib/include/pj/config_site.h source/user.mak Makefile.rules
+build.mak: source/.unpacked $(addprefix source/pjlib/include/pj/,$(notdir $(wildcard ./patches/*.h))) source/user.mak Makefile.rules
 	$(ECHO_PREFIX) Configuring with $(PJPROJECT_CONFIG_OPTS)
 	$(CMD_PREFIX) (cd source ; autoconf aconfigure.ac > aconfigure && ./aconfigure $(QUIET_CONFIGURE) $(PJPROJECT_CONFIG_OPTS))
 	@sed -r -e "/prefix|export PJ_SHARED_LIBRARIES|MACHINE_NAME|OS_NAME|HOST_NAME|CC_NAME|CROSS_COMPILE|LINUX_POLL/d" source/build.mak > build.mak
@@ -114,6 +118,7 @@
 	$(ECHO_PREFIX) Generating symbols
 	$(CMD_PREFIX) nm -Pog $(PJ_LIB_FILES) | sed -n -r -e "s/.+: ([pP][jJ][^ ]+) .+/\1/gp" | sort -u > pjproject.symbols
 
+source/pjsip-apps/bin/pjsua-$(TARGET_NAME): LDFLAGS += $(if $(findstring MALLOC_DEBUG,$(MENUSELECT_CFLAGS)),$(MULDEFS))
 source/pjsip-apps/bin/pjsua-$(TARGET_NAME): source/pjlib/lib/libpj-$(TARGET_NAME).a
 	$(ECHO_PREFIX) Compiling apps
 	$(CMD_PREFIX) $(SUBMAKE) -C source/pjsip-apps/build pjsua pjsystest $(REALLY_QUIET)
diff --git a/third-party/pjproject/patches/asterisk_malloc_debug.h b/third-party/pjproject/patches/asterisk_malloc_debug.h
new file mode 100644
index 0000000..8733aaa
--- /dev/null
+++ b/third-party/pjproject/patches/asterisk_malloc_debug.h
@@ -0,0 +1,133 @@
+/*
+ * Copyright (C) 2016 George Joseph <gjoseph at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+#ifndef ASTERISK_MALLOC_DEBUG_H_
+#define ASTERISK_MALLOC_DEBUG_H_
+
+/* Include these now to prevent them from messing up MALLOC_DEBUG */
+#include <sys/types.h>
+#include <pj/compat/string.h>
+#include <pj/compat/stdarg.h>
+#include <pj/compat/malloc.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int __ast_asprintf(const char *file, int lineno, const char *func, char **strp, const char *format, ...)
+	__attribute__((format(printf, 5, 6)));
+void *__ast_calloc(size_t nmemb, 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_malloc(size_t size, const char *file, int lineno, const char *func);
+void *__ast_realloc(void *ptr, size_t size, const char *file, int lineno, const char *func);
+char *__ast_strdup(const char *s, const char *file, int lineno, const char *func);
+char *__ast_strndup(const char *s, size_t n, const char *file, int lineno, const char *func);
+int __ast_vasprintf(char **strp, const char *format, va_list ap, const char *file, int lineno, const char *func)
+	__attribute__((format(printf, 2, 0)));
+
+/*
+ * We only want the debug APIs defined if we're compiling the pjsua
+ * and pjsystest applications.  The pjproject libraries will get them from asterisk.
+ */
+#if defined(__PJSUA_APP_H__) || defined(__SYSTEST_H__)
+
+int __ast_asprintf(const char *file, int lineno, const char *func, char **strp, const char *format, ...)
+{
+	va_list ap;
+	int rc = 0;
+
+	va_start(ap, format);
+	rc = vasprintf(strp, format, ap);
+	va_end(ap);
+
+	return rc;
+}
+
+void *__ast_calloc(size_t nmemb, size_t size, const char *file, int lineno, const char *func)
+{
+	return calloc(nmemb, size);
+}
+
+void __ast_free(void *ptr, const char *file, int lineno, const char *func)
+{
+	free(ptr);
+}
+
+void *__ast_malloc(size_t size, const char *file, int lineno, const char *func)
+{
+	return malloc(size);
+}
+
+void *__ast_realloc(void *ptr, size_t size, const char *file, int lineno, const char *func)
+{
+	return realloc(ptr, size);
+}
+
+char *__ast_strdup(const char *s, const char *file, int lineno, const char *func)
+{
+	return strdup(s);
+}
+
+char *__ast_strndup(const char *s, size_t n, const char *file, int lineno, const char *func)
+{
+	return strndup(s, n);
+}
+
+int __ast_vasprintf(char **strp, const char *format, va_list ap, const char *file, int lineno, const char *func)
+{
+	return vasprintf(strp, format, ap);
+}
+
+#endif /* defined(__PJSUA_APP_H__) || defined(__SYSTEST_H__) */
+
+/* Undefine any macros */
+#undef asprintf
+#undef calloc
+#undef free
+#undef malloc
+#undef realloc
+#undef strdup
+#undef strndup
+#undef vasprintf
+
+ /* Provide our own definitions */
+#define asprintf(a, b, c...) \
+	__ast_asprintf(__FILE__, __LINE__, __PRETTY_FUNCTION__, a, b, c)
+
+#define calloc(a,b) \
+	__ast_calloc(a,b,__FILE__, __LINE__, __PRETTY_FUNCTION__)
+
+#define free(a) \
+	__ast_free(a,__FILE__, __LINE__, __PRETTY_FUNCTION__)
+
+#define malloc(a) \
+	__ast_malloc(a,__FILE__, __LINE__, __PRETTY_FUNCTION__)
+
+#define realloc(a,b) \
+	__ast_realloc(a,b,__FILE__, __LINE__, __PRETTY_FUNCTION__)
+
+#define strdup(a) \
+	__ast_strdup(a,__FILE__, __LINE__, __PRETTY_FUNCTION__)
+
+#define strndup(a,b) \
+	__ast_strndup(a,b,__FILE__, __LINE__, __PRETTY_FUNCTION__)
+
+#define vasprintf(a,b,c) \
+	__ast_vasprintf(a,b,c,__FILE__, __LINE__, __PRETTY_FUNCTION__)
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* ASTERISK_MALLOC_DEBUG_H_ */
diff --git a/third-party/pjproject/patches/config_site.h b/third-party/pjproject/patches/config_site.h
index f9f76dc..0694f12 100644
--- a/third-party/pjproject/patches/config_site.h
+++ b/third-party/pjproject/patches/config_site.h
@@ -5,6 +5,14 @@
 #include <sys/select.h>
 
 /*
+ * Since both pjproject and asterisk source files will include config_site.h,
+ * we need to make sure that only pjproject source files include asterisk_malloc_debug.h.
+ */
+#if defined(MALLOC_DEBUG) && !defined(_ASTERISK_ASTMM_H)
+#include "asterisk_malloc_debug.h"
+#endif
+
+/*
  * Defining PJMEDIA_HAS_SRTP to 0 does NOT disable Asterisk's ability to use srtp.
  * It only disables the pjmedia srtp transport which Asterisk doesn't use.
  * The reason for the disable is that while Asterisk works fine with older libsrtp

-- 
To view, visit https://gerrit.asterisk.org/4030
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Icc5e3d658fbfb00e0a46b44c66dcc2522d5171b0
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: 13
Gerrit-Owner: George Joseph <gjoseph at digium.com>



More information about the asterisk-code-review mailing list