From 1e9b70919573f1e7f6729dbb94d15e9a9f920fbc Mon Sep 17 00:00:00 2001 From: Jason Endo Date: Wed, 24 Aug 2011 09:02:19 -0700 Subject: [PATCH 1/5] got rid of some trailing white space --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4edb53b..52415d5 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ The overhead to allocation should be small. Because it hooks malloc, all allocations that occur with a library also use the library. At this time, we have only tested it on 32 bit builds. -We developed and used this in production at Brightroll.com. +We developed and used this in production at Brightroll.com. ## license From 699fa016df91c7b28852a6201d2b9e848dffd5b7 Mon Sep 17 00:00:00 2001 From: Martin Brown Date: Mon, 25 Jun 2012 19:20:30 +0000 Subject: [PATCH 2/5] Correcting/Improving some printf formatting strings --- memalloc.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/memalloc.c b/memalloc.c index 937b4b8..e416f7e 100644 --- a/memalloc.c +++ b/memalloc.c @@ -96,11 +96,11 @@ memalloc_alloc(void * arena, char * type, char clear, size_t size) memalloc_stats_alloc++; - DBGLOG("> memalloc_alloc %s %d %d", type, clear, size); + DBGLOG("> memalloc_alloc %s %d %zu", type, clear, size); if (size == 0) { - DBGLOG("< memalloc_alloc 0x%08x", size); + DBGLOG("< memalloc_alloc %zu", size); return NULL; } @@ -135,7 +135,7 @@ memalloc_alloc(void * arena, char * type, char clear, size_t size) if (result > memalloc_hi_mem) memalloc_hi_mem = result; - DBGLOG("< memalloc_alloc 0x%08x", (uint) result); + DBGLOG("< memalloc_alloc %p", result); return result; } @@ -155,7 +155,7 @@ memalloc_free(void * arena, char * type, void * ptr) struct memalloc_header * hdr = (struct memalloc_header *) p; if (hdr->border != '|') { - DBGLOG("> memalloc_free 0x%08x - BAD/LEGACY FREE", (uint) ptr); + DBGLOG("> memalloc_free %p - BAD/LEGACY FREE", ptr); __free_hook = old_free_hook; free(ptr); old_free_hook = __free_hook; @@ -163,9 +163,9 @@ memalloc_free(void * arena, char * type, void * ptr) } else { - DBGLOG("> memalloc_free 0x%08x - %s - %d", (uint) ptr, hdr->type, hdr->size); + DBGLOG("> memalloc_free %p - %s - %d", ptr, hdr->type, hdr->size); if (hdr->flags == 'F') - ERRLOG("* memalloc_free DOUBLE 0x%08x - %s - %d", (uint) ptr, hdr->type, hdr->size); + ERRLOG("* memalloc_free DOUBLE %p - %s - %d", ptr, hdr->type, hdr->size); hdr->flags = 'F'; __free_hook = old_free_hook; free(p); @@ -203,24 +203,24 @@ __wrap_malloc(size_t size, const void * caller) { if (memalloc_init_state == 0) do_init(); - DBGLOG("> malloc %d", size); + DBGLOG("> malloc %zu", size); void * p = memalloc_alloc(NULL, memalloc_default_type, 0, size); - DBGLOG("< malloc 0x%08x", (uint) p); + DBGLOG("< malloc %p", p); return p; } void __wrap_free(void *ptr, const void * caller) { - DBGLOG("> free 0x%08x", (uint) ptr); + DBGLOG("> free %p", ptr); memalloc_free(NULL, memalloc_default_type, ptr); } void * __wrap_realloc(void *ptr, size_t size, const void * caller ) { - DBGLOG("> realloc 0x%08x %d", (uint) ptr, size); + DBGLOG("> realloc %p %zu", ptr, size); // Error case if ((ptr == NULL) && (size == 0)) return NULL; @@ -238,10 +238,10 @@ void * __wrap_realloc(void *ptr, size_t size, const void * caller ) // The typical case (ptr && size) int actual = memalloc_size(NULL, memalloc_default_type, ptr); - DBGLOG("| realloc %d -> %d", actual, size); + DBGLOG("| realloc %d -> %zu", actual, size); if (size <= actual) { - DBGLOG("< realloc 0x%08x - same", (uint) ptr); + DBGLOG("< realloc %p - same", ptr); return ptr; } @@ -251,7 +251,7 @@ void * __wrap_realloc(void *ptr, size_t size, const void * caller ) memalloc_free(NULL, memalloc_default_type, ptr); __realloc_hook = __wrap_realloc; - DBGLOG("< realloc 0x%08x", (uint) new_ptr); + DBGLOG("< realloc %p", new_ptr); return new_ptr; } From 71615893c2c4ac8b66523fde9eca37e3a28bc1c3 Mon Sep 17 00:00:00 2001 From: Aaron Stone Date: Fri, 13 Jul 2012 00:51:20 +0000 Subject: [PATCH 3/5] Workaround for old malloc.h by checking that __MALLOC_HOOK_VOLATILE is defined. --- memalloc.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/memalloc.c b/memalloc.c index e416f7e..f09a4a8 100644 --- a/memalloc.c +++ b/memalloc.c @@ -25,6 +25,10 @@ static char memalloc_output_buff[1024]; /* function prototypes */ static void do_init(void); +/* Workaround for old glibc */ +#ifndef __MALLOC_HOOK_VOLATILE +#define __MALLOC_HOOK_VOLATILE +#endif /* Variables to save original hooks. */ static void *(*old_malloc_hook)(size_t, const void *); @@ -54,7 +58,7 @@ static void do_init(void); } /* Override initializing hook from the C library. */ - void (*__malloc_initialize_hook) (void) = my_init_hook; + void (* __MALLOC_HOOK_VOLATILE __malloc_initialize_hook) (void) = my_init_hook; void memalloc_init(void) { From e1bceca963edff5a830258b0d6a5d1a1c4caf0a3 Mon Sep 17 00:00:00 2001 From: Aaron Stone Date: Fri, 13 Jul 2012 00:51:39 +0000 Subject: [PATCH 4/5] Switch from ./b to Makefile. --- Makefile | 21 +++++++++++++++++++++ b | 22 ---------------------- 2 files changed, 21 insertions(+), 22 deletions(-) create mode 100644 Makefile delete mode 100755 b diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..79566f5 --- /dev/null +++ b/Makefile @@ -0,0 +1,21 @@ +CFLAGS=-Wall -g -O0 +CC=gcc + +ifneq ($(shell uname -s),Linux) +$(error "This code requires Linux with Glibc") +endif + +.PHONY: static shared + +all: static shared + +static: memalloc.o + +shared: libmemalloc.so + +lib%.so: %.c + $(CC) $(CFLAGS) -shared -o $@ $^ + +clean: + -rm -f memalloc.o + -rm -f libmemalloc.so diff --git a/b b/b deleted file mode 100755 index a25ce91..0000000 --- a/b +++ /dev/null @@ -1,22 +0,0 @@ -#!/bin/bash - -# This is now a rake script please run it according or update the rake script on changes -# see lib/tasks/build.rake - -OS=`uname -s` - -case $OS in - Linux) - echo "Compiling for Linux..." - ;; - *) - echo "We do not support other OSs" - exit 1 - ;; -esac - -gcc -Wall -g -O0 -c memalloc.c - -# For building a LD_PRELOAD version -# gcc -g -O0 -shared -o libmemalloc.so memalloc.c - From 20683c875e1dd5029e438ecb86d8a22dfa0011b1 Mon Sep 17 00:00:00 2001 From: Aaron Stone Date: Fri, 13 Jul 2012 02:27:55 +0000 Subject: [PATCH 5/5] fPIC required for shared library on x86_64. --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 79566f5..140506e 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ static: memalloc.o shared: libmemalloc.so lib%.so: %.c - $(CC) $(CFLAGS) -shared -o $@ $^ + $(CC) $(CFLAGS) -fPIC -shared -o $@ $^ clean: -rm -f memalloc.o