00001
00002 #ifndef CXXUTILS_PAGEADDRESS_H
00003 #define CXXUTILS_PAGEADDRESS_H 1
00004 #include <cstdio>
00005 #include <sys/mman.h>
00006 #define DEBUG 1
00007 namespace athena {
00008 const size_t PAGESIZE= 0x1000;
00009
00010 void* page_address(void* addr);
00011 void* next_page_address(void* addr);
00012 int page_protect(void* addr, int prot);
00015 template <typename T>
00016 size_t page_protect(T* addr, int prot) {
00017 void* pageAddr(page_address((void*)addr));
00018 size_t lProtected((long)addr-(long)pageAddr+sizeof(T));
00019 int rc=mprotect(pageAddr, lProtected, prot);
00020 if (rc) printf("page_protect WARNING: mprotect heap failed for address %p\n", (void*)addr);
00021 #ifdef DEBUG
00022 else printf("page_protect DEBUG: set protection @%i for range @%lx - @%lx containing addr=%p\n",
00023 prot,(long unsigned int)page_address(addr),(long unsigned int)addr+sizeof(T), (void*)addr);
00024 #endif
00025 return (rc == 0 ? lProtected : 0);
00026 }
00027 }
00028 #endif