/// <summary> /// Internal implementation of ComputeFieldOffsets. /// </summary> /// <param name="types">The list of the types.</param> /// <param name="triedTypeIDs">Used to avoid infinite loop.</param> private void ComputeFieldOffsetsInternal(List <HeapType> types, ref RCSet <short> triedTypeIDs) { if (this.fieldIndices == null) { return; } if (!triedTypeIDs.Add(this.id)) { throw new HeapException(string.Format("Infinite cycle found in the layout of element '{0}'!", this.name)); } int allocationSize = 0; for (int fieldIdx = 0; fieldIdx < this.fieldTypeIDs.Count; fieldIdx++) { this.fieldOffsets[fieldIdx] = allocationSize; short fieldTypeID = this.fieldTypeIDs[fieldIdx]; HeapType fieldType = types[fieldTypeID]; if (fieldType.AllocationSize == -1) { /// Compute the allocation size of the field type first. fieldType.ComputeFieldOffsetsInternal(types, ref triedTypeIDs); } allocationSize += fieldType.AllocationSize; } this.allocationSize = allocationSize; triedTypeIDs.Remove(this.id); }
/// <summary> /// Internal recursive method for registering the pointer types that are implicitly or explicitly in the type name string. /// </summary> /// <param name="pointedTypeName">The name of the pointed type.</param> /// <param name="typeIDs">The list of the type IDs mapped by their names.</param> /// <param name="types">The list of the types.</param> private void RegisterPointer(string pointedTypeName, ref Dictionary <string, short> typeIDs, ref List <HeapType> types) { /// If the pointed type is also a pointer then continue the recursion. if (pointedTypeName.EndsWith("*")) { this.RegisterPointer(pointedTypeName.Substring(0, pointedTypeName.Length - 1), ref typeIDs, ref types); } string pointerTypeName = string.Format("{0}*", pointedTypeName); if (typeIDs.ContainsKey(pointerTypeName)) { return; } if (types.Count == short.MaxValue) { throw new HeapException(string.Format("Number of possible types exceeded the limit of {0}!", short.MaxValue)); } HeapType ptrType = new HeapType(pointerTypeName, typeIDs[pointedTypeName]); ptrType.SetID((short)types.Count); typeIDs.Add(ptrType.Name, (short)types.Count); types.Add(ptrType); }
/// <summary> /// Constructs a HeapConnector instance. /// </summary> public HeapConnector(int dataAddress, HeapType dataType, IHeap heap, IHeapConnectorFactory heapDataFactory, DeallocationFunc deallocFunc) { this.dataAddress = dataAddress; this.dataType = dataType; this.heap = heap; this.heapDataFactory = heapDataFactory; this.deallocationFunc = deallocFunc; }
public HeapIntConnector(int dataAddress, HeapType dataType, IHeap heap, IHeapConnectorFactory heapDataFactory, DeallocationFunc deallocFunc) : base(dataAddress, dataType, heap, heapDataFactory, deallocFunc) { if (dataType.BuiltInType != BuiltInTypeEnum.Integer) { throw new InvalidOperationException("Invalid heap type!"); } }
/// <summary> /// Internal method for registering a new type to this manager. /// </summary> /// <param name="type">The type to be registered.</param> /// <param name="typeIDs">The list of the type IDs mapped by their names.</param> /// <param name="types">The list of the types.</param> private void RegisterType(HeapType type) { if (this.typeIDs.ContainsKey(type.Name)) { throw new HeapException(string.Format("Type '{0}' already defined!", type.Name)); } if (this.types.Count == short.MaxValue) { throw new HeapException(string.Format("Number of possible types exceeded the limit of {0}!", short.MaxValue)); } type.SetID((short)this.types.Count); this.typeIDs.Add(type.Name, (short)this.types.Count); this.types.Add(type); }
/// <see cref="IHeapManagerInternals.New"/> public IHeapConnector New(short typeID) { if (this.heap == null) { throw new InvalidOperationException("No simulation heap created or loaded currently!"); } if (typeID < 0 || typeID >= this.types.Count) { throw new ArgumentOutOfRangeException("typeID"); } HeapType type = this.types[typeID]; HeapSection sectToAlloc = this.AllocateSection(type.AllocationSize); return(this.CreateHeapConnector(sectToAlloc.Address, typeID)); }
/// <see cref="IHeapManagerInternals.NewArray"/> public IHeapConnector NewArray(short typeID, int count) { if (this.heap == null) { throw new InvalidOperationException("No simulation heap created or loaded currently!"); } if (typeID < 0 || typeID >= this.types.Count) { throw new ArgumentOutOfRangeException("typeID"); } if (count <= 0) { throw new ArgumentOutOfRangeException("count"); } HeapType type = this.types[typeID]; HeapSection sectToAlloc = this.AllocateSection(count * type.AllocationSize + 4); /// +4 is for storing the number of array-items. this.heap.WriteInt(sectToAlloc.Address, count); return(this.CreateHeapConnector(sectToAlloc.Address + 4, typeID)); }
/// <see cref="IHeapConnectorFactory.CreateHeapConnector"/> public IHeapConnector CreateHeapConnector(int address, short typeID) { HeapType type = this.types[typeID]; switch (type.BuiltInType) { case BuiltInTypeEnum.NonBuiltIn: return(new HeapConnector(address, type, this.heap, this, this.DeallocateSection)); case BuiltInTypeEnum.Byte: return(new HeapByteConnector(address, type, this.heap, this, this.DeallocateSection)); case BuiltInTypeEnum.Short: return(new HeapShortConnector(address, type, this.heap, this, this.DeallocateSection)); case BuiltInTypeEnum.Integer: return(new HeapIntConnector(address, type, this.heap, this, this.DeallocateSection)); case BuiltInTypeEnum.Long: return(new HeapLongConnector(address, type, this.heap, this, this.DeallocateSection)); case BuiltInTypeEnum.Number: return(new HeapNumberConnector(address, type, this.heap, this, this.DeallocateSection)); case BuiltInTypeEnum.IntVector: return(new HeapIntVectorConnector(address, type, this.heap, this, this.DeallocateSection)); case BuiltInTypeEnum.NumVector: return(new HeapNumVectorConnector(address, type, this.heap, this, this.DeallocateSection)); case BuiltInTypeEnum.IntRectangle: return(new HeapIntRectangleConnector(address, type, this.heap, this, this.DeallocateSection)); case BuiltInTypeEnum.NumRectangle: return(new HeapNumRectangleConnector(address, type, this.heap, this, this.DeallocateSection)); default: throw new InvalidOperationException("Impossible case happened!"); } }
/// <see cref="IComponent.Start"/> public void Start() { /// Collect the heap types defined in the heap type containers List <HeapType> heapTypes = new List <HeapType>(); foreach (Assembly container in this.heapTypeContainers) { foreach (Type type in container.GetTypes()) { if (type.IsSubclassOf(typeof(HeapedObject))) { HeapType heapType = this.CreateHeapType(type); this.inheritenceTree.Add(type.Name, null); heapTypes.Add(heapType); } } } /// Register the found heap types. this.RegisterNonBuiltInTypes(heapTypes); /// Create the inheritence tree. foreach (IHeapType heapType in heapTypes) { List <IHeapType> inheritencePath = new List <IHeapType>(); IHeapType currHeapType = heapType; inheritencePath.Add(currHeapType); while (currHeapType.HasField(Constants.NAME_OF_BASE_TYPE_FIELD)) { currHeapType = this.GetHeapType(currHeapType.GetFieldTypeID(Constants.NAME_OF_BASE_TYPE_FIELD)); inheritencePath.Add(currHeapType); } inheritencePath.Reverse(); this.inheritenceTree[heapType.Name] = inheritencePath.ToArray(); } }
/// <summary> /// Constructs a BinaryHeap object of the given type. /// </summary> /// <param name="type">The type of the heap.</param> public BinaryHeap(HeapType type) { this.type = type; this.nextIndex = 0; this.heapArray = new List <T>(); }