cgroups_rs/systemd/
memory.rs

1// Copyright (c) 2025 Ant Group
2//
3// SPDX-License-Identifier: Apache-2.0 or MIT
4//
5
6use crate::systemd::error::{Error, Result};
7use crate::systemd::{MEMORY_LIMIT, MEMORY_LOW, MEMORY_MAX, MEMORY_SWAP_MAX};
8
9/// Returns the property for memory limit.
10pub fn limit(limit: i64, v2: bool) -> Result<(&'static str, u64)> {
11    let id = if v2 { MEMORY_MAX } else { MEMORY_LIMIT };
12
13    Ok((id, limit as u64))
14}
15
16/// Returns the property for memory limit.
17pub fn low(low: i64, v2: bool) -> Result<(&'static str, u64)> {
18    if !v2 {
19        return Err(Error::CgroupsV1NotSupported);
20    }
21
22    Ok((MEMORY_LOW, low as u64))
23}
24
25/// Returns the property for memory swap.
26pub fn swap(swap: i64, v2: bool) -> Result<(&'static str, u64)> {
27    if !v2 {
28        return Err(Error::CgroupsV1NotSupported);
29    }
30
31    Ok((MEMORY_SWAP_MAX, swap as u64))
32}