const int ELOOP(_SIGN 40) /* too many levels of symlinks detected */ #endif public static void Free(void *Address, uint Size) { switch (OS) { case OS.Windows: InternalWindows.VirtualFree(Address, 0, MEM_RELEASE); break; default: InternalUnix.munmap(Address, Size); break; } }
private static void *_AllocRange(void *Address, uint Size, bool Guard) { switch (OS) { case OS.Windows: { byte *Pointer; if (Guard) { Pointer = InternalWindows.VirtualAlloc(Address, Size, MEM_RESERVE, PAGE_GUARD); } else { Pointer = InternalWindows.VirtualAlloc(Address, Size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); } if (Pointer != null) { if ((void *)Pointer != (void *)Address) { throw (new Exception( $"Not allocated the desired address! Expected {new IntPtr(Address):X}, Allocated: {new IntPtr(Pointer):X}")); } PointerUtils.Memset(Pointer, 0, (int)Size); } return(Pointer); } default: { int errno; InternalUnix.reset_errno(); void *AllocatedAddress = InternalUnix.mmap( Address, Size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0 ); errno = InternalUnix.errno(); if (errno != 0) { Console.Error.WriteLine("mmap errno: {0} : {1}", errno, InternalUnix.strerror(errno)); } if (AllocatedAddress == (void *)-1) { AllocatedAddress = null; } if (AllocatedAddress != Address) { Console.WriteLine("Alloc pointer mismatch! {0}, {1}", new IntPtr(AllocatedAddress), new IntPtr(Address)); //Console.ReadKey(); } #if false InternalUnix.reset_errno(); int result3 = InternalUnix.mprotect(Address, Size, PROT_READ | PROT_WRITE); errno = InternalUnix.errno(); if (errno != 0) { Console.Error.WriteLine("mprotect errno: {0} : {1}", errno, InternalUnix.strerror(errno)); } if (result3 != 0) { Console.Error.WriteLine("mprotect result3: {0}", result3); } #endif return(AllocatedAddress); } } throw (new NotImplementedException()); }
public static void reset_errno() { *InternalUnix.__errno_location() = 0; }
public static int errno() { return(*InternalUnix.__errno_location()); }