Ejemplo n.º 1
0
        public AlignedArray(byte[] buffer, int alignment, params int[] lengths)
        {
            _buffer    = buffer;
            _alignment = alignment;
            _length    = Utils.GetTotalSize(lengths);
            _lengths   = lengths;

            if (_length > buffer.Length / Marshal.SizeOf <T>())
            {
                throw new ArgumentException($"Buffer is to small to hold array of size {nameof(lengths)}", nameof(buffer));
            }

            _pin = PinnedGCHandle.Pin(buffer);

            long value  = _pin.Pointer.ToInt64();
            int  offset = alignment - (int)(value % alignment);

            _alignedPtr = new IntPtr(value + offset);
            int maxLength = (_buffer.Length - offset) / Marshal.SizeOf <T>();

            if (_length > maxLength)
            {
                _pin.Free();
                throw new ArgumentException($"Buffer is to small to hold array of size {nameof(lengths)}", nameof(buffer));
            }
        }
Ejemplo n.º 2
0
 public PinnedArray(Array array)
 {
     if (array.GetType().GetElementType() != typeof(T))
     {
         throw new ArgumentException($"Must have elements of type {typeof(T).FullName}.", nameof(array));
     }
     _buffer = array;
     _pin    = PinnedGCHandle.Pin(_buffer);
 }