Utility.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 coreSleepNanos(i64 nanos);
  42. i64 coreGetNanos(void);
  43. #ifdef IMPORT_CORE
  44. #define popCount corePopCount
  45. #define interpolate coreInterpolate
  46. #define PI CORE_PI
  47. #define radianToDegree coreRadianToDegree
  48. #define degreeToRadian coreDegreeToRadian
  49. #define maxSize coreMaxSize
  50. #define minSize coreMinSize
  51. #define toString coreToString
  52. #define toStringSize coreToStringSize
  53. #define toStringInt coreToStringInt
  54. #define stringAdd coreStringAdd
  55. #define ExitHandler CoreExitHandler
  56. #define setExitHandler coreSetExitHandler
  57. #define EXIT CORE_EXIT
  58. #define OutOfMemoryHandler CoreOutOfMemoryHandler
  59. #define setOutOfMemoryHandler coreSetOutOfMemoryHandler
  60. #define printMemoryReport corePrintMemoryReport
  61. #define cAllocate coreAllocate
  62. #define cReallocate coreReallocate
  63. #define cFree coreFree
  64. #define sleepNanos coreSleepNanos
  65. #define getNanos coreGetNanos
  66. #endif
  67. #endif