2025-03-14 11:56:21 -04:00
|
|
|
// Copyright 2025 System76 <info@system76.com>
|
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
2025-02-12 18:30:32 +01:00
|
|
|
use std::os::raw::c_int;
|
|
|
|
|
|
|
|
|
|
const M_MMAP_THRESHOLD: c_int = -3;
|
|
|
|
|
|
2025-03-19 19:23:34 +01:00
|
|
|
unsafe extern "C" {
|
2025-02-19 18:13:24 +01:00
|
|
|
fn malloc_trim(pad: usize);
|
|
|
|
|
|
2025-02-12 18:30:32 +01:00
|
|
|
fn mallopt(param: c_int, value: c_int) -> c_int;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-19 18:13:24 +01:00
|
|
|
pub fn trim(pad: usize) {
|
|
|
|
|
unsafe {
|
|
|
|
|
malloc_trim(pad);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-12 18:30:32 +01:00
|
|
|
/// Prevents glibc from hoarding memory via memory fragmentation.
|
|
|
|
|
pub fn limit_mmap_threshold(threshold: i32) {
|
|
|
|
|
unsafe {
|
|
|
|
|
mallopt(M_MMAP_THRESHOLD, threshold as c_int);
|
|
|
|
|
}
|
|
|
|
|
}
|