/// <summary>
        /// Creates a <see cref="MemoryBuffer"/> and attempts to retrieve it by searching for it in memory.
        /// </summary>
        private void GetBuffers(MemoryBufferHelper bufferHelper)
        {
            // Options
            int size        = 4096;
            int repetitions = 128;
            int increment   = 2048;

            // Setup
            MemoryBuffer[] memoryBuffers = new MemoryBuffer[repetitions];

            for (int x = 0; x < repetitions; x++)
            {
                int newSize = size + (x * increment);
                memoryBuffers[x] = bufferHelper.CreateMemoryBuffer(newSize);
            }

            // Search for our buffers with exact originally given buffer sizes and try find the exact buffer.
            for (int x = 0; x < repetitions; x++)
            {
                int newSize = size + (x * increment);
                var buffers = bufferHelper.FindBuffers(newSize);

                if (!buffers.Contains(memoryBuffers[x]))
                {
                    Assert.True(false, $"Failed to find existing buffer in memory of minimum size {newSize} bytes.");
                }
            }

            // Cleanup.
            for (int x = 0; x < repetitions; x++)
            {
                Internal.Testing.Buffers.FreeBuffer(memoryBuffers[x]);
            }
        }
        /*
         * ----------
         * Core Tests
         * ----------
         */


        /// <summary>
        /// [Testing Purposes]
        /// Creates a buffer, then frees the memory belonging to the buffer.
        /// </summary>
        private void CreateBufferBase(MemoryBufferHelper bufferHelper)
        {
            var buffer = bufferHelper.CreateMemoryBuffer(4096);

            // Cleanup
            Internal.Testing.Buffers.FreeBuffer(buffer);
        }
        /// <summary>
        /// Same as <see cref="GetBuffersInRange"/>, except disables the caching when acquiring <see cref="MemoryBuffer"/>s.
        /// </summary>
        private unsafe void GetBuffersInRangeNoCache(MemoryBufferHelper bufferHelper, IntPtr maxApplicationAddress)
        {
            /* The reason that testing the upper half is sufficient is because the buffer allocation
             * functions work in such a manner that they allocate from the lowest address.
             * As such, normally the only allocated addresses would be in the lower half... until enough memory is allocated to cross the upper half.
             */

            // Options
            int sizeStart   = 0;    // Default page size for x86 and x64.
            int repetitions = 128;
            int increment   = 4096; // Equal to allocation granularity.

            // Minimum address is start of upper half of 32/64 bit address range.
            // Maximum is the maximum address in 32/64 bit address range.
            long minAddress = (long)maxApplicationAddress - ((long)maxApplicationAddress / 2);
            long maxAddress = (long)maxApplicationAddress;

            MemoryBuffer[] buffers = new MemoryBuffer[repetitions];

            // Allocate <repetitions> buffers, and try to find them all.
            for (int x = 0; x < repetitions; x++)
            {
                int newSize = sizeStart + (x * increment);
                buffers[x] = bufferHelper.CreateMemoryBuffer(newSize, (long)minAddress, (long)maxAddress);
            }

            // Validate whether each buffer is present and in range.
            for (int x = 0; x < repetitions; x++)
            {
                int newSize      = sizeStart + (x * increment);
                var foundBuffers = bufferHelper.FindBuffers(newSize, (IntPtr)minAddress, (IntPtr)maxAddress, false);

                if (!foundBuffers.Contains(buffers[x]))
                {
                    Assert.True(false, $"Failed to find existing buffer in memory of minimum size {newSize} bytes.");
                }

                foreach (var buffer in foundBuffers)
                {
                    AssertBufferInRange(buffer, (IntPtr)minAddress, (IntPtr)maxAddress);
                }
            }

            // Cleanup
            for (int x = 0; x < buffers.Length; x++)
            {
                Internal.Testing.Buffers.FreeBuffer(buffers[x]);
            }
        }
Пример #4
0
        /// <summary>
        /// Finds an existing <see cref="MemoryBuffer"/> or creates one satisfying the given size.
        /// </summary>
        /// <param name="size">The required size of buffer.</param>
        /// <param name="minimumAddress">Maximum address of the buffer.</param>
        /// <param name="maximumAddress">Minimum address of the buffer.</param>
        /// <param name="alignment">Required alignment of the item to add to the buffer.</param>
        public static MemoryBuffer FindOrCreateBufferInRange(int size, long minimumAddress = 1, long maximumAddress = int.MaxValue, int alignment = 4)
        {
            var buffers = _bufferHelper.FindBuffers(size + alignment, (IntPtr)minimumAddress, (IntPtr)maximumAddress);

            return(buffers.Length > 0 ? buffers[0] : _bufferHelper.CreateMemoryBuffer(size, minimumAddress, maximumAddress));
        }
        /*
         * ---------------
         * Utility Methods
         * ---------------
         */

        private MemoryBuffer CreateMemoryBuffer(MemoryBufferHelper helper)
        {
            return(helper.CreateMemoryBuffer(4096));
        }