Пример #1
0
        public static BufferHandle Create <T>(OwnedBuffer <T> owner, int index, GCHandle handle = default(GCHandle))
        {
            void *pointer;

            if (owner.TryGetPointerInternal(out pointer))
            {
                pointer = Buffer <T> .Add(pointer, index);
            }
            else
            {
                ArraySegment <T> buffer;
                if (owner.TryGetArrayInternal(out buffer))
                {
                    handle  = GCHandle.Alloc(buffer.Array, GCHandleType.Pinned);
                    pointer = Buffer <T> .Add((void *)handle.AddrOfPinnedObject(), buffer.Offset + index);
                }
                else
                {
                    throw new InvalidOperationException("Memory cannot be pinned");
                }
            }

            owner.AddReference();

            return(new BufferHandle {
                _owner = owner,
                _handle = handle,
                _pointer = pointer
            });
        }
Пример #2
0
 public unsafe bool TryGetArray(out ArraySegment <T> buffer)
 {
     if (!_owner.TryGetArrayInternal(out buffer))
     {
         return(false);
     }
     buffer = new ArraySegment <T>(buffer.Array, buffer.Offset + _index, _length);
     return(true);
 }