| 1 |
/* -*- C++ -*- */ |
| 2 |
|
| 3 |
/* |
| 4 |
|
| 5 |
Heap Layers: An Extensible Memory Allocation Infrastructure |
| 6 |
|
| 7 |
Copyright (C) 2000-2003 by Emery Berger |
| 8 |
http://www.cs.umass.edu/~emery |
| 9 |
[email protected] |
| 10 |
|
| 11 |
*/ |
| 12 |
|
| 13 |
/* |
| 14 |
* @file libreap.cpp |
| 15 |
* @brief Replaces malloc and adds reap functionality to your application. |
| 16 |
* @author Emery Berger <http://www.cs.umass.edu/~emery> |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <stdlib.h> |
| 20 |
#include <new> |
| 21 |
|
| 22 |
#include "heaplayers.h" |
| 23 |
#include "slopheap.h" |
| 24 |
#include "regionheap.h" |
| 25 |
#include "chunkheap.h" |
| 26 |
#include "oneheap.h" |
| 27 |
#include "regionheapapi.h" |
| 28 |
#include "nestedheap.h" |
| 29 |
|
| 30 |
// Conservative assumption here... |
| 31 |
// Note: this is used by wrapper.cpp. |
| 32 |
volatile int anyThreadCreated = 1; |
| 33 |
|
| 34 |
// All reaps eventually come from mmap. |
| 35 |
class OneStore : public MmapHeap {}; |
| 36 |
|
| 37 |
// We'll grab chunks of 8K. |
| 38 |
class TopHeap : |
| 39 |
// public ChunkHeap<8192 - 20, SlopHeap<RegionHeap<OneStore>, 16> > { |
| 40 |
public ChunkHeap<8192 - 20, RegionHeap<OneStore> > { |
| 41 |
}; |
| 42 |
|
| 43 |
#define MAIN_ALLOCATOR TopHeap |
| 44 |
|
| 45 |
class TheCustomHeapType : public Reap<MAIN_ALLOCATOR> {}; |
| 46 |
|
| 47 |
inline static TheCustomHeapType * getCustomHeap (void) { |
| 48 |
static char thBuf[sizeof(TheCustomHeapType)]; |
| 49 |
static TheCustomHeapType * th = new (thBuf) TheCustomHeapType; |
| 50 |
return th; |
| 51 |
} |
| 52 |
|
| 53 |
extern "C" void regionCreate (void ** reg, void ** parent) |
| 54 |
{ |
| 55 |
Reap<MAIN_ALLOCATOR> * psr; |
| 56 |
psr = new Reap<MAIN_ALLOCATOR> (); |
| 57 |
if (parent) { |
| 58 |
(*((Reap<MAIN_ALLOCATOR> **) parent))->addChild (psr); |
| 59 |
} |
| 60 |
|
| 61 |
*((Reap<MAIN_ALLOCATOR> **) reg) = psr; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
extern "C" void regionDestroy (void ** reg) |
| 66 |
{ |
| 67 |
delete ((Reap<MAIN_ALLOCATOR> *) *reg); |
| 68 |
*reg = NULL; |
| 69 |
} |
| 70 |
|
| 71 |
extern "C" void * regionAllocate (void ** reg, size_t sz) |
| 72 |
{ |
| 73 |
void * ptr = ((Reap<MAIN_ALLOCATOR> *) *reg)->malloc (sz); |
| 74 |
return ptr; |
| 75 |
} |
| 76 |
|
| 77 |
extern "C" void regionFreeAll (void ** reg) |
| 78 |
{ |
| 79 |
((Reap<MAIN_ALLOCATOR> *) *reg)->clear (); |
| 80 |
} |
| 81 |
|
| 82 |
extern "C" void regionFree (void ** reg, void * ptr) |
| 83 |
{ |
| 84 |
((Reap<MAIN_ALLOCATOR> *) *reg)->free (ptr); |
| 85 |
} |
| 86 |
|
| 87 |
// Reap API wrappers. |
| 88 |
|
| 89 |
|
| 90 |
extern "C" void reapcreate (void ** reg, void ** parent) |
| 91 |
{ |
| 92 |
regionCreate (reg, parent); |
| 93 |
} |
| 94 |
|
| 95 |
extern "C" void reapdestroy (void ** reg) |
| 96 |
{ |
| 97 |
regionDestroy (reg); |
| 98 |
} |
| 99 |
|
| 100 |
extern "C" void * reapmalloc (void ** reg, size_t sz) |
| 101 |
{ |
| 102 |
regionAllocate (reg, sz); |
| 103 |
} |
| 104 |
|
| 105 |
extern "C" void reapclear (void ** reg) |
| 106 |
{ |
| 107 |
regionFreeAll (reg); |
| 108 |
} |
| 109 |
|
| 110 |
extern "C" void reapfree (void ** reg, void * ptr) |
| 111 |
{ |
| 112 |
regionFree (reg, ptr); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
extern "C" size_t reapgetsize (void ** reg, void * ptr) |
| 117 |
{ |
| 118 |
return ((Reap<MAIN_ALLOCATOR> *) *reg)->getSize (ptr); |
| 119 |
} |