Debian Codegraph @

vendor/Heap-Layers/legacy/reap/libreap.cpp

raw
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
31 // Conservative assumption here...
32 // Note: this is used by wrapper.cpp.
33 volatile int anyThreadCreated = 1;
34
35 // All reaps eventually come from mmap.
36 class OneStore : public MmapHeap {};
37
38 // We'll grab chunks of 8K.
39 class TopHeap :
40 public ChunkHeap<8192 - 20, SlopHeap<RegionHeap<OneStore>, 16> > {
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 #include "wrapper.cpp"
54
55 extern "C" void regionCreate (void ** reg, void ** parent)
56 {
57 Reap<MAIN_ALLOCATOR> * psr;
58 psr = new Reap<MAIN_ALLOCATOR> ();
59 if (parent) {
60 (*((Reap<MAIN_ALLOCATOR> **) parent))->addChild (psr);
61 }
62
63 *((Reap<MAIN_ALLOCATOR> **) reg) = psr;
64 }
65
66
67 extern "C" void regionDestroy (void ** reg)
68 {
69 delete ((Reap<MAIN_ALLOCATOR> *) *reg);
70 *reg = NULL;
71 }
72
73 extern "C" void * regionAllocate (void ** reg, size_t sz)
74 {
75 void * ptr = ((Reap<MAIN_ALLOCATOR> *) *reg)->malloc (sz);
76 return ptr;
77 }
78
79 extern "C" void regionFreeAll (void ** reg)
80 {
81 ((Reap<MAIN_ALLOCATOR> *) *reg)->clear ();
82 }
83
84 extern "C" void regionFree (void ** reg, void * ptr)
85 {
86 ((Reap<MAIN_ALLOCATOR> *) *reg)->free (ptr);
87 }
88
89 // Reap API wrappers.
90
91
92 extern "C" void reapcreate (void ** reg, void ** parent)
93 {
94 regionCreate (reg, parent);
95 }
96
97 extern "C" void reapdestroy (void ** reg)
98 {
99 regionDestroy (reg);
100 }
101
102 extern "C" void * reapmalloc (void ** reg, size_t sz)
103 {
104 return regionAllocate (reg, sz);
105 }
106
107 extern "C" void reapclear (void ** reg)
108 {
109 regionFreeAll (reg);
110 }
111
112 extern "C" void reapfree (void ** reg, void * ptr)
113 {
114 regionFree (reg, ptr);
115 }
116