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   emery@cs.umass.edu
10 
11   This program is free software; you can redistribute it and/or modify
12   it under the terms of the GNU General Public License as published by
13   the Free Software Foundation; either version 2 of the License, or
14   (at your option) any later version.
15 
16   This program is distributed in the hope that it will be useful,
17   but WITHOUT ANY WARRANTY; without even the implied warranty of
18   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   GNU General Public License for more details.
20 
21   You should have received a copy of the GNU General Public License
22   along with this program; if not, write to the Free Software
23   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24 
25 */
26 
27 /*
28  * @file   libreap.cpp
29  * @brief  Replaces malloc and adds reap functionality to your application.
30  * @author Emery Berger <http://www.cs.umass.edu/~emery>
31  */
32 
33 #include <stdlib.h>
34 #include <new>
35 
36 #include "heaplayers.h"
37 #include "slopheap.h"
38 #include "regionheap.h"
39 #include "chunkheap.h"
40 #include "oneheap.h"
41 #include "regionheapapi.h"
42 #include "nestedheap.h"
43 
44 // Conservative assumption here...
45 // Note: this is used by wrapper.cpp.
46 volatile int anyThreadCreated = 1;
47 
48 // All reaps eventually come from mmap.
49 class OneStore : public MmapHeap {};
50 
51 // We'll grab chunks of 8K.
52 class TopHeap :
53   //  public ChunkHeap<8192 - 20, SlopHeap<RegionHeap<OneStore>, 16> > {
54   public ChunkHeap<8192 - 20, RegionHeap<OneStore> > {
55 };
56 
57 #define MAIN_ALLOCATOR TopHeap
58 
59 class TheCustomHeapType : public Reap<MAIN_ALLOCATOR> {};
60 
getCustomHeap(void)61 inline static TheCustomHeapType * getCustomHeap (void) {
62   static char thBuf[sizeof(TheCustomHeapType)];
63   static TheCustomHeapType * th = new (thBuf) TheCustomHeapType;
64   return th;
65 }
66 
regionCreate(void ** reg,void ** parent)67 extern "C" void regionCreate (void ** reg, void ** parent)
68 {
69   Reap<MAIN_ALLOCATOR> * psr;
70   psr = new Reap<MAIN_ALLOCATOR> ();
71   if (parent) {
72     (*((Reap<MAIN_ALLOCATOR> **) parent))->addChild (psr);
73   }
74 
75   *((Reap<MAIN_ALLOCATOR> **) reg) = psr;
76 }
77 
78 
regionDestroy(void ** reg)79 extern "C" void regionDestroy (void ** reg)
80 {
81   delete ((Reap<MAIN_ALLOCATOR> *) *reg);
82   *reg = NULL;
83 }
84 
regionAllocate(void ** reg,size_t sz)85 extern "C" void * regionAllocate (void ** reg, size_t sz)
86 {
87   void * ptr = ((Reap<MAIN_ALLOCATOR> *) *reg)->malloc (sz);
88   return ptr;
89 }
90 
regionFreeAll(void ** reg)91 extern "C" void regionFreeAll (void ** reg)
92 {
93   ((Reap<MAIN_ALLOCATOR> *) *reg)->clear ();
94 }
95 
regionFree(void ** reg,void * ptr)96 extern "C" void regionFree (void ** reg, void * ptr)
97 {
98   ((Reap<MAIN_ALLOCATOR> *) *reg)->free (ptr);
99 }
100 
101 // Reap API wrappers.
102 
103 
reapcreate(void ** reg,void ** parent)104 extern "C" void reapcreate (void ** reg, void ** parent)
105 {
106   regionCreate (reg, parent);
107 }
108 
reapdestroy(void ** reg)109 extern "C" void reapdestroy (void ** reg)
110 {
111   regionDestroy (reg);
112 }
113 
reapmalloc(void ** reg,size_t sz)114 extern "C" void * reapmalloc (void ** reg, size_t sz)
115 {
116   regionAllocate (reg, sz);
117 }
118 
reapclear(void ** reg)119 extern "C" void reapclear (void ** reg)
120 {
121   regionFreeAll (reg);
122 }
123 
reapfree(void ** reg,void * ptr)124 extern "C" void reapfree (void ** reg, void * ptr)
125 {
126   regionFree (reg, ptr);
127 }
128 
129 
reapgetsize(void ** reg,void * ptr)130 extern "C" size_t reapgetsize (void ** reg, void * ptr)
131 {
132   return ((Reap<MAIN_ALLOCATOR> *) *reg)->getSize (ptr);
133 }
134