gio/auto/
memory_monitor.rs

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