gmemorymonitor.go RawHistoryFolder
1// Package gio was automatically generated by github.com/bnema/puregotk DO NOT EDIT
2package gio
3
4import (
5 "structs"
6 "unsafe"
7
8 "github.com/ebitengine/purego"
9
10 "github.com/bnema/puregotk/pkg/core"
11 "github.com/bnema/puregotk/v4/gobject/types"
12)
13
14// The virtual function table for #GMemoryMonitor.
15type MemoryMonitorInterface struct {
16 _ structs.HostLayout
17
18 GIface uintptr
19
20 xLowMemoryWarning uintptr
21}
22
23func (x *MemoryMonitorInterface) GoPointer() uintptr {
24 return uintptr(unsafe.Pointer(x))
25}
26
27// OverrideLowMemoryWarning sets the "low_memory_warning" callback function.
28// the virtual function pointer for the
29//
30// #GMemoryMonitor::low-memory-warning signal.
31func (x *MemoryMonitorInterface) OverrideLowMemoryWarning(cb func(MemoryMonitor, MemoryMonitorWarningLevel)) {
32 if cb == nil {
33 x.xLowMemoryWarning = 0
34 } else {
35 x.xLowMemoryWarning = purego.NewCallback(func(MonitorVarp uintptr, LevelVarp MemoryMonitorWarningLevel) {
36 cb(&MemoryMonitorBase{Ptr: MonitorVarp}, LevelVarp)
37 })
38 }
39}
40
41// GetLowMemoryWarning gets the "low_memory_warning" callback function.
42// the virtual function pointer for the
43//
44// #GMemoryMonitor::low-memory-warning signal.
45func (x *MemoryMonitorInterface) GetLowMemoryWarning() func(MemoryMonitor, MemoryMonitorWarningLevel) {
46 if x.xLowMemoryWarning == 0 {
47 return nil
48 }
49 var rawCallback func(MonitorVarp uintptr, LevelVarp MemoryMonitorWarningLevel)
50 purego.RegisterFunc(&rawCallback, x.xLowMemoryWarning)
51 return func(MonitorVar MemoryMonitor, LevelVar MemoryMonitorWarningLevel) {
52 rawCallback(MonitorVar.GoPointer(), LevelVar)
53 }
54}
55
56// `GMemoryMonitor` will monitor system memory and suggest to the application
57// when to free memory so as to leave more room for other applications.
58// It is implemented on Linux using the
59// [Low Memory Monitor](https://gitlab.freedesktop.org/hadess/low-memory-monitor/)
60// ([API documentation](https://hadess.pages.freedesktop.org/low-memory-monitor/)).
61//
62// There is also an implementation for use inside Flatpak sandboxes.
63//
64// Possible actions to take when the signal is received are:
65//
66// - Free caches
67// - Save files that haven’t been looked at in a while to disk, ready to be reopened when needed
68// - Run a garbage collection cycle
69// - Try and compress fragmented allocations
70// - Exit on idle if the process has no reason to stay around
71// - Call [`malloc_trim(3)`](man:malloc_trim(3)) to return cached heap pages to
72// the kernel (if supported by your libc)
73//
74// Note that some actions may not always improve system performance, and so
75// should be profiled for your application. `malloc_trim()`, for example, may
76// make future heap allocations slower (due to releasing cached heap pages back
77// to the kernel).
78//
79// See [type@Gio.MemoryMonitorWarningLevel] for details on the various warning
80// levels.
81//
82// ```c
83// static void
84// warning_cb (GMemoryMonitor *m, GMemoryMonitorWarningLevel level)
85//
86// {
87// g_debug ("Warning level: %d", level);
88// if (warning_level > G_MEMORY_MONITOR_WARNING_LEVEL_LOW)
89// drop_caches ();
90// }
91//
92// static GMemoryMonitor *
93// monitor_low_memory (void)
94//
95// {
96// GMemoryMonitor *m;
97// m = g_memory_monitor_dup_default ();
98// g_signal_connect (G_OBJECT (m), "low-memory-warning",
99// G_CALLBACK (warning_cb), NULL);
100// return m;
101// }
102//
103// ```
104//
105// Don’t forget to disconnect the [signal@Gio.MemoryMonitor::low-memory-warning]
106// signal, and unref the `GMemoryMonitor` itself when exiting.
107type MemoryMonitor interface {
108 GoPointer() uintptr
109 SetGoPointer(uintptr)
110}
111
112var xMemoryMonitorGLibType func() types.GType
113
114func MemoryMonitorGLibType() types.GType {
115 return xMemoryMonitorGLibType()
116}
117
118type MemoryMonitorBase struct {
119 Ptr uintptr
120}
121
122func (x *MemoryMonitorBase) GoPointer() uintptr {
123 if x == nil {
124 return 0
125 }
126 return x.Ptr
127}
128
129func (x *MemoryMonitorBase) SetGoPointer(ptr uintptr) {
130 x.Ptr = ptr
131}
132
133const (
134 // Extension point for memory usage monitoring functionality.
135 // See [Extending GIO](overview.html#extending-gio).
136 MEMORY_MONITOR_EXTENSION_POINT_NAME string = "gio-memory-monitor"
137)
138
139var xMemoryMonitorDupDefault func() uintptr
140
141// Gets a reference to the default #GMemoryMonitor for the system.
142func MemoryMonitorDupDefault() *MemoryMonitorBase {
143 var cls *MemoryMonitorBase
144
145 cret := xMemoryMonitorDupDefault()
146
147 if cret == 0 {
148 return nil
149 }
150 cls = &MemoryMonitorBase{}
151 cls.Ptr = cret
152 return cls
153}
154
155func init() {
156 core.SetPackageName("GIO", "gio-2.0")
157 core.SetSharedLibraries("GIO", []string{"libgio-2.0.so.0"})
158 var libs []uintptr
159 for _, libPath := range core.GetPaths("GIO") {
160 lib, err := purego.Dlopen(libPath, purego.RTLD_NOW|purego.RTLD_GLOBAL)
161 if err != nil {
162 panic(err)
163 }
164 libs = append(libs, lib)
165 }
166
167 core.PuregoSafeRegister(&xMemoryMonitorDupDefault, libs, "g_memory_monitor_dup_default")
168
169 core.PuregoSafeRegister(&xMemoryMonitorGLibType, libs, "g_memory_monitor_get_type")
170
171}