Navigation Menu

Skip to content

Commit

Permalink
Merge branch '90-gc-cleanup'
Browse files Browse the repository at this point in the history
Fix #90.

Reduce memory allocations and GC pressure for send, receive, and
forward operations.
  • Loading branch information
jgoz committed Oct 15, 2012
2 parents 9e882bc + 76397c9 commit e3d5cb5
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 150 deletions.
17 changes: 3 additions & 14 deletions src/ZeroMQ/Interop/DisposableIntPtr.cs
Expand Up @@ -3,32 +3,21 @@
using System;
using System.Runtime.InteropServices;

internal class DisposableIntPtr : IDisposable
internal struct DisposableIntPtr : IDisposable
{
public IntPtr Ptr;

public DisposableIntPtr(int size)
{
Ptr = Marshal.AllocHGlobal(size);
}

~DisposableIntPtr()
{
Dispose(false);
}

public IntPtr Ptr { get; private set; }

public static implicit operator IntPtr(DisposableIntPtr disposablePtr)
{
return disposablePtr.Ptr;
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (Ptr != IntPtr.Zero)
{
Expand Down
33 changes: 33 additions & 0 deletions src/ZeroMQ/Interop/Retry.cs
@@ -0,0 +1,33 @@
namespace ZeroMQ.Interop
{
using System;

internal static class Retry
{
public static int IfInterrupted<T1, T2, T3>(Func<T1, T2, T3, int> operation, T1 arg1, T2 arg2, T3 arg3)
{
int rc;

do
{
rc = operation(arg1, arg2, arg3);
}
while (rc == -1 && LibZmq.zmq_errno() == ErrorCode.EINTR);

return rc;
}

public static int IfInterrupted<T1, T2, T3, T4>(Func<T1, T2, T3, T4, int> operation, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
{
int rc;

do
{
rc = operation(arg1, arg2, arg3, arg4);
}
while (rc == -1 && LibZmq.zmq_errno() == ErrorCode.EINTR);

return rc;
}
}
}

0 comments on commit e3d5cb5

Please sign in to comment.