FreePOOMA
2.4.1
|
A Pool maintains a set of page-sized chunks of memory, and hands out small blocks very quickly. More...
#include <Pool.h>
Classes | |
struct | Link |
Public Member Functions | |
Pool (size_t sz) | |
Pool () | |
~Pool () | |
void * | alloc () |
void | free (void *b) |
A Pool maintains a set of page-sized chunks of memory, and hands out small blocks very quickly.
It does this by considering the large chunks to be a set of small blocks and connecting the blocks in a singly linked list. When asked to hand over a block, it returns first one in the list. When a block is handed back, it goes at the front of the list.
The intent is that a user class will have a static Pool and implement new and delete operators that use the pool to get memory. For example, if the user is building a lightweight Node class, the relevant parts would look like:
class Node { public:
// Get memory from the pool. // Don't need to pass the size_t to the pool because // that is in the ctor for the pool. void *operator new(size_t) { return pool_s.alloc() }
// Return memory to the pool. // Once again, the size_t is already in the pool. void *operator delete(void *p, size_t) { pool_s.free(p); }
private:
// The static memory pool. static Pool pool_s; };
Then in the Node.cpp file you would have:
// Initialize the memory pool with the size of the blocks it will manage. Pool Node::pool_s(sizeof(Node));
Pool::Pool | ( | size_t | sz | ) |
Pool::Pool | ( | ) |
Pool::~Pool | ( | ) |
void* Pool::alloc | ( | ) | [inline] |
void Pool::free | ( | void * | b | ) | [inline] |