Example #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ArrayBlock"/> class.
        /// </summary>
        /// <param name="arrayBlockPool">The array block pool.</param>
        /// <param name="id">The Id.</param>
        /// <param name="buffer">The buffer.</param>
        /// <param name="length">The length.</param>
        public ArrayBlock(IArrayBlockPool arrayBlockPool, int id, byte[] buffer, int length)
        {
            Ensure.GreaterOrEqualZero(Log, id, "Id is negative");
            Ensure.IsNotNull(Log, buffer, "buffer is null");
            Ensure.GreaterZero(Log, length, "length <= 0");
            Ensure.Greater(Log, buffer.Length, (id + 1) * length - 1, "length must be less than maximum offset");
            Ensure.IsTrue(Log, buffer.Length % length == 0, "buffer length must be divided by length");

            this.arrayBlockPool = arrayBlockPool;

            this.id     = id;
            this.buffer = buffer;

            this.length = length;
            this.offset = id * length;

            // buffer must be pinned.
            this.ptrToData = this.arrayBlockPool != null?Marshal.UnsafeAddrOfPinnedArrayElement(buffer, this.offset) : IntPtr.Zero;
        }
Example #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ArrayBlock"/> class.
        /// </summary>
        /// <param name="arrayBlockPool">The array block pool.</param>
        /// <param name="id">The Id.</param>
        /// <param name="buffer">The buffer.</param>
        /// <param name="length">The length.</param>
        public ArrayBlock(IArrayBlockPool arrayBlockPool, int id, byte[] buffer, int length)
        {
            Ensure.GreaterOrEqualZero(Log, id, "Id is negative");
            Ensure.IsNotNull(Log, buffer, "buffer is null");
            Ensure.GreaterZero(Log, length, "length <= 0");
            Ensure.Greater(Log, buffer.Length, (id + 1) * length - 1, "length must be less than maximum offset");
            Ensure.IsTrue(Log, buffer.Length % length == 0, "buffer length must be divided by length");

            this.arrayBlockPool = arrayBlockPool;

            this.id = id;
            this.buffer = buffer;

            this.length = length;
            this.offset = id * length;

            // buffer must be pinned.
            this.ptrToData = this.arrayBlockPool != null ? Marshal.UnsafeAddrOfPinnedArrayElement(buffer, this.offset) : IntPtr.Zero;
        }