[176] | 1 | #ifndef _LOCALMALLOCHEAP_H_ |
---|
| 2 | #define _LOCALMALLOCHEAP_H_ |
---|
| 3 | |
---|
| 4 | #include <dlfcn.h> |
---|
| 5 | |
---|
| 6 | #include "ansiwrapper.h" |
---|
| 7 | #include "sizeheap.h" |
---|
| 8 | #include "staticheap.h" |
---|
| 9 | |
---|
| 10 | extern "C" { |
---|
| 11 | size_t malloc_usable_size (void *); |
---|
| 12 | |
---|
| 13 | typedef void * mallocFunction (size_t); |
---|
| 14 | typedef void freeFunction (void *); |
---|
| 15 | typedef size_t msizeFunction (void *); |
---|
| 16 | |
---|
| 17 | typedef void exitFunction (int); |
---|
| 18 | exitFunction * trueExitFunction; |
---|
| 19 | } |
---|
| 20 | |
---|
| 21 | namespace HL { |
---|
| 22 | |
---|
| 23 | class LocalMallocHeap { |
---|
| 24 | public: |
---|
| 25 | |
---|
| 26 | LocalMallocHeap (void) |
---|
| 27 | : freefn (NULL), |
---|
| 28 | msizefn (NULL), |
---|
| 29 | mallocfn (NULL), |
---|
| 30 | firsttime (true) |
---|
| 31 | {} |
---|
| 32 | |
---|
| 33 | inline void * malloc (size_t sz) { |
---|
| 34 | if (firsttime) { |
---|
| 35 | |
---|
| 36 | firsttime = false; |
---|
| 37 | |
---|
| 38 | // We haven't initialized anything yet. |
---|
| 39 | // Initialize all of the malloc shim functions. |
---|
| 40 | |
---|
| 41 | freefn = (freeFunction *) dlsym (RTLD_NEXT, "free"); |
---|
| 42 | msizefn = (msizeFunction *) dlsym (RTLD_NEXT, "malloc_usable_size"); |
---|
| 43 | trueExitFunction = (exitFunction *) dlsym (RTLD_NEXT, "exit"); |
---|
| 44 | mallocfn = (mallocFunction *) dlsym (RTLD_NEXT, "malloc"); |
---|
| 45 | |
---|
| 46 | if (!(freefn && msizefn && trueExitFunction && mallocfn)) { |
---|
| 47 | fprintf (stderr, "Serious problem!\n"); |
---|
| 48 | abort(); |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | assert (freefn); |
---|
| 52 | assert (msizefn); |
---|
| 53 | assert (trueExitFunction); |
---|
| 54 | assert (mallocfn); |
---|
| 55 | |
---|
| 56 | // Go get some memory from malloc! |
---|
| 57 | return (*mallocfn)(sz); |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | // Now, once we have mallocfn resolved, we can use it. |
---|
| 61 | // Otherwise, we're still in dlsym-land, and have to use our local heap. |
---|
| 62 | |
---|
| 63 | if (mallocfn) { |
---|
| 64 | return (*mallocfn)(sz); |
---|
| 65 | } else { |
---|
| 66 | void * ptr = localHeap.malloc (sz); |
---|
| 67 | assert (ptr); |
---|
| 68 | return ptr; |
---|
| 69 | } |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | inline void free (void * ptr) { |
---|
| 73 | if (mallocfn) { |
---|
| 74 | if (localHeap.isValid (ptr)) { |
---|
| 75 | // We got a pointer to the temporary allocation buffer. |
---|
| 76 | localHeap.free (ptr); |
---|
| 77 | } else { |
---|
| 78 | (*freefn)(ptr); |
---|
| 79 | } |
---|
| 80 | } |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | inline size_t getSize (void * ptr) { |
---|
| 84 | if (localHeap.isValid (ptr)) { |
---|
| 85 | return localHeap.getSize (ptr); |
---|
| 86 | } else if (mallocfn) { |
---|
| 87 | return (*msizefn)(ptr); |
---|
| 88 | } else { |
---|
| 89 | // This should never happen. |
---|
| 90 | return 0; |
---|
| 91 | } |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | private: |
---|
| 95 | |
---|
| 96 | bool firsttime; /// True iff we haven't initialized the shim functions. |
---|
| 97 | |
---|
| 98 | // Shim functions below. |
---|
| 99 | |
---|
| 100 | freeFunction * freefn; |
---|
| 101 | msizeFunction * msizefn; |
---|
| 102 | mallocFunction * mallocfn; |
---|
| 103 | |
---|
| 104 | /// The local heap (for use while we are in dlsym, installing the |
---|
| 105 | /// shim functions). Hopefully 64K is enough... |
---|
| 106 | |
---|
| 107 | ANSIWrapper<SizeHeap<StaticHeap<65536> > > localHeap; |
---|
| 108 | |
---|
| 109 | }; |
---|
| 110 | |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | #endif |
---|