Change log level at g_log_remove_handler
[platform/upstream/glib.git] / glib / gtypes.h
index 0b843c6..9d912d5 100644 (file)
@@ -1,10 +1,12 @@
 /* GLIB - Library of useful routines for C programming
  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
  *
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ *
  * 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 of the License, or (at your option) any later version.
+ * 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
@@ -12,9 +14,7 @@
  * 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, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  */
 
 /*
  * GLib at ftp://ftp.gtk.org/pub/gtk/.
  */
 
-#if defined(G_DISABLE_SINGLE_INCLUDES) && !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
-#error "Only <glib.h> can be included directly."
-#endif
-
 #ifndef __G_TYPES_H__
 #define __G_TYPES_H__
 
+#if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
+#error "Only <glib.h> can be included directly."
+#endif
+
 #include <glibconfig.h>
 #include <glib/gmacros.h>
+#include <glib/gversionmacros.h>
+
+/* Must be included after the 3 headers above */
+#include <glib/glib-visibility.h>
+
 #include <time.h>
 
 G_BEGIN_DECLS
@@ -59,21 +64,47 @@ typedef float   gfloat;
 typedef double  gdouble;
 
 /* Define min and max constants for the fixed size numerical types */
-#define G_MININT8      ((gint8)  0x80)
+/**
+ * G_MININT8: (value -128)
+ *
+ * The minimum value which can be held in a #gint8.
+ *
+ * Since: 2.4
+ */
+#define G_MININT8      ((gint8) (-G_MAXINT8 - 1))
 #define G_MAXINT8      ((gint8)  0x7f)
 #define G_MAXUINT8     ((guint8) 0xff)
 
-#define G_MININT16     ((gint16)  0x8000)
+/**
+ * G_MININT16: (value -32768)
+ *
+ * The minimum value which can be held in a #gint16.
+ *
+ * Since: 2.4
+ */
+#define G_MININT16     ((gint16) (-G_MAXINT16 - 1))
 #define G_MAXINT16     ((gint16)  0x7fff)
 #define G_MAXUINT16    ((guint16) 0xffff)
 
-#define G_MININT32     ((gint32)  0x80000000)
+/**
+ * G_MININT32: (value -2147483648)
+ *
+ * The minimum value which can be held in a #gint32.
+ *
+ * Since: 2.4
+ */
+#define G_MININT32     ((gint32) (-G_MAXINT32 - 1))
 #define G_MAXINT32     ((gint32)  0x7fffffff)
 #define G_MAXUINT32    ((guint32) 0xffffffff)
 
-#define G_MININT64     ((gint64) G_GINT64_CONSTANT(0x8000000000000000))
+/**
+ * G_MININT64: (value -9223372036854775808)
+ *
+ * The minimum value which can be held in a #gint64.
+ */
+#define G_MININT64     ((gint64) (-G_MAXINT64 - G_GINT64_CONSTANT(1)))
 #define G_MAXINT64     G_GINT64_CONSTANT(0x7fffffffffffffff)
-#define G_MAXUINT64    G_GINT64_CONSTANT(0xffffffffffffffffU)
+#define G_MAXUINT64    G_GUINT64_CONSTANT(0xffffffffffffffff)
 
 typedef void* gpointer;
 typedef const void *gconstpointer;
@@ -85,6 +116,27 @@ typedef gint            (*GCompareDataFunc)     (gconstpointer  a,
                                                 gpointer       user_data);
 typedef gboolean        (*GEqualFunc)           (gconstpointer  a,
                                                  gconstpointer  b);
+
+/**
+ * GEqualFuncFull:
+ * @a: a value
+ * @b: a value to compare with
+ * @user_data: user data provided by the caller
+ *
+ * Specifies the type of a function used to test two values for
+ * equality. The function should return %TRUE if both values are equal
+ * and %FALSE otherwise.
+ *
+ * This is a version of #GEqualFunc which provides a @user_data closure from
+ * the caller.
+ *
+ * Returns: %TRUE if @a = @b; %FALSE otherwise
+ * Since: 2.74
+ */
+typedef gboolean        (*GEqualFuncFull)       (gconstpointer  a,
+                                                 gconstpointer  b,
+                                                 gpointer       user_data);
+
 typedef void            (*GDestroyNotify)       (gpointer       data);
 typedef void            (*GFunc)                (gpointer       data,
                                                  gpointer       user_data);
@@ -92,6 +144,29 @@ typedef guint           (*GHashFunc)            (gconstpointer  key);
 typedef void            (*GHFunc)               (gpointer       key,
                                                  gpointer       value,
                                                  gpointer       user_data);
+
+/**
+ * GCopyFunc:
+ * @src: (not nullable): A pointer to the data which should be copied
+ * @data: Additional data
+ *
+ * A function of this signature is used to copy the node data
+ * when doing a deep-copy of a tree.
+ *
+ * Returns: (not nullable): A pointer to the copy
+ *
+ * Since: 2.4
+ */
+typedef gpointer       (*GCopyFunc)            (gconstpointer  src,
+                                                 gpointer       data);
+/**
+ * GFreeFunc:
+ * @data: a data pointer
+ *
+ * Declares a type of function which takes an arbitrary
+ * data pointer argument and has no return value. It is
+ * not currently used in GLib or GTK.
+ */
 typedef void            (*GFreeFunc)            (gpointer       data);
 
 /**
@@ -171,10 +246,16 @@ typedef const gchar *   (*GTranslateFunc)       (const gchar   *str,
 /* Arch specific stuff for speed
  */
 #if defined (__GNUC__) && (__GNUC__ >= 2) && defined (__OPTIMIZE__)
+
+#  if __GNUC__ >= 4 && defined (__GNUC_MINOR__) && __GNUC_MINOR__ >= 3
+#    define GUINT32_SWAP_LE_BE(val) ((guint32) __builtin_bswap32 ((guint32) (val)))
+#    define GUINT64_SWAP_LE_BE(val) ((guint64) __builtin_bswap64 ((guint64) (val)))
+#  endif
+
 #  if defined (__i386__)
 #    define GUINT16_SWAP_LE_BE_IA32(val) \
-       (__extension__                                          \
-       ({ register guint16 __v, __x = ((guint16) (val));       \
+       (G_GNUC_EXTENSION                                       \
+       ({ guint16 __v, __x = ((guint16) (val));                \
           if (__builtin_constant_p (__x))                      \
             __v = GUINT16_SWAP_LE_BE_CONSTANT (__x);           \
           else                                                 \
@@ -187,8 +268,8 @@ typedef const gchar *   (*GTranslateFunc)       (const gchar   *str,
        && !defined (__pentium__) && !defined (__i686__) \
        && !defined (__pentiumpro__) && !defined (__pentium4__)
 #       define GUINT32_SWAP_LE_BE_IA32(val) \
-         (__extension__                                        \
-          ({ register guint32 __v, __x = ((guint32) (val));    \
+         (G_GNUC_EXTENSION                                     \
+          ({ guint32 __v, __x = ((guint32) (val));             \
              if (__builtin_constant_p (__x))                   \
                __v = GUINT32_SWAP_LE_BE_CONSTANT (__x);        \
              else                                              \
@@ -201,8 +282,8 @@ typedef const gchar *   (*GTranslateFunc)       (const gchar   *str,
              __v; }))
 #    else /* 486 and higher has bswap */
 #       define GUINT32_SWAP_LE_BE_IA32(val) \
-         (__extension__                                        \
-          ({ register guint32 __v, __x = ((guint32) (val));    \
+         (G_GNUC_EXTENSION                                     \
+          ({ guint32 __v, __x = ((guint32) (val));             \
              if (__builtin_constant_p (__x))                   \
                __v = GUINT32_SWAP_LE_BE_CONSTANT (__x);        \
              else                                              \
@@ -212,7 +293,7 @@ typedef const gchar *   (*GTranslateFunc)       (const gchar   *str,
              __v; }))
 #    endif /* processor specific 32-bit stuff */
 #    define GUINT64_SWAP_LE_BE_IA32(val) \
-       (__extension__                                                  \
+       (G_GNUC_EXTENSION                                               \
        ({ union { guint64 __ll;                                        \
                   guint32 __l[2]; } __w, __r;                          \
           __w.__ll = ((guint64) (val));                                \
@@ -226,12 +307,16 @@ typedef const gchar *   (*GTranslateFunc)       (const gchar   *str,
           __r.__ll; }))
      /* Possibly just use the constant version and let gcc figure it out? */
 #    define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_IA32 (val))
-#    define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_IA32 (val))
-#    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_IA32 (val))
+#    ifndef GUINT32_SWAP_LE_BE
+#      define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_IA32 (val))
+#    endif
+#    ifndef GUINT64_SWAP_LE_BE
+#      define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_IA32 (val))
+#    endif
 #  elif defined (__ia64__)
 #    define GUINT16_SWAP_LE_BE_IA64(val) \
-       (__extension__                                          \
-       ({ register guint16 __v, __x = ((guint16) (val));       \
+       (G_GNUC_EXTENSION                                       \
+       ({ guint16 __v, __x = ((guint16) (val));                \
           if (__builtin_constant_p (__x))                      \
             __v = GUINT16_SWAP_LE_BE_CONSTANT (__x);           \
           else                                                 \
@@ -241,8 +326,8 @@ typedef const gchar *   (*GTranslateFunc)       (const gchar   *str,
                                    : "r" (__x));               \
            __v; }))
 #    define GUINT32_SWAP_LE_BE_IA64(val) \
-       (__extension__                                          \
-        ({ register guint32 __v, __x = ((guint32) (val));      \
+       (G_GNUC_EXTENSION                                       \
+        ({ guint32 __v, __x = ((guint32) (val));               \
            if (__builtin_constant_p (__x))                     \
              __v = GUINT32_SWAP_LE_BE_CONSTANT (__x);          \
            else                                                \
@@ -252,8 +337,8 @@ typedef const gchar *   (*GTranslateFunc)       (const gchar   *str,
                                    : "r" (__x));               \
            __v; }))
 #    define GUINT64_SWAP_LE_BE_IA64(val) \
-       (__extension__                                          \
-       ({ register guint64 __v, __x = ((guint64) (val));       \
+       (G_GNUC_EXTENSION                                       \
+       ({ guint64 __v, __x = ((guint64) (val));                \
           if (__builtin_constant_p (__x))                      \
             __v = GUINT64_SWAP_LE_BE_CONSTANT (__x);           \
           else                                                 \
@@ -262,12 +347,16 @@ typedef const gchar *   (*GTranslateFunc)       (const gchar   *str,
                                   : "r" (__x));                \
           __v; }))
 #    define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_IA64 (val))
-#    define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_IA64 (val))
-#    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_IA64 (val))
+#    ifndef GUINT32_SWAP_LE_BE
+#      define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_IA64 (val))
+#    endif
+#    ifndef GUINT64_SWAP_LE_BE
+#      define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_IA64 (val))
+#    endif
 #  elif defined (__x86_64__)
 #    define GUINT32_SWAP_LE_BE_X86_64(val) \
-       (__extension__                                          \
-        ({ register guint32 __v, __x = ((guint32) (val));      \
+       (G_GNUC_EXTENSION                                       \
+        ({ guint32 __v, __x = ((guint32) (val));               \
            if (__builtin_constant_p (__x))                     \
              __v = GUINT32_SWAP_LE_BE_CONSTANT (__x);          \
            else                                                \
@@ -276,8 +365,8 @@ typedef const gchar *   (*GTranslateFunc)       (const gchar   *str,
                      : "0" (__x));                             \
            __v; }))
 #    define GUINT64_SWAP_LE_BE_X86_64(val) \
-       (__extension__                                          \
-       ({ register guint64 __v, __x = ((guint64) (val));       \
+       (G_GNUC_EXTENSION                                       \
+       ({ guint64 __v, __x = ((guint64) (val));                \
           if (__builtin_constant_p (__x))                      \
             __v = GUINT64_SWAP_LE_BE_CONSTANT (__x);           \
           else                                                 \
@@ -287,12 +376,20 @@ typedef const gchar *   (*GTranslateFunc)       (const gchar   *str,
           __v; }))
      /* gcc seems to figure out optimal code for this on its own */
 #    define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
-#    define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_X86_64 (val))
-#    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_X86_64 (val))
+#    ifndef GUINT32_SWAP_LE_BE
+#      define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_X86_64 (val))
+#    endif
+#    ifndef GUINT64_SWAP_LE_BE
+#      define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_X86_64 (val))
+#    endif
 #  else /* generic gcc */
 #    define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
-#    define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
-#    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT (val))
+#    ifndef GUINT32_SWAP_LE_BE
+#      define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
+#    endif
+#    ifndef GUINT64_SWAP_LE_BE
+#      define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT (val))
+#    endif
 #  endif
 #else /* generic */
 #  define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
@@ -341,7 +438,6 @@ typedef const gchar *   (*GTranslateFunc)       (const gchar   *str,
 #define GSIZE_FROM_BE(val)     (GSIZE_TO_BE (val))
 #define GSSIZE_FROM_BE(val)    (GSSIZE_TO_BE (val))
 
-
 /* Portable versions of host-network order stuff
  */
 #define g_ntohl(val) (GUINT32_FROM_BE (val))
@@ -349,6 +445,69 @@ typedef const gchar *   (*GTranslateFunc)       (const gchar   *str,
 #define g_htonl(val) (GUINT32_TO_BE (val))
 #define g_htons(val) (GUINT16_TO_BE (val))
 
+/* Overflow-checked unsigned integer arithmetic
+ */
+#ifndef _GLIB_TEST_OVERFLOW_FALLBACK
+/* https://bugzilla.gnome.org/show_bug.cgi?id=769104 */
+#if __GNUC__ >= 5 && !defined(__INTEL_COMPILER)
+#define _GLIB_HAVE_BUILTIN_OVERFLOW_CHECKS
+#elif g_macro__has_builtin(__builtin_add_overflow)
+#define _GLIB_HAVE_BUILTIN_OVERFLOW_CHECKS
+#endif
+#endif
+
+#ifdef _GLIB_HAVE_BUILTIN_OVERFLOW_CHECKS
+
+#define g_uint_checked_add(dest, a, b) \
+    (!__builtin_add_overflow(a, b, dest))
+#define g_uint_checked_mul(dest, a, b) \
+    (!__builtin_mul_overflow(a, b, dest))
+
+#define g_uint64_checked_add(dest, a, b) \
+    (!__builtin_add_overflow(a, b, dest))
+#define g_uint64_checked_mul(dest, a, b) \
+    (!__builtin_mul_overflow(a, b, dest))
+
+#define g_size_checked_add(dest, a, b) \
+    (!__builtin_add_overflow(a, b, dest))
+#define g_size_checked_mul(dest, a, b) \
+    (!__builtin_mul_overflow(a, b, dest))
+
+#else  /* !_GLIB_HAVE_BUILTIN_OVERFLOW_CHECKS */
+
+/* The names of the following inlines are private.  Use the macro
+ * definitions above.
+ */
+static inline gboolean _GLIB_CHECKED_ADD_UINT (guint *dest, guint a, guint b) {
+  *dest = a + b; return *dest >= a; }
+static inline gboolean _GLIB_CHECKED_MUL_UINT (guint *dest, guint a, guint b) {
+  *dest = a * b; return !a || *dest / a == b; }
+static inline gboolean _GLIB_CHECKED_ADD_UINT64 (guint64 *dest, guint64 a, guint64 b) {
+  *dest = a + b; return *dest >= a; }
+static inline gboolean _GLIB_CHECKED_MUL_UINT64 (guint64 *dest, guint64 a, guint64 b) {
+  *dest = a * b; return !a || *dest / a == b; }
+static inline gboolean _GLIB_CHECKED_ADD_SIZE (gsize *dest, gsize a, gsize b) {
+  *dest = a + b; return *dest >= a; }
+static inline gboolean _GLIB_CHECKED_MUL_SIZE (gsize *dest, gsize a, gsize b) {
+  *dest = a * b; return !a || *dest / a == b; }
+
+#define g_uint_checked_add(dest, a, b) \
+    _GLIB_CHECKED_ADD_UINT(dest, a, b)
+#define g_uint_checked_mul(dest, a, b) \
+    _GLIB_CHECKED_MUL_UINT(dest, a, b)
+
+#define g_uint64_checked_add(dest, a, b) \
+    _GLIB_CHECKED_ADD_UINT64(dest, a, b)
+#define g_uint64_checked_mul(dest, a, b) \
+    _GLIB_CHECKED_MUL_UINT64(dest, a, b)
+
+#define g_size_checked_add(dest, a, b) \
+    _GLIB_CHECKED_ADD_SIZE(dest, a, b)
+#define g_size_checked_mul(dest, a, b) \
+    _GLIB_CHECKED_MUL_SIZE(dest, a, b)
+
+#endif  /* !_GLIB_HAVE_BUILTIN_OVERFLOW_CHECKS */
+
 /* IEEE Standard 754 Single Precision Storage Format (gfloat):
  *
  *        31 30           23 22            0
@@ -416,45 +575,17 @@ union _GDoubleIEEE754
 #error unknown ENDIAN type
 #endif /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
 
-typedef struct _GTimeVal                GTimeVal;
+typedef struct _GTimeVal GTimeVal GLIB_DEPRECATED_TYPE_IN_2_62_FOR(GDateTime);
 
 struct _GTimeVal
 {
   glong tv_sec;
   glong tv_usec;
-};
+} GLIB_DEPRECATED_TYPE_IN_2_62_FOR(GDateTime);
 
-typedef struct _GTimeSpec               GTimeSpec;
-
-struct _GTimeSpec
-{
-  time_t tv_sec;
-  glong  tv_nsec;
-};
+typedef gint grefcount;
+typedef gint gatomicrefcount;  /* should be accessed only using atomics */
 
 G_END_DECLS
 
-/* We prefix variable declarations so they can
- * properly get exported in Windows DLLs.
- */
-#ifndef GLIB_VAR
-#  ifdef G_PLATFORM_WIN32
-#    ifdef GLIB_STATIC_COMPILATION
-#      define GLIB_VAR extern
-#    else /* !GLIB_STATIC_COMPILATION */
-#      ifdef GLIB_COMPILATION
-#        ifdef DLL_EXPORT
-#          define GLIB_VAR __declspec(dllexport)
-#        else /* !DLL_EXPORT */
-#          define GLIB_VAR extern
-#        endif /* !DLL_EXPORT */
-#      else /* !GLIB_COMPILATION */
-#        define GLIB_VAR extern __declspec(dllimport)
-#      endif /* !GLIB_COMPILATION */
-#    endif /* !GLIB_STATIC_COMPILATION */
-#  else /* !G_PLATFORM_WIN32 */
-#    define GLIB_VAR extern
-#  endif /* !G_PLATFORM_WIN32 */
-#endif /* GLIB_VAR */
-
 #endif /* __G_TYPES_H__ */