Imported Upstream version 2.74.3
[platform/upstream/glib.git] / glib / glib-private.h
1 /* glib-private.h - GLib-internal private API, shared between glib, gobject, gio
2  * Copyright (C) 2011 Red Hat, Inc.
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #ifndef __GLIB_PRIVATE_H__
21 #define __GLIB_PRIVATE_H__
22
23 #include <glib.h>
24 #include "gwakeup.h"
25 #include "gstdioprivate.h"
26
27 /* gcc defines __SANITIZE_ADDRESS__, clang sets the address_sanitizer
28  * feature flag.
29  *
30  * MSVC defines __SANITIZE_ADDRESS__ as well when AddressSanitizer
31  * is enabled but __lsan_ignore_object() equivalent method is not supported
32  * See also
33  * https://docs.microsoft.com/en-us/cpp/sanitizers/asan-building?view=msvc-160
34  */
35 #if !defined(_MSC_VER) && (defined(__SANITIZE_ADDRESS__) || g_macro__has_feature(address_sanitizer))
36
37 /*
38  * %_GLIB_ADDRESS_SANITIZER:
39  *
40  * Private macro defined if the AddressSanitizer is in use.
41  */
42 #define _GLIB_ADDRESS_SANITIZER
43
44 #include <sanitizer/lsan_interface.h>
45
46 #endif
47
48 /*
49  * g_ignore_leak:
50  * @p: any pointer
51  *
52  * Tell AddressSanitizer and similar tools that if the object pointed to
53  * by @p is leaked, it is not a problem. Use this to suppress memory leak
54  * reports when a potentially unreachable pointer is deliberately not
55  * going to be deallocated.
56  */
57 static inline void
58 g_ignore_leak (gconstpointer p)
59 {
60 #ifdef _GLIB_ADDRESS_SANITIZER
61   if (p != NULL)
62     __lsan_ignore_object (p);
63 #endif
64 }
65
66 /*
67  * g_ignore_strv_leak:
68  * @strv: (nullable) (array zero-terminated=1): an array of strings
69  *
70  * The same as g_ignore_leak(), but for the memory pointed to by @strv,
71  * and for each element of @strv.
72  */
73 static inline void
74 g_ignore_strv_leak (GStrv strv)
75 {
76 #ifdef _GLIB_ADDRESS_SANITIZER
77   gchar **item;
78
79   if (strv)
80     {
81       g_ignore_leak (strv);
82
83       for (item = strv; *item != NULL; item++)
84         g_ignore_leak (*item);
85     }
86 #endif
87 }
88
89 /*
90  * g_begin_ignore_leaks:
91  *
92  * Tell AddressSanitizer and similar tools to ignore all leaks from this point
93  * onwards, until g_end_ignore_leaks() is called.
94  *
95  * Try to use g_ignore_leak() where possible to target deliberate leaks more
96  * specifically.
97  */
98 static inline void
99 g_begin_ignore_leaks (void)
100 {
101 #ifdef _GLIB_ADDRESS_SANITIZER
102   __lsan_disable ();
103 #endif
104 }
105
106 /*
107  * g_end_ignore_leaks:
108  *
109  * Start ignoring leaks again; this must be paired with a previous call to
110  * g_begin_ignore_leaks().
111  */
112 static inline void
113 g_end_ignore_leaks (void)
114 {
115 #ifdef _GLIB_ADDRESS_SANITIZER
116   __lsan_enable ();
117 #endif
118 }
119
120 GMainContext *          g_get_worker_context            (void);
121 gboolean                g_check_setuid                  (void);
122 GMainContext *          g_main_context_new_with_next_id (guint next_id);
123
124 #ifdef G_OS_WIN32
125 GLIB_AVAILABLE_IN_ALL
126 gchar *_glib_get_locale_dir    (void);
127 #endif
128
129 GDir * g_dir_open_with_errno (const gchar *path, guint flags);
130 GDir * g_dir_new_from_dirp (gpointer dirp);
131
132 #define GLIB_PRIVATE_CALL(symbol) (glib__private__()->symbol)
133
134 typedef struct {
135   /* See gwakeup.c */
136   GWakeup *             (* g_wakeup_new)                (void);
137   void                  (* g_wakeup_free)               (GWakeup *wakeup);
138   void                  (* g_wakeup_get_pollfd)         (GWakeup *wakeup,
139                                                         GPollFD *poll_fd);
140   void                  (* g_wakeup_signal)             (GWakeup *wakeup);
141   void                  (* g_wakeup_acknowledge)        (GWakeup *wakeup);
142
143   /* See gmain.c */
144   GMainContext *        (* g_get_worker_context)        (void);
145
146   gboolean              (* g_check_setuid)              (void);
147   GMainContext *        (* g_main_context_new_with_next_id) (guint next_id);
148
149   GDir *                (* g_dir_open_with_errno)       (const gchar *path,
150                                                          guint        flags);
151   GDir *                (* g_dir_new_from_dirp)         (gpointer dirp);
152
153   /* See glib-init.c */
154   void                  (* glib_init)                   (void);
155
156   /* See gstdio.c */
157 #ifdef G_OS_WIN32
158   int                   (* g_win32_stat_utf8)           (const gchar        *filename,
159                                                          GWin32PrivateStat  *buf);
160
161   int                   (* g_win32_lstat_utf8)          (const gchar        *filename,
162                                                          GWin32PrivateStat  *buf);
163
164   int                   (* g_win32_readlink_utf8)       (const gchar        *filename,
165                                                          gchar              *buf,
166                                                          gsize               buf_size,
167                                                          gchar             **alloc_buf,
168                                                          gboolean            terminate);
169
170   int                   (* g_win32_fstat)               (int                 fd,
171                                                          GWin32PrivateStat  *buf);
172
173   /* See gwin32.c */
174   gchar *(*g_win32_find_helper_executable_path) (const gchar *process_name,
175                                                  void *dll_handle);
176
177   int                   (* g_win32_reopen_noninherited) (int      fd,
178                                                          int      mode,
179                                                          GError **err);
180
181   gboolean              (* g_win32_handle_is_socket)    (void *handle);
182
183 #endif
184
185
186   /* Add other private functions here, initialize them in glib-private.c */
187 } GLibPrivateVTable;
188
189 GLIB_AVAILABLE_IN_ALL
190 GLibPrivateVTable *glib__private__ (void);
191
192 /* Please see following for the use of ".ACP" over ""
193  * on Windows, although both are accepted at compile-time
194  * but "" renders translated console messages unreadable if
195  * built with Visual Studio 2012 and later (this is, unfortunately,
196  * undocumented):
197  *
198  * https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/setlocale-wsetlocale
199  * https://gitlab.gnome.org/GNOME/glib/merge_requests/895#note_525881
200  * https://gitlab.gnome.org/GNOME/glib/merge_requests/895#note_525900
201  *
202  * Additional related items:
203  * https://stackoverflow.com/questions/22604329/php-5-5-setlocale-not-working-in-cli-on-windows
204  * https://bugs.php.net/bug.php?id=66265
205  */
206
207 #ifdef G_OS_WIN32
208 # define GLIB_DEFAULT_LOCALE ".ACP"
209 #else
210 # define GLIB_DEFAULT_LOCALE ""
211 #endif
212
213 #endif /* __GLIB_PRIVATE_H__ */