Memory.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include "il2cpp-config.h"
  2. #if IL2CPP_TARGET_POSIX || IL2CPP_TARGET_N3DS && !RUNTIME_TINY
  3. #include "os/Memory.h"
  4. #include <stdint.h>
  5. #include <stdlib.h>
  6. namespace il2cpp
  7. {
  8. namespace os
  9. {
  10. namespace Memory
  11. {
  12. void* AlignedAlloc(size_t size, size_t alignment)
  13. {
  14. #if IL2CPP_TARGET_ANDROID || IL2CPP_TARGET_PSP2
  15. return memalign(alignment, size);
  16. #else
  17. void* ptr = NULL;
  18. posix_memalign(&ptr, alignment, size);
  19. return ptr;
  20. #endif
  21. }
  22. void* AlignedReAlloc(void* memory, size_t newSize, size_t alignment)
  23. {
  24. void* newMemory = realloc(memory, newSize);
  25. // Fast path: realloc returned aligned memory
  26. if ((reinterpret_cast<uintptr_t>(newMemory) & (alignment - 1)) == 0)
  27. return newMemory;
  28. // Slow path: realloc returned non-aligned memory
  29. void* alignedMemory = AlignedAlloc(newSize, alignment);
  30. memcpy(alignedMemory, newMemory, newSize);
  31. free(newMemory);
  32. return alignedMemory;
  33. }
  34. void AlignedFree(void* memory)
  35. {
  36. free(memory);
  37. }
  38. }
  39. }
  40. }
  41. #endif