|
PLearn 0.1
|
A PMemArena is a fixed-size contiguous block of memory for allocating objects of the SAME SIZE. More...
#include <PMemPool.h>


Classes | |
| union | Aligner |
| Utility union to ensure alignment across platforms. More... | |
Public Member Functions | |
| PMemArena (size_t object_size, size_t max_num_objects) | |
| The constructor needs the size of each object (minimum size = sizeof(void*)), and how many objects the arena should contain. | |
| ~PMemArena () | |
| Free the allocated memory. | |
| void * | allocate () |
| Allocate one object from the arena. Return NULL if cannot allocate anything. | |
| void | deallocate (void *p) |
| Free one object from the arena. | |
| bool | belongsToArena (void *p) const |
| Return true if the pointer corresponds to an object that might have been allocated from the arena. | |
| bool | empty () const |
| Return true if no objects remain allocated within the arena. | |
Protected Attributes | |
| void * | storage |
| ptr to physical storage | |
| void * | end_of_storage |
| "one"-past end-of-storage | |
| size_t | object_size |
| size of each allocated object | |
| size_t | allocated_objects |
| number of allocated objects | |
| char * | watermark |
| location of high-watermark | |
| void * | free_list |
| next free object | |
Private Member Functions | |
| PMemArena (const PMemArena &) | |
| No copy or assignment. | |
| void | operator= (const PMemArena &) |
Friends | |
| class | PMemPool |
| access to "storage" | |
A PMemArena is a fixed-size contiguous block of memory for allocating objects of the SAME SIZE.
Allocation and deallocation operations are extremely fast, with the following provision: the arena cannot be resized (see PMemPool for a class that encapsulates several arenas). Internally, memory is either allocated using the "high-watermark" or the free list. The high-watermark is a pointer that starts at the end of the arena and always decreases, until it reaches the beginning of the block; allocations are always extremely fast, since they correspond to pointer decrements. As objects are DEALLOCATED, they are added to the free list, from where they can thence be re-allocated once the watermark reaches the bottom.
Definition at line 75 of file PMemPool.h.
| PLearn::PMemArena::PMemArena | ( | size_t | object_size, |
| size_t | max_num_objects | ||
| ) |
The constructor needs the size of each object (minimum size = sizeof(void*)), and how many objects the arena should contain.
Definition at line 56 of file PMemPool.cc.
References end_of_storage, object_size, PLERROR, storage, and watermark.
: storage(0), end_of_storage(0), object_size(object_size_), allocated_objects(0), watermark(0), free_list(0) { if (object_size < sizeof(void*)) PLERROR("PMemArena::PMemArena: object_size must be at least %ld; passed size is %ld", long(sizeof(void*)), long(object_size)); size_t mem_size = object_size * max_num_objects; size_t num_align = mem_size / sizeof(Aligner); if (mem_size % sizeof(Aligner) != 0) num_align++; storage = new Aligner[num_align]; if (storage) { watermark = static_cast<char*>(storage) + object_size * max_num_objects; end_of_storage = watermark; } }
| PLearn::PMemArena::~PMemArena | ( | ) |
Free the allocated memory.
Note that since a PMemArena deals with RAW STORAGE, no destructors are ever called before freeing memory
Definition at line 74 of file PMemPool.cc.
References storage.
{
delete[] static_cast<Aligner*>(storage);
}
| PLearn::PMemArena::PMemArena | ( | const PMemArena & | ) | [private] |
No copy or assignment.
| void * PLearn::PMemArena::allocate | ( | ) | [inline] |
Allocate one object from the arena. Return NULL if cannot allocate anything.
This.
Definition at line 230 of file PMemPool.h.
References allocated_objects, free_list, object_size, storage, and watermark.
Referenced by PLearn::PMemPool::allocate().
{
// See if we can allocate from watermark
if (watermark > storage) {
watermark -= object_size;
allocated_objects++;
return watermark;
}
// Otherwise check if we can allocate from free list
else if (free_list) {
void** to_return = static_cast<void**>(free_list);
free_list = *to_return;
allocated_objects++;
return to_return;
}
// Otherwise, cannot allocate at all: return NULL
else
return 0;
}

| bool PLearn::PMemArena::belongsToArena | ( | void * | p | ) | const [inline] |
Return true if the pointer corresponds to an object that might have been allocated from the arena.
Definition at line 113 of file PMemPool.h.
References end_of_storage, and storage.
Referenced by PLearn::PMemPool::deallocate(), and deallocate().
{ return p >= storage && p < end_of_storage; }

| void PLearn::PMemArena::deallocate | ( | void * | p | ) |
Free one object from the arena.
Definition at line 79 of file PMemPool.cc.
References allocated_objects, belongsToArena(), free_list, object_size, PLASSERT, and watermark.
Referenced by PLearn::PMemPool::deallocate().
{
// If the freed object is exactly at the watermark location, add it back
// to watermark
if (p == watermark)
watermark += object_size;
else {
// Otherwise, add p to free_list
PLASSERT( belongsToArena(p) );
void** new_free_head = static_cast<void**>(p);
*new_free_head = free_list;
free_list = new_free_head;
}
allocated_objects--;
}


| bool PLearn::PMemArena::empty | ( | ) | const [inline] |
Return true if no objects remain allocated within the arena.
Definition at line 117 of file PMemPool.h.
References allocated_objects.
Referenced by PLearn::PMemPool::deallocate().
{ return allocated_objects == 0; }

| void PLearn::PMemArena::operator= | ( | const PMemArena & | ) | [private] |
friend class PMemPool [friend] |
access to "storage"
Definition at line 77 of file PMemPool.h.
size_t PLearn::PMemArena::allocated_objects [protected] |
number of allocated objects
Definition at line 83 of file PMemPool.h.
Referenced by allocate(), deallocate(), and empty().
void* PLearn::PMemArena::end_of_storage [protected] |
"one"-past end-of-storage
Definition at line 81 of file PMemPool.h.
Referenced by belongsToArena(), and PMemArena().
void* PLearn::PMemArena::free_list [protected] |
next free object
Definition at line 85 of file PMemPool.h.
Referenced by allocate(), and deallocate().
size_t PLearn::PMemArena::object_size [protected] |
size of each allocated object
Definition at line 82 of file PMemPool.h.
Referenced by allocate(), deallocate(), and PMemArena().
void* PLearn::PMemArena::storage [protected] |
ptr to physical storage
Definition at line 80 of file PMemPool.h.
Referenced by allocate(), belongsToArena(), PMemArena(), and ~PMemArena().
char* PLearn::PMemArena::watermark [protected] |
location of high-watermark
Definition at line 84 of file PMemPool.h.
Referenced by allocate(), deallocate(), and PMemArena().
1.7.4