Deferrer.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Пул памяти для отложенных вызовов
  2. #include "common.h"
  3. #include "Deferrer.h"
  4. MemPool * MemPool::ptr = null;
  5. MemPool::MemPool()
  6. {
  7. #ifndef STOP_DEBUG
  8. numBlocks = 196 * (MAX_MCALLS + 1);
  9. #else
  10. numBlocks = 196 * 6;
  11. #endif
  12. chunkSize = 80;
  13. buffer = NEW char[numBlocks * chunkSize];
  14. list = NEW word[numBlocks];
  15. list[0] = 0xFFFF;
  16. for (dword i=1; i<numBlocks; i++) list[i] = i - 1;
  17. lastIndex = numBlocks - 1;
  18. }
  19. MemPool::~MemPool()
  20. {
  21. DELETE(buffer);
  22. DELETE(list);
  23. }
  24. void MemPool::Reset()
  25. {
  26. }
  27. void * MemPool::Alloc(dword allocSize)
  28. {
  29. // отдаем память из пулла если есть еще свободные блоки
  30. if (lastIndex != 0xFFFF && allocSize <= chunkSize)
  31. {
  32. void * ptr = (void*)&buffer[lastIndex * chunkSize];
  33. lastIndex = list[lastIndex];
  34. return ptr;
  35. }
  36. // Если память кончилась или размер большой то выделяем через api
  37. return api->Reallocate(null, allocSize, _FL_);
  38. }
  39. void MemPool::Free(void * ptr)
  40. {
  41. // Если поинтер был выделен с помощью api, освобождаем его
  42. if ((char*)ptr < buffer || (char*)ptr >= (buffer + numBlocks * chunkSize))
  43. {
  44. api->Free(ptr, _FL_);
  45. return;
  46. }
  47. // освобождаем блок
  48. dword idx = dword((char*)ptr - buffer) / chunkSize;
  49. list[idx] = lastIndex;
  50. lastIndex = word(idx);
  51. }
  52. #ifndef STOP_DEBUG
  53. void AddFloat(string & str, float value)
  54. {
  55. char res[MAX_PATH];
  56. sprintf_s(res, sizeof(res), "%f", value);
  57. // удаляем лишние нули
  58. dword len = strlen(res) - 1;
  59. while (true)
  60. {
  61. if (len < 4) break;
  62. if (res[len - 1] == '.') break;
  63. if (res[len] != '0') break;
  64. len--;
  65. }
  66. res[len] = 0;
  67. str += res;
  68. };
  69. void DeferrerTrace(string & str, dword value)
  70. {
  71. str += value;
  72. }
  73. void DeferrerTrace(string & str, long value)
  74. {
  75. str += value;
  76. }
  77. void DeferrerTrace(string & str, int value)
  78. {
  79. str += long(value);
  80. }
  81. void DeferrerTrace(string & str, float value)
  82. {
  83. AddFloat(str, value);
  84. }
  85. void DeferrerTrace(string & str, const Vector & value)
  86. {
  87. str += "Vector(";
  88. AddFloat(str, value.x); str += ", ";
  89. AddFloat(str, value.y); str += ", ";
  90. AddFloat(str, value.z); str += ")";
  91. }
  92. void DeferrerTrace(string & str, const Matrix & mtx)
  93. {
  94. str += "Matrix(";
  95. string tmp;
  96. tmp.Format("vx = (%f, %f, %f; %f), ", mtx.vx.x, mtx.vx.y, mtx.vx.z, mtx.wx);
  97. str += tmp;
  98. tmp.Format("vy = (%f, %f, %f; %f), ", mtx.vy.x, mtx.vy.y, mtx.vy.z, mtx.wy);
  99. str += tmp;
  100. tmp.Format("vz = (%f, %f, %f; %f), ", mtx.vz.x, mtx.vz.y, mtx.vz.z, mtx.wz);
  101. str += tmp;
  102. tmp.Format("pos = (%f, %f, %f; %f)", mtx.pos.x, mtx.pos.y, mtx.pos.z, mtx.w);
  103. str += tmp; str += ")";
  104. }
  105. void DeferrerTrace(string & str, const Vector4 & value)
  106. {
  107. str += "Vector4(";
  108. AddFloat(str, value.x); str += ", ";
  109. AddFloat(str, value.y); str += ", ";
  110. AddFloat(str, value.z); str += ", ";
  111. AddFloat(str, value.w); str += ")";
  112. }
  113. void DeferrerTrace(string & str, bool value)
  114. {
  115. str += (value) ? "true" : "false";
  116. }
  117. void DeferrerTrace(string & str, const Plane & value)
  118. {
  119. str += "Plane(";
  120. AddFloat(str, value.n.x); str += ", ";
  121. AddFloat(str, value.n.y); str += ", ";
  122. AddFloat(str, value.n.z); str += ", ";
  123. AddFloat(str, value.d); str += ")";
  124. }
  125. void DeferrerTrace(string & str, PhysicsCollisionGroup value)
  126. {
  127. str += dword(value);
  128. }
  129. void DeferrerTrace(string & str, IPhysMaterial * material)
  130. {
  131. str += "IPhysMaterial";
  132. }
  133. void DeferrerTrace(string & str, IAnimation * animation)
  134. {
  135. str += "IAnimation";
  136. }
  137. void DeferrerTrace(string & str, IPhysRigidBody & body)
  138. {
  139. str += "IPhysRigidBody";
  140. }
  141. #endif