b2Settings.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
  3. * Copyright (c) 2013 Google, Inc.
  4. *
  5. * This software is provided 'as-is', without any express or implied
  6. * warranty. In no event will the authors be held liable for any damages
  7. * arising from the use of this software.
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. * 1. The origin of this software must not be misrepresented; you must not
  12. * claim that you wrote the original software. If you use this software
  13. * in a product, an acknowledgment in the product documentation would be
  14. * appreciated but is not required.
  15. * 2. Altered source versions must be plainly marked as such, and must not be
  16. * misrepresented as being the original software.
  17. * 3. This notice may not be removed or altered from any source distribution.
  18. */
  19. #include <Box2D/Common/b2Settings.h>
  20. #include <stdio.h>
  21. #include <stdarg.h>
  22. #include <stdlib.h>
  23. b2Version b2_version = {2, 3, 0};
  24. #define LIQUIDFUN_VERSION_MAJOR 1
  25. #define LIQUIDFUN_VERSION_MINOR 1
  26. #define LIQUIDFUN_VERSION_REVISION 0
  27. #define LIQUIDFUN_STRING_EXPAND(X) #X
  28. #define LIQUIDFUN_STRING(X) LIQUIDFUN_STRING_EXPAND(X)
  29. static void* b2AllocDefault(int32 size, void* callbackData);
  30. static void b2FreeDefault(void* mem, void* callbackData);
  31. const b2Version b2_liquidFunVersion = {
  32. LIQUIDFUN_VERSION_MAJOR, LIQUIDFUN_VERSION_MINOR,
  33. LIQUIDFUN_VERSION_REVISION,
  34. };
  35. const char *b2_liquidFunVersionString =
  36. "LiquidFun "
  37. LIQUIDFUN_STRING(LIQUIDFUN_VERSION_MAJOR) "."
  38. LIQUIDFUN_STRING(LIQUIDFUN_VERSION_MINOR) "."
  39. LIQUIDFUN_STRING(LIQUIDFUN_VERSION_REVISION);
  40. static int32 b2_numAllocs = 0;
  41. // Initialize default allocator.
  42. static b2AllocFunction b2_allocCallback = b2AllocDefault;
  43. static b2FreeFunction b2_freeCallback = b2FreeDefault;
  44. static void *b2_callbackData = NULL;
  45. // Default implementation of b2AllocFunction.
  46. static void* b2AllocDefault(int32 size, void* callbackData)
  47. {
  48. B2_NOT_USED(callbackData);
  49. return malloc(size);
  50. }
  51. // Default implementation of b2FreeFunction.
  52. static void b2FreeDefault(void* mem, void* callbackData)
  53. {
  54. B2_NOT_USED(callbackData);
  55. free(mem);
  56. }
  57. /// Set alloc and free callbacks to override the default behavior of using
  58. /// malloc() and free() for dynamic memory allocation.
  59. /// Set allocCallback and freeCallback to NULL to restore the default
  60. /// allocator (malloc / free).
  61. void b2SetAllocFreeCallbacks(b2AllocFunction allocCallback,
  62. b2FreeFunction freeCallback, void* callbackData)
  63. {
  64. b2Assert((allocCallback && freeCallback) ||
  65. (!allocCallback && !freeCallback));
  66. b2Assert(0 == b2GetNumAllocs());
  67. if (allocCallback && freeCallback)
  68. {
  69. b2_allocCallback = allocCallback;
  70. b2_freeCallback = freeCallback;
  71. b2_callbackData = callbackData;
  72. }
  73. else
  74. {
  75. b2_allocCallback = b2AllocDefault;
  76. b2_freeCallback = b2FreeDefault;
  77. b2_callbackData = NULL;
  78. }
  79. }
  80. // Memory allocators. Modify these to use your own allocator.
  81. void* b2Alloc(int32 size)
  82. {
  83. b2_numAllocs++;
  84. return b2_allocCallback(size, b2_callbackData);
  85. }
  86. void b2Free(void* mem)
  87. {
  88. b2_numAllocs--;
  89. b2_freeCallback(mem, b2_callbackData);
  90. }
  91. void b2SetNumAllocs(const int32 numAllocs)
  92. {
  93. b2_numAllocs = numAllocs;
  94. }
  95. int32 b2GetNumAllocs()
  96. {
  97. return b2_numAllocs;
  98. }
  99. // You can modify this to use your logging facility.
  100. void b2Log(const char* string, ...)
  101. {
  102. #if DEBUG
  103. va_list args;
  104. va_start(args, string);
  105. vprintf(string, args);
  106. va_end(args);
  107. #else
  108. B2_NOT_USED(string);
  109. #endif
  110. }
  111. class Validator
  112. {
  113. public:
  114. Validator()
  115. {
  116. b2Assert(sizeof(uint64)==8);
  117. b2Assert(sizeof(int64)==8);
  118. }
  119. } validate;