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