• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
21 static const char *
get_level_string(GMemoryMonitorWarningLevel level)22 get_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 (eclass, level);
29 
30   if (value == NULL)
31     return "unknown";
32 
33   return value->value_nick;
34 }
35 
36 static void
test_dup_default(void)37 test_dup_default (void)
38 {
39   GMemoryMonitor *monitor;
40 
41   monitor = g_memory_monitor_dup_default ();
42   g_assert_nonnull (monitor);
43   g_object_unref (monitor);
44 }
45 
46 static void
warning_cb(GMemoryMonitor * m,GMemoryMonitorWarningLevel level)47 warning_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 
56 static void
do_watch_memory(void)57 do_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 
70 int
main(int argc,char ** argv)71 main (int argc, char **argv)
72 {
73   int ret;
74 
75   if (argc == 2 && !strcmp (argv[1], "--watch"))
76     {
77       do_watch_memory ();
78       return 0;
79     }
80 
81   g_test_init (&argc, &argv, NULL);
82 
83   g_test_add_func ("/memory-monitor/default", test_dup_default);
84 
85   ret = g_test_run ();
86 
87   return ret;
88 }
89