14 int AllocateMemory(uintptr_t VirtAddr, size_t ByteCount);
15 uintptr_t FindFreeRange(size_t ByteCount, int MaxBits);
18 int AllocateMemory(uintptr_t VirtAddr, size_t ByteCount)
20 uintptr_t base = (VirtAddr >> 12) << 12;
21 size_t size = (VirtAddr & 0xFFF) + ByteCount;
24 tmp = VirtualAlloc(base, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
26 printf("ERROR: Unable to allocate memory (%i)\n", GetLastError());
30 tmp = mmap((void*)base, size, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0);
31 if( tmp == MAP_FAILED ) {
38 uintptr_t FindFreeRange(size_t ByteCount, int MaxBits)
41 # error "Windows FindFreeRange() unimplemented"
43 uintptr_t base, ofs, size;
45 static const int PAGE_SIZE = 0x1000;
47 size = (ByteCount + PAGE_SIZE - 1) / PAGE_SIZE;
50 end <<= (sizeof(intptr_t)*8-MaxBits);
51 end >>= (sizeof(intptr_t)*8-MaxBits);
52 printf("end = %p\n", (void*)end);
54 // for( base = 0; base < end - size; base -= PAGE_SIZE )
55 for( base = end - size + 1; base > 0; base -= PAGE_SIZE )
57 for( ofs = 0; ofs < size; ofs += PAGE_SIZE ) {
58 if( msync( (void*)(base+ofs), 1, 0) == 0 )
61 perror("FindFreeRange, msync");