lf_allocX64.cpp 3.1 KB

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