StackObjectAllocator.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. using ILRuntime.Runtime.Intepreter;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. namespace ILRuntime.Runtime.Stack
  7. {
  8. unsafe struct MemoryBlockInfo
  9. {
  10. public StackObject* RequestAddress;
  11. public StackObject* StartAddress;
  12. public int Size;
  13. public int ManagedIndex;
  14. public int ManagedCount;
  15. }
  16. public unsafe struct StackObjectAllocation
  17. {
  18. public StackObject* Address;
  19. public int ManagedIndex;
  20. }
  21. public unsafe delegate void StackObjectAllocateCallback(int size, out StackObject* ptr, out int managedIdx);
  22. public unsafe class StackObjectAllocator
  23. {
  24. MemoryBlockInfo[] freeBlocks;
  25. StackObjectAllocateCallback allocCallback;
  26. public StackObjectAllocator(StackObjectAllocateCallback cb)
  27. {
  28. allocCallback = cb;
  29. freeBlocks = new MemoryBlockInfo[8];
  30. }
  31. public void Clear()
  32. {
  33. for(int i = 0; i < freeBlocks.Length; i++)
  34. {
  35. if (freeBlocks[i].StartAddress == null)
  36. break;
  37. freeBlocks[i] = default(MemoryBlockInfo);
  38. }
  39. }
  40. void ExpandFreeList()
  41. {
  42. int expandSize = Math.Min(freeBlocks.Length, 32);
  43. MemoryBlockInfo[] newArr = new MemoryBlockInfo[freeBlocks.Length + expandSize];
  44. freeBlocks.CopyTo(newArr, 0);
  45. freeBlocks = newArr;
  46. }
  47. void FreeBlock(int idx)
  48. {
  49. freeBlocks[idx].RequestAddress = null;
  50. var cnt = freeBlocks.Length;
  51. int freeSize = 0;
  52. int freeManaged = 0;
  53. int freeBlock = 0;
  54. for (int i = idx-1; i >= 0; i--)
  55. {
  56. if (freeBlocks[i].RequestAddress == null)
  57. {
  58. idx = i;
  59. }
  60. else
  61. break;
  62. }
  63. for (int i = idx + 1; i < cnt; i++)
  64. {
  65. if (freeBlocks[i].StartAddress == null)
  66. break;
  67. if (freeBlocks[i].RequestAddress == null)
  68. {
  69. freeSize += freeBlocks[i].Size;
  70. freeManaged += freeBlocks[i].ManagedCount;
  71. freeBlock++;
  72. }
  73. else
  74. break;
  75. }
  76. if (freeBlock > 0)
  77. {
  78. freeBlocks[idx].Size += freeSize;
  79. freeBlocks[idx].ManagedCount += freeManaged;
  80. int tail = idx + freeBlock + 1;
  81. if (tail < freeBlocks.Length)
  82. {
  83. Array.Copy(freeBlocks, tail, freeBlocks, idx + 1, cnt - tail);
  84. }
  85. for (int i = cnt - freeBlock; i < cnt; i++)
  86. {
  87. freeBlocks[i] = default(MemoryBlockInfo);
  88. }
  89. }
  90. }
  91. public void FreeBefore(StackObject* ptr)
  92. {
  93. int firstHit = -1;
  94. var cnt = freeBlocks.Length;
  95. for (int i = 0; i < cnt; i++)
  96. {
  97. if (freeBlocks[i].StartAddress == null)
  98. break;
  99. if (freeBlocks[i].StartAddress <= ptr)
  100. {
  101. freeBlocks[i] = default(MemoryBlockInfo);
  102. if (firstHit < 0)
  103. firstHit = i;
  104. }
  105. }
  106. if (firstHit >= 0)
  107. {
  108. int validIdx = 0;
  109. for (int i = firstHit; i < cnt; i++)
  110. {
  111. if (freeBlocks[i].StartAddress != null)
  112. {
  113. if (validIdx != i)
  114. {
  115. freeBlocks[validIdx++] = freeBlocks[i];
  116. }
  117. }
  118. }
  119. }
  120. }
  121. public void Free(StackObject* ptr)
  122. {
  123. var cnt = freeBlocks.Length;
  124. for (int i = 0; i < cnt; i++)
  125. {
  126. if (freeBlocks[i].StartAddress == null)
  127. break;
  128. if (freeBlocks[i].RequestAddress == ptr)
  129. {
  130. FreeBlock(i);
  131. break;
  132. }
  133. }
  134. }
  135. public void RegisterAllocation(StackObject* ptr, StackObject* src, int size, int managedIndex, int managedCount)
  136. {
  137. int emptyIndex = -1;
  138. int cnt = freeBlocks.Length;
  139. for (int i = 0; i < cnt; i++)
  140. {
  141. if (freeBlocks[i].StartAddress == null)
  142. {
  143. emptyIndex = i;
  144. break;
  145. }
  146. }
  147. if (emptyIndex == -1)
  148. {
  149. emptyIndex = freeBlocks.Length;
  150. ExpandFreeList();
  151. }
  152. StackObject* dst;
  153. int mIdx;
  154. allocCallback(size, out dst, out mIdx);
  155. if (dst != src)
  156. throw new NotSupportedException();
  157. freeBlocks[emptyIndex] = new MemoryBlockInfo()
  158. {
  159. StartAddress = dst,
  160. RequestAddress = ptr,
  161. Size = size,
  162. ManagedCount = managedCount,
  163. ManagedIndex = managedIndex != int.MaxValue ? managedIndex : mIdx
  164. };
  165. }
  166. public bool AllocExisting(StackObject* ptr, int size, int managedSize, out StackObjectAllocation alloc)
  167. {
  168. int cnt = freeBlocks.Length;
  169. for (int i = 0; i < cnt; i++)
  170. {
  171. if (freeBlocks[i].StartAddress == null)
  172. {
  173. break;
  174. }
  175. if (freeBlocks[i].RequestAddress == ptr || freeBlocks[i].RequestAddress == null)
  176. {
  177. if (freeBlocks[i].Size >= size && freeBlocks[i].ManagedCount >= managedSize)
  178. {
  179. freeBlocks[i].RequestAddress = ptr;
  180. alloc = new StackObjectAllocation()
  181. {
  182. Address = freeBlocks[i].StartAddress,
  183. ManagedIndex = freeBlocks[i].ManagedIndex
  184. };
  185. return true;
  186. }
  187. }
  188. }
  189. alloc = new StackObjectAllocation();
  190. return false;
  191. }
  192. public StackObjectAllocation Alloc(StackObject* ptr, int size, int managedSize)
  193. {
  194. int found = -1;
  195. int emptyIndex = -1;
  196. StackObjectAllocation alloc;
  197. int cnt = freeBlocks.Length;
  198. for (int i = 0; i < cnt; i++)
  199. {
  200. if (freeBlocks[i].StartAddress == null)
  201. {
  202. emptyIndex = i;
  203. break;
  204. }
  205. if (freeBlocks[i].RequestAddress == ptr)
  206. {
  207. if (freeBlocks[i].Size >= size && freeBlocks[i].ManagedCount >= managedSize)
  208. {
  209. return new StackObjectAllocation()
  210. {
  211. Address = freeBlocks[i].StartAddress,
  212. ManagedIndex = freeBlocks[i].ManagedIndex
  213. };
  214. }
  215. //freeBlocks[i].RequestAddress = null;
  216. //freeIndex = i;
  217. FreeBlock(i);
  218. }
  219. }
  220. for (int i = 0; i < cnt; i++)
  221. {
  222. if (freeBlocks[i].StartAddress == null)
  223. break;
  224. if (freeBlocks[i].RequestAddress == null)
  225. {
  226. if (freeBlocks[i].Size >= size && freeBlocks[i].ManagedCount >= managedSize)
  227. {
  228. found = i;
  229. break;
  230. }
  231. }
  232. }
  233. if (found >= 0)
  234. {
  235. freeBlocks[found].RequestAddress = ptr;
  236. return new StackObjectAllocation()
  237. {
  238. Address = freeBlocks[found].StartAddress,
  239. ManagedIndex = freeBlocks[found].ManagedIndex
  240. };
  241. }
  242. else
  243. {
  244. if (emptyIndex == -1)
  245. {
  246. emptyIndex = freeBlocks.Length;
  247. ExpandFreeList();
  248. }
  249. allocCallback(size, out alloc.Address, out alloc.ManagedIndex);
  250. freeBlocks[emptyIndex] = new MemoryBlockInfo()
  251. {
  252. StartAddress = alloc.Address,
  253. RequestAddress = ptr,
  254. Size = size,
  255. ManagedCount = managedSize,
  256. ManagedIndex = alloc.ManagedIndex
  257. };
  258. }
  259. return alloc;
  260. }
  261. }
  262. }