X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Floader%2FPagedAllocator.cpp;h=464d9d1f4d9c96de0abd1b7503353285d909152e;hb=8c8061a4f8b88410d6d93c039afe6affc4b69cf2;hp=77af8adb008e768e897372b4ec0b6def5570be64;hpb=0f30d8254ff8b9e63795960ec031577cf68fbf95;p=l2e.git diff --git a/src/loader/PagedAllocator.cpp b/src/loader/PagedAllocator.cpp index 77af8ad..464d9d1 100644 --- a/src/loader/PagedAllocator.cpp +++ b/src/loader/PagedAllocator.cpp @@ -1,6 +1,10 @@ #include "PagedAllocator.h" +#include + using std::deque; +using std::runtime_error; + namespace loader { @@ -32,6 +36,25 @@ char *PagedAllocator::Alloc(unsigned int size) { return chunk; } +unsigned int PagedAllocator::PageOf(void *ptrIn) const { + char *ptr = reinterpret_cast(ptrIn); + unsigned int counter = 0; + for (deque::const_iterator i(pages.begin()), end(pages.end()); i != end; ++i, ++counter) { + if (ptr < *i) continue; + if (*i < ptr) return counter; + } + throw runtime_error("PagedAllocator::PageOf"); +} + +unsigned int PagedAllocator::PageOffsetOf(void *ptrIn) const { + char *ptr = reinterpret_cast(ptrIn); + for (deque::const_iterator i(pages.begin()), end(pages.end()); i != end; ++i) { + if (ptr < *i) continue; + if (*i < ptr) return ptr - *i; + } + throw runtime_error("PagedAllocator::PageOffsetOf"); +} + unsigned int PagedAllocator::Free() const { return pageSize - (head - CurrentPage()); }