Skip to content
This repository was archived by the owner on Aug 8, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 0 additions & 22 deletions b

This file was deleted.

32 changes: 18 additions & 14 deletions memalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 *);
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -155,17 +159,17 @@ 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;
__free_hook = __wrap_free;
}
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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down