/// <summary> /// Implementation of the Dispose pattern. /// </summary> private void Dispose(bool disposing) { if (disposed) { return; } logger.LogDebug("Disposing ByteBufChunk [{0}].", ToString()); if (!IsUnsafe && memory != null) { memory = null; segmentQueue.Clear(); } if (BufId != IntPtr.Zero) { RioNative.DeregisterRIOBuffer(BufId); } // If the unsafedMemory is still valid, free it. if (unsafeMemory != IntPtr.Zero) { var heapBlock = unsafeMemory; unsafeMemory = IntPtr.Zero; FreeToProcessHeap(heapBlock); } if (disposing) { GC.SuppressFinalize(this); } disposed = true; }
public static ByteBufChunk NewChunk(ByteBufPool pool, int segmentSize, int chunkSize, bool isUnsafe) { ByteBufChunk chunk = null; if (!isUnsafe) { chunk = new ByteBufChunk(pool, new byte[chunkSize], segmentSize, chunkSize); return(chunk); } // allocate buffers from process heap var token = HeapAlloc(GetProcessHeap(), 0, chunkSize); if (token == IntPtr.Zero) { throw new OutOfMemoryException("Failed to allocate memory by calling HeapAlloc()"); } // register this heap buffer to RIO buffer var bufferId = RioNative.RegisterRIOBuffer(token, (uint)chunkSize); if (bufferId == IntPtr.Zero) { FreeToProcessHeap(token); throw new Exception(string.Format("Failed to register RIO buffer with error code {0}", Marshal.GetLastWin32Error())); } try { chunk = new ByteBufChunk(pool, token, bufferId, segmentSize, chunkSize); token = IntPtr.Zero; bufferId = IntPtr.Zero; return(chunk); } finally { if (chunk == null && token != IntPtr.Zero) { if (bufferId != IntPtr.Zero) { RioNative.DeregisterRIOBuffer(bufferId); } FreeToProcessHeap(token); } } }