Beispiel #1
0
        // Convert a byte array to an array of structs
        public static T[] DeserializeArray <T>(MarshalMemPool pool, byte[] src, int byteOffset = 0, int bytes = -1) where T : struct
        {
            //if (Marshal.SizeOf(typeof(T)) < data.Length)
            //    throw new VoxeException("Input data too small");

            int objSize    = TSSize <T> .ValueSize;
            int len        = (bytes <= 0) ? src.Length : bytes;
            int objArrSize = len / objSize;

            // Make sure we don't cut some data in half
            //Assert.IsTrue(objArrSize*objSize == bytes);
            if (objArrSize * objSize != bytes)
            {
                return(null);
            }

            T[]    ret    = new T[objArrSize];
            IntPtr buffer = pool.Pop(len);

            Marshal.Copy(src, 0, buffer, len);

            long pBuffer = (long)buffer;

            for (int i = 0; i < objArrSize; i++, pBuffer += objSize)
            {
                ret[i] = (T)Marshal.PtrToStructure((IntPtr)pBuffer, typeof(T));
            }

            pool.Push(len);

            return(ret);
        }
Beispiel #2
0
        // Converts a struct to a byte array
        public static void Serialize <T>(MarshalMemPool pool, ref T src, ref byte[] dst) where T : struct
        {
            int objSize = TSSize <T> .ValueSize;

            IntPtr buffer = pool.Pop(objSize);

            {
                Marshal.StructureToPtr(src, buffer, true);
                Marshal.Copy(buffer, dst, 0, objSize);
            }
            pool.Push(objSize);
        }
Beispiel #3
0
        // Converts a byte array to a struct
        public static T Deserialize <T>(MarshalMemPool pool, byte[] src, int offset = 0) where T : struct
        {
            //if(Marshal.SizeOf(typeof (T))<data.Length)
            //    throw new VoxeException("Input data too small");

            int    objSize = src.Length;
            IntPtr buffer  = pool.Pop(objSize);

            Marshal.Copy(src, offset, buffer, objSize);
            T ret = (T)Marshal.PtrToStructure(buffer, typeof(T));

            pool.Push(objSize);

            return(ret);
        }
Beispiel #4
0
        // Converts an array of structs to a byte array
        public static void SerializeArray <T>(MarshalMemPool pool, T[] src, ref byte[] dst, int items = -1)
            where T : struct
        {
            int itemsToConvert = (items < 0) ? src.Length : items;
            int objSize        = TSSize <T> .ValueSize;
            int objArrSize     = objSize * itemsToConvert;

            IntPtr pBuffer = pool.Pop(objArrSize);

            {
                long pDst = (long)pBuffer;
                for (int i = 0; i < itemsToConvert; i++, pDst += objSize)
                {
                    Marshal.StructureToPtr(src[i], (IntPtr)pDst, true);
                    Marshal.Copy((IntPtr)pDst, dst, i * objSize, objSize);
                }
            }
            pool.Push(objArrSize);
        }