Exemple #1
0
        /// <summary>
        /// 时间事件
        /// </summary>
        private unsafe void runTime()
        {
            DateTime now = Date.NowTime.Set();

            Monitor.Enter(timeLock);
            if (times.Length != 0)
            {
                fixed(DateTime *timeFixed = times.Array)
                {
                    if (*timeFixed <= now)
                    {
                        times.Length = 0;
                        minTime      = DateTime.MaxValue;
                    }
                    else
                    {
                        DateTime *end = timeFixed + times.Length;
                        while (*--end <= now)
                        {
                            ;
                        }
                        minTime      = *end;
                        times.Length = (int)(end - timeFixed) + 1;
                    }
                }
            }
            Monitor.Exit(timeLock);
            run();
        }
Exemple #2
0
        static void Main(string[] args)
        {
            unsafe
            {
                int  x        = 1;
                int *p_before = &x;
                x = 2;
                int *p_after = &x;
                Console.WriteLine((int)p_before);
                Console.WriteLine((int)p_after);

                DateTime  s         = new DateTime(2016, 12, 20);
                DateTime *ps_before = &s;
                s = new DateTime(2016, 12, 19);
                DateTime *ps_after = &s;
                Console.WriteLine((int)ps_before);
                Console.WriteLine((int)ps_after);
            }

            string a = "A";
            string b = a;

            Console.WriteLine(ReferenceEquals(a, b));
            a = "B";
            Console.WriteLine(a);
            Console.WriteLine(b);
            Console.WriteLine(ReferenceEquals(a, b));

            int c = 1;
            int d = c;

            Console.WriteLine(ReferenceEquals(c, d));
        }
Exemple #3
0
        /// <summary>
        /// 时间事件
        /// </summary>
        private unsafe void runTime()
        {
            DateTime now = date.Now;

            Monitor.Enter(timeLock);
            if (times.Count != 0)
            {
                fixed(DateTime *timeFixed = times.UnsafeArray)
                {
                    if (*timeFixed <= now)
                    {
                        times.Empty();
                        minTime = DateTime.MaxValue;
                    }
                    else
                    {
                        DateTime *end = timeFixed + times.Count;
                        while (*--end <= now)
                        {
                            ;
                        }
                        minTime = *end;
                        times.UnsafeSetLength((int)(end - timeFixed) + 1);
                    }
                }
            }
            Monitor.Exit(timeLock);
            run();
        }
        public unsafe void EqualityTest1()
        {
            DateTime *sample   = stackalloc DateTime[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();

            DateTimePointer address3 = new DateTimePointer(address2);

            Console.WriteLine("DateTimePointer Address (from IntPtr): {0:X}", address3.ToInt32());
            checksum += address3.ToInt32();

            DateTimePointer address4 = new DateTimePointer(address1);

            Console.WriteLine("DateTimePointer 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());
        }
Exemple #5
0
        /// <summary>
        /// 时间事件
        /// </summary>
        private unsafe void runTime()
        {
            DateTime now = AutoCSer.Threading.SecondTimer.SetNow();

            Monitor.Enter(timeLock);
            if (times.Length != 0)
            {
                fixed(DateTime *timeFixed = times.GetFixedBuffer())
                {
                    if (*timeFixed <= now)
                    {
                        times.Length = 0;
                        minTime      = DateTime.MaxValue;
                    }
                    else
                    {
                        DateTime *end = timeFixed + times.Length;
                        while (*--end <= now)
                        {
                            ;
                        }
                        minTime      = *end;
                        times.Length = (int)(end - timeFixed) + 1;
                    }
                }
            }
            Monitor.Exit(timeLock);
            run();
        }
        public unsafe void SizeOfTest1()
        {
            DateTime *sample = stackalloc DateTime[4];

            int totalSize = 0;

            int ptrSize1 = Marshal.SizeOf(new DateTimePointer(sample));

            Console.WriteLine("Marshal.SizeOf(new DateTimePointer(...)): {0}", ptrSize1);
            totalSize += ptrSize1;

            int ptrSize2 = Marshal.SizeOf(typeof(DateTimePointer));

            Console.WriteLine("Marshal.SizeOf(typeof(DateTimePointer)): {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(DateTime *));

            Console.WriteLine("Marshal.SizeOf(typeof(DateTime*)): {0}", ptrSize5);
            totalSize += ptrSize5;

            Assert.AreEqual(totalSize, DateTimePointer.Size * 5);
        }
Exemple #7
0
        /// <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 DateTime[0]);
                return;
            }
            DateTime[] array;
            unsafe
            {
                fixed(byte *pByte = &data[offset])
                {
                    int arrLength = *(int *)pByte;

                    array = new DateTime[arrLength];
                    if (arrLength > 10)
                    {
                        fixed(DateTime *point = array)
                        {
                            Buffer.MemoryCopy((void *)new IntPtr(pByte + 4), (void *)new IntPtr((byte *)point), (uint)(Size.DateTime * arrLength), (uint)(Size.DateTime * arrLength));
                            //Native.Win32API.memcpy(new IntPtr((byte*)point), new IntPtr(pByte + 4), (uint)(Size.DateTime * arrLength));
                        }
                    }
                    else
                    {
                        DateTime *point = (DateTime *)(pByte + 4);
                        for (int i = 0; i < arrLength; i++)
                        {
                            array[i] = *(point++);
                        }
                    }
                }
            }
            result.SetValue(instance, array);
        }
Exemple #8
0
        public static unsafe void DateTimeFromUnixTime(uint utm, DateTime *res)
        {
            System.DateTime u0time  = new System.DateTime(1970, 1, 1);
            long            u0ticks = u0time.Ticks;

            u0ticks += (long)utm * 10000000L;
            Convert(res, new System.DateTime(u0ticks));
        }
Exemple #9
0
        unsafe public static void Pointers(DateTime *x)
        {
            *   x   = DateTime.Now;
            var foo = x->Day;
            var bar = x->IsDaylightSavingTime();
            var qux = (*x).Day;

            PointersAux(*x);
        }
Exemple #10
0
 static unsafe void PointersAsArgsTest(int *ip, int **ipp, int *[] ipa, int **[] ippa,
                                       DateTime *dtp, DateTime **dtpp, DateTime *[] dtpa, DateTime **[] dtppa)
 {
     Console.WriteLine($"break here!");
     if (ip == null)
     {
         Console.WriteLine($"ip is null");
     }
     Console.WriteLine($"done!");
 }
Exemple #11
0
 private static unsafe void Convert(DateTime *r, System.DateTime s)
 {
     r->year     = (uint)s.Year;
     r->month    = (byte)s.Month;
     r->day      = (byte)s.Day;
     r->hour     = (byte)s.Hour;
     r->minute   = (byte)s.Minute;
     r->second   = (byte)s.Second;
     r->fraction = 0;
     r->zone     = (short)
                   System.TimeZone.CurrentTimeZone.GetUtcOffset(s).Minutes;
 }
Exemple #12
0
        /// <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)
        {
            DateTime[] array = new DateTime[data.Length / Size.DateTime];
            fixed(byte *pByte = data)
            {
                DateTime *pData = (DateTime *)pByte;

                for (int i = 0; i < array.Length; i++)
                {
                    array[i] = *pData++;
                }
            }

            return(array);
        }
Exemple #13
0
        public override bool Equals(object obj)
        {
            DateTime *pointer = null;

            if (obj is IntPtr)
            {
                pointer = (DateTime *)(IntPtr)obj;
            }
            else if (obj is DateTimePointer)
            {
                pointer = (DateTime *)(DateTimePointer)obj;
            }

            return(pointer == this.internalPointer);
        }
Exemple #14
0
        /// <summary>
        /// Writes a list of System.DateTime integers to the current stream using the specified buffer.
        /// </summary>
        /// <param name="stream">The stream to write.</param>
        /// <param name="array">A list of System.DateTime integers.</param>
        /// <param name="startIndex">A position in the list where the writing starts.</param>
        /// <param name="count">The number of integers to be written into the current stream.
        /// <para>!!! Note this number should be no larger than the number of integers from <paramref name="startIndex" /> to the end of the array.</para></param>
        /// <param name="buffer">A byte array used to temporarily store data to write.</param>
        public unsafe static void WriteDateTimes(this Stream stream, IList <DateTime> list, int startIndex, int count, byte[] buffer)
        {
            fixed(byte *ptr = buffer)
            {
                DateTime *iptr2 = (DateTime *)ptr;

                for (int i = 0, j = startIndex; i < count;)
                {
                    iptr2[i] = list[j];
                    ++i;
                    ++j;
                }
            }

            stream.Write(buffer, 0, buffer.Length);
        }
Exemple #15
0
        public static unsafe void LocalPointers()
        {
            int ivalue0 = 5;
            int ivalue1 = 10;

            int * ip       = &ivalue0;
            int * ip_null  = null;
            int **ipp      = &ip;
            int **ipp_null = &ip_null;

            int *[]  ipa     = new int *[] {&ivalue0, &ivalue1, null };
            int **[] ippa    = new int **[] {&ip, &ip_null, ipp, ipp_null, null };
            char     cvalue0 = 'q';
            char *   cp      = &cvalue0;

            DateTime dt       = new DateTime(5, 6, 7, 8, 9, 10);
            void *   vp       = &dt;
            void *   vp_null  = null;
            void **  vpp      = &vp;
            void **  vpp_null = &vp_null;

            DateTime *    dtp      = &dt;
            DateTime *    dtp_null = null;
            DateTime *[]  dtpa     = new DateTime *[] { dtp, dtp_null };
            DateTime **[] dtppa    = new DateTime * *[] {&dtp, &dtp_null, null };
            Console.WriteLine($"-- break here: ip_null==null: {ip_null == null}, ipp_null: {ipp_null == null}, *ipp_null==ip_null: {*ipp_null == ip_null}, *ipp_null==null: {*ipp_null == null}");

            var gs = new GenericStructWithUnmanagedT <DateTime> {
                Value = new DateTime(1, 2, 3, 4, 5, 6), IntField = 4, DTPP = &dtp
            };
            var gs_null = new GenericStructWithUnmanagedT <DateTime> {
                Value = new DateTime(1, 2, 3, 4, 5, 6), IntField = 4, DTPP = &dtp_null
            };
            var gsp      = &gs;
            var gsp_null = &gs_null;
            var gspa     = new GenericStructWithUnmanagedT <DateTime> *[] { null, gsp, gsp_null };

            var cwp = new GenericClassWithPointers <DateTime> {
                Ptr = dtp
            };
            var cwp_null = new GenericClassWithPointers <DateTime>();
            Console.WriteLine($"{(int)*ip}, {(int)**ipp}, {ipp_null == null}, {ip_null == null}, {ippa == null}, {ipa}, {(char)*cp}, {(vp == null ? "null" : "not null")}, {dtp->Second}, {gsp->IntField}, {cwp}, {cwp_null}, {gs_null}");

            PointersAsArgsTest(ip, ipp, ipa, ippa, &dt, &dtp, dtpa, dtppa);
        }
Exemple #16
0
        private unsafe int CalculateInt()
        {
            int x = this.CalculateInt().GetHashCode();

            x = this.CalculateInt().GetHashCode();
            x = this.CalculateInt().
                GetHashCode();
            x = this.CalculateInt()
                .GetHashCode();

            DateTime *date = null;
            int       day2 = date->Day;
            int       day3 = date->Day;
            int       day4 = date->
                             Day;
            int day5 = date
                       ->Day;

            return(x + day2 + day3 + day4 + day5);
        }
        public unsafe void AddressTest1()
        {
            DateTime *      sample = stackalloc DateTime[4];
            DateTimePointer a      = new DateTimePointer(sample);
            DateTimePointer b      = (a + 1);

            Console.WriteLine("Address offset: {0}", b.ToInt32() - a.ToInt32());

            Assert.AreEqual(sizeof(DateTime), b.ToInt32() - a.ToInt32());
            Assert.False(Object.ReferenceEquals(a, b));

            // xPlatform's typed pointers are value type.
            DateTimePointer c = new DateTimePointer(sample + 1);
            DateTimePointer d = (++c);

            Console.WriteLine("Address offset: {0}", d.ToInt32() - c.ToInt32());

            Assert.AreEqual(0, d.ToInt32() - c.ToInt32());
            Assert.False(Object.ReferenceEquals(c, d));
        }
        private int * [] y4 = null; // BUGBUG

        private void MethodName(int p)
        {
            int
            *   y1 = null;
            int z1 = *
                     y1;

            int *y2 = null;
            int  z2 = *y2;
            int *y3 = null;        // BUGBUG
            int  z3 = *y3;         // BUGBUG

            int * [] y4 = null;    // BUGBUG
            int[]    zz = null;
            int      z4 = zz[*y3]; // BUGBUG

            this.MethodName(*y3);  // BUGBUG

            DateTime *date = null;
            int       day  = (*date).Day; // BUGBUG
        }
        public unsafe void StackallocTest3()
        {
            const int       bufferSize = 4;
            DateTime *      sample     = stackalloc DateTime[bufferSize];
            DateTimePointer pointer    = new DateTimePointer(sample);

            DateTime[] results = new DateTime[bufferSize];

            for (int i = 0; i < bufferSize; i++)
            {
                results[i] = *(sample + i) = GenerateRandomDateTime();
            }

            // Pointer conversion test
            for (int i = 0; i < bufferSize; i++)
            {
                object x = results[i];
                object y = *(DateTime *)(pointer + i);
                Console.WriteLine("[{0}] <Left: {1}> {2} <Right: {3}>", i, x, x.Equals(y) ? "==" : "<>", y);
                Assert.AreEqual(x, y);
            }
        }
Exemple #20
0
    public static unsafe void PointersTest()
    {
        int ivalue0 = 5;
        int ivalue1 = 10;

        int * ip      = &ivalue0;
        int * ip_null = null;
        int **ipp     = &ip;

        int *[] ipa = new int *[] {&ivalue0, &ivalue1, null };

        char  cvalue0 = 'q';
        char *cp      = &cvalue0;

        DateTime dt      = new DateTime(5, 6, 7, 8, 9, 10);
        void *   vp      = &dt;
        void *   vp_null = null;

        DateTime *dtp      = &dt;
        DateTime *dtp_null = null;
        Console.WriteLine($"-- break here");
    }
        public unsafe void SizeOfTest2()
        {
            DateTime *sample = stackalloc DateTime[4];

            int totalSize = 0;

            int ptrSize1 = sizeof(DateTimePointer);

            Console.WriteLine("sizeof(DateTimePointer): {0}", ptrSize1);
            totalSize += ptrSize1;

            int ptrSize2 = sizeof(IntPtr);

            Console.WriteLine("sizeof(IntPtr): {0}", ptrSize2);
            totalSize += ptrSize2;

            int ptrSize3 = sizeof(DateTime *);

            Console.WriteLine("sizeof(DateTime*): {0}", ptrSize3);
            totalSize += ptrSize3;

            Assert.AreEqual(totalSize, DateTimePointer.Size * 3);
        }
        public unsafe void StackallocTest4()
        {
            const int       bufferSize = 4;
            DateTime *      sample     = stackalloc DateTime[bufferSize];
            DateTimePointer pointer    = new DateTimePointer(sample);

            DateTime[] results = new DateTime[bufferSize];

            // SetData method
            for (int i = 0; i < bufferSize; i++)
            {
                pointer.SetData(results[i] = GenerateRandomDateTime(), 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 StackallocTest5()
        {
            const int       bufferSize = 4;
            DateTime *      sample     = stackalloc DateTime[bufferSize];
            DateTimePointer pointer    = new DateTimePointer(sample);

            DateTime[] results = new DateTime[bufferSize];

            // Indexer based memory writing
            for (int i = 0; i < bufferSize; i++)
            {
                results[i] = pointer[i] = GenerateRandomDateTime();
            }

            // 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);
            }
        }
Exemple #24
0
        /// <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 DateTimeArrayValueStored(new DateTime[0]));
                return;
            }
            DateTime[] array = new DateTime[length / Size.DateTime];
            fixed(byte *pByte = (&data[offsetStart]))
            {
                DateTime *pData = (DateTime *)pByte;

                for (int j = 0; j < array.Length; j++)
                {
                    array[j] = *pData++;
                }
            }

            metadataObject.SetAttribute(id, new DateTimeArrayValueStored(array));
        }
Exemple #25
0
 public DateTimePointer(DateTime *target)
 {
     this.internalPointer = target;
 }
Exemple #26
0
 public static unsafe void GetClock(DateTime *data)
 {
     System.DateTime now = System.DateTime.Now;
     Convert(data, now);
 }
Exemple #27
0
 /// <summary>
 ///     写入一个指定类型的值
 /// </summary>
 /// <param name="value">指定类型的值</param>
 public void WriteDateTime(DateTime *value)
 {
     *(long *)(&_startData[_currentOffset]) = (*value).Ticks;
     _currentOffset += Size.DateTime;
 }