allocator.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright 2010-2025 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx/blob/master/LICENSE
  4. */
  5. #include <bx/allocator.h>
  6. #include <malloc.h>
  7. #ifndef BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT
  8. # define BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT 8
  9. #endif // BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT
  10. namespace bx
  11. {
  12. DefaultAllocator::DefaultAllocator()
  13. {
  14. }
  15. DefaultAllocator::~DefaultAllocator()
  16. {
  17. }
  18. void* DefaultAllocator::realloc(void* _ptr, size_t _size, size_t _align, const char* _filePath, uint32_t _line)
  19. {
  20. if (0 == _size)
  21. {
  22. if (NULL != _ptr)
  23. {
  24. if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align)
  25. {
  26. ::free(_ptr);
  27. return NULL;
  28. }
  29. # if BX_COMPILER_MSVC
  30. BX_UNUSED(_filePath, _line);
  31. _aligned_free(_ptr);
  32. # else
  33. alignedFree(this, _ptr, _align, Location(_filePath, _line) );
  34. # endif // BX_
  35. }
  36. return NULL;
  37. }
  38. else if (NULL == _ptr)
  39. {
  40. if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align)
  41. {
  42. return ::malloc(_size);
  43. }
  44. # if BX_COMPILER_MSVC
  45. BX_UNUSED(_filePath, _line);
  46. return _aligned_malloc(_size, _align);
  47. # else
  48. return alignedAlloc(this, _size, _align, Location(_filePath, _line) );
  49. # endif // BX_
  50. }
  51. if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align)
  52. {
  53. return ::realloc(_ptr, _size);
  54. }
  55. # if BX_COMPILER_MSVC
  56. BX_UNUSED(_filePath, _line);
  57. return _aligned_realloc(_ptr, _size, _align);
  58. # else
  59. return alignedRealloc(this, _ptr, _size, _align, Location(_filePath, _line) );
  60. # endif // BX_
  61. }
  62. } // namespace bx