]> git.localhorst.tv Git - l2e.git/blobdiff - src/loader/PagedAllocator.cpp
initialize objects after loading
[l2e.git] / src / loader / PagedAllocator.cpp
index 4853a1e6de9dd22af4aafa654b50aa3620575256..464d9d1f4d9c96de0abd1b7503353285d909152e 100644 (file)
@@ -1,13 +1,10 @@
-/*
- * PagedAllocator.cpp
- *
- *  Created on: Oct 9, 2012
- *      Author: holy
- */
-
 #include "PagedAllocator.h"
 
+#include <stdexcept>
+
 using std::deque;
+using std::runtime_error;
+
 
 namespace loader {
 
@@ -39,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());
 }