public unsafe void EqualityTest1() { TimeSpan *sample = stackalloc TimeSpan[4]; int checksum = 0; int address1 = (int)sample; Console.WriteLine("Original Address: {0:X}", address1); checksum += address1; IntPtr address2 = new IntPtr(sample); Console.WriteLine("IntPtr Address: {0:X}", address2.ToInt32()); checksum += address2.ToInt32(); TimeSpanPointer address3 = new TimeSpanPointer(address2); Console.WriteLine("TimeSpanPointer Address (from IntPtr): {0:X}", address3.ToInt32()); checksum += address3.ToInt32(); TimeSpanPointer address4 = new TimeSpanPointer(address1); Console.WriteLine("TimeSpanPointer Address (from Int32): {0:X}", address4.ToInt32()); checksum += address4.ToInt32(); int checksumDigest = checksum / 4; Assert.AreEqual(checksumDigest, address1); Assert.AreEqual(checksumDigest, address2.ToInt32()); Assert.AreEqual(checksumDigest, address3.ToInt32()); Assert.AreEqual(checksumDigest, address4.ToInt32()); }
private void Draw(object sender, InkCanvasStrokeCollectedEventArgs e) { TimeSpan *t = stackalloc TimeSpan[1]; (*t) = DateTime.Now - dt; ts.Add((IntPtr)t); }
/// <summary> /// 从元数据转换为第三方客户数据 /// </summary> /// <param name="instance">目标对象</param> /// <param name="result">分析结果</param> /// <param name="data">元数据</param> /// <param name="offset">元数据所在的偏移量</param> /// <param name="length">元数据长度</param> public override void Process(object instance, GetObjectAnalyseResult result, byte[] data, int offset, int length = 0) { if (length == 4) { result.SetValue(instance, new TimeSpan[0]); return; } TimeSpan[] array; unsafe { fixed(byte *pByte = &data[offset]) { int arrLength = *(int *)pByte; array = new TimeSpan[arrLength]; if (arrLength > 10) { fixed(TimeSpan *point = array) { Buffer.MemoryCopy((void *)new IntPtr(pByte + 4), (void *)new IntPtr((byte *)point), (uint)(Size.TimeSpan * arrLength), (uint)(Size.TimeSpan * arrLength)); //Native.Win32API.memcpy(new IntPtr((byte*)point), new IntPtr(pByte + 4), (uint)(Size.TimeSpan * arrLength)); } } else { TimeSpan *point = (TimeSpan *)(pByte + 4); for (int i = 0; i < arrLength; i++) { array[i] = *(point++); } } } } result.SetValue(instance, array); }
public unsafe void SizeOfTest1() { TimeSpan *sample = stackalloc TimeSpan[4]; int totalSize = 0; int ptrSize1 = Marshal.SizeOf(new TimeSpanPointer(sample)); Console.WriteLine("Marshal.SizeOf(new TimeSpanPointer(...)): {0}", ptrSize1); totalSize += ptrSize1; int ptrSize2 = Marshal.SizeOf(typeof(TimeSpanPointer)); Console.WriteLine("Marshal.SizeOf(typeof(TimeSpanPointer)): {0}", ptrSize2); totalSize += ptrSize2; int ptrSize3 = Marshal.SizeOf(IntPtr.Zero); Console.WriteLine("Marshal.SizeOf(Intptr.Zero): {0}", ptrSize3); totalSize += ptrSize3; int ptrSize4 = Marshal.SizeOf(typeof(IntPtr)); Console.WriteLine("Marshal.SizeOf(typeof(IntPtr)): {0}", ptrSize4); totalSize += ptrSize4; int ptrSize5 = Marshal.SizeOf(typeof(TimeSpan *)); Console.WriteLine("Marshal.SizeOf(typeof(TimeSpan*)): {0}", ptrSize5); totalSize += ptrSize5; Assert.AreEqual(totalSize, TimeSpanPointer.Size * 5); }
/// <summary> /// 从元数据转换为第三方客户数据 /// </summary> /// <param name="attribute">当前字段标注的属性</param> /// <param name="data">元数据</param> /// <returns>返回转换后的第三方客户数据</returns> /// <exception cref="Exception">转换失败</exception> public unsafe override object Process(IntellectPropertyAttribute attribute, byte[] data) { TimeSpan[] array = new TimeSpan[data.Length / Size.TimeSpan]; fixed(byte *pByte = data) { TimeSpan *pData = (TimeSpan *)pByte; for (int i = 0; i < array.Length; i++) { array[i] = *pData++; } } return(array); }
public override bool Equals(object obj) { TimeSpan *pointer = null; if (obj is IntPtr) { pointer = (TimeSpan *)(IntPtr)obj; } else if (obj is TimeSpanPointer) { pointer = (TimeSpan *)(TimeSpanPointer)obj; } return(pointer == this.internalPointer); }
public unsafe void AddressTest1() { TimeSpan * sample = stackalloc TimeSpan[4]; TimeSpanPointer a = new TimeSpanPointer(sample); TimeSpanPointer b = (a + 1); Console.WriteLine("Address offset: {0}", b.ToInt32() - a.ToInt32()); Assert.AreEqual(sizeof(TimeSpan), b.ToInt32() - a.ToInt32()); Assert.False(Object.ReferenceEquals(a, b)); // xPlatform's typed pointers are value type. TimeSpanPointer c = new TimeSpanPointer(sample + 1); TimeSpanPointer d = (++c); Console.WriteLine("Address offset: {0}", d.ToInt32() - c.ToInt32()); Assert.AreEqual(0, d.ToInt32() - c.ToInt32()); Assert.False(Object.ReferenceEquals(c, d)); }
public unsafe void StackallocTest3() { const int bufferSize = 4; TimeSpan * sample = stackalloc TimeSpan[bufferSize]; TimeSpanPointer pointer = new TimeSpanPointer(sample); TimeSpan[] results = new TimeSpan[bufferSize]; for (int i = 0; i < bufferSize; i++) { results[i] = *(sample + i) = GenerateRandomNumber(); } // Pointer conversion test for (int i = 0; i < bufferSize; i++) { object x = results[i]; object y = *(TimeSpan *)(pointer + i); Console.WriteLine("[{0}] <Left: {1}> {2} <Right: {3}>", i, x, x.Equals(y) ? "==" : "<>", y); Assert.AreEqual(x, y); } }
unsafe static CacheExpires() { Debug.Assert(NUMBUCKETS < Byte.MaxValue); _tsPerBucket = new TimeSpan(0, 1, 0); _tsPerCycle = new TimeSpan(NUMBUCKETS * _tsPerBucket.Ticks); _bucketTsFromCycleStart = new TimeSpan[NUMBUCKETS]; fixed(TimeSpan *pBucketsFixed = _bucketTsFromCycleStart) { TimeSpan *pBucket = pBucketsFixed; TimeSpan ts = TimeSpan.Zero; int c = NUMBUCKETS; while (c-- > 0) { *pBucket = ts; ts += _tsPerBucket; pBucket++; } } }
public unsafe void StackallocTest4() { const int bufferSize = 4; TimeSpan * sample = stackalloc TimeSpan[bufferSize]; TimeSpanPointer pointer = new TimeSpanPointer(sample); TimeSpan[] results = new TimeSpan[bufferSize]; // SetData method for (int i = 0; i < bufferSize; i++) { pointer.SetData(results[i] = GenerateRandomNumber(), i); } // GetData method for (int i = 0; i < bufferSize; i++) { object x = results[i]; object y = pointer.GetData(i); Console.WriteLine("[{0}] <Left: {1}> {2} <Right: {3}>", i, x, x.Equals(y) ? "==" : "<>", y); Assert.AreEqual(x, y); } }
public unsafe void SizeOfTest2() { TimeSpan *sample = stackalloc TimeSpan[4]; int totalSize = 0; int ptrSize1 = sizeof(TimeSpanPointer); Console.WriteLine("sizeof(TimeSpanPointer): {0}", ptrSize1); totalSize += ptrSize1; int ptrSize2 = sizeof(IntPtr); Console.WriteLine("sizeof(IntPtr): {0}", ptrSize2); totalSize += ptrSize2; int ptrSize3 = sizeof(TimeSpan *); Console.WriteLine("sizeof(TimeSpan*): {0}", ptrSize3); totalSize += ptrSize3; Assert.AreEqual(totalSize, TimeSpanPointer.Size * 3); }
public unsafe void StackallocTest5() { const int bufferSize = 4; TimeSpan * sample = stackalloc TimeSpan[bufferSize]; TimeSpanPointer pointer = new TimeSpanPointer(sample); TimeSpan[] results = new TimeSpan[bufferSize]; // Indexer based memory writing for (int i = 0; i < bufferSize; i++) { results[i] = pointer[i] = GenerateRandomNumber(); } // Indexer based memory navigation for (int i = 0; i < bufferSize; i++) { object x = results[i]; object y = pointer[i]; Console.WriteLine("[{0}] <Left: {1}> {2} <Right: {3}>", i, x, x.Equals(y) ? "==" : "<>", y); Assert.AreEqual(x, y); } }
/// <summary> /// 元数据转换成第三方数据 /// </summary> /// <param name="metadataObject">元数据集合</param> /// <param name="id">属性对应key</param> /// <param name="data">属性对应byte数组</param> /// <param name="offsetStart">属性在数组中的偏移值</param> /// <param name="length">属性在byte数组中的长度</param> /// <exception cref="ArgumentNullException">参数不能为空</exception> public unsafe void DataProcessor(MetadataContainer metadataObject, byte id, byte[] data, int offsetStart, uint length) { if (metadataObject == null) { throw new ArgumentNullException("metadataObject"); } if (length == 0) { metadataObject.SetAttribute(id, new TimeSpanArrayValueStored(new TimeSpan[0])); return; } TimeSpan[] array = new TimeSpan[length / Size.TimeSpan]; fixed(byte *pByte = (&data[offsetStart])) { TimeSpan *pData = (TimeSpan *)pByte; for (int j = 0; j < array.Length; j++) { array[j] = *pData++; } } metadataObject.SetAttribute(id, new TimeSpanArrayValueStored(array)); }
/// <summary> /// 写入一个指定类型的值 /// </summary> /// <param name="value">指定类型的值</param> public void WriteTimeSpan(TimeSpan *value) { *(long *)(&_startData[_currentOffset]) = (*value).Ticks; _currentOffset += Size.TimeSpan; }
public TimeSpanPointer(TimeSpan *target) { this.internalPointer = target; }