Utility.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. check_format(3, 4) size_t
  17. coreToString(char* buffer, size_t n, const char* format, ...);
  18. size_t coreToStringSize(const void* p, char* buffer, size_t n);
  19. size_t coreToStringInt(const void* p, char* buffer, size_t n);
  20. void coreStringAdd(size_t* w, char** buffer, size_t* n, size_t shift);
  21. typedef void (*CoreExitHandler)(int, void*);
  22. [[noreturn]] void coreExitWithHandler(const char* file, int line, int value);
  23. void coreSetExitHandler(CoreExitHandler h, void* data);
  24. #define CORE_EXIT(exitValue) coreExitWithHandler(__FILE__, __LINE__, exitValue)
  25. typedef void (*CoreOutOfMemoryHandler)(void*);
  26. void coreSetOutOfMemoryHandler(CoreOutOfMemoryHandler h, void* data);
  27. #ifdef CORE_CHECK_MEMORY
  28. void* coreDebugAllocate(const char* file, int line, size_t n);
  29. void* coreDebugReallocate(const char* file, int line, void* p, size_t n);
  30. void coreFreeDebug(const char* file, int line, void* p);
  31. void corePrintMemoryReport(void);
  32. #define coreAllocate(n) coreDebugAllocate(__FILE__, __LINE__, n)
  33. #define coreReallocate(p, n) coreDebugReallocate(__FILE__, __LINE__, p, n)
  34. #define coreFree(p) coreFreeDebug(__FILE__, __LINE__, p)
  35. #else
  36. void* coreAllocate(size_t n);
  37. void* coreReallocate(void* p, size_t n);
  38. void coreFree(void* p);
  39. #define corePrintMemoryReport()
  40. #endif
  41. bool coreSleep(i64 nanos);
  42. i64 coreNanos(void);
  43. #endif