Permalink
Browse files

Updated.

  • Loading branch information...
emeryberger committed Mar 6, 2017
1 parent 1ca1383 commit 27d6ebb6a677a5c2ae0b72479b20854e4047226f
View
@@ -0,0 +1 @@
This code is here for historical reasons. It may have suffered from bitrot and has not been tested recently.
View
@@ -0,0 +1,14 @@
This directory contains the implementation of the Reap memory
manager. Reap is a hybrid region-heap allocator. You can use Reap to
create multiple memory allocation regions ("reaps"), that each have
malloc-free functionality. These may be nested and deleted en
masse. See doc/berger-oopsla2002.pdf for more information.
The Reap API is in reap.h. libreap.cpp produces a shared object that
replaces malloc/free and provides reap functionality. If you just want
reap functionality, you can compile regionheap.cpp instead (which
differs from libreap.cpp by exactly one line).
The files regionsimulator.{h,cpp} are wrappers that provide reap and
obstack functionality on top of malloc/free, and were used for
experimental purposes in the paper mentioned above.
View
@@ -0,0 +1,54 @@
#if !defined(_ADDHEADER_H_)
#define _ADDHEADER_H_
/**
* @class AddHeader
* @brief Adds LeaHeap-compatible metadata to objects.
*/
template <class SuperHeap>
class AddHeader : public SuperHeap {
public:
inline AddHeader (void)
: prevSize (sizeof(Header))
{
}
inline void * malloc (const size_t sz) {
Header h (prevSize, sz);
prevSize = sz;
Header * p = (Header *) SuperHeap::malloc (sz + sizeof(Header));
*p = h;
return (p + 1);
}
inline void clear (void) {
prevSize = sizeof(Header);
SuperHeap::clear();
}
private:
inline void free (void * ptr);
/// Object headers for use by the Lea allocator.
class Header {
public:
Header (size_t p, size_t s)
: prevSize (p),
size (s)
{}
/// The size of the previous (contiguous) object.
size_t prevSize;
/// The size of the current (immediately-following) object.
size_t size;
};
/// The previous size allocated.
size_t prevSize;
};
#endif
@@ -0,0 +1,75 @@
#if !defined(_CLEAROPTIMIZEHEAP_H_)
#define _CLEAROPTIMIZEHEAP_H_
/**
* @class ClearOptimizeHeap
* @brief A heap layer that is optimized for region-like allocation patterns.
* @param Heap1 This heap provides memory for freshly allocated objects.
* @param Heap2 This heap provides memory for recycled objects.
*/
template <class Heap1, class Heap2>
class ClearOptimizeHeap: public Heap1, Heap2 {
public:
inline ClearOptimizeHeap (void)
: nothingOnHeap (true)
{
}
inline void * malloc (const size_t sz) {
if (nothingOnHeap) {
return Heap1::malloc (sz);
} else {
void * ptr = Heap2::malloc (sz);
if (ptr == NULL) {
// Right now we assume that this means that the heap is in fact
// exhausted. This is not necessarily true, but it's a reasonable
// approximation, and is a lot cheaper than tracking the complete
// state of the parent heap.
nothingOnHeap = true;
ptr = Heap1::malloc (sz);
}
return ptr;
}
}
inline void free (void * ptr) {
#if 1
nothingOnHeap = false;
#else
if (nothingOnHeap) {
nothingOnHeap = false;
}
#endif
Heap2::free (ptr);
}
inline int remove (void * ptr) {
return Heap2::remove (ptr); // heap2.remove (ptr);
}
inline void clear (void) {
Heap1::clear();
if (!nothingOnHeap) {
Heap2::clear(); ///heap2.clear();
}
nothingOnHeap = true;
}
inline size_t getSize (const void * ptr) {
return Heap2::getSize (ptr);
}
private:
/// True iff there is nothing on heap 2.
#ifdef WIN32
BOOL nothingOnHeap;
#else
bool nothingOnHeap;
#endif
// Heap2 heap2;
};
#endif
View
@@ -0,0 +1,22 @@
#! /bin/sh
case "$OSTYPE" in
darwin8.0)
echo "Compiling for Darwin"
g++ -pipe -O3 -finline-limit=65000 -fkeep-inline-functions -finline-functions -ffast-math -fomit-frame-pointer -DNDEBUG -I. -I../../heaplayers -I../../heaplayers/util -D_REENTRANT=1 -compatibility_version 1 -current_version 1 -dynamiclib libreap.cpp -o libreap.dylib;;
[Ll]inux*)
echo "Compiling for Linux"
g++ -pipe -O3 -finline-limit=5000 -DNDEBUG -I. -I../../heaplayers -D_REENTRANT=1 -fPIC -c libreap.cpp
g++ -pipe -O3 -finline-limit=5000 -DNDEBUG -I. -I../../heaplayers -D_REENTRANT=1 -shared libreap.o -o libreap.so;;
solaris)
echo "Compiling for Solaris"
#CC -xildoff -native -noex -xipo=2 -xO5 -mt -DNDEBUG -I. -I.. -D_REENTRANT=1 -G -PIC libreap.cpp -o libreap.so;;
# g++ -pipe -DNDEBUG -I. -I.. -D_REENTRANT=1 -fPIC -shared libreap.cpp -o libreap.so;;
g++ -fPIC -pipe -mcpu=ultrasparc -O3 -finline-limit=5000 -fkeep-inline-functions -finline-functions -ffast-math -fomit-frame-pointer -DNDEBUG -I. -I../../heaplayers -I../../heaplayers/util -D_REENTRANT=1 -shared libreap.cpp -o libreap.so;;
# g++ -pipe -mcpu=ultrasparc -O3 -finline-limit=65000 -fkeep-inline-functions -finline-functions -ffast-math -fomit-frame-pointer -DNDEBUG -I. -I.. -D_REENTRANT=1 -shared libreap.cpp -o libreap.so;;
*)
echo "hmmm"
esac
View
@@ -0,0 +1,130 @@
/* -*- C++ -*- */
/*
Heap Layers: An Extensible Memory Allocation Infrastructure
Copyright (C) 2000-2003 by Emery Berger
http://www.cs.umass.edu/~emery
emery@cs.umass.edu
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* @file libreap.cpp
* @brief Replaces malloc and adds reap functionality to your application.
* @author Emery Berger <http://www.cs.umass.edu/~emery>
*/
#include <stdlib.h>
#include <new>
#include "heaplayers.h"
#include "slopheap.h"
#include "regionheap.h"
#include "chunkheap.h"
#include "oneheap.h"
#include "regionheapapi.h"
#include "nestedheap.h"
// Conservative assumption here...
// Note: this is used by wrapper.cpp.
volatile int anyThreadCreated = 1;
// All reaps eventually come from mmap.
class OneStore : public MmapHeap {};
// We'll grab chunks of 8K.
class TopHeap :
public ChunkHeap<8192 - 20, SlopHeap<RegionHeap<OneStore>, 16> > {
};
#define MAIN_ALLOCATOR TopHeap
class TheCustomHeapType : public Reap<MAIN_ALLOCATOR> {};
inline static TheCustomHeapType * getCustomHeap (void) {
static char thBuf[sizeof(TheCustomHeapType)];
static TheCustomHeapType * th = new (thBuf) TheCustomHeapType;
return th;
}
#include "wrapper.cpp"
extern "C" void regionCreate (void ** reg, void ** parent)
{
Reap<MAIN_ALLOCATOR> * psr;
psr = new Reap<MAIN_ALLOCATOR> ();
if (parent) {
(*((Reap<MAIN_ALLOCATOR> **) parent))->addChild (psr);
}
*((Reap<MAIN_ALLOCATOR> **) reg) = psr;
}
extern "C" void regionDestroy (void ** reg)
{
delete ((Reap<MAIN_ALLOCATOR> *) *reg);
*reg = NULL;
}
extern "C" void * regionAllocate (void ** reg, size_t sz)
{
void * ptr = ((Reap<MAIN_ALLOCATOR> *) *reg)->malloc (sz);
return ptr;
}
extern "C" void regionFreeAll (void ** reg)
{
((Reap<MAIN_ALLOCATOR> *) *reg)->clear ();
}
extern "C" void regionFree (void ** reg, void * ptr)
{
((Reap<MAIN_ALLOCATOR> *) *reg)->free (ptr);
}
// Reap API wrappers.
extern "C" void reapcreate (void ** reg, void ** parent)
{
regionCreate (reg, parent);
}
extern "C" void reapdestroy (void ** reg)
{
regionDestroy (reg);
}
extern "C" void * reapmalloc (void ** reg, size_t sz)
{
return regionAllocate (reg, sz);
}
extern "C" void reapclear (void ** reg)
{
regionFreeAll (reg);
}
extern "C" void reapfree (void ** reg, void * ptr)
{
regionFree (reg, ptr);
}
View
@@ -0,0 +1,55 @@
/*
Heap Layers: An Extensible Memory Allocation Infrastructure
Copyright (C) 2000-2003 by Emery Berger
http://www.cs.umass.edu/~emery
emery@cs.umass.edu
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#if !defined(_REAP_H_)
#define _REAP_H_
/**
* @file reap.h
* @brief The Reap API (plus the deprecated region-named API).
* @author Emery Berger <http://www.cs.umass.edu/~emery>
*/
#if defined(_cplusplus)
extern "C" {
#endif
void reapcreate (void ** reap, void ** parent);
void * reapmalloc (void ** reap, size_t sz);
void reapfree (void ** reap, void * ptr);
void reapclear (void ** reap);
void reapdestroy (void ** reap);
int regionFind (void ** reap, void * ptr);
void regionCreate (void ** reap, void ** parent);
void * regionAllocate (void ** reap, size_t sz);
void regionFree (void ** reap, void * ptr);
void regionFreeAll (void ** reap);
void regionDestroy (void ** reap);
#if defined(_cplusplus)
}
#endif
#endif
Oops, something went wrong.

0 comments on commit 27d6ebb

Please sign in to comment.