|
|
hai 6 meses | |
|---|---|---|
| .. | ||
| README.md | hai 6 meses | |
使用 FMallocAnsi 分配内存时,根据平台不同,调用不同平台的内存申请函数 _aligned_malloc、memalign、malloc
void* FMallocAnsi::TryMalloc(SIZE_T Size, uint32 Alignment)
{
#if !UE_BUILD_SHIPPING
uint64 LocalMaxSingleAlloc = MaxSingleAlloc.Load(EMemoryOrder::Relaxed);
if (LocalMaxSingleAlloc != 0 && Size > LocalMaxSingleAlloc)
{
return nullptr;
}
#endif
Alignment = FMath::Max(Size >= 16 ? (uint32)16 : (uint32)8, Alignment);
void* Result = AnsiMalloc(Size, Alignment);
return Result;
}
void* AnsiMalloc(SIZE_T Size, uint32 Alignment)
{
#if PLATFORM_USES__ALIGNED_MALLOC
void* Result = _aligned_malloc( Size, Alignment );
#elif PLATFORM_USE_ANSI_POSIX_MALLOC
void* Result;
if (UNLIKELY(posix_memalign(&Result, Alignment, Size) != 0))
{
Result = nullptr;
}
#elif PLATFORM_USE_ANSI_MEMALIGN
void* Result = memalign(Alignment, Size);
#else
void* Ptr = malloc(Size + Alignment + sizeof(void*) + sizeof(SIZE_T));
void* Result = nullptr;
if (Ptr)
{
Result = Align((uint8*)Ptr + sizeof(void*) + sizeof(SIZE_T), Alignment);
*((void**)((uint8*)Result - sizeof(void*))) = Ptr;
*((SIZE_T*)((uint8*)Result - sizeof(void*) - sizeof(SIZE_T))) = Size;
}
#endif
return Result;
}
释放内存也是,根据平台不同调用不同的释放函数 _aligned_free、free
void AnsiFree(void* Ptr)
{
#if PLATFORM_USES__ALIGNED_MALLOC
_aligned_free(Ptr);
#elif PLATFORM_USE_ANSI_POSIX_MALLOC || PLATFORM_USE_ANSI_MEMALIGN
free(Ptr);
#else
if (Ptr)
{
free(*((void**)((uint8*)Ptr - sizeof(void*))));
}
#endif
}