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

@@ -2,22 +2,23 @@
// Licensed under the MIT License.
use core::ffi::c_void;
pub static MI_ALIGNMENT_MAX: usize = 1024 * 1024; // 1 MiB
pub const MI_ALIGNMENT_MAX: usize = 1024 * 1024; // 1 MiB
// Define core functions from mimalloc needed for the allocator
extern "C" {
/// Allocate size bytes aligned by alignment.
/// size: the number of bytes to allocate
/// alignment: the minimal alignment of the allocated memory. Must be less than `MI_ALIGNMENT_MAX`
/// returns: a pointer to the allocated memory, or null if out of memory. The returned pointer is aligned by alignment
/// Allocate `size` bytes aligned by `alignment`.
/// Returns a pointer to the allocated memory, or null if out of memory. The returned pointer is aligned by `alignment`.
pub fn mi_malloc_aligned(size: usize, alignment: usize) -> *mut c_void;
pub fn mi_zalloc_aligned(size: usize, alignment: usize) -> *mut c_void;
/// Free previously allocated memory.
/// The pointer p must have been allocated before (or be nullptr).
/// p: the pointer to the memory to free or nullptr
/// The pointer `p` must have been allocated before (or be nullptr).
pub fn mi_free(p: *mut c_void);
pub fn mi_realloc_aligned(p: *mut c_void, newsize: usize, alignment: usize) -> *mut c_void;
/// Reset allocator statistics.
pub fn mi_stats_reset();
}
#[cfg(test)]