Utility.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #include "core/Utility.hpp"
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <ctime>
  6. #include "core/Logger.hpp"
  7. #include "core/Thread.hpp"
  8. static Core::ExitHandler exitHandler = nullptr;
  9. static void* exitData = nullptr;
  10. static Core::OutOfMemoryHandler outOfMemoryHandler = nullptr;
  11. static void* outOfMemoryData = nullptr;
  12. [[noreturn]] void Core::exitWithHandler(const char* file, int line, int value) {
  13. if(value != 0) {
  14. file = getShortFileName(file);
  15. LOG_ERROR("Exit from #:# with value #", file, line, value);
  16. }
  17. if(exitHandler != nullptr) {
  18. exitHandler(value, exitData);
  19. }
  20. exit(value);
  21. }
  22. void Core::setExitHandler(ExitHandler eh, void* data) {
  23. exitHandler = eh;
  24. exitData = data;
  25. }
  26. void Core::setOutOfMemoryHandler(OutOfMemoryHandler h, void* data) {
  27. outOfMemoryHandler = h;
  28. outOfMemoryData = data;
  29. std::set_new_handler([]() {
  30. if(outOfMemoryHandler != nullptr) {
  31. outOfMemoryHandler(outOfMemoryData);
  32. } else {
  33. LOG_ERROR("Out of memory");
  34. EXIT(1);
  35. }
  36. });
  37. }
  38. static void* exitOnNull(void* p, size_t n) {
  39. if(p == nullptr) {
  40. LOG_ERROR("Out of memory, requested '#' bytes", n);
  41. EXIT(1);
  42. }
  43. return p;
  44. }
  45. static void* realAllocate(size_t n) {
  46. void* p = malloc(n);
  47. while(p == nullptr && outOfMemoryHandler != nullptr) {
  48. outOfMemoryHandler(outOfMemoryData);
  49. p = malloc(n);
  50. }
  51. return exitOnNull(p, n);
  52. }
  53. static void* realReallocate(void* oldP, size_t n) {
  54. if(n <= 0) {
  55. free(oldP);
  56. return nullptr;
  57. }
  58. void* p = realloc(oldP, n);
  59. if(p == nullptr) {
  60. while(p == nullptr && outOfMemoryHandler != nullptr) {
  61. outOfMemoryHandler(outOfMemoryData);
  62. p = realloc(oldP, n);
  63. }
  64. }
  65. return exitOnNull(p, n);
  66. }
  67. static void realFree(void* p) {
  68. free(p);
  69. }
  70. #ifdef CHECK_MEMORY
  71. static const u8 CANARY[16] = {0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
  72. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF};
  73. static Core::Mutex memoryInfoMutex;
  74. struct MemoryInfo {
  75. MemoryInfo* next;
  76. MemoryInfo* previous;
  77. size_t size;
  78. int line;
  79. char buffer[64 - 2 * sizeof(void*) - sizeof(size_t) - sizeof(int)];
  80. char canary[sizeof(CANARY)];
  81. };
  82. static_assert(sizeof(MemoryInfo) == 80, "memory info has invalid size");
  83. static MemoryInfo* headMemoryInfo = nullptr;
  84. static void addMemoryInfo(MemoryInfo* i, const char* file, int line, size_t n) {
  85. i->next = nullptr;
  86. i->previous = nullptr;
  87. i->size = n;
  88. i->line = line;
  89. snprintf(i->buffer, sizeof(i->buffer), "%s", Core::getShortFileName(file));
  90. memcpy(i->canary, CANARY, sizeof(CANARY));
  91. memcpy(reinterpret_cast<char*>(i) + n, CANARY, sizeof(CANARY));
  92. Core::MutexGuard mg(memoryInfoMutex);
  93. if(headMemoryInfo == nullptr) {
  94. headMemoryInfo = i;
  95. } else {
  96. headMemoryInfo->previous = i;
  97. i->next = headMemoryInfo;
  98. headMemoryInfo = i;
  99. }
  100. }
  101. static void removeMemoryInfo(MemoryInfo* info) {
  102. Core::MutexGuard mg(memoryInfoMutex);
  103. if(info->previous == nullptr) {
  104. if(info->next == nullptr) {
  105. headMemoryInfo = nullptr;
  106. } else {
  107. headMemoryInfo = info->next;
  108. info->next->previous = nullptr;
  109. }
  110. } else {
  111. if(info->next == nullptr) {
  112. info->previous->next = nullptr;
  113. } else {
  114. info->previous->next = info->next;
  115. info->next->previous = info->previous;
  116. }
  117. }
  118. }
  119. void* Core::debugAllocateRaw(const char* file, int line, size_t n) {
  120. n += sizeof(MemoryInfo);
  121. void* p = realAllocate(n + sizeof(CANARY));
  122. addMemoryInfo(static_cast<MemoryInfo*>(p), file, line, n);
  123. return static_cast<char*>(p) + sizeof(MemoryInfo);
  124. }
  125. void* Core::debugZeroAllocateRaw(const char* file, int line, size_t n) {
  126. void* p = debugAllocateRaw(file, line, n);
  127. memset(p, 0, n);
  128. return p;
  129. }
  130. void* Core::debugReallocateRaw(const char* file, int line, void* p, size_t n) {
  131. if(n > 0) {
  132. n += sizeof(MemoryInfo) + sizeof(CANARY);
  133. }
  134. void* rp = p;
  135. if(rp != nullptr) {
  136. rp = static_cast<char*>(rp) - sizeof(MemoryInfo);
  137. removeMemoryInfo(static_cast<MemoryInfo*>(rp));
  138. }
  139. void* np = realReallocate(rp, n);
  140. if(np == nullptr) {
  141. return nullptr;
  142. }
  143. addMemoryInfo(static_cast<MemoryInfo*>(np), file, line, n - sizeof(CANARY));
  144. return static_cast<char*>(np) + sizeof(MemoryInfo);
  145. }
  146. static bool checkCanary(void* p) {
  147. return memcmp(p, CANARY, sizeof(CANARY)) != 0;
  148. }
  149. void Core::debugDeallocateRaw(void* p) {
  150. if(p == nullptr) {
  151. return;
  152. }
  153. void* w = static_cast<char*>(p) - sizeof(MemoryInfo);
  154. MemoryInfo* rp = static_cast<MemoryInfo*>(w);
  155. rp->buffer[sizeof(rp->buffer) - 1] = '\0'; // end might be broken
  156. if(checkCanary(rp->canary)) {
  157. LOG_ERROR("Free at #:# violated pre canary", rp->buffer, rp->line);
  158. EXIT(1);
  159. } else if(checkCanary(reinterpret_cast<char*>(rp) + rp->size)) {
  160. LOG_ERROR("Free at #:# violated post canary", rp->buffer, rp->line);
  161. EXIT(1);
  162. }
  163. removeMemoryInfo(rp);
  164. realFree(rp);
  165. }
  166. void Core::printMemoryReport() {
  167. Core::MutexGuard mg(memoryInfoMutex);
  168. size_t counter = 0;
  169. for(MemoryInfo* i = headMemoryInfo; i != nullptr; i = i->next) {
  170. if(i->line < 0) {
  171. counter++;
  172. } else {
  173. LOG_ERROR("#:# was not freed", i->buffer, i->line);
  174. }
  175. }
  176. if(counter > 0) {
  177. LOG_ERROR("# unknown entries were not freed", counter);
  178. }
  179. }
  180. void* operator new(size_t count) {
  181. return Core::debugAllocateRaw("unknown", -1, count);
  182. }
  183. void* operator new(size_t count, const char* file, int line) {
  184. return Core::debugAllocateRaw(file, line, count);
  185. }
  186. void* operator new[](size_t count) {
  187. return Core::debugAllocateRaw("unknown", -1, count);
  188. }
  189. void* operator new[](size_t count, const char* file, int line) {
  190. return Core::debugAllocateRaw(file, line, count);
  191. }
  192. void operator delete(void* p) noexcept {
  193. Core::debugDeallocateRaw(p);
  194. }
  195. void operator delete(void* p, size_t) noexcept {
  196. Core::debugDeallocateRaw(p);
  197. }
  198. void operator delete[](void* p) noexcept {
  199. Core::debugDeallocateRaw(p);
  200. }
  201. void operator delete[](void* p, size_t) noexcept {
  202. Core::debugDeallocateRaw(p);
  203. }
  204. #else
  205. void* Core::allocateRaw(size_t n) {
  206. return realAllocate(n);
  207. }
  208. void* Core::zeroAllocateRaw(size_t n) {
  209. void* p = allocateRaw(n);
  210. memset(p, 0, n);
  211. return p;
  212. }
  213. void* Core::reallocateRaw(void* p, size_t n) {
  214. return realReallocate(p, n);
  215. }
  216. void Core::deallocateRaw(void* p) {
  217. realFree(p);
  218. }
  219. #endif