Ejemplo n.º 1
0
 protected SafeBuffer(int length, params T[] values) : base(IntPtr.Zero, true)
 {
     if (BufferHelpers.IsReferenceOrContainsReferences(typeof(T), out FieldInfo field))
     {
         throw new ArgumentException($"Only structures without reference fields can be used with this class. The field {field.Name} has type {field.FieldType.Name}.");
     }
     if (values.Length > length)
     {
         throw new ArgumentException("The length of the list of values must be smaller or equal to the length of the buffer");
     }
     SizeInBytes = NotAllocated;
     base.SetHandle(Allocate(length));
     if (IsAllocated)
     {
         CopyFrom(values);
     }
 }
Ejemplo n.º 2
0
        protected unsafe void InitSegments()
        {
            int n = (int)((Length - 1) / Int32.MaxValue) + 1;

            segments     = new IntPtr[n];
            segments2    = new Tuple <IntPtr, int> [segments.Length];
            segments[0]  = handle;
            segments2[0] = new Tuple <IntPtr, int>(handle, n == 1 ? (int)Length : Int32.MaxValue);
            for (int i = 1; i < n; i++)
            {
                segments[i] = BufferHelpers.Add <T>(segments[i - 1], Int32.MaxValue);
                if (i < n - 1)
                {
                    segments2[i] = new Tuple <IntPtr, int>(segments[i], Int32.MaxValue);
                }
            }
            segments2[n - 1] = new Tuple <IntPtr, int>(segments[n - 1], (int)(Length - (ulong)((n - 1) * Int32.MaxValue)));
        }
Ejemplo n.º 3
0
        public unsafe Vector <T> AcquireSliceSegmentAsVector(ulong index)
        {
            ThrowIfNotAllocatedOrInvalid();
            ThrowIfNotNumeric();
            if ((Length - index) < (ulong)VectorLength)
            {
                ThrowIfIndexOutOfRange(index);
            }
            ThrowIfCannotAcquire();
            T v = this[index];

            GetSegment(index, out void *ptr, out int offset);
            IntPtr             p      = new IntPtr(ptr);
            Span <T>           span   = new Span <T>(BufferHelpers.Add <T>(p, offset).ToPointer(), VectorLength);
            Span <Vector <T> > vector = span.NonPortableCast <T, Vector <T> >();

            return(vector[0]);
        }
Ejemplo n.º 4
0
        protected HugeBuffer(ulong length, params T[] values) : base(IntPtr.Zero, true)
        {
            ulong l = (ulong)values.Length;

            if (BufferHelpers.IsReferenceOrContainsReferences <T>())
            {
                throw new ArgumentException("Only structures without reference fields can be used with this class.");
            }
            if (l > length)
            {
                throw new ArgumentException("The length of the list of values must be smaller or equal to the length of the buffer");
            }
            SizeInBytes = NotAllocated;
            base.SetHandle(Allocate(length));
            if (IsAllocated)
            {
                CopyFrom(values);
            }
        }
Ejemplo n.º 5
0
 protected SafeBuffer(int length, params T[] values) : base(IntPtr.Zero, true)
 {
     if (BufferHelpers.IsReferenceOrContainsReferences <T>())
     {
         throw new ArgumentException("Only structures without reference fields can be used with this class.");
     }
     if (values.Length > length)
     {
         throw new ArgumentException("The length of the list of values must be smaller or equal to the length of the buffer");
     }
     SizeInBytes = NotAllocated;
     base.SetHandle(Allocate(length));
     if (IsAllocated)
     {
         for (int i = 0; i < values.Length; i++)
         {
             this[i] = values[i];
         }
     }
 }
Ejemplo n.º 6
0
 public unsafe void *PtrTo(int index)
 {
     return(BufferHelpers.Add <T>(_Ptr, index).ToPointer());
 }