X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=AcessNative%2Fld-acess_src%2Fmemory.c;h=99238a5222719485605347661dd33333820780da;hb=13078002b01ee4f63eb2001d2ef479a2a006ea32;hp=f1147c265242f5e5e60dc97bd6c8e74e70db364d;hpb=b3398bcfe17be9751d98a0dcd8ccd27bb1568039;p=tpg%2Facess2.git diff --git a/AcessNative/ld-acess_src/memory.c b/AcessNative/ld-acess_src/memory.c index f1147c26..99238a52 100644 --- a/AcessNative/ld-acess_src/memory.c +++ b/AcessNative/ld-acess_src/memory.c @@ -1,5 +1,6 @@ /* */ +#define _GNU_SOURCE // needed for MAP_ANONYMOUS to be avaliable #include "common.h" #include #include @@ -21,9 +22,26 @@ int AllocateMemory(uintptr_t VirtAddr, size_t ByteCount) size_t size = (VirtAddr & 0xFFF) + ByteCount; void *tmp; #if __WIN32__ - tmp = VirtualAlloc(base, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE); + do + { + MEMORY_BASIC_INFORMATION info; + VirtualQuery( (void*)base, &info, sizeof(info) ); + if( info.State != MEM_FREE ) { + printf("ERROR: Unable to allocate memory %p+0x%x, already allocated\n", + (void*)base, size); + base += 0x1000; + if( size < 0x1000 ) + return 0; + size -= 0x1000; + } + else + break; + } while( size >= 0x1000 ); + tmp = VirtualAlloc((void*)base, size, MEM_RESERVE|MEM_COMMIT, PAGE_EXECUTE_READWRITE); if( tmp == NULL ) { - printf("ERROR: Unable to allocate memory (%i)\n", GetLastError()); + printf("ERROR: Unable to allocate memory %p+%x (0x%x)\n", + (void*)base, size, + (int)GetLastError()); return -1; } #else @@ -41,9 +59,6 @@ int AllocateMemory(uintptr_t VirtAddr, size_t ByteCount) uintptr_t FindFreeRange(size_t ByteCount, int MaxBits) { - #if __WIN32__ - # error "Windows FindFreeRange() unimplemented" - #else uintptr_t base, ofs, size; uintptr_t end = -1; static const int PAGE_SIZE = 0x1000; @@ -53,21 +68,27 @@ uintptr_t FindFreeRange(size_t ByteCount, int MaxBits) end <<= (sizeof(intptr_t)*8-MaxBits); end >>= (sizeof(intptr_t)*8-MaxBits); - printf("end = %p\n", (void*)end); +// printf("end = %p\n", (void*)end); // for( base = 0; base < end - size; base -= PAGE_SIZE ) for( base = end - size + 1; base > 0; base -= PAGE_SIZE ) { for( ofs = 0; ofs < size; ofs += PAGE_SIZE ) { + #if __WIN32__ + MEMORY_BASIC_INFORMATION info; + VirtualQuery( (void*)(base + ofs), &info, sizeof(info) ); + if( info.State != MEM_FREE ) + break; + #else if( msync( (void*)(base+ofs), 1, 0) == 0 ) break; if( errno != ENOMEM ) perror("FindFreeRange, msync"); + #endif } if( ofs >= size ) { return base; } } return 0; - #endif }