예제 #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}");
            }
        }
예제 #2
0
        public static int TryFreeAll()
        {
            int c = Allocations.Count;

            foreach (IntPtr p in Allocations)
            {
                Jem.Free(p);
            }
            Allocations = new ConcurrentBag <IntPtr>();
            return(c);
        }
예제 #3
0
 public void Release()
 {
     if (IsInvalid)
     {
         return;
     }
     else if (Interlocked.Exchange(ref _Ptr, IntPtr.Zero) != IntPtr.Zero)
     {
         Jem.Free(_Ptr);
     }
 }
예제 #4
0
파일: JemApi.cs 프로젝트: tedd/jemalloc.NET
        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);
        }
예제 #5
0
 public bool Free()
 {
     if (IsInvalid)
     {
         return(false);
     }
     if (IsRetained)
     {
         return(false);
     }
     return(Jem.FreeFixedBuffer(_Ptr));
 }
예제 #6
0
파일: JemApi.cs 프로젝트: tedd/jemalloc.NET
        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);
        }
예제 #7
0
 public bool Release()
 {
     if (IsNotAllocated || IsInvalid)
     {
         return(false);
     }
     else
     {
         Jem.DecrementRefCount(handle);
         DangerousRelease();
         return(true);
     }
 }
예제 #8
0
 public bool Release()
 {
     ThrowIfInvalid();
     if (RefCount == 0)
     {
         return(false);
     }
     else
     {
         Jem.DecrementRefCount(_Ptr);
         return(true);
     }
 }
예제 #9
0
파일: JemApi.cs 프로젝트: tedd/jemalloc.NET
        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);
            }
        }
예제 #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);
     }
 }
예제 #11
0
        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);
        }
예제 #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);
            }
        }
예제 #13
0
파일: JemApi.cs 프로젝트: tedd/jemalloc.NET
 public override string ToString() => Jem.GetCallerDetails(this);
예제 #14
0
 protected override bool ReleaseHandle()
 {
     return(Jem.Free(handle));
 }
예제 #15
0
 public override string ToString()
 {
     return(Jem.GetCallerDetails(this));
 }
예제 #16
0
 public void Retain()
 {
     ThrowIfInvalid();
     Jem.IncrementRefCount(_Ptr);
 }