public BlockMemory Rent() { lock (this) { foreach (BigChunk chunk in datablocks) { if (chunk.HasFreeBlocks()) { int index = chunk.Reserve(); BlockMemory memory = new BlockMemory(chunk, index, NumBytes); return(memory); } } BigChunk newChunk = new BigChunk(numKB, bigChunkSizeKb / numKB); datablocks.Add(newChunk); int i = newChunk.Reserve(); BlockMemory m = new BlockMemory(newChunk, i, NumBytes); return(m); } }
public ComponentMemoryBlock(EntityArchetype archetype, BlockAllocator allocator) { id = nextId++; this.archetype = archetype; //_typeLocations = new Dictionary<Type, ComponentSliceValues>(); //_typeLocations = new Dictionary<int, ComponentSliceValues>(); OptimizeDictionary(archetype, out dictionarySize, out dictionaryMask); _typeLocations = new ComponentSliceValues[dictionarySize]; _componentVersions = new long[dictionarySize]; _data = allocator.Rent(); _data.Memory.Span.Fill(0); int amountOfBytes = _data.Size(); int entitySize = Unsafe.SizeOf <Entity>(); int bytesPerEntity = entitySize; foreach (var component in archetype.components) { bytesPerEntity += component.Value; } int maxPaddingBytes = (28 * archetype.components.Count); //calculate maximum size of entities held by this memory block _maxSize = (int)MathF.Floor((float)(amountOfBytes - maxPaddingBytes) / (float)bytesPerEntity); _size = 0; //place entities as the first "component" entityComponentSlice = new ComponentSliceValues { start = 0, length = entitySize * _maxSize, componentSize = entitySize }; int nextIdx = entitySize * _maxSize; //start from after entities nextIdx = ScanToNext32(nextIdx); foreach (var component in archetype.components) { int componentLength = component.Value * _maxSize; //_typeLocations.Add(component.Key.GetHashCode(), new ComponentSliceValues { start = nextIdx, length = componentLength, componentSize = component.Value}); //_componentVersions.Add(component.Key.GetHashCode(), 0); _typeLocations[GetDictionaryKey(component.Key)] = new ComponentSliceValues { start = nextIdx, length = componentLength, componentSize = component.Value }; _componentVersions[GetDictionaryKey(component.Key)] = 0; nextIdx += componentLength; nextIdx = ScanToNext32(nextIdx); } #if DEBUG unsafe { fixed(byte *bytes = _data.memory.Span) { long address = (long)bytes; DebugHelper.AssertThrow <ApplicationException>(_typeLocations.All(x => (address + x.start) % 32 == 0)); } } #endif }