protected ShapeBatch(BufferPool pool, int initialShapeCount) { this.pool = pool; TypeId = default(TShape).TypeId; InternalResize(initialShapeCount, 0); idPool = new IdPool(initialShapeCount, pool); }
public Horizon(Switch Ns) { this.Ns = Ns; IdGen = new IdPool(); NvMapIds = new IdPool(); Handles = new IdPoolWithObj(); Fds = new IdPoolWithObj(); Displays = new IdPoolWithObj(); Mutexes = new ConcurrentDictionary <long, Mutex>(); CondVars = new ConcurrentDictionary <long, CondVar>(); Processes = new ConcurrentDictionary <int, Process>(); Allocator = new AMemoryAlloc(); HidOffset = Allocator.Alloc(HidSize); FontOffset = Allocator.Alloc(FontSize); HidSharedMem = new HSharedMem(HidOffset); HidSharedMem.MemoryMapped += HidInit; HidHandle = Handles.GenerateId(HidSharedMem); FontHandle = Handles.GenerateId(new HSharedMem(FontOffset)); }
protected ShapeBatch(BufferPool pool, int initialShapeCount) { this.pool = pool; TypeId = default(TShape).TypeId; InternalResize(initialShapeCount, 0); IdPool <Buffer <int> > .Create(pool.SpecializeFor <int>(), initialShapeCount, out idPool); }
private static async Task TestIdPool_parallel(IdPool pool) { const int COUNT = 10000; List <Task> tasks = new(); TaskCompletionSource tcs = new(); SemaphoreSlim sem = new(1, 1); HashSet <int> ids = new(); for (int i = 0; i < COUNT; i++) { tasks.Add(Task.Run(async() => { await tcs.Task; //synchronize tasks _ = pool.GetNextId(out int id); await sem.WaitAsync(); try { Assert.IsFalse(ids.Contains(id), $"id {id} was given out multiple times"); ids.Add(id); } finally { sem.Release(); } })); } tcs.SetResult(); await Task.WhenAll(tasks); }
/// <summary> /// Creates a character controller systme. /// </summary> /// <param name="pool">Pool to allocate resources from.</param> /// <param name="initialCharacterCapacity">Number of characters to initially allocate space for.</param> /// <param name="initialBodyHandleCapacity">Number of body handles to initially allocate space for in the body handle->character mapping.</param> public CharacterControllers(BufferPool pool, int initialCharacterCapacity = 4096, int initialBodyHandleCapacity = 4096) { this.pool = pool; characters = new QuickList <CharacterController>(initialCharacterCapacity, pool); characterIdPool = new IdPool(initialCharacterCapacity, pool); ResizeBodyHandleCapacity(initialBodyHandleCapacity); analyzeContactsWorker = AnalyzeContactsWorker; }
public void should_allocate_and_remove_next_available_id() { var idPool = new IdPool(10, 10); var id = idPool.AllocateInstance(); Assert.InRange(id, 1, 10); Assert.DoesNotContain(id, idPool.AvailableIds); }
public Deactivator(Bodies bodies, Solver solver, BufferPool pool) { this.bodies = bodies; this.solver = solver; this.pool = pool; IdPool <Buffer <int> > .Create(pool.SpecializeFor <int>(), 16, out islandIdPool); workDelegate = Work; }
/// <summary> /// Creates a character controller systme. /// </summary> /// <param name="pool">Pool to allocate resources from.</param> /// <param name="initialCharacterCapacity">Number of characters to initially allocate space for.</param> /// <param name="initialBodyHandleCapacity">Number of body handles to initially allocate space for in the body handle->character mapping.</param> public CharacterControllers(BufferPool pool, int initialCharacterCapacity = 4096, int initialBodyHandleCapacity = 4096) { this.pool = pool; characters = new QuickList <CharacterController>(initialCharacterCapacity, pool); IdPool <Buffer <int> > .Create(pool.SpecializeFor <int>(), initialCharacterCapacity, out characterIdPool); ResizeBodyHandleCapacity(initialBodyHandleCapacity); analyzeContactsWorker = AnalyzeContactsWorker; }
private void CreateTypes(ref TranslationUnitData transUnit) { var idPool = Environment !.IdPool; foreach (ref var astUnit in transUnit.AstUnits.Span) { foreach (var nm in astUnit.Ast.Namespaces) { ArrayPointer <byte> namespaceName; using (var nameArr = nm.NamespaceName.ToPooledChars()) namespaceName = idPool.GetIdentifier(nameArr); var namespaceBuilder = EnvironmentBuilder !.GetOrCreateNamespace(namespaceName); foreach (var type in nm.Contents) { switch (type) { case ES_AstClassDefinition classDef: CreateTypes_Aggregate(ref transUnit, namespaceBuilder, ES_TypeTag.Class, classDef); break; case ES_AstStructDefinition structDef: CreateTypes_Aggregate(ref transUnit, namespaceBuilder, ES_TypeTag.Struct, structDef); break; case ES_AstEnumDefinition enumDef: { var typeName = Environment !.IdPool.GetIdentifier(enumDef.Name.Text.Span); if (namespaceBuilder.CheckTypeExists(typeName, null) != null) { errorList.Add(ES_FrontendErrors.GenTypeAlreadyDefined( namespaceBuilder.NamespaceData.NamespaceNameString, enumDef.Name.Text.Span.GetPooledString(), enumDef.Name )); break; } var builder = namespaceBuilder.GetOrCreateEnum(enumDef.AccessModifier, typeName, transUnit.Name); EnvironmentBuilder !.PointerAstMap.Add((IntPtr)builder.EnumData, enumDef); break; } case ES_AstFunctionDefinition: // Functions are handled in a pass of their own. break; default: throw new NotImplementedException("Node type not implemented yet."); } } } } }
private Pointer <ES_TypeInfo> GenerateBuiltinTypes_Float(ES_FloatSize size) { var floatDataPtr = EnvironmentBuilder !.MemoryManager.GetMemory <ES_FloatTypeData> (); var namePtr = Environment !.IdPool.GetIdentifier(ES_PrimitiveTypes.GetFloatName(size)); var fqn = new ES_FullyQualifiedName(Environment.GlobalTypesNamespace, namePtr); *floatDataPtr = new ES_FloatTypeData(ES_AccessModifier.Public, ArrayPointer <byte> .Null, fqn, size); return(new Pointer <ES_TypeInfo> (&floatDataPtr->TypeInfo)); }
private Pointer <ES_TypeInfo> GenerateBuiltinTypes_Simple(ReadOnlySpan <char> name, ES_TypeTag tag, int runtimeSize) { var voidDataPtr = EnvironmentBuilder !.MemoryManager.GetMemory <ES_TypeInfo> (); var namePtr = Environment !.IdPool.GetIdentifier(name); var fqn = new ES_FullyQualifiedName(Environment.GlobalTypesNamespace, namePtr); *voidDataPtr = new ES_TypeInfo(tag, ES_AccessModifier.Public, ES_TypeFlag.NoRefs | ES_TypeFlag.NoNew, ArrayPointer <byte> .Null, fqn); voidDataPtr->RuntimeSize = runtimeSize; return(new Pointer <ES_TypeInfo> (voidDataPtr)); }
public unsafe Statics(BufferPool pool, Shapes shapes, Bodies bodies, BroadPhase broadPhase, int initialCapacity = 4096) { this.pool = pool; InternalResize(Math.Max(1, initialCapacity)); this.shapes = shapes; this.bodies = bodies; this.broadPhase = broadPhase; IdPool <Buffer <int> > .Create(pool.SpecializeFor <int>(), initialCapacity, out HandlePool); }
public int GetId(int?id = null) { if (false == id.HasValue) { return(IdPool.AllocateInstance()); } IdPool.AllocateSpecificId(id.Value); return(id.Value); }
public unsafe Statics(BufferPool pool, PhysicsShapes3D shapes, Bodies3D bodies3D, BroadPhase broadPhase, int initialCapacity = 4096) { this.pool = pool; InternalResize(Math.Max(1, initialCapacity)); this.shapes = shapes; this.bodies3D = bodies3D; this.broadPhase = broadPhase; HandlePool = new IdPool(initialCapacity, pool); }
public void should_expand_and_allocate_and_remove_specific_id_when_claiming_bigger_than_available() { var explicitNewId = 30; var idPool = new IdPool(10, 10); idPool.AllocateSpecificId(explicitNewId); Assert.Equal(idPool.AvailableIds.Count, explicitNewId - 1); var expectedIdEntries = Enumerable.Range(1, explicitNewId - 2).ToArray(); Assert.All(idPool.AvailableIds, x => expectedIdEntries.Contains(x)); }
public void should_expand_correctly_for_explicit_id() { var explicitNewId = 30; var idPool = new IdPool(10, 10); idPool.Expand(explicitNewId); Assert.Equal(idPool.AvailableIds.Count, explicitNewId); var expectedIdEntries = Enumerable.Range(1, explicitNewId - 1).ToArray(); Assert.All(idPool.AvailableIds, x => expectedIdEntries.Contains(x)); }
public void should_expand_correctly_with_auto_expansion() { var defaultExpansionAmount = 30; var originalSize = 10; var expectedSize = defaultExpansionAmount + originalSize; var idPool = new IdPool(defaultExpansionAmount, originalSize); idPool.Expand(); Assert.Equal(idPool.AvailableIds.Count, expectedSize); var expectedIdEntries = Enumerable.Range(1, expectedSize - 1).ToArray(); Assert.All(idPool.AvailableIds, x => expectedIdEntries.Contains(x)); }
public ref T CreateContinuation(int slotsInContinuation, BufferPool pool, out int index) { if (!Continuations.Allocated) { Debug.Assert(!IdPool.Allocated); //Lazy initialization. pool.Take(InitialCapacity, out Continuations); IdPool <Buffer <int> > .Create(pool.SpecializeFor <int>(), InitialCapacity, out IdPool); } index = IdPool.Take(); if (index >= Continuations.Length) { pool.Resize(ref Continuations, index + 1, index); } ref var continuation = ref Continuations[index];
public ref T CreateContinuation(int slotsInContinuation, BufferPool pool, out int index) { if (!Continuations.Allocated) { Debug.Assert(!IdPool.Allocated); //Lazy initialization. pool.TakeAtLeast(InitialCapacity, out Continuations); IdPool = new IdPool(InitialCapacity, pool); } index = IdPool.Take(); if (index >= Continuations.Length) { pool.ResizeToAtLeast(ref Continuations, index + 1, index); } ref var continuation = ref Continuations[index];
public void should_expand_and_allocate_and_remove_next_available_id_when_empty() { var defaultExpansionAmount = 30; var originalSize = 10; var expectedSize = defaultExpansionAmount + originalSize; var idPool = new IdPool(defaultExpansionAmount, originalSize); idPool.AvailableIds.Clear(); var id = idPool.AllocateInstance(); var expectedIdEntries = Enumerable.Range(1, expectedSize - 1).ToList(); Assert.InRange(id, 1, expectedSize); Assert.DoesNotContain(id, idPool.AvailableIds); Assert.All(idPool.AvailableIds, x => expectedIdEntries.Contains(x)); }
/// <summary> /// Constructs a new bodies collection. Initialize must be called for the instance to be ready for use. /// </summary> /// <param name="pool">Pool for the collection to pull persistent allocations from.</param> /// <param name="shapes">Shapes referenced by the collection's bodies.</param> /// <param name="broadPhase">Broad phase containing the body collidables.</param> /// <param name="initialBodyCapacity">Initial number of bodies to allocate space for in the active set.</param> /// <param name="initialIslandCapacity">Initial number of islands to allocate space for in the Sets buffer.</param> /// <param name="initialConstraintCapacityPerBody">Expected number of constraint references per body to allocate space for.</param> public unsafe Bodies(BufferPool pool, Shapes shapes, BroadPhase broadPhase, int initialBodyCapacity, int initialIslandCapacity, int initialConstraintCapacityPerBody) { this.pool = pool; //Note that the id pool only grows upon removal, so this is just a heuristic initialization. //You could get by with something a lot less aggressive, but it does tend to avoid resizes in the case of extreme churn. IdPool <Buffer <int> > .Create(pool.SpecializeFor <int>(), initialBodyCapacity, out HandlePool); ResizeHandles(initialBodyCapacity); ResizeSetsCapacity(initialIslandCapacity + 1, 0); ActiveSet = new BodySet(initialBodyCapacity, pool); this.shapes = shapes; this.broadPhase = broadPhase; MinimumConstraintCapacityPerBody = initialConstraintCapacityPerBody; }
public void should_correctly_keep_expanding_when_continually_allocating() { var expectedSize = 5000; var idPool = new IdPool(); var expectedAllocations = Enumerable.Range(1, expectedSize).ToList(); var actualAllocations = new List <int>(); for (var i = 0; i < expectedSize; i++) { var allocation = idPool.AllocateInstance(); actualAllocations.Add(allocation); } Assert.Equal(expectedSize, actualAllocations.Count); Assert.All(actualAllocations, x => expectedAllocations.Contains(x)); }
private ES_NamespaceData?GetNamespace(SourceData src, ES_AstNodeBounds nodeBounds, ReadOnlySpan <char> namespaceStr) { var namespaceName = Environment !.IdPool.GetIdentifier(namespaceStr); if (!Environment !.Namespaces.TryGetValue(namespaceName, out var namespaceData)) { var err = ES_FrontendErrors.GenNamespaceDoesntExist( namespaceStr.GetPooledString(), src, nodeBounds ); errorList.Add(err); return(null); } return(namespaceData); }
private void CreateTypes_Aggregate( ref TranslationUnitData transUnit, ES_NamespaceData.Builder namespaceBuilder, ES_TypeTag type, ES_AstAggregateDefinition typeDef ) { var namespaceName = namespaceBuilder.NamespaceData.NamespaceName; var typeName = Environment !.IdPool.GetIdentifier(typeDef.Name.Text.Span); if (namespaceBuilder.CheckTypeExists(typeName, null) != null) { errorList.Add(ES_FrontendErrors.GenTypeAlreadyDefined( namespaceBuilder.NamespaceData.NamespaceNameString, typeDef.Name.Text.Span.GetPooledString(), typeDef.Name )); return; } ES_TypeInfo *typeData = null; if (type == ES_TypeTag.Class) { var classBuilder = namespaceBuilder.GetOrCreateClass(typeDef.AccessModifier, typeName, transUnit.Name); typeData = &classBuilder.ClassData->TypeInfo; } else if (type == ES_TypeTag.Struct) { var structBuilder = namespaceBuilder.GetOrCreateStruct(typeDef.AccessModifier, typeName, transUnit.Name); typeData = &structBuilder.StructData->TypeInfo; } else { Debug.Fail("Not implemented/supported."); } EnvironmentBuilder !.PointerAstMap.Add((IntPtr)typeData, typeDef); }
public void should_allocate_up_front_ids() { var idPool = new IdPool(10, 10); Assert.Equal(idPool.AvailableIds.Count, 10); }
public IdPoolTests() { this.sut = new IdPool <string>(); this.id1 = this.sut.Add("1"); this.id2 = this.sut.Add("2"); }
public ContinuationCache(BufferPool pool) { IdPool <Buffer <int> > .Create(pool.SpecializeFor <int>(), 32, out Ids); pool.SpecializeFor <T>().Take(128, out Caches); }
public void Destroy(int entityId) => IdPool.ReleaseInstance(entityId);
public HDomain(HSession Session) : base(Session) { Objects = new Dictionary <int, object>(); ObjIds = new IdPool(); }
public IdPoolTests() { this.sut = new IdPool <string>(); this.id1 = this.sut.CreateIdFor("1"); this.id2 = this.sut.CreateIdFor("2"); }