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}"); } }
public static int TryFreeAll() { int c = Allocations.Count; foreach (IntPtr p in Allocations) { Jem.Free(p); } Allocations = new ConcurrentBag <IntPtr>(); return(c); }
public void Release() { if (IsInvalid) { return; } else if (Interlocked.Exchange(ref _Ptr, IntPtr.Zero) != IntPtr.Zero) { Jem.Free(_Ptr); } }
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); }
public bool Free() { if (IsInvalid) { return(false); } if (IsRetained) { return(false); } return(Jem.FreeFixedBuffer(_Ptr)); }
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); }
public bool Release() { if (IsNotAllocated || IsInvalid) { return(false); } else { Jem.DecrementRefCount(handle); DangerousRelease(); return(true); } }
public bool Release() { ThrowIfInvalid(); if (RefCount == 0) { return(false); } else { Jem.DecrementRefCount(_Ptr); return(true); } }
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); } }
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); }
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); } }
public override string ToString() => Jem.GetCallerDetails(this);
protected override bool ReleaseHandle() { return(Jem.Free(handle)); }
public override string ToString() { return(Jem.GetCallerDetails(this)); }
public void Retain() { ThrowIfInvalid(); Jem.IncrementRefCount(_Ptr); }