Esempio n. 1
0
        public void CannotResizeIfExternalMemory()
        {
            var intptr = Marshal.AllocCoTaskMem(10);

            using (var array = new ArrayPtr(intptr))
            {
                Assert.That(() => array.Resize(12), Throws.TypeOf <ApplicationException>());
            }
        }
Esempio n. 2
0
 public void DoOwnMemory_2()
 {
     using (var array = new ArrayPtr())
     {
         Assert.AreEqual(true, array.OwnMemory, "Should own memory");
         array.Resize(10);
         Assert.AreEqual(true, array.OwnMemory, "Should still own memory after resize");
     }
 }
Esempio n. 3
0
        public void CannotResizeIfExternalMemory()
        {
            var intptr = Marshal.AllocCoTaskMem(10);

            using (var array = new ArrayPtr(intptr))
            {
                array.Resize(12);
            }
        }
Esempio n. 4
0
        public void CanResizeIfOwnMemory()
        {
            int originalSize = 10;

            using (var array = new ArrayPtr(originalSize))
            {
                Assert.AreEqual(originalSize, array.Size);

                int newSize = 12;
                array.Resize(newSize);
                Assert.AreEqual(newSize, array.Size);
            }
        }
Esempio n. 5
0
        public void DisposingArrayPtrOfOwnMemoryDoesFree_2()
        {
            IntPtr arrayIntPtr;

            using (var array = new ArrayPtr())
            {
                array.Resize(10);

                arrayIntPtr = array.IntPtr;
                Marshal.WriteByte(arrayIntPtr, (byte)123);
            }
            byte b = Marshal.ReadByte(arrayIntPtr);

            Assert.AreEqual((byte)0, b, "Owned memory should have been freed");
        }
Esempio n. 6
0
 /// -----------------------------------------------------------------------------------
 /// <summary>
 /// Re-Allocates memory for <paramref name="nMaxSize"/> elements and returns a pointer
 /// suitable to be passed as array to a COM method.
 /// </summary>
 /// <param name="arrayPtr">Pointer with previously allocated memory</param>
 /// <param name="nMaxSize">Max. number of elements in the array</param>
 /// <typeparam name="T">Type of elements in array</typeparam>
 /// -----------------------------------------------------------------------------------
 static public void ArrayToNative <T>(ref ArrayPtr arrayPtr, int nMaxSize)
 {
     arrayPtr.Resize(nMaxSize * SizeOf(typeof(T)));
 }