1/* GIO - GLib Input, Output and Streaming Library
2 *
3 * Copyright 2019 Red Hat, Inc.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <gio/gio.h>
20
21static const char *
22get_level_string (GMemoryMonitorWarningLevel level)
23{
24 GEnumClass *eclass;
25 GEnumValue *value;
26
27 eclass = G_ENUM_CLASS (g_type_class_peek (G_TYPE_MEMORY_MONITOR_WARNING_LEVEL));
28 value = g_enum_get_value (enum_class: eclass, value: level);
29
30 if (value == NULL)
31 return "unknown";
32
33 return value->value_nick;
34}
35
36static void
37test_dup_default (void)
38{
39 GMemoryMonitor *monitor;
40
41 monitor = g_memory_monitor_dup_default ();
42 g_assert_nonnull (monitor);
43 g_object_unref (object: monitor);
44}
45
46static void
47warning_cb (GMemoryMonitor *m,
48 GMemoryMonitorWarningLevel level)
49{
50 const char *str;
51
52 str = get_level_string (level);
53 g_debug ("Warning level: %s (%d)", str , level);
54}
55
56static void
57do_watch_memory (void)
58{
59 GMemoryMonitor *m;
60 GMainLoop *loop;
61
62 m = g_memory_monitor_dup_default ();
63 g_signal_connect (G_OBJECT (m), "low-memory-warning",
64 G_CALLBACK (warning_cb), NULL);
65
66 loop = g_main_loop_new (NULL, TRUE);
67 g_main_loop_run (loop);
68}
69
70int
71main (int argc, char **argv)
72{
73 int ret;
74
75 if (argc == 2 && !strcmp (s1: argv[1], s2: "--watch"))
76 {
77 do_watch_memory ();
78 return 0;
79 }
80
81 g_test_init (argc: &argc, argv: &argv, NULL);
82
83 g_test_add_func (testpath: "/memory-monitor/default", test_func: test_dup_default);
84
85 ret = g_test_run ();
86
87 return ret;
88}
89

source code of gtk/subprojects/glib/gio/tests/memory-monitor.c