diff --git a/Makefile.in b/Makefile.in index c497d2d2..a3a9ed59 100755 --- a/Makefile.in +++ b/Makefile.in @@ -20,6 +20,7 @@ SRCS= $(NACL_SOURCES) $(SERVAL_SOURCES) MONITORCLIENTSRCS=conf.c \ log.c \ mkdir.c \ + mem.c \ monitor-client.c \ net.c \ str.c \ @@ -29,6 +30,7 @@ MONITORCLIENTSRCS=conf.c \ MDPCLIENTSRCS=conf.c \ dataformats.c \ mkdir.c \ + mem.c \ log.c \ mdp_client.c \ net.c \ diff --git a/headerfiles.mk b/headerfiles.mk index 3e37bc92..db18eac6 100644 --- a/headerfiles.mk +++ b/headerfiles.mk @@ -6,6 +6,7 @@ HDRS= fifo.h \ rhizome.h \ serval.h \ str.h \ + mem.h \ strbuf.h \ strbuf_helpers.h \ sha2.h \ diff --git a/mem.c b/mem.c index 86d74dab..180e2fe8 100644 --- a/mem.c +++ b/mem.c @@ -52,3 +52,30 @@ char *_str_edup(struct __sourceloc __whence, const char *str) { return _strn_edup(__whence, str, strlen(str)); } + +#undef malloc +#undef calloc +#undef free +#undef realloc + +#define SDM_GUARD_AFTER 16384 + +void *_serval_debug_malloc(unsigned int bytes, struct __sourceloc __whence) +{ + void *r=malloc(bytes+SDM_GUARD_AFTER); + DEBUGF("malloc(%d) -> %p", bytes, r); + return r; +} + +void *_serval_debug_calloc(unsigned int bytes, unsigned int count, struct __sourceloc __whence) +{ + void *r=calloc((bytes*count)+SDM_GUARD_AFTER,1); + DEBUGF("calloc(%d,%d) -> %p", bytes, count, r); + return r; +} + +void _serval_debug_free(void *p, struct __sourceloc __whence) +{ + free(p); + DEBUGF("free(%p)", p); +} diff --git a/mem.h b/mem.h index 0d4132df..7fb40f74 100644 --- a/mem.h +++ b/mem.h @@ -23,6 +23,17 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include #include "log.h" +// #define MALLOC_PARANOIA + +#ifdef MALLOC_PARANOIA +#define malloc(X) _serval_debug_malloc(X,__WHENCE__) +#define calloc(X,Y) _serval_debug_calloc(X,Y,__WHENCE__) +#define free(X) _serval_debug_free(X,__WHENCE__) +void *_serval_debug_malloc(unsigned int bytes, struct __sourceloc whence); +void *_serval_debug_calloc(unsigned int bytes, unsigned int count, struct __sourceloc whence); +void _serval_debug_free(void *p, struct __sourceloc whence); +#endif + /* Equivalent to malloc(3), but logs an error before returning NULL. * * @author Andrew Bettison diff --git a/overlay_buffer.c b/overlay_buffer.c index d1242e17..84f7ac88 100644 --- a/overlay_buffer.c +++ b/overlay_buffer.c @@ -17,6 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +#include "mem.h" #include "serval.h" #include "overlay_buffer.h" @@ -453,29 +454,3 @@ int ob_dump(struct overlay_buffer *b,char *desc) dump(NULL, b->bytes, b->position); return 0; } - -#undef malloc -#undef calloc -#undef free -#undef realloc - -#define SDM_GUARD_AFTER 16384 -void *_serval_debug_malloc(unsigned int bytes, struct __sourceloc __whence) -{ - void *r=malloc(bytes+SDM_GUARD_AFTER); - DEBUGF("malloc(%d) -> %p", bytes, r); - return r; -} - -void *_serval_debug_calloc(unsigned int bytes, unsigned int count, struct __sourceloc __whence) -{ - void *r=calloc((bytes*count)+SDM_GUARD_AFTER,1); - DEBUGF("calloc(%d,%d) -> %p", bytes, count, r); - return r; -} - -void _serval_debug_free(void *p, struct __sourceloc __whence) -{ - free(p); - DEBUGF("free(%p)", p); -} diff --git a/serval.h b/serval.h index c2031a25..8d9b7db2 100644 --- a/serval.h +++ b/serval.h @@ -21,8 +21,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #ifndef __SERVALD_SERVALD_H #define __SERVALD_SERVALD_H -// #define MALLOC_PARANOIA - #include #include #include @@ -112,6 +110,7 @@ struct in_addr { #include #include "constants.h" +#include "mem.h" #include "xprintf.h" #include "log.h" #include "net.h" @@ -623,17 +622,6 @@ int dump_payload(struct overlay_frame *p, char *message); int urandombytes(unsigned char *x,unsigned long long xlen); -#ifdef MALLOC_PARANOIA -#define malloc(X) _serval_debug_malloc(X,__WHENCE__) -#define calloc(X,Y) _serval_debug_calloc(X,Y,__WHENCE__) -#define free(X) _serval_debug_free(X,__WHENCE__) - -void *_serval_debug_malloc(unsigned int bytes, struct __sourceloc whence); -void *_serval_debug_calloc(unsigned int bytes, unsigned int count, struct __sourceloc whence); -void _serval_debug_free(void *p, struct __sourceloc whence); -#endif - - struct vomp_call_state; void set_codec_flag(int codec, unsigned char *flags); diff --git a/sourcefiles.mk b/sourcefiles.mk index 4cc6281c..4f02b3b3 100644 --- a/sourcefiles.mk +++ b/sourcefiles.mk @@ -17,6 +17,7 @@ SERVAL_SOURCES = $(SERVAL_BASE)audiodevices.c \ $(SERVAL_BASE)main.c \ $(SERVAL_BASE)mdp_client.c \ $(SERVAL_BASE)mkdir.c \ + $(SERVAL_BASE)mem.c \ $(SERVAL_BASE)monitor.c \ $(SERVAL_BASE)monitor-client.c \ $(SERVAL_BASE)monitor-cli.c \