feat(memory): Allocator-backed global memory limits (#544)

Policy evaluation at scale needs to be able to set memory limits
so that a bad policy does not hog memory or to ensure that
policy evaluation itself does not use too much memory which could
cause other components to suffer.

This PR introduces capability to set and enforce global memory limits.
It also lays the groundwork for enabling per evaluation limits in future.

Once a global memory limit is set, Regorus maintains per thread counters
to track memory activity (allocation, deallocation) of a thread.
These counters are periodically flushed to global memory counters.
Per thread counters avoid the contention that updating global counters
on each alloc/free would cause.

Policy evaluation periodically checks these counters and raises errors
if allocated memory has exceeded the configured limit.

Currently memory limit capability is exposed only to FFI and C#.

Also update mimalloc to v2.2.6

Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
This commit is contained in:
Anand Krishnamoorthi
2026-01-24 07:08:54 +05:30
committed by GitHub
parent 80686d6ed1
commit fd59bb5a91
93 changed files with 9772 additions and 3779 deletions

View File

@@ -28,6 +28,46 @@ namespace Regorus.Internal
#endregion
#region Memory Limit Methods
/// <summary>
/// Set the global memory limit. Pass hasLimit=false to clear the limit.
/// </summary>
[DllImport(LibraryName, EntryPoint = "regorus_set_global_memory_limit", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
internal static extern RegorusResult regorus_set_global_memory_limit(ulong limit, [MarshalAs(UnmanagedType.U1)] bool hasLimit);
/// <summary>
/// Get the current global memory limit. bool_value indicates whether a limit is set.
/// </summary>
[DllImport(LibraryName, EntryPoint = "regorus_get_global_memory_limit", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
internal static extern RegorusResult regorus_get_global_memory_limit();
/// <summary>
/// Check the global memory limit immediately.
/// </summary>
[DllImport(LibraryName, EntryPoint = "regorus_check_global_memory_limit", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
internal static extern RegorusResult regorus_check_global_memory_limit();
/// <summary>
/// Flush the current thread's pending allocation counters into global aggregates.
/// </summary>
[DllImport(LibraryName, EntryPoint = "regorus_flush_thread_memory_counters", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
internal static extern RegorusResult regorus_flush_thread_memory_counters();
/// <summary>
/// Set the per-thread flush threshold override. Pass hasThreshold=false to restore defaults.
/// </summary>
[DllImport(LibraryName, EntryPoint = "regorus_set_thread_flush_threshold_override", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
internal static extern RegorusResult regorus_set_thread_flush_threshold_override(ulong threshold, [MarshalAs(UnmanagedType.U1)] bool hasThreshold);
/// <summary>
/// Get the per-thread flush threshold. bool_value indicates whether a threshold is configured.
/// </summary>
[DllImport(LibraryName, EntryPoint = "regorus_get_thread_memory_flush_threshold", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
internal static extern RegorusResult regorus_get_thread_memory_flush_threshold();
#endregion
#region Engine Methods
/// <summary>
@@ -520,6 +560,7 @@ namespace Regorus.Internal
/// Boolean value.
/// Valid when data_type is Boolean.
/// </summary>
[MarshalAs(UnmanagedType.I1)]
public bool bool_value;
/// <summary>
/// Integer value.