]> git.localhorst.tv Git - l2e.git/blobdiff - src/loader/PagedAllocator.cpp
new object file format in compiler
[l2e.git] / src / loader / PagedAllocator.cpp
index 77af8adb008e768e897372b4ec0b6def5570be64..464d9d1f4d9c96de0abd1b7503353285d909152e 100644 (file)
@@ -1,6 +1,10 @@
 #include "PagedAllocator.h"
 
+#include <stdexcept>
+
 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<char *>(ptrIn);
+       unsigned int counter = 0;
+       for (deque<char *>::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<char *>(ptrIn);
+       for (deque<char *>::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());
 }