| 1 |
// -*- C++ -*- |
| 2 |
|
| 3 |
/** |
| 4 |
* @file gnuwrapper.cpp |
| 5 |
* @brief Replaces malloc family on GNU/Linux with custom versions. |
| 6 |
* @author Emery Berger <http://www.emeryberger.com> |
| 7 |
* @note Copyright (C) 2010-2020 by Emery Berger. |
| 8 |
|
| 9 |
Heap Layers is distributed under the terms of the Apache 2.0 license. |
| 10 |
|
| 11 |
You may obtain a copy of the License at |
| 12 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 13 |
|
| 14 |
*/ |
| 15 |
|
| 16 |
|
| 17 |
#ifndef __GNUC__ |
| 18 |
#error "This file requires the GNU compiler." |
| 19 |
#endif |
| 20 |
|
| 21 |
#include <errno.h> |
| 22 |
#include <string.h> |
| 23 |
#include <stdlib.h> |
| 24 |
#include <stdio.h> |
| 25 |
#include <malloc.h> |
| 26 |
#include <new> |
| 27 |
#include <pthread.h> |
| 28 |
|
| 29 |
#include "heaplayers.h" |
| 30 |
|
| 31 |
/* |
| 32 |
To use this library, |
| 33 |
you only need to define the following allocation functions: |
| 34 |
|
| 35 |
- xxmalloc |
| 36 |
- xxfree |
| 37 |
- xxmalloc_usable_size |
| 38 |
- xxmalloc_lock |
| 39 |
- xxmalloc_unlock |
| 40 |
|
| 41 |
See the extern "C" block below for function prototypes and more |
| 42 |
details. YOU SHOULD NOT NEED TO MODIFY ANY OF THE CODE HERE TO |
| 43 |
SUPPORT ANY ALLOCATOR. |
| 44 |
|
| 45 |
|
| 46 |
LIMITATIONS: |
| 47 |
|
| 48 |
- This wrapper assumes that the underlying allocator will do "the |
| 49 |
right thing" when xxfree() is called with a pointer internal to an |
| 50 |
allocated object. Header-based allocators, for example, need not |
| 51 |
apply. |
| 52 |
|
| 53 |
*/ |
| 54 |
|
| 55 |
#ifndef __THROW |
| 56 |
#define __THROW |
| 57 |
#endif |
| 58 |
|
| 59 |
static bool initialized = false; |
| 60 |
|
| 61 |
//#include "wrapper.cpp" |
| 62 |
|
| 63 |
extern "C" { |
| 64 |
|
| 65 |
void * xxmalloc(size_t); |
| 66 |
void xxfree(void *); |
| 67 |
size_t xxmalloc_usable_size(void *); |
| 68 |
void xxmalloc_lock(); |
| 69 |
void xxmalloc_unlock(); |
| 70 |
|
| 71 |
static void my_init_hook(); |
| 72 |
|
| 73 |
// New hooks for allocation functions. |
| 74 |
static void * my_malloc_hook (size_t, const void *); |
| 75 |
static void my_free_hook (void *, const void *); |
| 76 |
static void * my_realloc_hook (void *, size_t, const void *); |
| 77 |
static void * my_memalign_hook (size_t, size_t, const void *); |
| 78 |
|
| 79 |
// Store the old hooks just in case. |
| 80 |
static void * (*old_malloc_hook) (size_t, const void *); |
| 81 |
static void (*old_free_hook) (void *, const void *); |
| 82 |
static void * (*old_realloc_hook)(void *ptr, size_t size, const void *caller); |
| 83 |
static void * (*old_memalign_hook)(size_t alignment, size_t size, const void *caller); |
| 84 |
|
| 85 |
// From GNU libc 2.14 this macro is defined, to declare |
| 86 |
// hook variables as volatile. Define it as empty for |
| 87 |
// older glibc versions |
| 88 |
#ifndef __MALLOC_HOOK_VOLATILE |
| 89 |
#define __MALLOC_HOOK_VOLATILE |
| 90 |
#endif |
| 91 |
|
| 92 |
void (*__MALLOC_HOOK_VOLATILE __malloc_initialize_hook)() = my_init_hook; |
| 93 |
|
| 94 |
#pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 95 |
|
| 96 |
static void my_init_hook() { |
| 97 |
if (!initialized) { |
| 98 |
// Store the old hooks. |
| 99 |
old_malloc_hook = __malloc_hook; |
| 100 |
old_free_hook = __free_hook; |
| 101 |
old_realloc_hook = __realloc_hook; |
| 102 |
old_memalign_hook = __memalign_hook; |
| 103 |
|
| 104 |
// Point the hooks to the replacement functions. |
| 105 |
__malloc_hook = my_malloc_hook; |
| 106 |
__free_hook = my_free_hook; |
| 107 |
__realloc_hook = my_realloc_hook; |
| 108 |
__memalign_hook = my_memalign_hook; |
| 109 |
|
| 110 |
// Set up everything so that fork behaves properly. |
| 111 |
pthread_atfork(xxmalloc_lock, xxmalloc_unlock, xxmalloc_unlock); |
| 112 |
|
| 113 |
initialized = true; |
| 114 |
|
| 115 |
} |
| 116 |
|
| 117 |
} |
| 118 |
|
| 119 |
static void * my_malloc_hook (size_t size, const void *) { |
| 120 |
return xxmalloc(size); |
| 121 |
} |
| 122 |
|
| 123 |
static void my_free_hook (void * ptr, const void *) { |
| 124 |
xxfree(ptr); |
| 125 |
} |
| 126 |
|
| 127 |
static void * my_realloc_hook (void * ptr, size_t sz, const void *) { |
| 128 |
return CUSTOM_PREFIX(realloc)(ptr, sz); |
| 129 |
} |
| 130 |
|
| 131 |
static void * my_memalign_hook (size_t size, size_t alignment, const void *) { |
| 132 |
return CUSTOM_PREFIX(memalign)(size, alignment); |
| 133 |
} |
| 134 |
|
| 135 |
} |