DS_BytePool.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. #include "DS_BytePool.h"
  11. #include "RakAssert.h"
  12. #ifndef __APPLE__
  13. // Use stdlib and not malloc for compatibility
  14. #include <stdlib.h>
  15. #endif
  16. using namespace DataStructures;
  17. BytePool::BytePool()
  18. {
  19. pool128.SetPageSize(8192*4);
  20. pool512.SetPageSize(8192*4);
  21. pool2048.SetPageSize(8192*4);
  22. pool8192.SetPageSize(8192*4);
  23. }
  24. BytePool::~BytePool()
  25. {
  26. }
  27. void BytePool::SetPageSize(int size)
  28. {
  29. pool128.SetPageSize(size);
  30. pool512.SetPageSize(size);
  31. pool2048.SetPageSize(size);
  32. pool8192.SetPageSize(size);
  33. }
  34. unsigned char *BytePool::Allocate(int bytesWanted, const char *file, unsigned int line)
  35. {
  36. #ifdef _DISABLE_BYTE_POOL
  37. return rakMalloc_Ex(bytesWanted, _FILE_AND_LINE_);
  38. #endif
  39. unsigned char *out;
  40. if (bytesWanted <= 127)
  41. {
  42. #ifdef _THREADSAFE_BYTE_POOL
  43. mutex128.Lock();
  44. #endif
  45. out = (unsigned char*) pool128.Allocate(file, line);
  46. #ifdef _THREADSAFE_BYTE_POOL
  47. mutex128.Unlock();
  48. #endif
  49. out[0]=0;
  50. return ((unsigned char*) out)+1;
  51. }
  52. if (bytesWanted <= 511)
  53. {
  54. #ifdef _THREADSAFE_BYTE_POOL
  55. mutex512.Lock();
  56. #endif
  57. out = (unsigned char*) pool512.Allocate(file, line);
  58. #ifdef _THREADSAFE_BYTE_POOL
  59. mutex512.Unlock();
  60. #endif
  61. out[0]=1;
  62. return ((unsigned char*) out)+1;
  63. }
  64. if (bytesWanted <= 2047)
  65. {
  66. #ifdef _THREADSAFE_BYTE_POOL
  67. mutex2048.Lock();
  68. #endif
  69. out = (unsigned char*) pool2048.Allocate(file, line);
  70. #ifdef _THREADSAFE_BYTE_POOL
  71. mutex2048.Unlock();
  72. #endif
  73. out[0]=2;
  74. return ((unsigned char*) out)+1;
  75. }
  76. if (bytesWanted <= 8191)
  77. {
  78. #ifdef _THREADSAFE_BYTE_POOL
  79. mutex8192.Lock();
  80. #endif
  81. out = (unsigned char*) pool8192.Allocate(file, line);
  82. #ifdef _THREADSAFE_BYTE_POOL
  83. mutex8192.Unlock();
  84. #endif
  85. out[0]=3;
  86. return ((unsigned char*) out)+1;
  87. }
  88. out = (unsigned char*) rakMalloc_Ex(bytesWanted+1, _FILE_AND_LINE_);
  89. out[0]=(unsigned char)255;
  90. return out+1;
  91. }
  92. void BytePool::Release(unsigned char *data, const char *file, unsigned int line)
  93. {
  94. #ifdef _DISABLE_BYTE_POOL
  95. _rakFree_Ex(data, _FILE_AND_LINE_ );
  96. #endif
  97. unsigned char *realData = data-1;
  98. switch (realData[0])
  99. {
  100. case 0:
  101. #ifdef _THREADSAFE_BYTE_POOL
  102. mutex128.Lock();
  103. #endif
  104. pool128.Release((unsigned char(*)[128]) realData, file, line );
  105. #ifdef _THREADSAFE_BYTE_POOL
  106. mutex128.Unlock();
  107. #endif
  108. break;
  109. case 1:
  110. #ifdef _THREADSAFE_BYTE_POOL
  111. mutex512.Lock();
  112. #endif
  113. pool512.Release((unsigned char(*)[512]) realData, file, line );
  114. #ifdef _THREADSAFE_BYTE_POOL
  115. mutex512.Unlock();
  116. #endif
  117. break;
  118. case 2:
  119. #ifdef _THREADSAFE_BYTE_POOL
  120. mutex2048.Lock();
  121. #endif
  122. pool2048.Release((unsigned char(*)[2048]) realData, file, line );
  123. #ifdef _THREADSAFE_BYTE_POOL
  124. mutex2048.Unlock();
  125. #endif
  126. break;
  127. case 3:
  128. #ifdef _THREADSAFE_BYTE_POOL
  129. mutex8192.Lock();
  130. #endif
  131. pool8192.Release((unsigned char(*)[8192]) realData, file, line );
  132. #ifdef _THREADSAFE_BYTE_POOL
  133. mutex8192.Unlock();
  134. #endif
  135. break;
  136. case 255:
  137. rakFree_Ex(realData, file, line );
  138. break;
  139. default:
  140. RakAssert(0);
  141. break;
  142. }
  143. }
  144. void BytePool::Clear(const char *file, unsigned int line)
  145. {
  146. (void) file;
  147. (void) line;
  148. #ifdef _THREADSAFE_BYTE_POOL
  149. pool128.Clear(file, line);
  150. pool512.Clear(file, line);
  151. pool2048.Clear(file, line);
  152. pool8192.Clear(file, line);
  153. #endif
  154. }
粤ICP备19079148号