public void CreatePooledBuffer() { managedBufPool = new ByteBufPool(1024, ByteBufPool.DefaultChunkOrder, false); if (SocketFactory.IsRioSockSupported()) { unsafeBufPool = new ByteBufPool(1024, ByteBufPool.DefaultChunkOrder, true); } }
/// <summary> /// Initializes a SocketStream with a RioSocketWrapper object. /// </summary> /// <param name="socket">a RioSocketWrapper object</param> public SocketStream(RioSocketWrapper socket) { if (socket == null) { throw new ArgumentNullException("socket"); } streamSocket = socket; bufPool = ByteBufPool.UnsafeDefault; }
private ByteBufChunk(ByteBufPool pool, IntPtr memory, IntPtr bufId, int segmentSize, int chunkSize) : this(pool, segmentSize, chunkSize) { if (memory == IntPtr.Zero || chunkSize == 0) { throw new ArgumentNullException("memory", "Must be initialized with a valid heap block."); } IsUnsafe = true; unsafeMemory = memory; this.memory = null; BufId = bufId; }
private ByteBufChunk(ByteBufPool pool, byte[] memory, int segmentSize, int chunkSize) : this(pool, segmentSize, chunkSize) { if (memory == null || chunkSize == 0) { throw new ArgumentNullException("memory", "Must be initialized with a valid byte array"); } IsUnsafe = false; this.memory = memory; unsafeMemory = IntPtr.Zero; BufId = IntPtr.Zero; }
private ByteBufChunk(ByteBufPool pool, int segmentSize, int chunkSize) { Pool = pool; FreeBytes = chunkSize; Size = chunkSize; this.segmentSize = segmentSize; segmentQueue = new Queue<Segment>(); var numSegment = chunkSize / segmentSize; for (var i = 0; i < numSegment; i++) { segmentQueue.Enqueue(new Segment(i * segmentSize, segmentSize)); } }
private ByteBufChunk(ByteBufPool pool, int segmentSize, int chunkSize) { Pool = pool; FreeBytes = chunkSize; Size = chunkSize; this.segmentSize = segmentSize; segmentQueue = new Queue <Segment>(); var numSegment = chunkSize / segmentSize; for (var i = 0; i < numSegment; i++) { segmentQueue.Enqueue(new Segment(i * segmentSize, segmentSize)); } }
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); } } }
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); } } }
public void CreatePooledBuffer() { managedBufPool = new ByteBufPool(1024, 2, false); unsafeBufPool = new ByteBufPool(1024, 2, true); }
public void CreatePooledBuffer() { bufPool = new ByteBufPool(1024, 2, false); }