diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..140506e --- /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) -fPIC -shared -o $@ $^ + +clean: + -rm -f memalloc.o + -rm -f libmemalloc.so 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 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 - diff --git a/memalloc.c b/memalloc.c index 937b4b8..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) { @@ -96,11 +100,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 +139,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 +159,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 +167,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 +207,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 +242,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 +255,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; }