96 lines
1.9 KiB
C
96 lines
1.9 KiB
C
////////////////////////////////////////////////////////////
|
|
//~ Win32 memory allocation
|
|
|
|
#if IsPlatformWindows
|
|
|
|
//- Reserve
|
|
void *ReserveMemory(u64 size)
|
|
{
|
|
void *ptr = VirtualAlloc(0, size, MEM_RESERVE, PAGE_NOACCESS);
|
|
return ptr;
|
|
}
|
|
|
|
void ReleaseMemory(void *address)
|
|
{
|
|
VirtualFree(address, 0, MEM_RELEASE);
|
|
}
|
|
|
|
//- Commit
|
|
void *CommitMemory(void *address, u64 size)
|
|
{
|
|
void *ptr = VirtualAlloc(address, size, MEM_COMMIT, PAGE_READWRITE);
|
|
return ptr;
|
|
}
|
|
|
|
void DecommitMemory(void *address, u64 size)
|
|
{
|
|
VirtualFree(address, size, MEM_DECOMMIT);
|
|
}
|
|
|
|
//- Protect
|
|
void SetMemoryReadonly(void *address, u64 size)
|
|
{
|
|
DWORD old;
|
|
VirtualProtect(address, size, PAGE_READONLY, &old);
|
|
}
|
|
|
|
void SetMemoryReadWrite(void *address, u64 size)
|
|
{
|
|
DWORD old;
|
|
VirtualProtect(address, size, PAGE_READWRITE, &old);
|
|
}
|
|
|
|
#else
|
|
# error Memory allocation not implemented for this platform
|
|
#endif /* IsPlatformWindows */
|
|
|
|
////////////////////////////////////////////////////////////
|
|
//~ Crtlib mem op stubs
|
|
|
|
#if !IsCrtlibEnabled
|
|
|
|
//- memcpy
|
|
__attribute((section(".text.memcpy")))
|
|
void *memcpy(void *__restrict dst, const void *__restrict src, u64 count)
|
|
{
|
|
char *dst_pchar = dst;
|
|
char *src_pchar = src;
|
|
for (u64 i = 0; i < count; ++i)
|
|
{
|
|
dst_pchar[i] = src_pchar[i];
|
|
}
|
|
return dst;
|
|
}
|
|
|
|
//- memset
|
|
__attribute((section(".text.memset")))
|
|
void *memset(void *dst, i32 c, u64 count)
|
|
{
|
|
char *dst_pchar = dst;
|
|
for (u64 i = 0; i < count; ++i)
|
|
{
|
|
dst_pchar[i] = (char)c;
|
|
}
|
|
return dst;
|
|
}
|
|
|
|
//- memcmp
|
|
__attribute((section(".text.memcmp")))
|
|
i32 memcmp(const void *p1, const void *p2, u64 count)
|
|
{
|
|
i32 result = 0;
|
|
char *p1_pchar = p1;
|
|
char *p2_pchar = p2;
|
|
for (u64 i = 0; i < count; ++i)
|
|
{
|
|
result = p1_pchar[i] - p2_pchar[i];
|
|
if (result != 0)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
#endif /* !IsCrtlibEnabled */
|