Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

base fork: sodabrew/memalloc
base: 85e43eec5d
...
head fork: sodabrew/memalloc
compare: 20683c875e
  • 5 commits
  • 4 files changed
  • 0 commit comments
  • 3 contributors
Showing with 40 additions and 37 deletions.
  1. +21 −0 Makefile
  2. +1 −1  README.md
  3. +0 −22 b
  4. +18 −14 memalloc.c
21 Makefile
View
@@ -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  README.md
View
@@ -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
22 b
View
@@ -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
-
32 memalloc.c
View
@@ -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;
}

No commit comments for this range

Something went wrong with that request. Please try again.