Utility.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #ifndef CORE_UTILITY_H
  2. #define CORE_UTILITY_H
  3. #include "core/Check.h"
  4. #include "core/Types.h"
  5. size_t corePopCount(u64 u);
  6. #define coreInterpolate(a, b, factor) (a * (1.0f - factor) + b * factor)
  7. #define CORE_PI 3.14159265358979323846f
  8. #define coreRadianToDegree(radians) (radians * (180.0f / CORE_PI))
  9. #define coreDegreeToRadian(degrees) (degrees * (CORE_PI / 180.0f))
  10. inline size_t coreMaxSize(size_t a, size_t b) {
  11. return a > b ? a : b;
  12. }
  13. inline size_t coreMinSize(size_t a, size_t b) {
  14. return a < b ? a : b;
  15. }
  16. size_t coreToStringSize(const void* p, char* buffer, size_t n);
  17. size_t coreToStringInt(const void* p, char* buffer, size_t n);
  18. void coreStringAdd(size_t* w, char** buffer, size_t* n, size_t shift);
  19. void coreStringAddI(size_t* w, char** buffer, size_t* n, int shift);
  20. typedef void (*CoreExitHandler)(int, void*);
  21. [[noreturn]] void coreExitWithHandler(const char* file, int line, int value);
  22. void coreSetExitHandler(CoreExitHandler h, void* data);
  23. #define CORE_EXIT(exitValue) coreExitWithHandler(__FILE__, __LINE__, exitValue)
  24. typedef void (*CoreOutOfMemoryHandler)(void*);
  25. void coreSetOutOfMemoryHandler(CoreOutOfMemoryHandler h, void* data);
  26. #ifdef CORE_CHECK_MEMORY
  27. void* coreDebugAllocate(const char* file, int line, size_t n);
  28. void* coreDebugReallocate(const char* file, int line, void* p, size_t n);
  29. void coreFreeDebug(const char* file, int line, void* p);
  30. void corePrintMemoryReport(void);
  31. #define coreAllocate(n) coreDebugAllocate(__FILE__, __LINE__, n)
  32. #define coreReallocate(p, n) coreDebugReallocate(__FILE__, __LINE__, p, n)
  33. #define coreFree(p) coreFreeDebug(__FILE__, __LINE__, p)
  34. #else
  35. void* coreAllocate(size_t n);
  36. void* coreReallocate(void* p, size_t n);
  37. void coreFree(void* p);
  38. #define corePrintMemoryReport()
  39. #endif
  40. bool coreSleep(i64 nanos);
  41. i64 coreNanos(void);
  42. #endif