lf_allocX64.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include "lf_allocX64.h"
  2. //__arm__ id defined in clang for 32-bit ARM architectures only
  3. #if defined(__arm__) && !defined(__aarch64__)
  4. #error This allocator does not support 32-bit architectures. Please use another allocator instead!
  5. #endif
  6. //////////////////////////////////////////////////////////////////////////
  7. // hooks
  8. #if defined(USE_INTELCC) || defined(_darwin_) || defined(_freebsd_) || defined(_STLPORT_VERSION)
  9. #define OP_THROWNOTHING noexcept
  10. #else
  11. #define OP_THROWNOTHING
  12. #endif
  13. #ifndef _darwin_
  14. #if !defined(YMAKE)
  15. void* operator new(size_t size) {
  16. return LFAlloc(size);
  17. }
  18. void* operator new(size_t size, const std::nothrow_t&) OP_THROWNOTHING {
  19. return LFAlloc(size);
  20. }
  21. void operator delete(void* p)OP_THROWNOTHING {
  22. LFFree(p);
  23. }
  24. void operator delete(void* p, const std::nothrow_t&)OP_THROWNOTHING {
  25. LFFree(p);
  26. }
  27. void* operator new[](size_t size) {
  28. return LFAlloc(size);
  29. }
  30. void* operator new[](size_t size, const std::nothrow_t&) OP_THROWNOTHING {
  31. return LFAlloc(size);
  32. }
  33. void operator delete[](void* p) OP_THROWNOTHING {
  34. LFFree(p);
  35. }
  36. void operator delete[](void* p, const std::nothrow_t&) OP_THROWNOTHING {
  37. LFFree(p);
  38. }
  39. #endif
  40. //#ifndef _MSC_VER
  41. extern "C" void* malloc(size_t size) {
  42. return LFAlloc(size);
  43. }
  44. extern "C" void* valloc(size_t size) {
  45. return LFVAlloc(size);
  46. }
  47. extern "C" int posix_memalign(void** memptr, size_t alignment, size_t size) {
  48. return LFPosixMemalign(memptr, alignment, size);
  49. }
  50. extern "C" void* memalign(size_t alignment, size_t size) {
  51. void* ptr;
  52. int res = LFPosixMemalign(&ptr, alignment, size);
  53. return res ? nullptr : ptr;
  54. }
  55. extern "C" void* aligned_alloc(size_t alignment, size_t size) {
  56. return memalign(alignment, size);
  57. }
  58. #if !defined(_MSC_VER) && !defined(_freebsd_)
  59. // Workaround for pthread_create bug in linux.
  60. extern "C" void* __libc_memalign(size_t alignment, size_t size) {
  61. return memalign(alignment, size);
  62. }
  63. #endif
  64. extern "C" void free(void* ptr) {
  65. LFFree(ptr);
  66. }
  67. extern "C" void* calloc(size_t n, size_t elem_size) {
  68. // Overflow check
  69. const size_t size = n * elem_size;
  70. if (elem_size != 0 && size / elem_size != n)
  71. return nullptr;
  72. void* result = LFAlloc(size);
  73. if (result != nullptr) {
  74. memset(result, 0, size);
  75. }
  76. return result;
  77. }
  78. extern "C" void cfree(void* ptr) {
  79. LFFree(ptr);
  80. }
  81. extern "C" void* realloc(void* old_ptr, size_t new_size) {
  82. if (old_ptr == nullptr) {
  83. void* result = LFAlloc(new_size);
  84. return result;
  85. }
  86. if (new_size == 0) {
  87. LFFree(old_ptr);
  88. return nullptr;
  89. }
  90. void* new_ptr = LFAlloc(new_size);
  91. if (new_ptr == nullptr) {
  92. return nullptr;
  93. }
  94. size_t old_size = LFGetSize(old_ptr);
  95. memcpy(new_ptr, old_ptr, ((old_size < new_size) ? old_size : new_size));
  96. LFFree(old_ptr);
  97. return new_ptr;
  98. }
  99. extern "C" size_t malloc_usable_size(void* ptr) {
  100. if (ptr == nullptr) {
  101. return 0;
  102. }
  103. return LFGetSize(ptr);
  104. }
  105. NMalloc::TMallocInfo NMalloc::MallocInfo() {
  106. NMalloc::TMallocInfo r;
  107. #if defined(LFALLOC_DBG)
  108. r.Name = "lfalloc_dbg";
  109. #elif defined(LFALLOC_YT)
  110. r.Name = "lfalloc_yt";
  111. #else
  112. r.Name = "lfalloc";
  113. #endif
  114. r.SetParam = &LFAlloc_SetParam;
  115. r.GetParam = &LFAlloc_GetParam;
  116. return r;
  117. }
  118. #else
  119. NMalloc::TMallocInfo NMalloc::MallocInfo() {
  120. NMalloc::TMallocInfo r;
  121. r.Name = "system-darwin";
  122. return r;
  123. }
  124. #endif