Utility.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #ifndef CORE_UTILITY_H
  2. #define CORE_UTILITY_H
  3. #include <string.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. typedef void (*CoreExitHandler)(int, void*);
  17. [[noreturn]] void coreExitWithHandler(const char* file, int line, int value);
  18. void coreSetExitHandler(CoreExitHandler h, void* data);
  19. #define CORE_EXIT(exitValue) coreExitWithHandler(__FILE__, __LINE__, exitValue)
  20. typedef void (*CoreOutOfMemoryHandler)(void*);
  21. void coreSetOutOfMemoryHandler(CoreOutOfMemoryHandler h, void* data);
  22. #ifdef CORE_CHECK_MEMORY
  23. void* coreDebugAllocate(const char* file, int line, size_t n);
  24. void* coreDebugReallocate(const char* file, int line, void* p, size_t n);
  25. void coreFreeDebug(const char* file, int line, void* p);
  26. void corePrintMemoryReport(void);
  27. #define coreAllocate(n) coreDebugAllocate(__FILE__, __LINE__, n)
  28. #define coreReallocate(p, n) coreDebugReallocate(__FILE__, __LINE__, p, n)
  29. #define coreFree(p) coreFreeDebug(__FILE__, __LINE__, p)
  30. #else
  31. void* coreAllocate(size_t n);
  32. void* coreReallocate(void* p, size_t n);
  33. void coreFree(void* p);
  34. #define corePrintMemoryReport()
  35. #endif
  36. bool coreSleepNanos(i64 nanos);
  37. i64 coreGetNanos(void);
  38. // TODO: replace typeof with auto when available
  39. #define coreSwap(a, b) \
  40. do { \
  41. typeof(*(a)) tmp = *(a); \
  42. *(a) = *(b); \
  43. *(b) = tmp; \
  44. } while(0)
  45. #ifdef IMPORT_CORE
  46. #define popCount corePopCount
  47. #define interpolate coreInterpolate
  48. #define PI CORE_PI
  49. #define radianToDegree coreRadianToDegree
  50. #define degreeToRadian coreDegreeToRadian
  51. #define maxSize coreMaxSize
  52. #define minSize coreMinSize
  53. #define ExitHandler CoreExitHandler
  54. #define setExitHandler coreSetExitHandler
  55. #define EXIT CORE_EXIT
  56. #define OutOfMemoryHandler CoreOutOfMemoryHandler
  57. #define setOutOfMemoryHandler coreSetOutOfMemoryHandler
  58. #define printMemoryReport corePrintMemoryReport
  59. #define cAllocate coreAllocate
  60. #define cReallocate coreReallocate
  61. #define cFree coreFree
  62. #define sleepNanos coreSleepNanos
  63. #define getNanos coreGetNanos
  64. #define swap coreSwap
  65. #endif
  66. #endif