1 #include "PagedAllocator.h"
6 using std::runtime_error;
11 PagedAllocator::PagedAllocator(unsigned int pageSize)
13 , pageSize(pageSize) {
17 PagedAllocator::~PagedAllocator() {
18 for (deque<char *>::const_iterator i(pages.begin()), end(pages.end()); i != end; ++i) {
24 char *PagedAllocator::Alloc(unsigned int size) {
25 if (size > pageSize) {
26 char *page(new char[size]);
27 pages.push_front(page);
30 unsigned int free(Free());
39 unsigned int PagedAllocator::PageOf(void *ptrIn) const {
40 char *ptr = reinterpret_cast<char *>(ptrIn);
41 unsigned int counter = 0;
42 for (deque<char *>::const_iterator i(pages.begin()), end(pages.end()); i != end; ++i, ++counter) {
43 if (ptr < *i) continue;
44 if (*i < ptr) return counter;
46 throw runtime_error("PagedAllocator::PageOf");
49 unsigned int PagedAllocator::PageOffsetOf(void *ptrIn) const {
50 char *ptr = reinterpret_cast<char *>(ptrIn);
51 for (deque<char *>::const_iterator i(pages.begin()), end(pages.end()); i != end; ++i) {
52 if (ptr < *i) continue;
53 if (*i < ptr) return ptr - *i;
55 throw runtime_error("PagedAllocator::PageOffsetOf");
58 unsigned int PagedAllocator::Free() const {
59 return pageSize - (head - CurrentPage());
62 void PagedAllocator::NewPage() {
63 char *page(new char[pageSize]);
64 pages.push_back(page);
68 char *PagedAllocator::CurrentPage() {
72 const char *PagedAllocator::CurrentPage() const {