Skip to content

AzureHaze21/AprPoolAllocator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 

Repository files navigation

AprPoolAllocator

C++ wrapper for APR pools

Using an APR pool

apr_initialize() must be called before using any APR lib and apr_terminate() when you're done.

Creating objects

Pool p;

int *i = p.construct<int>(45);
char *str = p("string");
MyObject *o = p.construct<MyObject>();
void *mem = p.alloc(20);

/* or with the macro poolNew(p, obj, ...) */

struct MyObject {  MyObject(int a, int b, int c) {} };

MyObject *o = poolNew(p, o, 1, 2, 3);

Getting an allocator

Allocators must be initialized from already existing memory pools by calling the specific constructor or using the getAllocator() method.

Pool p;

auto intAllocator1 = p.getAllocator<int>();
auto intAllocator2 = Pool::AprAllocator<int>(p);

Using an allocator with STL containers

Pool p;

auto allocator = p.getAllocator<int>();

// all of these are equivalent
std::vector<int, Pool::AprAllocator<int>> v({ 1, 2, 3 }, allocator);
auto vec2 = std::vector<int, decltype(allocator)>({ 4, 5, 6 }, allocator);
decltype(vec1) vec3({ 7, 8, 9 }, allocator););

Attaching/Detaching objects

You can specify a cleanup function when attaching an already allocated object that will be called when the memory pool it's attached to is destroyed.

apr_status_t my_cleanup_function(void *data)
{
  reinterpret_cast<MyObject*>(data)->~MyObject();
  delete MyObject;
  return APR_SUCCESS;
}

[...]

Pool p;

MyObject *o = new MyObject():
p.attach(o, (Pool::Callback)my_cleanup_function); //attach
p.detach(o, (Pool::Callback)my_cleanup_function); //unregister the specific cleanup function

About

C++ wrapper for APR pools

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages