Utility.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #include "core/Utility.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <threads.h>
  6. #include <time.h>
  7. #include "ErrorSimulator.h"
  8. #include "core/Logger.h"
  9. extern inline size_t coreMaxSize(size_t a, size_t b);
  10. extern inline size_t coreMinSize(size_t a, size_t b);
  11. extern inline void coreSwapSize(size_t* a, size_t* b);
  12. extern inline void coreSwapPointer(void** a, void** b);
  13. static CoreExitHandler exitHandler = nullptr;
  14. static void* exitData = nullptr;
  15. static CoreOutOfMemoryHandler outOfMemoryHandler = nullptr;
  16. static void* outOfMemoryData = nullptr;
  17. size_t corePopCount(u64 u) {
  18. static const u64 map[16] = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
  19. size_t sum = 0;
  20. for(size_t i = 0; i < sizeof(u64) * 8; i += 4) {
  21. sum += map[(u >> i) & 0xF];
  22. }
  23. return sum;
  24. }
  25. size_t coreToStringSize(const void* p, char* buffer, size_t n) {
  26. int w = snprintf(buffer, n, "%zu", *(const size_t*)p);
  27. return w < 0 ? 0 : (size_t)w;
  28. }
  29. void coreStringAdd(size_t* w, char** buffer, size_t* n, size_t shift) {
  30. *w += shift;
  31. if(*n > shift) {
  32. *buffer += shift;
  33. *n -= shift;
  34. } else {
  35. *n = 0;
  36. }
  37. }
  38. void coreStringAddI(size_t* w, char** buffer, size_t* n, int shift) {
  39. coreStringAdd(w, buffer, n, shift < 0 ? 0 : (size_t)shift);
  40. }
  41. [[noreturn]] void coreExitWithHandler(const char* file, int line, int value) {
  42. if(value != 0) {
  43. file = coreGetShortFileName(file);
  44. CORE_LOG_ERROR("Exit from %s:%d with value %d", file, line, value);
  45. }
  46. if(exitHandler != nullptr) {
  47. exitHandler(value, exitData);
  48. }
  49. exit(value);
  50. }
  51. void coreSetExitHandler(CoreExitHandler eh, void* data) {
  52. exitHandler = eh;
  53. exitData = data;
  54. }
  55. void coreSetOutOfMemoryHandler(CoreOutOfMemoryHandler h, void* data) {
  56. outOfMemoryHandler = h;
  57. outOfMemoryData = data;
  58. }
  59. static void* exitOnNull(void* p, size_t n) {
  60. if(p == nullptr) {
  61. CORE_LOG_ERROR("Out of memory, requested '%zu' bytes", n);
  62. CORE_EXIT(1);
  63. }
  64. return p;
  65. }
  66. static void* coreRealAllocate(size_t n) {
  67. void* p = malloc(n);
  68. while(p == nullptr && outOfMemoryHandler != nullptr) {
  69. outOfMemoryHandler(outOfMemoryData);
  70. p = malloc(n);
  71. }
  72. return exitOnNull(p, n);
  73. }
  74. static void* coreRealReallocate(void* oldP, size_t n) {
  75. if(n <= 0) {
  76. free(oldP);
  77. return nullptr;
  78. }
  79. void* p = realloc(oldP, n);
  80. if(p == nullptr) {
  81. while(p == nullptr && outOfMemoryHandler != nullptr) {
  82. outOfMemoryHandler(outOfMemoryData);
  83. p = realloc(oldP, n);
  84. }
  85. }
  86. return exitOnNull(p, n);
  87. }
  88. static void coreRealFree(void* p) {
  89. free(p);
  90. }
  91. #ifdef CORE_CHECK_MEMORY
  92. static const u8 CANARY[16] = {0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
  93. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF};
  94. struct CoreMemoryInfo;
  95. typedef struct CoreMemoryInfo CoreMemoryInfo;
  96. struct CoreMemoryInfo {
  97. CoreMemoryInfo* next;
  98. CoreMemoryInfo* previous;
  99. size_t size;
  100. int line;
  101. char buffer[64 - 2 * sizeof(void*) - sizeof(size_t) - sizeof(int)];
  102. char canary[sizeof(CANARY)];
  103. };
  104. static_assert(sizeof(CoreMemoryInfo) == 80, "memory info has invalid size");
  105. static CoreMemoryInfo* headMemoryInfo = nullptr;
  106. static void addMemoryInfo(CoreMemoryInfo* info, const char* file, int line,
  107. size_t n) {
  108. info->next = nullptr;
  109. info->previous = nullptr;
  110. info->size = n;
  111. info->line = line;
  112. strncpy(info->buffer, coreGetShortFileName(file), sizeof(info->buffer));
  113. memcpy(info->canary, CANARY, sizeof(CANARY));
  114. memcpy((char*)info + n - sizeof(CANARY), CANARY, sizeof(CANARY));
  115. if(headMemoryInfo == nullptr) {
  116. headMemoryInfo = info;
  117. } else {
  118. headMemoryInfo->previous = info;
  119. info->next = headMemoryInfo;
  120. headMemoryInfo = info;
  121. }
  122. }
  123. static void removeMemoryInfo(CoreMemoryInfo* info) {
  124. if(info->previous == nullptr) {
  125. if(info->next == nullptr) {
  126. headMemoryInfo = nullptr;
  127. } else {
  128. headMemoryInfo = info->next;
  129. info->next->previous = nullptr;
  130. }
  131. } else {
  132. if(info->next == nullptr) {
  133. info->previous->next = nullptr;
  134. } else {
  135. info->previous->next = info->next;
  136. info->next->previous = info->previous;
  137. }
  138. }
  139. }
  140. void* coreDebugAllocate(const char* file, int line, size_t n) {
  141. n += sizeof(CoreMemoryInfo) + sizeof(CANARY);
  142. void* p = coreRealAllocate(n + sizeof(CANARY));
  143. addMemoryInfo(p, file, line, n);
  144. return (char*)p + sizeof(CoreMemoryInfo);
  145. }
  146. void* coreDebugReallocate(const char* file, int line, void* p, size_t n) {
  147. if(n > 0) {
  148. n += sizeof(CoreMemoryInfo) + sizeof(CANARY);
  149. }
  150. void* rp = (void*)p;
  151. if(rp != nullptr) {
  152. rp = (char*)rp - sizeof(CoreMemoryInfo);
  153. removeMemoryInfo(rp);
  154. }
  155. void* np = coreRealReallocate(rp, n);
  156. if(np == nullptr) {
  157. return nullptr;
  158. }
  159. addMemoryInfo(np, file, line, n);
  160. return (char*)np + sizeof(CoreMemoryInfo);
  161. }
  162. static bool checkCanary(void* p) {
  163. return memcmp(p, CANARY, sizeof(CANARY)) != 0;
  164. }
  165. void coreFreeDebug(const char* file, int line, void* p) {
  166. if(p == nullptr) {
  167. return;
  168. }
  169. CoreMemoryInfo* rp = nullptr;
  170. void* w = (char*)p - sizeof(CoreMemoryInfo);
  171. memcpy(&rp, &w, sizeof(rp));
  172. // CoreMemoryInfo* rp = (CoreMemoryInfo*)((char*)p -
  173. // sizeof(CoreMemoryInfo));
  174. if(checkCanary(rp->canary)) {
  175. file = coreGetShortFileName(file);
  176. CORE_LOG_ERROR("Free at %s:%d violated pre canary", file, line);
  177. CORE_EXIT(1);
  178. } else if(checkCanary((char*)rp + rp->size - sizeof(CANARY))) {
  179. file = coreGetShortFileName(file);
  180. CORE_LOG_ERROR("Free at %s:%d violated post canary", file, line);
  181. CORE_EXIT(1);
  182. }
  183. removeMemoryInfo(rp);
  184. coreRealFree(rp);
  185. }
  186. void corePrintMemoryReport() {
  187. for(CoreMemoryInfo* i = headMemoryInfo; i != nullptr; i = i->next) {
  188. CORE_LOG_ERROR("%s:%d was not freed", i->buffer, i->line);
  189. }
  190. }
  191. #else
  192. void* coreAllocate(size_t n) {
  193. return coreRealAllocate(n);
  194. }
  195. void* coreReallocate(void* p, size_t n) {
  196. return coreRealReallocate(p, n);
  197. }
  198. void coreFree(void* p) {
  199. coreRealFree(p);
  200. }
  201. #endif
  202. bool coreSleep(i64 nanos) {
  203. struct timespec t;
  204. t.tv_nsec = nanos % 1'000'000'000;
  205. t.tv_sec = nanos / 1'000'000'000;
  206. return thrd_sleep(&t, nullptr) != 0;
  207. }
  208. i64 coreNanos(void) {
  209. struct timespec ts;
  210. if(timespec_get(&ts, TIME_UTC) == 0 || CORE_TIME_GET_FAIL) {
  211. return -1;
  212. }
  213. return (i64)ts.tv_sec * 1'000'000'000L + (i64)ts.tv_nsec;
  214. }