/// <summary> /// 非托管内存数据流 /// </summary> /// <param name="length">容器初始尺寸</param> protected UnmanagedStreamBase(int length) { if (length <= 0) { length = UnmanagedPool.TinySize; } Data.Set(Unmanaged.Get(length, false), length); IsUnmanaged = true; }
/// <summary> /// 设置容器尺寸 /// </summary> /// <param name="length">容器尺寸</param> protected void setStreamLength(int length) { if (length < UnmanagedPool.TinySize) { length = UnmanagedPool.TinySize; } void *newData = Unmanaged.Get(length, false); AutoCSer.Memory.CopyNotNull(Data.Data, newData, ByteSize); if (IsUnmanaged) { Unmanaged.Free(ref Data); } Data.Set(newData, length); IsUnmanaged = true; }
/// <summary> /// 重置数据 /// </summary> /// <param name="length">数据字节长度</param> internal virtual void Reset(int length = UnmanagedPool.TinySize) { if (length != Data.ByteSize) { if (IsUnmanaged) { Unmanaged.Free(ref this.Data); IsUnmanaged = false; } if (length <= 0) { length = UnmanagedPool.TinySize; } Data.Set(Unmanaged.Get(length, false), length); IsUnmanaged = true; } }
/// <summary> /// 重建数据 /// </summary> protected void create() { int heapIndex = Array.Length, newCount = heapIndex << 1, newHeapSize = newCount * sizeof(int); KeyValue <keyType, valueType>[] newArray = new KeyValue <keyType, valueType> [newCount]; Pointer.Size newHeap = Unmanaged.Get(newHeapSize, false, isStaticUnmanaged), oldHeap = Heap; int * newHeapFixed = newHeap.Int; Array.CopyTo(newArray, 0); AutoCSer.Memory.CopyNotNull(Heap.Byte, newHeapFixed, newHeapSize >> 1); do { --newCount; newHeapFixed[newCount] = newCount; }while (newCount != heapIndex); Array = newArray; Heap = newHeap; Unmanaged.Free(ref oldHeap, isStaticUnmanaged); }
/// <summary> /// 清除数据 /// </summary> internal void Clear() { if (Array.Length == defaultArrayLength) { int *heapFixed = Heap.Int; if (*heapFixed != 0) { reset(heapFixed); System.Array.Clear(Array, 0, Array.Length); } } else { KeyValue <keyType, valueType>[] newArray = new KeyValue <keyType, valueType> [defaultArrayLength]; Pointer.Size newHeap = Unmanaged.Get(defaultArrayLength * sizeof(int), false, isStaticUnmanaged), oldHeap = Heap; reset(newHeap.Int); Array = newArray; Heap = newHeap; Unmanaged.Free(ref oldHeap, isStaticUnmanaged); } }
internal byte *Get() { byte *data = TryGet(); return(data != null ? data : (byte *)Unmanaged.Get(Size, false)); }
internal Pointer.Size GetSize(int minSize) { return(new Pointer.Size { Data = minSize <= Size?Get() : (byte *)Unmanaged.Get(minSize, false), ByteSize = minSize }); }
/// <summary> /// 数组模拟最小堆 /// </summary> /// <param name="isStaticUnmanaged">是否固定内存申请</param> internal ArrayHeap(bool isStaticUnmanaged = false) { Array = new KeyValue <keyType, valueType> [defaultArrayLength]; Heap = Unmanaged.Get(defaultArrayLength * sizeof(int), false, this.isStaticUnmanaged = isStaticUnmanaged); reset(Heap.Int); }