Merge branch 'fix-gdbus-unix-addresses-test' into 'master'
[glib.git] / glib / glib-init.c
blob2e229667f2e2e6c1105f2e4304fea4b610e41fe9
1 /*
2 * Copyright © 2011 Canonical Limited
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 * Author: Ryan Lortie <desrt@desrt.ca>
20 #include "config.h"
22 #include "glib-init.h"
24 #include "glib-private.h"
25 #include "gtypes.h"
26 #include "gutils.h" /* for GDebugKey */
27 #include "gconstructor.h"
28 #include "gmem.h" /* for g_mem_gc_friendly */
30 #include <string.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <ctype.h>
35 /* This seems as good a place as any to make static assertions about platform
36 * assumptions we make throughout GLib. */
38 /* We do not support 36-bit bytes or other historical curiosities. */
39 G_STATIC_ASSERT (CHAR_BIT == 8);
41 /* We assume that data pointers are the same size as function pointers... */
42 G_STATIC_ASSERT (sizeof (gpointer) == sizeof (GFunc));
43 G_STATIC_ASSERT (_g_alignof (gpointer) == _g_alignof (GFunc));
44 /* ... and that all function pointers are the same size. */
45 G_STATIC_ASSERT (sizeof (GFunc) == sizeof (GCompareDataFunc));
46 G_STATIC_ASSERT (_g_alignof (GFunc) == _g_alignof (GCompareDataFunc));
48 /* We assume that "small" enums (those where all values fit in INT32_MIN
49 * to INT32_MAX) are exactly int-sized. In particular, we assume that if
50 * an enum has no members that exceed the range of char/short, the
51 * compiler will make it int-sized anyway, so adding a member later that
52 * *does* exceed the range of char/short is not an ABI break. */
53 typedef enum {
54 TEST_CHAR_0 = 0
55 } TestChar;
56 typedef enum {
57 TEST_SHORT_0 = 0,
58 TEST_SHORT_256 = 256
59 } TestShort;
60 typedef enum {
61 TEST_INT32_MIN = G_MININT32,
62 TEST_INT32_MAX = G_MAXINT32
63 } TestInt;
64 G_STATIC_ASSERT (sizeof (TestChar) == sizeof (int));
65 G_STATIC_ASSERT (sizeof (TestShort) == sizeof (int));
66 G_STATIC_ASSERT (sizeof (TestInt) == sizeof (int));
67 G_STATIC_ASSERT (_g_alignof (TestChar) == _g_alignof (int));
68 G_STATIC_ASSERT (_g_alignof (TestShort) == _g_alignof (int));
69 G_STATIC_ASSERT (_g_alignof (TestInt) == _g_alignof (int));
71 /**
72 * g_mem_gc_friendly:
74 * This variable is %TRUE if the `G_DEBUG` environment variable
75 * includes the key `gc-friendly`.
77 gboolean g_mem_gc_friendly = FALSE;
79 GLogLevelFlags g_log_msg_prefix = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_WARNING |
80 G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_DEBUG;
81 GLogLevelFlags g_log_always_fatal = G_LOG_FATAL_MASK;
83 static gboolean
84 debug_key_matches (const gchar *key,
85 const gchar *token,
86 guint length)
88 /* may not call GLib functions: see note in g_parse_debug_string() */
89 for (; length; length--, key++, token++)
91 char k = (*key == '_') ? '-' : tolower (*key );
92 char t = (*token == '_') ? '-' : tolower (*token);
94 if (k != t)
95 return FALSE;
98 return *key == '\0';
101 /* The GVariant documentation indirectly says that int is at least 32 bits
102 * (by saying that b, y, n, q, i, u, h are promoted to int). On any
103 * reasonable platform, int is in fact *exactly* 32 bits long, because
104 * otherwise, {signed char, short, int} wouldn't be sufficient to provide
105 * {int8_t, int16_t, int32_t}. */
106 G_STATIC_ASSERT (sizeof (int) == sizeof (gint32));
109 * g_parse_debug_string:
110 * @string: (nullable): a list of debug options separated by colons, spaces, or
111 * commas, or %NULL.
112 * @keys: (array length=nkeys): pointer to an array of #GDebugKey which associate
113 * strings with bit flags.
114 * @nkeys: the number of #GDebugKeys in the array.
116 * Parses a string containing debugging options
117 * into a %guint containing bit flags. This is used
118 * within GDK and GTK+ to parse the debug options passed on the
119 * command line or through environment variables.
121 * If @string is equal to "all", all flags are set. Any flags
122 * specified along with "all" in @string are inverted; thus,
123 * "all,foo,bar" or "foo,bar,all" sets all flags except those
124 * corresponding to "foo" and "bar".
126 * If @string is equal to "help", all the available keys in @keys
127 * are printed out to standard error.
129 * Returns: the combined set of bit flags.
131 guint
132 g_parse_debug_string (const gchar *string,
133 const GDebugKey *keys,
134 guint nkeys)
136 guint i;
137 guint result = 0;
139 if (string == NULL)
140 return 0;
142 /* this function is used during the initialisation of gmessages, gmem
143 * and gslice, so it may not do anything that causes memory to be
144 * allocated or risks messages being emitted.
146 * this means, more or less, that this code may not call anything
147 * inside GLib.
150 if (!strcasecmp (string, "help"))
152 /* using stdio directly for the reason stated above */
153 fprintf (stderr, "Supported debug values:");
154 for (i = 0; i < nkeys; i++)
155 fprintf (stderr, " %s", keys[i].key);
156 fprintf (stderr, " all help\n");
158 else
160 const gchar *p = string;
161 const gchar *q;
162 gboolean invert = FALSE;
164 while (*p)
166 q = strpbrk (p, ":;, \t");
167 if (!q)
168 q = p + strlen (p);
170 if (debug_key_matches ("all", p, q - p))
172 invert = TRUE;
174 else
176 for (i = 0; i < nkeys; i++)
177 if (debug_key_matches (keys[i].key, p, q - p))
178 result |= keys[i].value;
181 p = q;
182 if (*p)
183 p++;
186 if (invert)
188 guint all_flags = 0;
190 for (i = 0; i < nkeys; i++)
191 all_flags |= keys[i].value;
193 result = all_flags & (~result);
197 return result;
200 static guint
201 g_parse_debug_envvar (const gchar *envvar,
202 const GDebugKey *keys,
203 gint n_keys,
204 guint default_value)
206 const gchar *value;
208 #ifdef OS_WIN32
209 /* "fatal-warnings,fatal-criticals,all,help" is pretty short */
210 gchar buffer[100];
212 if (GetEnvironmentVariable (envvar, buffer, 100) < 100)
213 value = buffer;
214 else
215 return 0;
216 #else
217 value = getenv (envvar);
218 #endif
220 if (value == NULL)
221 return default_value;
223 return g_parse_debug_string (value, keys, n_keys);
226 static void
227 g_messages_prefixed_init (void)
229 const GDebugKey keys[] = {
230 { "error", G_LOG_LEVEL_ERROR },
231 { "critical", G_LOG_LEVEL_CRITICAL },
232 { "warning", G_LOG_LEVEL_WARNING },
233 { "message", G_LOG_LEVEL_MESSAGE },
234 { "info", G_LOG_LEVEL_INFO },
235 { "debug", G_LOG_LEVEL_DEBUG }
238 g_log_msg_prefix = g_parse_debug_envvar ("G_MESSAGES_PREFIXED", keys, G_N_ELEMENTS (keys), g_log_msg_prefix);
241 static void
242 g_debug_init (void)
244 const GDebugKey keys[] = {
245 { "gc-friendly", 1 },
246 {"fatal-warnings", G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL },
247 {"fatal-criticals", G_LOG_LEVEL_CRITICAL }
249 GLogLevelFlags flags;
251 flags = g_parse_debug_envvar ("G_DEBUG", keys, G_N_ELEMENTS (keys), 0);
253 g_log_always_fatal |= flags & G_LOG_LEVEL_MASK;
255 g_mem_gc_friendly = flags & 1;
258 void
259 glib_init (void)
261 static gboolean glib_inited;
263 if (glib_inited)
264 return;
266 glib_inited = TRUE;
268 g_messages_prefixed_init ();
269 g_debug_init ();
270 g_quark_init ();
273 #if defined (G_OS_WIN32)
275 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
276 DWORD fdwReason,
277 LPVOID lpvReserved);
279 HMODULE glib_dll;
281 BOOL WINAPI
282 DllMain (HINSTANCE hinstDLL,
283 DWORD fdwReason,
284 LPVOID lpvReserved)
286 switch (fdwReason)
288 case DLL_PROCESS_ATTACH:
289 glib_dll = hinstDLL;
290 g_clock_win32_init ();
291 #ifdef THREADS_WIN32
292 g_thread_win32_init ();
293 #endif
294 glib_init ();
295 break;
297 case DLL_THREAD_DETACH:
298 #ifdef THREADS_WIN32
299 g_thread_win32_thread_detach ();
300 #endif
301 break;
303 case DLL_PROCESS_DETACH:
304 #ifdef THREADS_WIN32
305 if (lpvReserved == NULL)
306 g_thread_win32_process_detach ();
307 #endif
308 break;
310 default:
311 /* do nothing */
315 return TRUE;
318 #elif defined (G_HAS_CONSTRUCTORS)
320 #ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
321 #pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(glib_init_ctor)
322 #endif
323 G_DEFINE_CONSTRUCTOR(glib_init_ctor)
325 static void
326 glib_init_ctor (void)
328 glib_init ();
331 #else
332 # error Your platform/compiler is missing constructor support
333 #endif