diff options
Diffstat (limited to 'examples/cxx/hybrid/allocator/arena.hxx')
-rw-r--r-- | examples/cxx/hybrid/allocator/arena.hxx | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/examples/cxx/hybrid/allocator/arena.hxx b/examples/cxx/hybrid/allocator/arena.hxx new file mode 100644 index 0000000..0463a38 --- /dev/null +++ b/examples/cxx/hybrid/allocator/arena.hxx @@ -0,0 +1,52 @@ +// file : examples/cxx/hybrid/allocator/arena.hxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : not copyrighted - public domain + +#ifndef ARENA_HXX +#define ARENA_HXX + +#include <stddef.h> // size_t + +// Sample pooled memory arena. The primary goal here is to provide a +// simple, if naive, implementation. As a result, it probably shouldn't +// be used in production. +// +class arena +{ +public: + arena (void* memory, size_t size); + + void* + allocate (size_t); + + void* + reallocate (void*, size_t); + + void + free (void*); + + void + print_statistics (); + +private: + struct block + { + size_t cap; // block capacity + size_t size; // allocated size + block* next; // next block + size_t used; // used flag + }; + + block* head_; // Linked list of blocks. + + // Statistics. + // + size_t alloc_count_; + size_t realloc_count_; + size_t free_count_; + + size_t cur_allocated_; + size_t max_allocated_; +}; + +#endif // ARENA_HXX |