/// <summary> /// 释放资源 /// </summary> public void Dispose() { if (isStaticUnmanaged) { Unmanaged.FreeStatic(ref Heap); } else { Unmanaged.Free(ref Heap); } }
/// <summary> /// 释放缓冲区 /// </summary> /// <param name="head"></param> private void clear(byte *head) { while (*(byte **)head != null) { byte *next = *(byte **)head; Unmanaged.Free(head, Size); if (*(byte **)next == null) { head = next; break; } head = *(byte **)next; Unmanaged.Free(next, Size); } Unmanaged.Free(head, Size); }
/// <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> private Random() { secureSeeds = (uint *)Unmanaged.GetStatic(64 * sizeof(uint) + 5 * 11 * sizeof(uint), false); seeds = secureSeeds + 64; current64 = 5 * 11 - 2; ulong tick = (ulong)Pub.StartTime.Ticks ^ (ulong)Environment.TickCount ^ ((ulong)Pub.Identity32 << 8) ^ ((ulong)Date.NowTimerInterval << 24); int isSeedArray = 0; FieldInfo seedField = typeof(Random).GetField("SeedArray", BindingFlags.Instance | BindingFlags.NonPublic); if (seedField != null) { int[] seedArray = seedField.GetValue(new Random()) as int[]; if (seedArray != null && seedArray.Length == 5 * 11 + 1) { tick *= 0xb163dUL; fixed(int *seedFixed = seedArray) { for (uint *write = seeds, end = seeds + 5 * 11, read = (uint *)seedFixed; write != end; tick >>= 1) { *write++ = *++read ^ (((uint)tick & 1U) << 31); } } isSeedArray = 1; } } if (isSeedArray == 0) { for (uint *start = seeds, end = start + 5 * 11; start != end; ++start) { *start = (uint)tick ^ (uint)(tick >> 32); tick *= 0xb163dUL; tick += tick >> 32; } } for (ulong *start = (ulong *)secureSeeds; start != seeds; *start++ = NextULong()) { ; } bits = (uint)Next(); bitCount = 32; }
/// <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); } }
static ReusableDictionary() { int[] capacityPrimesArray = new int[] { 3, 7, 17, 37, 89, 197, 431, 919, 1931, 4049, 8419, 17519, 36353, 75431, 156437, 324449, 672827, 1395263, 2893249, 5999471 }; capacityPrimes = Unmanaged.GetStaticPointer(capacityPrimesArray.Length * sizeof(int) + 4792 * sizeof(ushort), false); capacityPrimes.CurrentIndex = capacityPrimesArray.Length; capacityPrimesArray.AsSpan().CopyTo(new Span <int>(capacityPrimes.Data, capacityPrimes.ByteSize)); primes = new AutoCSer.Memory.Pointer(capacityPrimes.Int + capacityPrimesArray.Length, 0); ushort *endPrime = primes.UShort; for (ushort mod = 3; mod != 3 * 3; mod += 2) { *endPrime++ = mod; } for (ushort max = (ushort)(int)Math.Sqrt(int.MaxValue), mod = 11; mod <= max; mod += 2) { if (isPrime(mod)) { *endPrime++ = mod; } } endPrimes = new AutoCSer.Memory.Pointer(endPrime, 0); }
internal Pointer.Size GetSize64(int minSize) { return(new Pointer.Size { Data = minSize <= Size?Get() : (byte *)Unmanaged.Get64(minSize, false), ByteSize = minSize }); }
internal byte *Get() { byte *data = TryGet(); return(data != null ? data : (byte *)Unmanaged.Get(Size, false)); }
static Date() { if (Stopwatch.IsHighResolution) { StartTime = DateTime.UtcNow; startTimestamp = Stopwatch.GetTimestamp(); } else { StartTime = DateTime.UtcNow; startTimestamp = StartTime.Ticks; } #if DOTNET2 LocalTimeTicks = StartTime.ToLocalTime().Ticks - StartTime.Ticks; #else LocalTimeTicks = TimeZoneInfo.Local.BaseUtcOffset.Ticks; #endif long zoneChar0, localTimeTicks; if (LocalTimeTicks >= 0) { zoneChar0 = '+' + ((long)':' << 48); localTimeTicks = LocalTimeTicks; } else { zoneChar0 = '-' + ((long)':' << 48); localTimeTicks = -LocalTimeTicks; } long minute = (int)(LocalTimeTicks / TimeSpan.TicksPerMinute); int hour = (int)(((ulong)minute * Div60_32Mul) >> Div60_32Shift), high = (hour * (int)AutoCSer.Extension.Number.Div10_16Mul) >> AutoCSer.Extension.Number.Div10_16Shift; ZoneHourString = zoneChar0 + ((high + '0') << 16) + ((long)((hour - high * 10) + '0') << 32); minute -= hour * 60; high = ((int)minute * (int)AutoCSer.Extension.Number.Div10_16Mul) >> AutoCSer.Extension.Number.Div10_16Shift; ZoneMinuteString = (high + '0') + ((((int)minute - high * 10) + '0') << 16) + ((long)'"' << 32); #if !Serialize weekData = new Pointer { Data = Unmanaged.GetStatic64(8 * sizeof(int) + 12 * sizeof(int), false) }; monthData = new Pointer { Data = weekData.Byte + 8 * sizeof(int) }; int *write = weekData.Int; * write = 'S' + ('u' << 8) + ('n' << 16) + (',' << 24); *++write = 'M' + ('o' << 8) + ('n' << 16) + (',' << 24); *++write = 'T' + ('u' << 8) + ('e' << 16) + (',' << 24); *++write = 'W' + ('e' << 8) + ('d' << 16) + (',' << 24); *++write = 'T' + ('h' << 8) + ('u' << 16) + (',' << 24); *++write = 'F' + ('r' << 8) + ('i' << 16) + (',' << 24); *++write = 'S' + ('a' << 8) + ('t' << 16) + (',' << 24); write = monthData.Int; *write = 'J' + ('a' << 8) + ('n' << 16) + (' ' << 24); *++write = 'F' + ('e' << 8) + ('b' << 16) + (' ' << 24); *++write = 'M' + ('a' << 8) + ('r' << 16) + (' ' << 24); *++write = 'A' + ('p' << 8) + ('r' << 16) + (' ' << 24); *++write = 'M' + ('a' << 8) + ('y' << 16) + (' ' << 24); *++write = 'J' + ('u' << 8) + ('n' << 16) + (' ' << 24); *++write = 'J' + ('u' << 8) + ('l' << 16) + (' ' << 24); *++write = 'A' + ('u' << 8) + ('g' << 16) + (' ' << 24); *++write = 'S' + ('e' << 8) + ('p' << 16) + (' ' << 24); *++write = 'O' + ('c' << 8) + ('t' << 16) + (' ' << 24); *++write = 'N' + ('o' << 8) + ('v' << 16) + (' ' << 24); *++write = 'D' + ('e' << 8) + ('c' << 16) + (' ' << 24); #endif }
/// <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]; AutoCSer.Memory.Pointer newHeap = isStaticUnmanaged ? Unmanaged.GetStaticPointer(defaultArrayLength * sizeof(int), false) : Unmanaged.GetPointer(defaultArrayLength * sizeof(int), false), oldHeap = Heap; reset(newHeap.Int); Array = newArray; Heap = newHeap; if (isStaticUnmanaged) { Unmanaged.FreeStatic(ref oldHeap); } else { Unmanaged.Free(ref oldHeap); } } }
/// <summary> /// 数组模拟最小堆 /// </summary> /// <param name="isStaticUnmanaged">是否固定内存申请</param> internal ArrayHeap(bool isStaticUnmanaged = false) { Array = new KeyValue <keyType, valueType> [defaultArrayLength]; this.isStaticUnmanaged = isStaticUnmanaged; Heap = isStaticUnmanaged ? Unmanaged.GetStaticPointer(defaultArrayLength * sizeof(int), false) : Unmanaged.GetPointer(defaultArrayLength * sizeof(int), false); reset(Heap.Int); }
/// <summary> /// 释放资源 /// </summary> public void Dispose() { Unmanaged.Free(ref Heap, isStaticUnmanaged); }