/src/hearthstone-shared/Collision/g3dlite/G3D/System.h

https://github.com/Vanj-crew/HearthStone-Emu · C Header · 122 lines · 29 code · 24 blank · 69 comment · 0 complexity · 6658297c7423dc6c27765e5746b7f788 MD5 · raw file

  1. /**
  2. @file System.h
  3. @maintainer Morgan McGuire, matrix@graphics3d.com
  4. @cite Rob Wyatt http://www.gamasutra.com/features/wyatts_world/19990709/processor_detection_01.htm
  5. @cite Benjamin Jurke http://www.flipcode.com/cgi-bin/msg.cgi?showThread=COTD-ProcessorDetectionClass&forum=cotd&id=-1
  6. @cite Michael Herf http://www.stereopsis.com/memcpy.html
  7. @created 2003-01-25
  8. @edited 2006-04-26
  9. */
  10. #ifndef G3D_SYSTEM_H
  11. #define G3D_SYSTEM_H
  12. #include "Collision/g3dlite/G3D/platform.h"
  13. #include "Collision/g3dlite/G3D/g3dmath.h"
  14. #include <string>
  15. #ifdef G3D_OSX
  16. # include <CoreServices/CoreServices.h>
  17. #endif
  18. namespace G3D {
  19. typedef double RealTime;
  20. class System {
  21. public:
  22. /** Called automatically by the other System routines.*/
  23. static void init();
  24. /**
  25. Guarantees that the start of the array is aligned to the
  26. specified number of bytes.
  27. */
  28. static void* alignedMalloc(size_t bytes, size_t alignment);
  29. /**
  30. Uses pooled storage to optimize small allocations (1 byte to 5 kilobytes).
  31. Can be 10x to 100x faster than calling ::malloc or new.
  32. The result must be freed with free.
  33. Threadsafe on Win32.
  34. @sa calloc realloc OutOfMemoryCallback free
  35. */
  36. static void* malloc(size_t bytes);
  37. static void* calloc(size_t n, size_t x);
  38. /**
  39. @param size Size of memory that the system was trying to allocate
  40. @param recoverable If true, the system will attempt to allocate again
  41. if the callback returns true. If false, malloc is going to return
  42. NULL and this invocation is just to notify the application.
  43. @return Return true to force malloc to attempt allocation again if the
  44. error was recoverable.
  45. */
  46. typedef bool (*OutOfMemoryCallback)(size_t size, bool recoverable);
  47. /**
  48. When System::malloc fails to allocate memory because the system is
  49. out of memory, it invokes this handler (if it is not NULL).
  50. The argument to the callback is the amount of memory that malloc
  51. was trying to allocate when it ran out. If the callback returns
  52. true, System::malloc will attempt to allocate the memory again.
  53. If the callback returns false, then System::malloc will return NULL.
  54. You can use outOfMemoryCallback to free data structures or to
  55. register the failure.
  56. */
  57. static OutOfMemoryCallback outOfMemoryCallback;
  58. /**
  59. Version of realloc that works with System::malloc.
  60. */
  61. static void* realloc(void* block, size_t bytes);
  62. /** Returns a string describing how well System::malloc is using its internal pooled storage.
  63. "heap" memory was slow to allocate; the other data sizes are comparatively fast.*/
  64. static std::string mallocPerformance();
  65. static void resetMallocPerformanceCounters();
  66. /**
  67. Returns a string describing the current usage of the buffer pools used for
  68. optimizing System::malloc.
  69. */
  70. static std::string mallocStatus();
  71. /**
  72. Free data allocated with System::malloc.
  73. Threadsafe on Win32.
  74. */
  75. static void free(void* p);
  76. /**
  77. Frees memory allocated with alignedMalloc.
  78. */
  79. static void alignedFree(void* ptr);
  80. /** An implementation of memcpy that may be up to 2x as fast as the C library
  81. one on some processors. Guaranteed to have the same behavior as memcpy
  82. in all cases. */
  83. static void memcpy(void* dst, const void* src, size_t numBytes);
  84. /** An implementation of memset that may be up to 2x as fast as the C library
  85. one on some processors. Guaranteed to have the same behavior as memset
  86. in all cases. */
  87. static void memset(void* dst, uint8 value, size_t numBytes);
  88. };
  89. } // namespace
  90. #endif