D-Bus  1.10.12
dbus-internals.h
00001 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
00002 /* dbus-internals.h  random utility stuff (internal to D-Bus implementation)
00003  *
00004  * Copyright (C) 2002, 2003  Red Hat, Inc.
00005  *
00006  * Licensed under the Academic Free License version 2.1
00007  * 
00008  * This program is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  * 
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00021  *
00022  */
00023 #ifdef DBUS_INSIDE_DBUS_H
00024 #error "You can't include dbus-internals.h in the public header dbus.h"
00025 #endif
00026 
00027 #ifndef DBUS_INTERNALS_H
00028 #define DBUS_INTERNALS_H
00029 
00030 #include <dbus/dbus-memory.h>
00031 #include <dbus/dbus-types.h>
00032 #include <dbus/dbus-errors.h>
00033 #include <dbus/dbus-sysdeps.h>
00034 #include <dbus/dbus-threads-internal.h>
00035 
00036 DBUS_BEGIN_DECLS
00037 
00038 DBUS_PRIVATE_EXPORT
00039 void _dbus_warn               (const char *format,
00040                                ...) _DBUS_GNUC_PRINTF (1, 2);
00041 
00042 DBUS_PRIVATE_EXPORT
00043 void _dbus_warn_check_failed  (const char *format,
00044                                ...) _DBUS_GNUC_PRINTF (1, 2);
00045 
00046 
00047 #if defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
00048 #define _DBUS_FUNCTION_NAME __func__
00049 #elif defined(__GNUC__) || defined(_MSC_VER)
00050 #define _DBUS_FUNCTION_NAME __FUNCTION__
00051 #else
00052 #define _DBUS_FUNCTION_NAME "unknown function"
00053 #endif
00054 
00055 /*
00056  * (code from GLib)
00057  * 
00058  * The _DBUS_LIKELY and _DBUS_UNLIKELY macros let the programmer give hints to 
00059  * the compiler about the expected result of an expression. Some compilers
00060  * can use this information for optimizations.
00061  *
00062  * The _DBUS_BOOLEAN_EXPR macro is intended to trigger a gcc warning when
00063  * putting assignments in the macro arg
00064  */
00065 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
00066 #define _DBUS_BOOLEAN_EXPR(expr)                \
00067  __extension__ ({                               \
00068    int _dbus_boolean_var_;                      \
00069    if (expr)                                    \
00070       _dbus_boolean_var_ = 1;                   \
00071    else                                         \
00072       _dbus_boolean_var_ = 0;                   \
00073    _dbus_boolean_var_;                          \
00074 })
00075 #define _DBUS_LIKELY(expr) (__builtin_expect (_DBUS_BOOLEAN_EXPR(expr), 1))
00076 #define _DBUS_UNLIKELY(expr) (__builtin_expect (_DBUS_BOOLEAN_EXPR(expr), 0))
00077 #else
00078 #define _DBUS_LIKELY(expr) (expr)
00079 #define _DBUS_UNLIKELY(expr) (expr)
00080 #endif
00081 
00082 #ifdef DBUS_ENABLE_VERBOSE_MODE
00083 
00084 /*
00085  at least gnu cc and msvc compiler are known to 
00086  have support for variable macro argument lists
00087  add other compilers is required
00088 */
00089 #if defined(__GNUC__) || defined(_MSC_VER) 
00090 #define DBUS_CPP_SUPPORTS_VARIABLE_MACRO_ARGUMENTS
00091 #endif
00092 
00093 #ifdef DBUS_CPP_SUPPORTS_VARIABLE_MACRO_ARGUMENTS
00094 DBUS_PRIVATE_EXPORT
00095 void _dbus_verbose_real       (const char *file, const int line, const char *function, 
00096                                const char *format,...) _DBUS_GNUC_PRINTF (4, 5);
00097 #  define _dbus_verbose(fmt,...) _dbus_verbose_real( __FILE__,__LINE__,__FUNCTION__,fmt, ## __VA_ARGS__)
00098 #else
00099 DBUS_PRIVATE_EXPORT
00100 void _dbus_verbose_real       (const char *format,
00101                                ...) _DBUS_GNUC_PRINTF (1, 2);
00102 #  define _dbus_verbose _dbus_verbose_real
00103 #endif
00104 DBUS_PRIVATE_EXPORT
00105 void _dbus_verbose_reset_real (void);
00106 DBUS_PRIVATE_EXPORT
00107 dbus_bool_t _dbus_is_verbose_real (void);
00108 DBUS_PRIVATE_EXPORT
00109 dbus_bool_t _dbus_get_verbose (void);
00110 DBUS_PRIVATE_EXPORT
00111 void _dbus_set_verbose (dbus_bool_t state);
00112 
00113 #  define _dbus_verbose_reset _dbus_verbose_reset_real
00114 #  define _dbus_is_verbose _dbus_is_verbose_real
00115 #else
00116 #  ifdef HAVE_ISO_VARARGS
00117 #    define _dbus_verbose(...) do { } while (0)
00118 #  elif defined (HAVE_GNUC_VARARGS)
00119 #    define _dbus_verbose(format...) do { } while (0)
00120 #  else
00121 static void _dbus_verbose(const char * x,...) {;}
00122 #  endif
00123 #  define _dbus_verbose_reset() do { } while (0)
00124 #  define _dbus_is_verbose() FALSE 
00125 #endif /* !DBUS_ENABLE_VERBOSE_MODE */
00126 
00127 DBUS_PRIVATE_EXPORT
00128 void _dbus_trace_ref (const char *obj_name,
00129                       void       *obj,
00130                       int         old_refcount,
00131                       int         new_refcount,
00132                       const char *why,
00133                       const char *env_var,
00134                       int        *enabled);
00135 
00136 DBUS_PRIVATE_EXPORT
00137 const char* _dbus_strerror (int error_number);
00138 
00139 #ifdef DBUS_DISABLE_ASSERT
00140 #define _dbus_assert(condition) do { } while (0)
00141 #else
00142 DBUS_PRIVATE_EXPORT
00143 void _dbus_real_assert (dbus_bool_t  condition,
00144                         const char  *condition_text,
00145                         const char  *file,
00146                         int          line,
00147                         const char  *func);
00148 #define _dbus_assert(condition)                                         \
00149   _dbus_real_assert ((condition) != 0, #condition, __FILE__, __LINE__, _DBUS_FUNCTION_NAME)
00150 #endif /* !DBUS_DISABLE_ASSERT */
00151 
00152 #ifdef DBUS_DISABLE_ASSERT
00153 #define _dbus_assert_not_reached(explanation) do { } while (0)
00154 #else
00155 DBUS_PRIVATE_EXPORT
00156 void _dbus_real_assert_not_reached (const char *explanation,
00157                                     const char *file,
00158                                     int         line) _DBUS_GNUC_NORETURN;
00159 #define _dbus_assert_not_reached(explanation)                                   \
00160   _dbus_real_assert_not_reached (explanation, __FILE__, __LINE__)
00161 #endif /* !DBUS_DISABLE_ASSERT */
00162 
00163 #ifdef DBUS_DISABLE_CHECKS
00164 #define _dbus_return_if_fail(condition)
00165 #define _dbus_return_val_if_fail(condition, val)
00166 #else
00167 
00168 DBUS_PRIVATE_EXPORT
00169 extern const char *_dbus_return_if_fail_warning_format;
00170 
00171 #define _dbus_return_if_fail(condition) do {                                       \
00172    _dbus_assert ((*(const char*)_DBUS_FUNCTION_NAME) != '_');                      \
00173   if (!(condition)) {                                                              \
00174     _dbus_warn_check_failed (_dbus_return_if_fail_warning_format,                  \
00175                              _DBUS_FUNCTION_NAME, #condition, __FILE__, __LINE__); \
00176     return;                                                                        \
00177   } } while (0)
00178 
00179 #define _dbus_return_val_if_fail(condition, val) do {                                   \
00180    _dbus_assert ((*(const char*)_DBUS_FUNCTION_NAME) != '_');                           \
00181   if (!(condition)) {                                                                   \
00182     _dbus_warn_check_failed (_dbus_return_if_fail_warning_format,                       \
00183                              _DBUS_FUNCTION_NAME, #condition, __FILE__, __LINE__);      \
00184     return (val);                                                                       \
00185   } } while (0)
00186 
00187 #endif /* !DBUS_DISABLE_ASSERT */
00188 
00189 #define _DBUS_N_ELEMENTS(array) ((int) (sizeof ((array)) / sizeof ((array)[0])))
00190 
00191 #define _DBUS_POINTER_TO_INT(pointer) ((intptr_t)(pointer))
00192 #define _DBUS_INT_TO_POINTER(integer) ((void*)((intptr_t)(integer)))
00193 
00194 #define _DBUS_ZERO(object) (memset (&(object), '\0', sizeof ((object))))
00195 
00196 #define _DBUS_STRUCT_OFFSET(struct_type, member)        \
00197     ((intptr_t) ((unsigned char*) &((struct_type*) 0)->member))
00198 
00199 #define _DBUS_ALIGNOF(type) \
00200     (_DBUS_STRUCT_OFFSET (struct { char _1; type _2; }, _2))
00201 
00202 #ifdef DBUS_DISABLE_CHECKS
00203 /* this is an assert and not an error, but in the typical --disable-checks case (you're trying
00204  * to really minimize code size), disabling these assertions makes sense.
00205  */
00206 #define _DBUS_ASSERT_ERROR_IS_SET(error) do { } while (0)
00207 #define _DBUS_ASSERT_ERROR_IS_CLEAR(error) do { } while (0)
00208 #else
00209 static inline void
00210 _dbus_assert_error_is_set (const DBusError *error)
00211 {
00212     _dbus_assert (error == NULL || dbus_error_is_set (error));
00213 }
00214 
00215 static inline void
00216 _dbus_assert_error_is_clear (const DBusError *error)
00217 {
00218     _dbus_assert (error == NULL || !dbus_error_is_set (error));
00219 }
00220 
00221 #define _DBUS_ASSERT_ERROR_IS_SET(error) _dbus_assert_error_is_set(error)
00222 #define _DBUS_ASSERT_ERROR_IS_CLEAR(error) _dbus_assert_error_is_clear(error)
00223 #endif
00224 
00225 #define _dbus_return_if_error_is_set(error) _dbus_return_if_fail ((error) == NULL || !dbus_error_is_set ((error)))
00226 #define _dbus_return_val_if_error_is_set(error, val) _dbus_return_val_if_fail ((error) == NULL || !dbus_error_is_set ((error)), (val))
00227 
00228 /* This alignment thing is from ORBit2 */
00229 /* Align a value upward to a boundary, expressed as a number of bytes.
00230  * E.g. align to an 8-byte boundary with argument of 8.
00231  */
00232 
00233 /*
00234  *   (this + boundary - 1)
00235  *          &
00236  *    ~(boundary - 1)
00237  */
00238 
00239 #define _DBUS_ALIGN_VALUE(this, boundary) \
00240   (( ((uintptr_t)(this)) + (((uintptr_t)(boundary)) -1)) & (~(((uintptr_t)(boundary))-1)))
00241 
00242 #define _DBUS_ALIGN_ADDRESS(this, boundary) \
00243   ((void*)_DBUS_ALIGN_VALUE(this, boundary))
00244 
00245 
00246 DBUS_PRIVATE_EXPORT
00247 char*       _dbus_strdup                (const char  *str);
00248 void*       _dbus_memdup                (const void  *mem,
00249                                          size_t       n_bytes);
00250 DBUS_PRIVATE_EXPORT
00251 dbus_bool_t _dbus_string_array_contains (const char **array,
00252                                          const char  *str);
00253 char**      _dbus_dup_string_array      (const char **array);
00254 
00255 #define _DBUS_INT16_MIN  ((dbus_int16_t) 0x8000)
00256 #define _DBUS_INT16_MAX  ((dbus_int16_t) 0x7fff)
00257 #define _DBUS_UINT16_MAX ((dbus_uint16_t)0xffff)
00258 #define _DBUS_INT32_MIN  ((dbus_int32_t) 0x80000000)
00259 #define _DBUS_INT32_MAX  ((dbus_int32_t) 0x7fffffff)
00260 #define _DBUS_UINT32_MAX ((dbus_uint32_t)0xffffffff)
00261 /* using 32-bit here is sort of bogus */
00262 #define _DBUS_INT_MIN    _DBUS_INT32_MIN
00263 #define _DBUS_INT_MAX    _DBUS_INT32_MAX
00264 #define _DBUS_UINT_MAX   _DBUS_UINT32_MAX
00265 #define _DBUS_INT64_MAX  DBUS_INT64_CONSTANT  (0x7fffffffffffffff)
00266 #define _DBUS_UINT64_MAX DBUS_UINT64_CONSTANT (0xffffffffffffffff)
00267 #define _DBUS_ONE_KILOBYTE 1024
00268 #define _DBUS_ONE_MEGABYTE 1024 * _DBUS_ONE_KILOBYTE
00269 #define _DBUS_ONE_HOUR_IN_MILLISECONDS (1000 * 60 * 60)
00270 #define _DBUS_USEC_PER_SECOND          (1000000)
00271 
00272 #undef  MAX
00273 #define MAX(a, b)  (((a) > (b)) ? (a) : (b))
00274 
00275 #undef  MIN
00276 #define MIN(a, b)  (((a) < (b)) ? (a) : (b))
00277 
00278 #undef  ABS
00279 #define ABS(a)     (((a) < 0) ? -(a) : (a))
00280 
00281 #define _DBUS_ISASCII(c) ((c) != '\0' && (((c) & ~0x7f) == 0))
00282 
00283 typedef void (* DBusForeachFunction) (void *element,
00284                                       void *data);
00285 
00286 void _dbus_verbose_bytes           (const unsigned char *data,
00287                                     int                  len,
00288                                     int                  offset);
00289 DBUS_PRIVATE_EXPORT
00290 void _dbus_verbose_bytes_of_string (const DBusString    *str,
00291                                     int                  start,
00292                                     int                  len);
00293 
00294 DBUS_PRIVATE_EXPORT
00295 extern const char *_dbus_no_memory_message;
00296 #define _DBUS_SET_OOM(error) dbus_set_error_const ((error), DBUS_ERROR_NO_MEMORY, _dbus_no_memory_message)
00297 DBUS_PRIVATE_EXPORT
00298 void _dbus_set_error_valist (DBusError  *error,
00299                              const char *name,
00300                              const char *format,
00301                              va_list     args) _DBUS_GNUC_PRINTF (3, 0);
00302 
00303 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
00304 /* Memory debugging */
00305 void        _dbus_set_fail_alloc_counter        (int  until_next_fail);
00306 int         _dbus_get_fail_alloc_counter        (void);
00307 void        _dbus_set_fail_alloc_failures       (int  failures_per_failure);
00308 int         _dbus_get_fail_alloc_failures       (void);
00309 dbus_bool_t _dbus_decrement_fail_alloc_counter  (void);
00310 dbus_bool_t _dbus_disable_mem_pools             (void);
00311 DBUS_PRIVATE_EXPORT
00312 int         _dbus_get_malloc_blocks_outstanding (void);
00313 
00314 typedef dbus_bool_t (* DBusTestMemoryFunction)  (void *data);
00315 DBUS_PRIVATE_EXPORT
00316 dbus_bool_t _dbus_test_oom_handling (const char             *description,
00317                                      DBusTestMemoryFunction  func,
00318                                      void                   *data);
00319 #else
00320 #define _dbus_set_fail_alloc_counter(n)
00321 #define _dbus_get_fail_alloc_counter _DBUS_INT_MAX
00322 
00323 /* These are constant expressions so that blocks
00324  * they protect should be optimized away
00325  */
00326 #define _dbus_decrement_fail_alloc_counter() (FALSE)
00327 #define _dbus_disable_mem_pools()            (FALSE)
00328 #define _dbus_get_malloc_blocks_outstanding  (0)
00329 #endif /* !DBUS_ENABLE_EMBEDDED_TESTS */
00330 
00331 typedef void (* DBusShutdownFunction) (void *data);
00332 DBUS_PRIVATE_EXPORT
00333 dbus_bool_t _dbus_register_shutdown_func          (DBusShutdownFunction  function,
00334                                                    void                 *data);
00335 dbus_bool_t _dbus_register_shutdown_func_unlocked (DBusShutdownFunction  function,
00336                                                    void                 *data);
00337 
00338 extern int _dbus_current_generation;
00339 
00340 /* The weird case convention is to avoid having to change all the callers,
00341  * which would be quite a mega-patch. */
00342 typedef enum
00343 {
00344   /* index 0-4 */
00345   _DBUS_LOCK_list,
00346   _DBUS_LOCK_connection_slots,
00347   _DBUS_LOCK_pending_call_slots,
00348   _DBUS_LOCK_server_slots,
00349   _DBUS_LOCK_message_slots,
00350   /* index 5-9 */
00351   _DBUS_LOCK_bus,
00352   _DBUS_LOCK_bus_datas,
00353   _DBUS_LOCK_shutdown_funcs,
00354   _DBUS_LOCK_system_users,
00355   _DBUS_LOCK_message_cache,
00356   /* index 10-12 */
00357   _DBUS_LOCK_shared_connections,
00358   _DBUS_LOCK_machine_uuid,
00359   _DBUS_LOCK_sysdeps,
00360 
00361   _DBUS_N_GLOBAL_LOCKS
00362 } DBusGlobalLock;
00363 
00364 dbus_bool_t _dbus_lock   (DBusGlobalLock lock) _DBUS_GNUC_WARN_UNUSED_RESULT;
00365 void        _dbus_unlock (DBusGlobalLock lock);
00366 
00367 #define _DBUS_LOCK_NAME(name)           _DBUS_LOCK_##name
00368 #define _DBUS_LOCK(name)                _dbus_lock   (_DBUS_LOCK_##name)
00369 #define _DBUS_UNLOCK(name)              _dbus_unlock (_DBUS_LOCK_##name)
00370 
00371 DBUS_PRIVATE_EXPORT
00372 dbus_bool_t _dbus_threads_init_debug (void);
00373 
00374 dbus_bool_t   _dbus_address_append_escaped (DBusString       *escaped,
00375                                             const DBusString *unescaped);
00376 
00377 void          _dbus_set_bad_address        (DBusError         *error,
00378                                             const char        *address_problem_type,
00379                                             const char        *address_problem_field,
00380                                             const char        *address_problem_other);
00381 
00382 #define DBUS_UUID_LENGTH_BYTES 16
00383 #define DBUS_UUID_LENGTH_WORDS (DBUS_UUID_LENGTH_BYTES / 4)
00384 #define DBUS_UUID_LENGTH_HEX   (DBUS_UUID_LENGTH_BYTES * 2)
00385 
00390 union DBusGUID
00391 {
00392   dbus_uint32_t as_uint32s[DBUS_UUID_LENGTH_WORDS];     
00393   char as_bytes[DBUS_UUID_LENGTH_BYTES];                
00394 };
00395 
00396 DBUS_PRIVATE_EXPORT _DBUS_GNUC_WARN_UNUSED_RESULT
00397 dbus_bool_t _dbus_generate_uuid  (DBusGUID         *uuid,
00398                                   DBusError        *error);
00399 DBUS_PRIVATE_EXPORT
00400 dbus_bool_t _dbus_uuid_encode    (const DBusGUID   *uuid,
00401                                   DBusString       *encoded);
00402 dbus_bool_t _dbus_read_uuid_file (const DBusString *filename,
00403                                   DBusGUID         *uuid,
00404                                   dbus_bool_t       create_if_not_found,
00405                                   DBusError        *error);
00406 
00407 dbus_bool_t _dbus_write_uuid_file (const DBusString *filename,
00408                                    const DBusGUID   *uuid,
00409                                    DBusError        *error);
00410 
00411 dbus_bool_t _dbus_get_local_machine_uuid_encoded (DBusString *uuid_str,
00412                                                   DBusError  *error);
00413 
00414 #define _DBUS_PASTE2(a, b) a ## b
00415 #define _DBUS_PASTE(a, b) _DBUS_PASTE2 (a, b)
00416 #define _DBUS_STATIC_ASSERT(expr) \
00417   typedef struct { char _assertion[(expr) ? 1 : -1]; } \
00418   _DBUS_PASTE (_DBUS_STATIC_ASSERT_, __LINE__) _DBUS_GNUC_UNUSED
00419 
00420 DBUS_END_DECLS
00421 
00422 #endif /* DBUS_INTERNALS_H */