Skip to content

Commit

Permalink
Response to comments
Browse files Browse the repository at this point in the history
  • Loading branch information
anna-git committed Sep 15, 2023
1 parent 51b4a45 commit a6c44b7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
14 changes: 10 additions & 4 deletions tracer/src/Datadog.Trace/AppSec/Waf/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,16 @@ private Context(IntPtr contextHandle, Waf waf, WafLibraryInvoker wafLibraryInvok
lock (_stopwatch)
{
var pool = Encoder.Pool;
var pwArgs = Encoder.Encode(addresses, applySafetyLimits: true, argToFree: _argCache, pool: pool);
code = _waf.Run(_contextHandle, ref pwArgs, ref retNative, timeoutMicroSeconds);
pool.Return(_argCache);
_argCache.Clear();
try
{
var pwArgs = Encoder.Encode(addresses, applySafetyLimits: true, argToFree: _argCache, pool: pool);
code = _waf.Run(_contextHandle, ref pwArgs, ref retNative, timeoutMicroSeconds);
}
finally
{
pool.Return(_argCache);
_argCache.Clear();
}
}

_stopwatch.Stop();
Expand Down
41 changes: 20 additions & 21 deletions tracer/src/Datadog.Trace/Util/UnmanagedMemoryPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

namespace Datadog.Trace.Util;

/// <summary>
/// Beware that this type is not thread safe and should be used with [ThreadStatic]
/// </summary>
internal unsafe class UnmanagedMemoryPool : IDisposable
{
private readonly IntPtr* _items;
Expand Down Expand Up @@ -40,22 +43,24 @@ public UnmanagedMemoryPool(int blockSize, int poolSize)

public bool IsDisposed => _isDisposed;

/// <summary>
/// Beware that this method is not thread safe, and needs to be used with [ThreadStatic] in case of multiple thread scenarios
/// </summary>
/// <returns>Pointer to a memory block of size specified in the constructor</returns>
public IntPtr Rent()
{
if (IsDisposed)
{
ThrowObjectDisposedException();
}

var items = _items;
var length = _length;
for (var i = _initialSearchIndex; i < length; i++)
for (var i = _initialSearchIndex; i < _length; i++)
{
var inst = items[i];
var inst = _items[i];
if (inst != IntPtr.Zero)
{
_initialSearchIndex = i + 1;
items[i] = IntPtr.Zero;
_items[i] = IntPtr.Zero;
return inst;
}
}
Expand All @@ -75,13 +80,11 @@ public void Return(IntPtr block)
ThrowObjectDisposedException();
}

var items = _items;
var length = _length;
for (var i = 0; i < length; i++)
for (var i = 0; i < _length; i++)
{
if (items[i] == IntPtr.Zero)
if (_items[i] == IntPtr.Zero)
{
items[i] = block;
_items[i] = block;
_initialSearchIndex = 0;
return;
}
Expand All @@ -102,14 +105,12 @@ public void Return(IList<IntPtr> blocks)
return;
}

var items = _items;
var length = _length;
var blockIndex = 0;
for (var i = 0; i < length; i++)
for (var i = 0; i < _length; i++)
{
if (items[i] == IntPtr.Zero)
if (_items[i] == IntPtr.Zero)
{
items[i] = blocks[blockIndex++];
_items[i] = blocks[blockIndex++];
if (blockIndex == blocks.Count)
{
_initialSearchIndex = 0;
Expand All @@ -136,14 +137,12 @@ public void Dispose()
return;
}

var items = _items;
var length = _length;
for (var i = 0; i < length; i++)
for (var i = 0; i < _length; i++)
{
if (items[i] != IntPtr.Zero)
if (_items[i] != IntPtr.Zero)
{
Marshal.FreeCoTaskMem(items[i]);
items[i] = IntPtr.Zero;
Marshal.FreeCoTaskMem(_items[i]);
_items[i] = IntPtr.Zero;
}
}

Expand Down

0 comments on commit a6c44b7

Please sign in to comment.