LCOV - code coverage report
Current view: top level - gio - gmemorymonitor.c (source / functions) Coverage Total Hit
Test: unnamed Lines: 100.0 % 7 7
Test Date: 2024-11-26 05:23:01 Functions: 100.0 % 3 3
Branches: - 0 0

             Branch data     Line data    Source code
       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 "config.h"
      22                 :             : #include "glib.h"
      23                 :             : #include "glibintl.h"
      24                 :             : 
      25                 :             : #include "gmemorymonitor.h"
      26                 :             : #include "ginetaddress.h"
      27                 :             : #include "ginetsocketaddress.h"
      28                 :             : #include "ginitable.h"
      29                 :             : #include "gioenumtypes.h"
      30                 :             : #include "giomodule-priv.h"
      31                 :             : #include "gtask.h"
      32                 :             : 
      33                 :             : /**
      34                 :             :  * GMemoryMonitor:
      35                 :             :  *
      36                 :             :  * `GMemoryMonitor` will monitor system memory and suggest to the application
      37                 :             :  * when to free memory so as to leave more room for other applications.
      38                 :             :  * It is implemented on Linux using the
      39                 :             :  * [Low Memory Monitor](https://gitlab.freedesktop.org/hadess/low-memory-monitor/)
      40                 :             :  * ([API documentation](https://hadess.pages.freedesktop.org/low-memory-monitor/)).
      41                 :             :  *
      42                 :             :  * There is also an implementation for use inside Flatpak sandboxes.
      43                 :             :  *
      44                 :             :  * Possible actions to take when the signal is received are:
      45                 :             :  *
      46                 :             :  *  - Free caches
      47                 :             :  *  - Save files that haven’t been looked at in a while to disk, ready to be reopened when needed
      48                 :             :  *  - Run a garbage collection cycle
      49                 :             :  *  - Try and compress fragmented allocations
      50                 :             :  *  - Exit on idle if the process has no reason to stay around
      51                 :             :  *  - Call [`malloc_trim(3)`](man:malloc_trim(3)) to return cached heap pages to
      52                 :             :  *    the kernel (if supported by your libc)
      53                 :             :  *
      54                 :             :  * Note that some actions may not always improve system performance, and so
      55                 :             :  * should be profiled for your application. `malloc_trim()`, for example, may
      56                 :             :  * make future heap allocations slower (due to releasing cached heap pages back
      57                 :             :  * to the kernel).
      58                 :             :  *
      59                 :             :  * See [type@Gio.MemoryMonitorWarningLevel] for details on the various warning
      60                 :             :  * levels.
      61                 :             :  *
      62                 :             :  * ```c
      63                 :             :  * static void
      64                 :             :  * warning_cb (GMemoryMonitor *m, GMemoryMonitorWarningLevel level)
      65                 :             :  * {
      66                 :             :  *   g_debug ("Warning level: %d", level);
      67                 :             :  *   if (warning_level > G_MEMORY_MONITOR_WARNING_LEVEL_LOW)
      68                 :             :  *     drop_caches ();
      69                 :             :  * }
      70                 :             :  *
      71                 :             :  * static GMemoryMonitor *
      72                 :             :  * monitor_low_memory (void)
      73                 :             :  * {
      74                 :             :  *   GMemoryMonitor *m;
      75                 :             :  *   m = g_memory_monitor_dup_default ();
      76                 :             :  *   g_signal_connect (G_OBJECT (m), "low-memory-warning",
      77                 :             :  *                     G_CALLBACK (warning_cb), NULL);
      78                 :             :  *   return m;
      79                 :             :  * }
      80                 :             :  * ```
      81                 :             :  *
      82                 :             :  * Don’t forget to disconnect the [signal@Gio.MemoryMonitor::low-memory-warning]
      83                 :             :  * signal, and unref the `GMemoryMonitor` itself when exiting.
      84                 :             :  *
      85                 :             :  * Since: 2.64
      86                 :             :  */
      87                 :             : 
      88                 :             : /**
      89                 :             :  * GMemoryMonitorInterface:
      90                 :             :  * @g_iface: The parent interface.
      91                 :             :  * @low_memory_warning: the virtual function pointer for the
      92                 :             :  *  #GMemoryMonitor::low-memory-warning signal.
      93                 :             :  *
      94                 :             :  * The virtual function table for #GMemoryMonitor.
      95                 :             :  *
      96                 :             :  * Since: 2.64
      97                 :             :  */
      98                 :             : 
      99                 :         365 : G_DEFINE_INTERFACE_WITH_CODE (GMemoryMonitor, g_memory_monitor, G_TYPE_OBJECT,
     100                 :             :                               g_type_interface_add_prerequisite (g_define_type_id, G_TYPE_INITABLE))
     101                 :             : 
     102                 :             : enum {
     103                 :             :   LOW_MEMORY_WARNING,
     104                 :             :   LAST_SIGNAL
     105                 :             : };
     106                 :             : 
     107                 :             : static guint signals[LAST_SIGNAL] = { 0 };
     108                 :             : 
     109                 :             : /**
     110                 :             :  * g_memory_monitor_dup_default:
     111                 :             :  *
     112                 :             :  * Gets a reference to the default #GMemoryMonitor for the system.
     113                 :             :  *
     114                 :             :  * Returns: (not nullable) (transfer full): a new reference to the default #GMemoryMonitor
     115                 :             :  *
     116                 :             :  * Since: 2.64
     117                 :             :  */
     118                 :             : GMemoryMonitor *
     119                 :           1 : g_memory_monitor_dup_default (void)
     120                 :             : {
     121                 :           1 :   return g_object_ref (_g_io_module_get_default (G_MEMORY_MONITOR_EXTENSION_POINT_NAME,
     122                 :             :                                                  "GIO_USE_MEMORY_MONITOR",
     123                 :             :                                                  NULL));
     124                 :             : }
     125                 :             : 
     126                 :             : static void
     127                 :           2 : g_memory_monitor_default_init (GMemoryMonitorInterface *iface)
     128                 :             : {
     129                 :             :   /**
     130                 :             :    * GMemoryMonitor::low-memory-warning:
     131                 :             :    * @monitor: a #GMemoryMonitor
     132                 :             :    * @level: the #GMemoryMonitorWarningLevel warning level
     133                 :             :    *
     134                 :             :    * Emitted when the system is running low on free memory. The signal
     135                 :             :    * handler should then take the appropriate action depending on the
     136                 :             :    * warning level. See the #GMemoryMonitorWarningLevel documentation for
     137                 :             :    * details.
     138                 :             :    *
     139                 :             :    * Since: 2.64
     140                 :             :    */
     141                 :           2 :   signals[LOW_MEMORY_WARNING] =
     142                 :           2 :     g_signal_new (I_("low-memory-warning"),
     143                 :             :                   G_TYPE_MEMORY_MONITOR,
     144                 :             :                   G_SIGNAL_RUN_LAST,
     145                 :             :                   G_STRUCT_OFFSET (GMemoryMonitorInterface, low_memory_warning),
     146                 :             :                   NULL, NULL,
     147                 :             :                   NULL,
     148                 :             :                   G_TYPE_NONE, 1,
     149                 :             :                   G_TYPE_MEMORY_MONITOR_WARNING_LEVEL);
     150                 :           2 : }
        

Generated by: LCOV version 2.0-1