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