Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions Include/PipePlatform.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,8 @@
#ifndef P_PLATFORM_LINUX
#if defined(__linux__)
#define P_PLATFORM_LINUX 1
#if defined(__ANDROID__) || defined(ANDROID)
#define P_PLATFORM_ANDROID 1
#endif
#else
#define P_PLATFORM_LINUX 0
#define P_PLATFORM_ANDROID 0
#endif
#endif
#ifndef P_PLATFORM_APPLE
Expand Down Expand Up @@ -69,6 +65,13 @@
#define P_PLATFORM_BSD 0
#endif
#endif
#ifndef P_PLATFORM_ANDROID
#if defined(__ANDROID__) || defined(ANDROID)
#define P_PLATFORM_ANDROID 1
#else
#define P_PLATFORM_ANDROID 0
#endif
#endif


#if P_PLATFORM_WINDOWS
Expand Down
24 changes: 23 additions & 1 deletion Src/PipeMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

#if P_PLATFORM_WINDOWS
#include <malloc.h> // _aligned_malloc, _aligned_free, _aligned_realloc
#elif P_PLATFORM_ANDROID
#include <malloc.h> // memalign
#endif


Expand Down Expand Up @@ -119,6 +121,8 @@ namespace p

void* HeapAlloc(sizet size)
{
// Must stay paired with HeapFree: Windows uses the aligned CRT allocators, POSIX uses
// malloc. Both provide at least the default new alignment.
#if P_PLATFORM_WINDOWS
return _aligned_malloc(size, __STDCPP_DEFAULT_NEW_ALIGNMENT__);
#else
Expand All @@ -128,10 +132,28 @@ namespace p

void* HeapAlloc(sizet size, sizet align)
{
// Dispatches per platform:
// - Windows: _aligned_malloc -> _aligned_free
// - Android: memalign -> free
// - POSIX (Linux, macOS, iOS, BSD): posix_memalign -> free
// posix_memalign needs a power-of-two alignment of at least sizeof(void*); the others
// accept any power of two. None require the size to be a multiple of the alignment
// (unlike C11 aligned_alloc).
#if P_PLATFORM_WINDOWS
return _aligned_malloc(size, align);
#elif P_PLATFORM_ANDROID
return memalign(align, size);
#else
return aligned_alloc(align, size);
if (align < sizeof(void*))
{
align = sizeof(void*);
}
void* ptr = nullptr;
if (posix_memalign(&ptr, align, size) != 0)
{
ptr = nullptr;
}
return ptr;
#endif
}

Expand Down
Loading