gio/auto/memory_monitor.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT
use crate::{ffi, Initable, MemoryMonitorWarningLevel};
use glib::{
prelude::*,
signal::{connect_raw, SignalHandlerId},
translate::*,
};
use std::boxed::Box as Box_;
glib::wrapper! {
/// `GMemoryMonitor` will monitor system memory and suggest to the application
/// when to free memory so as to leave more room for other applications.
/// It is implemented on Linux using the
/// [Low Memory Monitor](https://gitlab.freedesktop.org/hadess/low-memory-monitor/)
/// ([API documentation](https://hadess.pages.freedesktop.org/low-memory-monitor/)).
///
/// There is also an implementation for use inside Flatpak sandboxes.
///
/// Possible actions to take when the signal is received are:
///
/// - Free caches
/// - Save files that haven’t been looked at in a while to disk, ready to be reopened when needed
/// - Run a garbage collection cycle
/// - Try and compress fragmented allocations
/// - Exit on idle if the process has no reason to stay around
/// - Call [`malloc_trim(3)`](man:malloc_trim(3)) to return cached heap pages to
/// the kernel (if supported by your libc)
///
/// Note that some actions may not always improve system performance, and so
/// should be profiled for your application. `malloc_trim()`, for example, may
/// make future heap allocations slower (due to releasing cached heap pages back
/// to the kernel).
///
/// See [type@Gio.MemoryMonitorWarningLevel] for details on the various warning
/// levels.
///
/// **⚠️ The following code is in c ⚠️**
///
/// ```c
/// static void
/// warning_cb (GMemoryMonitor *m, GMemoryMonitorWarningLevel level)
/// {
/// g_debug ("Warning level: %d", level);
/// if (warning_level > G_MEMORY_MONITOR_WARNING_LEVEL_LOW)
/// drop_caches ();
/// }
///
/// static GMemoryMonitor *
/// monitor_low_memory (void)
/// {
/// GMemoryMonitor *m;
/// m = g_memory_monitor_dup_default ();
/// g_signal_connect (G_OBJECT (m), "low-memory-warning",
/// G_CALLBACK (warning_cb), NULL);
/// return m;
/// }
/// ```
///
/// Don’t forget to disconnect the [`low-memory-warning`][struct@crate::MemoryMonitor#low-memory-warning]
/// signal, and unref the `GMemoryMonitor` itself when exiting.
///
/// ## Signals
///
///
/// #### `low-memory-warning`
/// Emitted when the system is running low on free memory. The signal
/// handler should then take the appropriate action depending on the
/// warning level. See the #GMemoryMonitorWarningLevel documentation for
/// details.
///
///
///
/// # Implements
///
/// [`MemoryMonitorExt`][trait@crate::prelude::MemoryMonitorExt], [`InitableExt`][trait@crate::prelude::InitableExt]
#[doc(alias = "GMemoryMonitor")]
pub struct MemoryMonitor(Interface<ffi::GMemoryMonitor, ffi::GMemoryMonitorInterface>) @requires Initable;
match fn {
type_ => || ffi::g_memory_monitor_get_type(),
}
}
impl MemoryMonitor {
pub const NONE: Option<&'static MemoryMonitor> = None;
/// Gets a reference to the default #GMemoryMonitor for the system.
///
/// # Returns
///
/// a new reference to the default #GMemoryMonitor
#[doc(alias = "g_memory_monitor_dup_default")]
pub fn dup_default() -> MemoryMonitor {
unsafe { from_glib_full(ffi::g_memory_monitor_dup_default()) }
}
}
mod sealed {
pub trait Sealed {}
impl<T: super::IsA<super::MemoryMonitor>> Sealed for T {}
}
/// Trait containing all [`struct@MemoryMonitor`] methods.
///
/// # Implementors
///
/// [`MemoryMonitor`][struct@crate::MemoryMonitor]
pub trait MemoryMonitorExt: IsA<MemoryMonitor> + sealed::Sealed + 'static {
/// Emitted when the system is running low on free memory. The signal
/// handler should then take the appropriate action depending on the
/// warning level. See the #GMemoryMonitorWarningLevel documentation for
/// details.
/// ## `level`
/// the #GMemoryMonitorWarningLevel warning level
#[cfg(feature = "v2_64")]
#[cfg_attr(docsrs, doc(cfg(feature = "v2_64")))]
#[doc(alias = "low-memory-warning")]
fn connect_low_memory_warning<F: Fn(&Self, MemoryMonitorWarningLevel) + 'static>(
&self,
f: F,
) -> SignalHandlerId {
unsafe extern "C" fn low_memory_warning_trampoline<
P: IsA<MemoryMonitor>,
F: Fn(&P, MemoryMonitorWarningLevel) + 'static,
>(
this: *mut ffi::GMemoryMonitor,
level: ffi::GMemoryMonitorWarningLevel,
f: glib::ffi::gpointer,
) {
let f: &F = &*(f as *const F);
f(
MemoryMonitor::from_glib_borrow(this).unsafe_cast_ref(),
from_glib(level),
)
}
unsafe {
let f: Box_<F> = Box_::new(f);
connect_raw(
self.as_ptr() as *mut _,
b"low-memory-warning\0".as_ptr() as *const _,
Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
low_memory_warning_trampoline::<Self, F> as *const (),
)),
Box_::into_raw(f),
)
}
}
}
impl<O: IsA<MemoryMonitor>> MemoryMonitorExt for O {}