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. The signal
71 /// handler should then take the appropriate action depending on the
72 /// warning level. See the #GMemoryMonitorWarningLevel documentation for
73 /// details.
74 ///
75 ///
76 ///
77 /// # Implements
78 ///
79 /// [`MemoryMonitorExt`][trait@crate::prelude::MemoryMonitorExt], [`InitableExt`][trait@crate::prelude::InitableExt]
80 #[doc(alias = "GMemoryMonitor")]
81 pub struct MemoryMonitor(Interface<ffi::GMemoryMonitor, ffi::GMemoryMonitorInterface>) @requires Initable;
82
83 match fn {
84 type_ => || ffi::g_memory_monitor_get_type(),
85 }
86}
87
88impl MemoryMonitor {
89 pub const NONE: Option<&'static MemoryMonitor> = None;
90
91 /// Gets a reference to the default #GMemoryMonitor for the system.
92 ///
93 /// # Returns
94 ///
95 /// a new reference to the default #GMemoryMonitor
96 #[doc(alias = "g_memory_monitor_dup_default")]
97 pub fn dup_default() -> MemoryMonitor {
98 unsafe { from_glib_full(ffi::g_memory_monitor_dup_default()) }
99 }
100}
101
102mod sealed {
103 pub trait Sealed {}
104 impl<T: super::IsA<super::MemoryMonitor>> Sealed for T {}
105}
106
107/// Trait containing all [`struct@MemoryMonitor`] methods.
108///
109/// # Implementors
110///
111/// [`MemoryMonitor`][struct@crate::MemoryMonitor]
112pub trait MemoryMonitorExt: IsA<MemoryMonitor> + sealed::Sealed + 'static {
113 /// Emitted when the system is running low on free memory. The signal
114 /// handler should then take the appropriate action depending on the
115 /// warning level. See the #GMemoryMonitorWarningLevel documentation for
116 /// details.
117 /// ## `level`
118 /// the #GMemoryMonitorWarningLevel warning level
119 #[cfg(feature = "v2_64")]
120 #[cfg_attr(docsrs, doc(cfg(feature = "v2_64")))]
121 #[doc(alias = "low-memory-warning")]
122 fn connect_low_memory_warning<F: Fn(&Self, MemoryMonitorWarningLevel) + 'static>(
123 &self,
124 f: F,
125 ) -> SignalHandlerId {
126 unsafe extern "C" fn low_memory_warning_trampoline<
127 P: IsA<MemoryMonitor>,
128 F: Fn(&P, MemoryMonitorWarningLevel) + 'static,
129 >(
130 this: *mut ffi::GMemoryMonitor,
131 level: ffi::GMemoryMonitorWarningLevel,
132 f: glib::ffi::gpointer,
133 ) {
134 let f: &F = &*(f as *const F);
135 f(
136 MemoryMonitor::from_glib_borrow(this).unsafe_cast_ref(),
137 from_glib(level),
138 )
139 }
140 unsafe {
141 let f: Box_<F> = Box_::new(f);
142 connect_raw(
143 self.as_ptr() as *mut _,
144 b"low-memory-warning\0".as_ptr() as *const _,
145 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
146 low_memory_warning_trampoline::<Self, F> as *const (),
147 )),
148 Box_::into_raw(f),
149 )
150 }
151 }
152}
153
154impl<O: IsA<MemoryMonitor>> MemoryMonitorExt for O {}