| 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 | - xxmemalign |
| 38 | - xxmalloc_usable_size |
| 39 | - xxmalloc_lock |
| 40 | - xxmalloc_unlock |
| 41 | |
| 42 | See the extern "C" block below for function prototypes and more |
| 43 | details. YOU SHOULD NOT NEED TO MODIFY ANY OF THE CODE HERE TO |
| 44 | SUPPORT ANY ALLOCATOR. |
| 45 | |
| 46 | */ |
| 47 | |
| 48 | #define WEAK(x) __attribute__ ((weak, alias(#x))) |
| 49 | #ifndef __THROW |
| 50 | #define __THROW |
| 51 | #endif |
| 52 | |
| 53 | #if defined(__clang__) |
| 54 | #pragma clang diagnostic push |
| 55 | #pragma clang diagnostic ignored "-Wmacro-redefined" |
| 56 | #endif |
| 57 | |
| 58 | #define CUSTOM_PREFIX(x) custom##x |
| 59 | |
| 60 | #if defined(__clang__) |
| 61 | #pragma clang diagnostic pop |
| 62 | #endif |
| 63 | |
| 64 | #define ATTRIBUTE_EXPORT __attribute__((visibility("default"))) |
| 65 | |
| 66 | #define WEAK_REDEF1(type,fname,arg1) ATTRIBUTE_EXPORT type fname(arg1) __THROW WEAK(custom##fname) |
| 67 | #define WEAK_REDEF2(type,fname,arg1,arg2) ATTRIBUTE_EXPORT type fname(arg1,arg2) __THROW WEAK(custom##fname) |
| 68 | #define WEAK_REDEF2_NOTHROW(type,fname,arg1,arg2) ATTRIBUTE_EXPORT type fname(arg1,arg2) WEAK(custom##fname) |
| 69 | #define WEAK_REDEF3(type,fname,arg1,arg2,arg3) ATTRIBUTE_EXPORT type fname(arg1,arg2,arg3) __THROW WEAK(custom##fname) |
| 70 | |
| 71 | extern "C" { |
| 72 | WEAK_REDEF1(void *, malloc, size_t); |
| 73 | WEAK_REDEF1(void, free, void *); |
| 74 | WEAK_REDEF1(void, cfree, void *); |
| 75 | WEAK_REDEF2(void *, calloc, size_t, size_t); |
| 76 | WEAK_REDEF2(void *, realloc, void *, size_t); |
| 77 | WEAK_REDEF3(void *, reallocarray, void *, size_t, size_t); |
| 78 | WEAK_REDEF2(void *, memalign, size_t, size_t); |
| 79 | WEAK_REDEF3(int, posix_memalign, void **, size_t, size_t); |
| 80 | #ifdef __USE_XOPEN2K // a work-around for an exception anomaly |
| 81 | // WEAK_REDEF2_NOTHROW(void *, aligned_alloc, size_t, size_t); |
| 82 | #else |
| 83 | WEAK_REDEF2(void *, aligned_alloc, size_t, size_t); |
| 84 | #endif |
| 85 | WEAK_REDEF1(size_t, malloc_usable_size, void *); |
| 86 | } |
| 87 | |
| 88 | #include "wrapper.cpp" |
| 89 | //// #include "gnuwrapper-hooks.cpp" |