Beispiel #1
0
        public FixedBuffer(int length)
        {
            _Ptr             = IntPtr.Zero;
            _Length          = 0;
            _SizeInBytes     = 0;
            _Timestamp       = 0;
            IsReadOnly       = false;
            AllocateThreadId = 0;
            Rid = JemUtil.Rng.Next(0, 4096);
            if (length == 0)
            {
                throw new ArgumentException("FixedBuffer Length cannot be zero.");
            }
            ThrowIfTypeNotPrimitive();
            long t  = DateTime.UtcNow.Ticks;
            int  th = Thread.CurrentThread.ManagedThreadId;

            _Ptr = Jem.AllocateFixedBuffer <T>((ulong)length, ElementSizeInBytes, t, th, Rid);
            if (_Ptr != IntPtr.Zero)
            {
                _Length          = length;
                _SizeInBytes     = (ulong)_Length * ElementSizeInBytes;
                _Timestamp       = t;
                AllocateThreadId = th;
            }
            else
            {
                throw new OutOfMemoryException($"Could not allocate {(ulong)_Length * ElementSizeInBytes} bytes for {Name}");
            }
        }
Beispiel #2
0
        public static int TryFreeAll()
        {
            int c = Allocations.Count;

            foreach (IntPtr p in Allocations)
            {
                Jem.Free(p);
            }
            Allocations = new ConcurrentBag <IntPtr>();
            return(c);
        }
Beispiel #3
0
 public void Release()
 {
     if (IsInvalid)
     {
         return;
     }
     else if (Interlocked.Exchange(ref _Ptr, IntPtr.Zero) != IntPtr.Zero)
     {
         Jem.Free(_Ptr);
     }
 }
Beispiel #4
0
        public static IntPtr AllocateFixedBuffer <T>(ulong length, ulong size, long timestamp, int tid, int rid, [CallerMemberName] string memberName = "", [CallerFilePath] string fileName = "", [CallerLineNumber] int lineNumber = 0)
        {
            IntPtr ptr = Jem.Calloc(length, size, memberName, fileName, lineNumber);

            if (ptr != IntPtr.Zero)
            {
                fixedBufferLock.EnterWriteLock();
                _FixedBufferAllocations.Add(ptr, new FixedBufferAllocation(ptr, length * size, timestamp, tid, rid));
                fixedBufferLock.ExitWriteLock();
            }
            return(ptr);
        }
Beispiel #5
0
 public bool Free()
 {
     if (IsInvalid)
     {
         return(false);
     }
     if (IsRetained)
     {
         return(false);
     }
     return(Jem.FreeFixedBuffer(_Ptr));
 }
Beispiel #6
0
        public static int TryFreeAll()
        {
            allocLock.EnterWriteLock();
            int c = _Allocations.Count;

            foreach (IntPtr p in _Allocations.Keys)
            {
                Jem.Free(p);
                _Allocations.Remove(p);
            }
            allocLock.ExitWriteLock();
            return(c);
        }
Beispiel #7
0
 public bool Release()
 {
     if (IsNotAllocated || IsInvalid)
     {
         return(false);
     }
     else
     {
         Jem.DecrementRefCount(handle);
         DangerousRelease();
         return(true);
     }
 }
Beispiel #8
0
 public bool Release()
 {
     ThrowIfInvalid();
     if (RefCount == 0)
     {
         return(false);
     }
     else
     {
         Jem.DecrementRefCount(_Ptr);
         return(true);
     }
 }
Beispiel #9
0
        public static bool FreeFixedBuffer(IntPtr ptr)
        {
            fixedBufferLock.EnterWriteLock();
            bool r = _FixedBufferAllocations.Remove(ptr);

            fixedBufferLock.ExitWriteLock();
            if (!r)
            {
                return(false);
            }
            else
            {
                Jem.Free(ptr);
                return(true);
            }
        }
Beispiel #10
0
 private unsafe bool Allocate(int length)
 {
     _Ptr = Jem.Calloc((ulong)length, ElementSizeInBytes);
     if (_Ptr != IntPtr.Zero)
     {
         _Length      = length;
         _SizeInBytes = (ulong)_Length * ElementSizeInBytes;
         _Timestamp   = DateTime.Now.Ticks;
         _VoidPointer = _Ptr.ToPointer();
         _Span        = new Span <T>(_VoidPointer, _Length);
         return(true);
     }
     else
     {
         return(false);
     }
 }
        protected unsafe virtual IntPtr Allocate(int length)
        {
            if (length <= 0)
            {
                throw new ArgumentOutOfRangeException("length");
            }
            Contract.EndContractBlock();
            ulong s = checked ((uint)length * ElementSizeInBytes);

            handle = Jem.Calloc((uint)length, ElementSizeInBytes);
            if (handle != IntPtr.Zero)
            {
                voidPtr     = handle.ToPointer();
                Length      = length;
                SizeInBytes = s;
                InitVector();
            }
            return(handle);
        }
Beispiel #12
0
        protected override bool ReleaseHandle()
        {
            bool r = Jem.Free(handle);

            if (!r)
            {
                return(false);
            }
            else
            {
                handle = IntPtr.Zero;
                unsafe
                {
                    voidPtr = (void *)0;
                }
                Length      = 0;
                SizeInBytes = 0;
                return(true);
            }
        }
Beispiel #13
0
 public override string ToString() => Jem.GetCallerDetails(this);
 protected override bool ReleaseHandle()
 {
     return(Jem.Free(handle));
 }
Beispiel #15
0
 public override string ToString()
 {
     return(Jem.GetCallerDetails(this));
 }
Beispiel #16
0
 public void Retain()
 {
     ThrowIfInvalid();
     Jem.IncrementRefCount(_Ptr);
 }