X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=AcessNative%2Fld-acess_src%2Fmemory.c;h=3f0091d3724676640cb07d6f709d11342a65c64c;hb=590ae24e57553f79a92d6ef52c0468c07aa5de22;hp=627f90245c2f0934951d448477df4d50b0480d78;hpb=a09032f44bba55ce1e60dfab92a39cf6c909220b;p=tpg%2Facess2.git diff --git a/AcessNative/ld-acess_src/memory.c b/AcessNative/ld-acess_src/memory.c index 627f9024..3f0091d3 100644 --- a/AcessNative/ld-acess_src/memory.c +++ b/AcessNative/ld-acess_src/memory.c @@ -21,49 +21,56 @@ 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); + tmp = VirtualAlloc((void*)base, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE); if( tmp == NULL ) { - printf("ERROR: Unable to allocate memory (%i)\n", GetLastError()); + printf("ERROR: Unable to allocate memory (0x%x)\n", (int)GetLastError()); return -1; } #else +// printf("AllocateMemory: mmap(%p, 0x%lx, ...)\n", (void*)base, ByteCount); tmp = mmap((void*)base, size, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0); if( tmp == MAP_FAILED ) { + printf("ERROR: Unable to allocate memory\n"); + perror("AllocateMemory"); return -1; } +// printf("AllocateMemory: RETURN 0\n"); #endif return 0; } uintptr_t FindFreeRange(size_t ByteCount, int MaxBits) { - #if __WIN32__ - # error "Windows FindFreeRange() unimplemented" - #else uintptr_t base, ofs, size; uintptr_t end = -1; - const int PAGE_SIZE = 0x1000; + static const int PAGE_SIZE = 0x1000; size = (ByteCount + PAGE_SIZE - 1) / PAGE_SIZE; size *= PAGE_SIZE; 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 }