Example #1
0
File: ABI.cs Project: ETLang/Gluon
        public static ArrayBlob ToABI_BlittableStruct <T>(T[] arr) where T : struct
        {
            var r = new ArrayBlob();

#if WINDOWS_UWP
            var sz = Marshal.SizeOf <T>();
#else
            var sz = Marshal.SizeOf(typeof(T));
#endif

            if (arr == null)
            {
                return(r);
            }

            r.Count = arr.Length;
            r.Ptr   = Marshal.AllocCoTaskMem(arr.Length * sz);

            for (int i = 0; i < arr.Length; i++)
            {
                Marshal.StructureToPtr(arr[i], (IntPtr)((long)r.Ptr + sz * i), false);
            }

            return(r);
        }
Example #2
0
File: ABI.cs Project: ETLang/Gluon
        public static unsafe ArrayBlob ToABI_string(string[] arr)
        {
            var r  = new ArrayBlob();
            var sz = IntPtr.Size;

            if (arr == null)
            {
                return(r);
            }

            r.Count = arr.Length;
            r.Ptr   = Marshal.AllocCoTaskMem(arr.Length * sz);

            for (int i = 0; i < arr.Length; i++)
            {
                var count = Encoding.UTF8.GetByteCount(arr[i]);
                var bytes = Marshal.AllocCoTaskMem(count + 1);
                fixed(char *c = arr[i])
                {
                    Encoding.UTF8.GetBytes(c, arr[i].Length, (byte *)bytes, count);
                }

                ((byte *)bytes)[count] = 0;


                Marshal.WriteIntPtr((IntPtr)((long)r.Ptr + sz * i), bytes);
            }

            return(r);
        }
Example #3
0
File: ABI.cs Project: ETLang/Gluon
        public static ArrayBlob ToABI_IntPtr(IntPtr[] arr)
        {
            var r = new ArrayBlob();

            if (arr == null)
            {
                return(r);
            }
            r.Count = arr.Length;
            r.Ptr   = Marshal.AllocCoTaskMem(arr.Length * IntPtr.Size);
            Marshal.Copy(arr, 0, r.Ptr, arr.Length);
            return(r);
        }
Example #4
0
File: ABI.cs Project: ETLang/Gluon
        public static ArrayBlob ToABI_long(long[] arr)
        {
            var r = new ArrayBlob();

            if (arr == null)
            {
                return(r);
            }
            r.Count = arr.Length;
            r.Ptr   = Marshal.AllocCoTaskMem(arr.Length * sizeof(long));
            Marshal.Copy(arr, 0, r.Ptr, arr.Length);
            return(r);
        }
Example #5
0
File: ABI.cs Project: ETLang/Gluon
        public static unsafe ArrayBlob ToABI_char(char[] arr)
        {
            var r = new ArrayBlob();

            if (arr == null)
            {
                return(r);
            }
            r.Count = arr.Length;
            r.Ptr   = Marshal.AllocCoTaskMem(arr.Length * sizeof(char));
            fixed(char *arrb = &arr[0])
            {
                Encoding.ASCII.GetBytes(arrb, arr.Length, (byte *)r.Ptr, arr.Length);
            }

            //Marshal.Copy(arr, 0, r.Ptr, arr.Length);
            return(r);
        }
Example #6
0
File: ABI.cs Project: ETLang/Gluon
        public static unsafe ArrayBlob ToABI_ulong(ulong[] arr)
        {
            var r  = new ArrayBlob();
            var sz = arr.Length * sizeof(ulong);

            if (arr == null)
            {
                return(r);
            }
            r.Count = arr.Length;
            r.Ptr   = Marshal.AllocCoTaskMem(sz);
            fixed(void *src = &arr[0])
            {
                memcpy((void *)r.Ptr, src, sz);
            }

            return(r);
        }