public void RegisterAllocation(StackObject *ptr, StackObject *src, int size, int managedIndex, int managedCount) { int emptyIndex = -1; int cnt = freeBlocks.Length; for (int i = 0; i < cnt; i++) { if (freeBlocks[i].StartAddress == null) { emptyIndex = i; break; } } if (emptyIndex == -1) { emptyIndex = freeBlocks.Length; ExpandFreeList(); } StackObject *dst; int mIdx; allocCallback(size, out dst, out mIdx); if (dst != src) { throw new NotSupportedException(); } freeBlocks[emptyIndex] = new MemoryBlockInfo() { StartAddress = dst, RequestAddress = ptr, Size = size, ManagedCount = managedCount, ManagedIndex = managedIndex != int.MaxValue ? managedIndex : mIdx }; }
void ExpandFreeList() { int expandSize = Math.Min(freeBlocks.Length, 32); MemoryBlockInfo[] newArr = new MemoryBlockInfo[freeBlocks.Length + expandSize]; freeBlocks.CopyTo(newArr, 0); freeBlocks = newArr; }
public StackObjectAllocation Alloc(StackObject *ptr, int size, int managedSize) { int found = -1; int emptyIndex = -1; StackObjectAllocation alloc; int cnt = freeBlocks.Length; for (int i = 0; i < cnt; i++) { if (freeBlocks[i].StartAddress == null) { emptyIndex = i; break; } if (freeBlocks[i].RequestAddress == ptr) { if (freeBlocks[i].Size >= size && freeBlocks[i].ManagedCount >= managedSize) { return(new StackObjectAllocation() { Address = freeBlocks[i].StartAddress, ManagedIndex = freeBlocks[i].ManagedIndex }); } //freeBlocks[i].RequestAddress = null; //freeIndex = i; FreeBlock(i); } } for (int i = 0; i < cnt; i++) { if (freeBlocks[i].StartAddress == null) { break; } if (freeBlocks[i].RequestAddress == null) { if (freeBlocks[i].Size >= size && freeBlocks[i].ManagedCount >= managedSize) { found = i; break; } } } if (found >= 0) { freeBlocks[found].RequestAddress = ptr; return(new StackObjectAllocation() { Address = freeBlocks[found].StartAddress, ManagedIndex = freeBlocks[found].ManagedIndex }); } else { if (emptyIndex == -1) { emptyIndex = freeBlocks.Length; ExpandFreeList(); } allocCallback(size, out alloc.Address, out alloc.ManagedIndex); freeBlocks[emptyIndex] = new MemoryBlockInfo() { StartAddress = alloc.Address, RequestAddress = ptr, Size = size, ManagedCount = managedSize, ManagedIndex = alloc.ManagedIndex }; } return(alloc); }