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
102/// Trait containing all [`struct@MemoryMonitor`] methods.
103///
104/// # Implementors
105///
106/// [`MemoryMonitor`][struct@crate::MemoryMonitor]
107pub trait MemoryMonitorExt: IsA<MemoryMonitor> + 'static {
108 /// Emitted when the system is running low on free memory. The signal
109 /// handler should then take the appropriate action depending on the
110 /// warning level. See the #GMemoryMonitorWarningLevel documentation for
111 /// details.
112 /// ## `level`
113 /// the #GMemoryMonitorWarningLevel warning level
114 #[cfg(feature = "v2_64")]
115 #[cfg_attr(docsrs, doc(cfg(feature = "v2_64")))]
116 #[doc(alias = "low-memory-warning")]
117 fn connect_low_memory_warning<F: Fn(&Self, MemoryMonitorWarningLevel) + 'static>(
118 &self,
119 f: F,
120 ) -> SignalHandlerId {
121 unsafe extern "C" fn low_memory_warning_trampoline<
122 P: IsA<MemoryMonitor>,
123 F: Fn(&P, MemoryMonitorWarningLevel) + 'static,
124 >(
125 this: *mut ffi::GMemoryMonitor,
126 level: ffi::GMemoryMonitorWarningLevel,
127 f: glib::ffi::gpointer,
128 ) {
129 let f: &F = &*(f as *const F);
130 f(
131 MemoryMonitor::from_glib_borrow(this).unsafe_cast_ref(),
132 from_glib(level),
133 )
134 }
135 unsafe {
136 let f: Box_<F> = Box_::new(f);
137 connect_raw(
138 self.as_ptr() as *mut _,
139 c"low-memory-warning".as_ptr() as *const _,
140 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
141 low_memory_warning_trampoline::<Self, F> as *const (),
142 )),
143 Box_::into_raw(f),
144 )
145 }
146 }
147}
148
149impl<O: IsA<MemoryMonitor>> MemoryMonitorExt for O {}