glib-init.c 13.2 KB
Newer Older
1
2
3
/*
 * Copyright © 2011 Canonical Limited
 *
4
5
 * SPDX-License-Identifier: LGPL-2.1-or-later
 *
swilmet's avatar
swilmet committed
6
7
8
9
 * 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.
10
11
12
13
14
15
16
 *
 * 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
17
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18
19
20
21
22
23
24
 *
 * Author: Ryan Lortie <desrt@desrt.ca>
 */

#include "config.h"

#include "glib-init.h"
25
#include "gmacros.h"
26
#include "gtypes.h"
27
#include "gutils.h"     /* for GDebugKey */
28
#include "gconstructor.h"
29
#include "gmem.h"       /* for g_mem_gc_friendly */
30
31
32
33
34
35

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>

36
37
38
39
40
/* Deliberately not checking HAVE_STDINT_H here: we officially require a
 * C99 toolchain, which implies <stdint.h>, int8_t and so on. If your
 * toolchain does not have this, now would be a good time to upgrade. */
#include <stdint.h>

41
42
43
/* This seems as good a place as any to make static assertions about platform
 * assumptions we make throughout GLib. */

44
45
46
/* We do not support 36-bit bytes or other historical curiosities. */
G_STATIC_ASSERT (CHAR_BIT == 8);

47
48
/* We assume that data pointers are the same size as function pointers... */
G_STATIC_ASSERT (sizeof (gpointer) == sizeof (GFunc));
49
G_STATIC_ASSERT (G_ALIGNOF (gpointer) == G_ALIGNOF (GFunc));
50
51
/* ... and that all function pointers are the same size. */
G_STATIC_ASSERT (sizeof (GFunc) == sizeof (GCompareDataFunc));
52
G_STATIC_ASSERT (G_ALIGNOF (GFunc) == G_ALIGNOF (GCompareDataFunc));
53

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* 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));
73
74
75
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));
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
G_STATIC_ASSERT (sizeof (gchar) == 1);
G_STATIC_ASSERT (sizeof (guchar) == 1);
G_STATIC_ASSERT (sizeof (gint8) * CHAR_BIT == 8);
G_STATIC_ASSERT (sizeof (guint8) * CHAR_BIT == 8);
G_STATIC_ASSERT (sizeof (gint16) * CHAR_BIT == 16);
G_STATIC_ASSERT (sizeof (guint16) * CHAR_BIT == 16);
G_STATIC_ASSERT (sizeof (gint32) * CHAR_BIT == 32);
G_STATIC_ASSERT (sizeof (guint32) * CHAR_BIT == 32);
G_STATIC_ASSERT (sizeof (gint64) * CHAR_BIT == 64);
G_STATIC_ASSERT (sizeof (guint64) * CHAR_BIT == 64);

G_STATIC_ASSERT (sizeof (void *) == GLIB_SIZEOF_VOID_P);
G_STATIC_ASSERT (sizeof (gintptr) == sizeof (void *));
G_STATIC_ASSERT (sizeof (guintptr) == sizeof (void *));

G_STATIC_ASSERT (sizeof (long) == GLIB_SIZEOF_LONG);

G_STATIC_ASSERT (G_HAVE_GINT64 == 1);

G_STATIC_ASSERT (sizeof (size_t) == GLIB_SIZEOF_SIZE_T);
/* Not a typo: ssize_t is POSIX, not Standard C, but if it exists then
 * it's the same size as size_t. */
G_STATIC_ASSERT (sizeof (size_t) == GLIB_SIZEOF_SSIZE_T);
G_STATIC_ASSERT (sizeof (gsize) == GLIB_SIZEOF_SSIZE_T);
G_STATIC_ASSERT (sizeof (gsize) == sizeof (size_t));
/* Again this is size_t not ssize_t, because ssize_t is POSIX, not C99 */
G_STATIC_ASSERT (sizeof (gssize) == sizeof (size_t));
G_STATIC_ASSERT (G_ALIGNOF (gsize) == G_ALIGNOF (size_t));
G_STATIC_ASSERT (G_ALIGNOF (gssize) == G_ALIGNOF (size_t));

/* goffset is always 64-bit, even if off_t is only 32-bit
 * (compiling without large-file-support on 32-bit) */
G_STATIC_ASSERT (sizeof (goffset) == sizeof (gint64));
G_STATIC_ASSERT (G_ALIGNOF (goffset) == G_ALIGNOF (gint64));

G_STATIC_ASSERT (sizeof (gfloat) == sizeof (float));
G_STATIC_ASSERT (G_ALIGNOF (gfloat) == G_ALIGNOF (float));
G_STATIC_ASSERT (sizeof (gdouble) == sizeof (double));
G_STATIC_ASSERT (G_ALIGNOF (gdouble) == G_ALIGNOF (double));

G_STATIC_ASSERT (sizeof (gintptr) == sizeof (intptr_t));
G_STATIC_ASSERT (sizeof (guintptr) == sizeof (uintptr_t));
G_STATIC_ASSERT (G_ALIGNOF (gintptr) == G_ALIGNOF (intptr_t));
G_STATIC_ASSERT (G_ALIGNOF (guintptr) == G_ALIGNOF (uintptr_t));

G_STATIC_ASSERT (sizeof (gint8) == sizeof (int8_t));
G_STATIC_ASSERT (sizeof (guint8) == sizeof (uint8_t));
G_STATIC_ASSERT (G_ALIGNOF (gint8) == G_ALIGNOF (int8_t));
G_STATIC_ASSERT (G_ALIGNOF (guint8) == G_ALIGNOF (uint8_t));

G_STATIC_ASSERT (sizeof (gint16) == sizeof (int16_t));
G_STATIC_ASSERT (sizeof (guint16) == sizeof (uint16_t));
G_STATIC_ASSERT (G_ALIGNOF (gint16) == G_ALIGNOF (int16_t));
G_STATIC_ASSERT (G_ALIGNOF (guint16) == G_ALIGNOF (uint16_t));

G_STATIC_ASSERT (sizeof (gint32) == sizeof (int32_t));
G_STATIC_ASSERT (sizeof (guint32) == sizeof (uint32_t));
G_STATIC_ASSERT (G_ALIGNOF (gint32) == G_ALIGNOF (int32_t));
G_STATIC_ASSERT (G_ALIGNOF (guint32) == G_ALIGNOF (uint32_t));

G_STATIC_ASSERT (sizeof (gint64) == sizeof (int64_t));
G_STATIC_ASSERT (sizeof (guint64) == sizeof (uint64_t));
G_STATIC_ASSERT (G_ALIGNOF (gint64) == G_ALIGNOF (int64_t));
G_STATIC_ASSERT (G_ALIGNOF (guint64) == G_ALIGNOF (uint64_t));

142
143
144
/**
 * g_mem_gc_friendly:
 *
Matthias Clasen's avatar
Matthias Clasen committed
145
 * This variable is %TRUE if the `G_DEBUG` environment variable
Matthias Clasen's avatar
Matthias Clasen committed
146
 * includes the key `gc-friendly`.
147
148
 */
gboolean g_mem_gc_friendly = FALSE;
149

150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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';
}

172
173
174
175
176
177
178
/* 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));

179
180
/**
 * g_parse_debug_string:
181
 * @string: (nullable): a list of debug options separated by colons, spaces, or
182
183
184
 * commas, or %NULL.
 * @keys: (array length=nkeys): pointer to an array of #GDebugKey which associate
 *     strings with bit flags.
Matthias Clasen's avatar
Matthias Clasen committed
185
 * @nkeys: the number of #GDebugKeys in the array.
186
187
188
189
190
191
 *
 * 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.
 *
Matthias Clasen's avatar
Matthias Clasen committed
192
193
194
195
 * 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".
196
 *
Matthias Clasen's avatar
Matthias Clasen committed
197
 * If @string is equal to "help", all the available keys in @keys
198
 * are printed out to standard error.
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
 *
 * 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.
   */

221
  if (!strcasecmp (string, "help"))
222
223
    {
      /* using stdio directly for the reason stated above */
224
      fprintf (stderr, "Supported debug values:");
225
      for (i = 0; i < nkeys; i++)
Matthias Clasen's avatar
Matthias Clasen committed
226
        fprintf (stderr, " %s", keys[i].key);
227
      fprintf (stderr, " all help\n");
228
229
230
231
232
    }
  else
    {
      const gchar *p = string;
      const gchar *q;
233
      gboolean invert = FALSE;
234
235
236
237
238

      while (*p)
       {
         q = strpbrk (p, ":;, \t");
         if (!q)
239
           q = p + strlen (p);
240

241
242
243
244
245
246
247
248
249
250
         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;
           }
251
252
253
254
255

         p = q;
         if (*p)
           p++;
       }
256
257
258
259
260
261
262
263
264
265

      if (invert)
        {
          guint all_flags = 0;

          for (i = 0; i < nkeys; i++)
            all_flags |= keys[i].value;

          result = all_flags & (~result);
        }
266
267
268
269
270
271
272
273
    }

  return result;
}

static guint
g_parse_debug_envvar (const gchar     *envvar,
                      const GDebugKey *keys,
274
275
                      gint             n_keys,
                      guint            default_value)
276
277
278
279
280
{
  const gchar *value;

#ifdef OS_WIN32
  /* "fatal-warnings,fatal-criticals,all,help" is pretty short */
281
  gchar buffer[100];
282
283
284
285
286
287
288
289
290

  if (GetEnvironmentVariable (envvar, buffer, 100) < 100)
    value = buffer;
  else
    return 0;
#else
  value = getenv (envvar);
#endif

291
292
293
  if (value == NULL)
    return default_value;

294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
  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 }
  };

309
  g_log_msg_prefix = g_parse_debug_envvar ("G_MESSAGES_PREFIXED", keys, G_N_ELEMENTS (keys), g_log_msg_prefix);
310
311
312
313
314
315
}

static void
g_debug_init (void)
{
  const GDebugKey keys[] = {
Matthias Clasen's avatar
Matthias Clasen committed
316
    { "gc-friendly", 1 },
317
318
319
320
321
    {"fatal-warnings",  G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL },
    {"fatal-criticals", G_LOG_LEVEL_CRITICAL }
  };
  GLogLevelFlags flags;

322
  flags = g_parse_debug_envvar ("G_DEBUG", keys, G_N_ELEMENTS (keys), 0);
323

Matthias Clasen's avatar
Matthias Clasen committed
324
  g_log_always_fatal |= flags & G_LOG_LEVEL_MASK;
325

Matthias Clasen's avatar
Matthias Clasen committed
326
  g_mem_gc_friendly = flags & 1;
327
328
}

329
void
330
331
glib_init (void)
{
332
333
334
335
336
337
338
  static gboolean glib_inited;

  if (glib_inited)
    return;

  glib_inited = TRUE;

339
340
  g_messages_prefixed_init ();
  g_debug_init ();
341
  g_quark_init ();
342
  g_error_init ();
343
344
}

345
#ifdef G_PLATFORM_WIN32
346

347
HMODULE glib_dll = NULL;
348
void glib_win32_init (void);
349

350
void
351
352
glib_win32_init (void)
{
353
354
355
356
357
358
359
  /* May be called more than once in static compilation mode */
  static gboolean win32_already_init = FALSE;
  if (!win32_already_init)
    {
      win32_already_init = TRUE;

      g_crash_handler_win32_init ();
360
#ifdef THREADS_WIN32
361
      g_thread_win32_init ();
362
363
#endif

364
365
366
367
368
      g_clock_win32_init ();
      glib_init ();
      /* must go after glib_init */
      g_console_win32_init ();
    }
369
370
371
372
373
374
375
376
377
378
379
380
}

static void
glib_win32_deinit (gboolean detach_thread)
{
#ifdef THREADS_WIN32
  if (detach_thread)
    g_thread_win32_process_detach ();
#endif
  g_crash_handler_win32_deinit ();
}

381
382
#ifndef GLIB_STATIC_COMPILATION

Dan Winship's avatar
Dan Winship committed
383
384
385
386
BOOL WINAPI DllMain (HINSTANCE hinstDLL,
                     DWORD     fdwReason,
                     LPVOID    lpvReserved);

387
388
389
390
391
392
393
394
395
BOOL WINAPI
DllMain (HINSTANCE hinstDLL,
         DWORD     fdwReason,
         LPVOID    lpvReserved)
{
  switch (fdwReason)
    {
    case DLL_PROCESS_ATTACH:
      glib_dll = hinstDLL;
396
      glib_win32_init ();
397
398
      break;

399
    case DLL_THREAD_DETACH:
400
#ifdef THREADS_WIN32
401
      g_thread_win32_thread_detach ();
402
#endif
403
404
      break;

405
    case DLL_PROCESS_DETACH:
406
      glib_win32_deinit (lpvReserved == NULL);
407
408
      break;

409
410
411
412
413
414
415
416
    default:
      /* do nothing */
      ;
    }

  return TRUE;
}

417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#elif defined(G_HAS_CONSTRUCTORS) /* && G_PLATFORM_WIN32 && GLIB_STATIC_COMPILATION */
#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(glib_init_ctor)
#endif
#ifdef G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA
#pragma G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(glib_init_dtor)
#endif

G_DEFINE_CONSTRUCTOR (glib_init_ctor)

static void
glib_init_ctor (void)
{
  glib_win32_init ();
}

G_DEFINE_DESTRUCTOR (glib_init_dtor)

static void
glib_init_dtor (void)
{
  glib_win32_deinit (FALSE);
}

#else /* G_PLATFORM_WIN32 && GLIB_STATIC_COMPILATION && !G_HAS_CONSTRUCTORS */
#error Your platform/compiler is missing constructor support
#endif /* GLIB_STATIC_COMPILATION */

445
#elif defined(G_HAS_CONSTRUCTORS) /* && !G_PLATFORM_WIN32 */
446

447
448
449
450
451
452
#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(glib_init_ctor)
#endif
G_DEFINE_CONSTRUCTOR(glib_init_ctor)

static void
453
454
455
456
457
glib_init_ctor (void)
{
  glib_init ();
}

458
#else /* !G_PLATFORM_WIN32 && !G_HAS_CONSTRUCTORS */
459
# error Your platform/compiler is missing constructor support
460
#endif /* G_PLATFORM_WIN32 */