allocator.cpp 1.5 KB

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