Change LGPL-2.1+ to LGPL-2.1-or-later
[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 #if (defined (HAVE__SET_THREAD_LOCAL_INVALID_PARAMETER_HANDLER) || \
125      defined (HAVE__SET_INVALID_PARAMETER_HANDLER)) && \
126     defined (HAVE__CRT_SET_REPORT_MODE)
127 # define USE_INVALID_PARAMETER_HANDLER
128 #endif
129
130 #ifdef USE_INVALID_PARAMETER_HANDLER
131 struct _GWin32InvalidParameterHandler
132 {
133   _invalid_parameter_handler old_handler;
134   _invalid_parameter_handler pushed_handler;
135   int prev_report_mode;
136   int pushed_report_mode;
137 };
138 #else
139 struct _GWin32InvalidParameterHandler
140 {
141   int unused_really;
142 };
143 #endif
144
145 #ifdef G_OS_WIN32
146 GLIB_AVAILABLE_IN_ALL
147 gchar *_glib_get_locale_dir    (void);
148 #endif
149
150 GDir * g_dir_open_with_errno (const gchar *path, guint flags);
151 GDir * g_dir_new_from_dirp (gpointer dirp);
152
153 typedef struct _GWin32InvalidParameterHandler GWin32InvalidParameterHandler;
154 void g_win32_push_empty_invalid_parameter_handler (GWin32InvalidParameterHandler *items);
155 void g_win32_pop_invalid_parameter_handler (GWin32InvalidParameterHandler *items);
156
157 char *g_find_program_for_path (const char *program,
158                                const char *path,
159                                const char *working_dir);
160
161 int g_uri_get_default_scheme_port (const char *scheme);
162
163 #define GLIB_PRIVATE_CALL(symbol) (glib__private__()->symbol)
164
165
166 typedef struct {
167   /* See gwakeup.c */
168   GWakeup *             (* g_wakeup_new)                (void);
169   void                  (* g_wakeup_free)               (GWakeup *wakeup);
170   void                  (* g_wakeup_get_pollfd)         (GWakeup *wakeup,
171                                                         GPollFD *poll_fd);
172   void                  (* g_wakeup_signal)             (GWakeup *wakeup);
173   void                  (* g_wakeup_acknowledge)        (GWakeup *wakeup);
174
175   /* See gmain.c */
176   GMainContext *        (* g_get_worker_context)        (void);
177
178   gboolean              (* g_check_setuid)              (void);
179   GMainContext *        (* g_main_context_new_with_next_id) (guint next_id);
180
181   GDir *                (* g_dir_open_with_errno)       (const gchar *path,
182                                                          guint        flags);
183   GDir *                (* g_dir_new_from_dirp)         (gpointer dirp);
184
185   /* See glib-init.c */
186   void                  (* glib_init)                   (void);
187
188   /* See gstdio.c */
189 #ifdef G_OS_WIN32
190   int                   (* g_win32_stat_utf8)           (const gchar        *filename,
191                                                          GWin32PrivateStat  *buf);
192
193   int                   (* g_win32_lstat_utf8)          (const gchar        *filename,
194                                                          GWin32PrivateStat  *buf);
195
196   int                   (* g_win32_readlink_utf8)       (const gchar        *filename,
197                                                          gchar              *buf,
198                                                          gsize               buf_size,
199                                                          gchar             **alloc_buf,
200                                                          gboolean            terminate);
201
202   int                   (* g_win32_fstat)               (int                 fd,
203                                                          GWin32PrivateStat  *buf);
204
205   /* See gwin32.c */
206   gchar *(*g_win32_find_helper_executable_path) (const gchar *process_name,
207                                                  void *dll_handle);
208
209   int                   (* g_win32_reopen_noninherited) (int      fd,
210                                                          int      mode,
211                                                          GError **err);
212
213   gboolean              (* g_win32_handle_is_socket)    (void *handle);
214
215 #endif
216
217   /* See glib-private.c */
218   void (* g_win32_push_empty_invalid_parameter_handler) (GWin32InvalidParameterHandler *items);
219
220   void (* g_win32_pop_invalid_parameter_handler)        (GWin32InvalidParameterHandler *items);
221
222   /* See gutils.c */
223   char *(* g_find_program_for_path) (const char *program,
224                                      const char *path,
225                                      const char *working_dir);
226
227   /* See guri.c */
228   int (* g_uri_get_default_scheme_port) (const char *scheme);
229
230   /* Add other private functions here, initialize them in glib-private.c */
231 } GLibPrivateVTable;
232
233 GLIB_AVAILABLE_IN_ALL
234 GLibPrivateVTable *glib__private__ (void);
235
236 /* Please see following for the use of ".ACP" over ""
237  * on Windows, although both are accepted at compile-time
238  * but "" renders translated console messages unreadable if
239  * built with Visual Studio 2012 and later (this is, unfortunately,
240  * undocumented):
241  *
242  * https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/setlocale-wsetlocale
243  * https://gitlab.gnome.org/GNOME/glib/merge_requests/895#note_525881
244  * https://gitlab.gnome.org/GNOME/glib/merge_requests/895#note_525900
245  *
246  * Additional related items:
247  * https://stackoverflow.com/questions/22604329/php-5-5-setlocale-not-working-in-cli-on-windows
248  * https://bugs.php.net/bug.php?id=66265
249  */
250
251 #ifdef G_OS_WIN32
252 # define GLIB_DEFAULT_LOCALE ".ACP"
253 #else
254 # define GLIB_DEFAULT_LOCALE ""
255 #endif
256
257 #endif /* __GLIB_PRIVATE_H__ */