-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added malloc wrapper and ./configure --with-fixed-allocator and --wit…
…h-malloc-wrapper flags.
- Loading branch information
Showing
5 changed files
with
86 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#define _GNU_SOURCE | ||
#include <dlfcn.h> | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
|
||
#include "config.h" | ||
#include "mman.h" | ||
#undef smalloc | ||
|
||
#ifdef SMALLOC_WRAP_MALLOC | ||
void *(*real_malloc)(size_t) = NULL; | ||
void (*real_free)(void *) = NULL; | ||
|
||
__attribute__ ((constructor)) | ||
static void initialize_handles(void) { | ||
# if defined(__APPLE__) | ||
void *handle = dlopen("libc.dylib", RTLD_NOW); | ||
real_malloc = (__typeof__(real_malloc)) dlsym(handle, "malloc"); | ||
real_free = (__typeof__(real_free)) dlsym(handle, "free"); | ||
# elif defined(__unix__) | ||
real_malloc = (__typeof__(real_malloc)) dlsym(RTLD_NEXT, "malloc"); | ||
real_free = (__typeof__(real_free)) dlsym(RTLD_NEXT, "free"); | ||
# else | ||
# error malloc wrapping not supported on non-*nix systems. | ||
# endif | ||
if (real_malloc == NULL || real_free == NULL) { | ||
puts("Failed to locate original malloc or free; aborting.\n"); | ||
exit(-1); | ||
} | ||
|
||
smalloc_allocator = (s_allocator) {real_malloc, real_free}; | ||
} | ||
|
||
__attribute__ ((malloc)) | ||
void *malloc(size_t size) { | ||
return smalloc(size, 0, UNIQUE, 0); | ||
} | ||
|
||
void free(void *ptr) { | ||
sfree(ptr); | ||
} | ||
#endif /* !SMALLOC_WRAP_MALLOC */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#ifndef WRAP_MALLOC_H_ | ||
# define WRAP_MALLOC_H_ | ||
|
||
#ifdef SMALLOC_WRAP_MALLOC | ||
extern void *(*real_malloc)(size_t); | ||
extern void (*real_free)(void *); | ||
#endif /* !SMALLOC_WRAP_MALLOC */ | ||
|
||
#endif /* !WRAP_MALLOC_H_ */ |