Ejemplo n.º 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);
            }

            var    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);
        }
        // Convert a byte array to an array of structs
        public static T[] DeserializeArray <T>(MarshalMemPool pool, byte[] src, int length = -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        = (length <= 0) ? src.Length : length;
            int objArrSize = len / objSize;

            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);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
0
        // Converts a struct to a byte array
        public static byte[] Serialize <T>(MarshalMemPool pool, ref T src) where T : struct
        {
            int objSize = TSSize <T> .ValueSize;
            var dst     = new byte[objSize];

            IntPtr buffer = pool.Pop(objSize);

            {
                Marshal.StructureToPtr(src, buffer, true);
                Marshal.Copy(buffer, dst, 0, objSize);
            }
            pool.Push(objSize);

            return(dst);
        }
Ejemplo n.º 6
0
        // Convert a struct to byte array
        public static byte[] Serialize <T>(ref T data, MarshalMemPool pool) where T : struct
        {
            int objSize = TSSize <T> .ValueSize;

            byte[] ret = new byte[objSize];

            IntPtr buffer = pool.Pop(objSize);

            Marshal.StructureToPtr(data, buffer, true);
            Marshal.Copy(buffer, ret, 0, objSize);

            pool.Push(objSize);

            return(ret);
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
0
        // Convert a struct array to byte array
        public static byte[] SerializeArray <T>(T[] data, MarshalMemPool pool) where T : struct
        {
            int objSize    = TSSize <T> .ValueSize;
            int objArrSize = objSize * data.Length;

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

            long pBuffer = (long)buffer;

            for (int i = 0; i < data.Length; i++, pBuffer += objSize)
            {
                Marshal.StructureToPtr(data[i], (IntPtr)pBuffer, true);// should be false in case struct uses pointers
                Marshal.Copy((IntPtr)pBuffer, ret, i * objSize, objSize);
            }

            pool.Push(objArrSize);

            return(ret);
        }