cosmic/
malloc.rs

1// Copyright 2025 System76 <info@system76.com>
2// SPDX-License-Identifier: MPL-2.0
3
4use std::os::raw::c_int;
5
6const M_MMAP_THRESHOLD: c_int = -3;
7
8unsafe extern "C" {
9    fn malloc_trim(pad: usize);
10
11    fn mallopt(param: c_int, value: c_int) -> c_int;
12}
13
14#[inline]
15pub fn trim(pad: usize) {
16    unsafe {
17        malloc_trim(pad);
18    }
19}
20
21/// Prevents glibc from hoarding memory via memory fragmentation.
22#[inline]
23pub fn limit_mmap_threshold(threshold: i32) {
24    unsafe {
25        mallopt(M_MMAP_THRESHOLD, threshold as c_int);
26    }
27}