Example #1
0
        public void Allocate_ValidSizeWorks()
        {
            //Act
            using (var target = SafeHGlobalHandle.Allocate(256))
            {
                var actual = target.Pointer;

                var written = WriteMemory(target.Pointer, 256);

                //Assert
                target.IsInvalid.Should().BeFalse();
                AssertMemory(target.Pointer, written);
            };
        }
Example #2
0
        public void Allocate_WithIntPtr()
        {
            IntPtr size = new IntPtr(1024);

            //Act
            using (var target = SafeHGlobalHandle.Allocate(size))
            {
                var actual = target.Pointer;

                var written = WriteMemory(target.Pointer, size.ToInt32());

                //Assert
                target.IsInvalid.Should().BeFalse();
                AssertMemory(target.Pointer, written);
            };
        }
Example #3
0
        /// <summary>
        ///     Ensures that there is sufficient memory allocated.
        /// </summary>
        /// <param name="capacity"> The required capacity of the block, in bytes. </param>
        /// <exception cref="OutOfMemoryException">There is insufficient memory to satisfy the request.</exception>
        public void EnsureCapacity(int capacity)
        {
            var currentSize = _memoryBlock.IsInvalid ? 0 : _memoryBlock.Size;

            if (capacity > currentSize)
            {
                if (0 != currentSize)
                {
                    currentSize <<= 1;
                }
                if (capacity > currentSize)
                {
                    currentSize = capacity;
                }

                if (!_memoryBlock.IsInvalid)
                {
                    _memoryBlock.Dispose();
                }
                _memoryBlock = SafeHGlobalHandle.Allocate(currentSize);
            }
        }
Example #4
0
        public void Allocate_InvalidSizeFails()
        {
            Action action = () => SafeHGlobalHandle.Allocate(-5);

            action.ShouldThrowArgumentOutOfRangeException();
        }