D-Bus  1.6.8
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 #ifndef DBUS_SESSION_BUS_DEFAULT_ADDRESS
00039 #define DBUS_SESSION_BUS_DEFAULT_ADDRESS        "autolaunch:"
00040 #endif
00041 
00042 void _dbus_warn               (const char *format,
00043                                ...) _DBUS_GNUC_PRINTF (1, 2);
00044 
00045 void _dbus_warn_check_failed  (const char *format,
00046                                ...) _DBUS_GNUC_PRINTF (1, 2);
00047 
00048 
00049 #if defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
00050 #define _DBUS_FUNCTION_NAME __func__
00051 #elif defined(__GNUC__) || defined(_MSC_VER)
00052 #define _DBUS_FUNCTION_NAME __FUNCTION__
00053 #else
00054 #define _DBUS_FUNCTION_NAME "unknown function"
00055 #endif
00056 
00057 /*
00058  * (code from GLib)
00059  * 
00060  * The _DBUS_LIKELY and _DBUS_UNLIKELY macros let the programmer give hints to 
00061  * the compiler about the expected result of an expression. Some compilers
00062  * can use this information for optimizations.
00063  *
00064  * The _DBUS_BOOLEAN_EXPR macro is intended to trigger a gcc warning when
00065  * putting assignments in the macro arg
00066  */
00067 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
00068 #define _DBUS_BOOLEAN_EXPR(expr)                \
00069  __extension__ ({                               \
00070    int _dbus_boolean_var_;                      \
00071    if (expr)                                    \
00072       _dbus_boolean_var_ = 1;                   \
00073    else                                         \
00074       _dbus_boolean_var_ = 0;                   \
00075    _dbus_boolean_var_;                          \
00076 })
00077 #define _DBUS_LIKELY(expr) (__builtin_expect (_DBUS_BOOLEAN_EXPR(expr), 1))
00078 #define _DBUS_UNLIKELY(expr) (__builtin_expect (_DBUS_BOOLEAN_EXPR(expr), 0))
00079 #else
00080 #define _DBUS_LIKELY(expr) (expr)
00081 #define _DBUS_UNLIKELY(expr) (expr)
00082 #endif
00083 
00084 #ifdef DBUS_ENABLE_VERBOSE_MODE
00085 
00086 /*
00087  at least gnu cc and msvc compiler are known to 
00088  have support for variable macro argument lists
00089  add other compilers is required
00090 */
00091 #if defined(__GNUC__) || defined(_MSC_VER) 
00092 #define DBUS_CPP_SUPPORTS_VARIABLE_MACRO_ARGUMENTS
00093 #endif
00094 
00095 #ifdef DBUS_CPP_SUPPORTS_VARIABLE_MACRO_ARGUMENTS
00096 void _dbus_verbose_real       (const char *file, const int line, const char *function, 
00097                                const char *format,...) _DBUS_GNUC_PRINTF (4, 5);
00098 #  define _dbus_verbose(fmt,...) _dbus_verbose_real( __FILE__,__LINE__,__FUNCTION__,fmt, ## __VA_ARGS__)
00099 #else
00100 void _dbus_verbose_real       (const char *format,
00101                                ...) _DBUS_GNUC_PRINTF (1, 2);
00102 #  define _dbus_verbose _dbus_verbose_real
00103 #endif
00104 void _dbus_verbose_reset_real (void);
00105 dbus_bool_t _dbus_is_verbose_real (void);
00106 
00107 #  define _dbus_verbose_reset _dbus_verbose_reset_real
00108 #  define _dbus_is_verbose _dbus_is_verbose_real
00109 #else
00110 #  ifdef HAVE_ISO_VARARGS
00111 #    define _dbus_verbose(...) do { } while (0)
00112 #  elif defined (HAVE_GNUC_VARARGS)
00113 #    define _dbus_verbose(format...) do { } while (0)
00114 #  else
00115 static void _dbus_verbose(const char * x,...) {;}
00116 #  endif
00117 #  define _dbus_verbose_reset() do { } while (0)
00118 #  define _dbus_is_verbose() FALSE 
00119 #endif /* !DBUS_ENABLE_VERBOSE_MODE */
00120 
00121 void _dbus_trace_ref (const char *obj_name,
00122                       void       *obj,
00123                       int         old_refcount,
00124                       int         new_refcount,
00125                       const char *why,
00126                       const char *env_var,
00127                       int        *enabled);
00128 
00129 const char* _dbus_strerror (int error_number);
00130 
00131 #ifdef DBUS_DISABLE_ASSERT
00132 #define _dbus_assert(condition) do { } while (0)
00133 #else
00134 void _dbus_real_assert (dbus_bool_t  condition,
00135                         const char  *condition_text,
00136                         const char  *file,
00137                         int          line,
00138                         const char  *func);
00139 #define _dbus_assert(condition)                                         \
00140   _dbus_real_assert ((condition) != 0, #condition, __FILE__, __LINE__, _DBUS_FUNCTION_NAME)
00141 #endif /* !DBUS_DISABLE_ASSERT */
00142 
00143 #ifdef DBUS_DISABLE_ASSERT
00144 #define _dbus_assert_not_reached(explanation) do { } while (0)
00145 #else
00146 void _dbus_real_assert_not_reached (const char *explanation,
00147                                     const char *file,
00148                                     int         line) _DBUS_GNUC_NORETURN;
00149 #define _dbus_assert_not_reached(explanation)                                   \
00150   _dbus_real_assert_not_reached (explanation, __FILE__, __LINE__)
00151 #endif /* !DBUS_DISABLE_ASSERT */
00152 
00153 #ifdef DBUS_DISABLE_CHECKS
00154 #define _dbus_return_if_fail(condition)
00155 #define _dbus_return_val_if_fail(condition, val)
00156 #else
00157 
00158 extern const char *_dbus_return_if_fail_warning_format;
00159 
00160 #define _dbus_return_if_fail(condition) do {                                       \
00161    _dbus_assert ((*(const char*)_DBUS_FUNCTION_NAME) != '_');                      \
00162   if (!(condition)) {                                                              \
00163     _dbus_warn_check_failed (_dbus_return_if_fail_warning_format,                  \
00164                              _DBUS_FUNCTION_NAME, #condition, __FILE__, __LINE__); \
00165     return;                                                                        \
00166   } } while (0)
00167 
00168 #define _dbus_return_val_if_fail(condition, val) do {                                   \
00169    _dbus_assert ((*(const char*)_DBUS_FUNCTION_NAME) != '_');                           \
00170   if (!(condition)) {                                                                   \
00171     _dbus_warn_check_failed (_dbus_return_if_fail_warning_format,                       \
00172                              _DBUS_FUNCTION_NAME, #condition, __FILE__, __LINE__);      \
00173     return (val);                                                                       \
00174   } } while (0)
00175 
00176 #endif /* !DBUS_DISABLE_ASSERT */
00177 
00178 #define _DBUS_N_ELEMENTS(array) ((int) (sizeof ((array)) / sizeof ((array)[0])))
00179 
00180 #define _DBUS_POINTER_TO_INT(pointer) ((intptr_t)(pointer))
00181 #define _DBUS_INT_TO_POINTER(integer) ((void*)((intptr_t)(integer)))
00182 
00183 #define _DBUS_ZERO(object) (memset (&(object), '\0', sizeof ((object))))
00184 
00185 #define _DBUS_STRUCT_OFFSET(struct_type, member)        \
00186     ((intptr_t) ((unsigned char*) &((struct_type*) 0)->member))
00187 
00188 #ifdef DBUS_DISABLE_CHECKS
00189 /* this is an assert and not an error, but in the typical --disable-checks case (you're trying
00190  * to really minimize code size), disabling these assertions makes sense.
00191  */
00192 #define _DBUS_ASSERT_ERROR_IS_SET(error) do { } while (0)
00193 #define _DBUS_ASSERT_ERROR_IS_CLEAR(error) do { } while (0)
00194 #else
00195 #define _DBUS_ASSERT_ERROR_IS_SET(error)   _dbus_assert ((error) == NULL || dbus_error_is_set ((error)))
00196 #define _DBUS_ASSERT_ERROR_IS_CLEAR(error) _dbus_assert ((error) == NULL || !dbus_error_is_set ((error)))
00197 #endif
00198 
00199 #define _dbus_return_if_error_is_set(error) _dbus_return_if_fail ((error) == NULL || !dbus_error_is_set ((error)))
00200 #define _dbus_return_val_if_error_is_set(error, val) _dbus_return_val_if_fail ((error) == NULL || !dbus_error_is_set ((error)), (val))
00201 
00202 /* This alignment thing is from ORBit2 */
00203 /* Align a value upward to a boundary, expressed as a number of bytes.
00204  * E.g. align to an 8-byte boundary with argument of 8.
00205  */
00206 
00207 /*
00208  *   (this + boundary - 1)
00209  *          &
00210  *    ~(boundary - 1)
00211  */
00212 
00213 #define _DBUS_ALIGN_VALUE(this, boundary) \
00214   (( ((uintptr_t)(this)) + (((uintptr_t)(boundary)) -1)) & (~(((uintptr_t)(boundary))-1)))
00215 
00216 #define _DBUS_ALIGN_ADDRESS(this, boundary) \
00217   ((void*)_DBUS_ALIGN_VALUE(this, boundary))
00218 
00219 
00220 char*       _dbus_strdup                (const char  *str);
00221 void*       _dbus_memdup                (const void  *mem,
00222                                          size_t       n_bytes);
00223 dbus_bool_t _dbus_string_array_contains (const char **array,
00224                                          const char  *str);
00225 char**      _dbus_dup_string_array      (const char **array);
00226 
00227 #define _DBUS_INT16_MIN  ((dbus_int16_t) 0x8000)
00228 #define _DBUS_INT16_MAX  ((dbus_int16_t) 0x7fff)
00229 #define _DBUS_UINT16_MAX ((dbus_uint16_t)0xffff)
00230 #define _DBUS_INT32_MIN  ((dbus_int32_t) 0x80000000)
00231 #define _DBUS_INT32_MAX  ((dbus_int32_t) 0x7fffffff)
00232 #define _DBUS_UINT32_MAX ((dbus_uint32_t)0xffffffff)
00233 /* using 32-bit here is sort of bogus */
00234 #define _DBUS_INT_MIN    _DBUS_INT32_MIN
00235 #define _DBUS_INT_MAX    _DBUS_INT32_MAX
00236 #define _DBUS_UINT_MAX   _DBUS_UINT32_MAX
00237 #ifdef DBUS_HAVE_INT64
00238 #define _DBUS_INT64_MAX  DBUS_INT64_CONSTANT  (0x7fffffffffffffff)
00239 #define _DBUS_UINT64_MAX DBUS_UINT64_CONSTANT (0xffffffffffffffff)
00240 #endif
00241 #define _DBUS_ONE_KILOBYTE 1024
00242 #define _DBUS_ONE_MEGABYTE 1024 * _DBUS_ONE_KILOBYTE
00243 #define _DBUS_ONE_HOUR_IN_MILLISECONDS (1000 * 60 * 60)
00244 #define _DBUS_USEC_PER_SECOND          (1000000)
00245 
00246 #undef  MAX
00247 #define MAX(a, b)  (((a) > (b)) ? (a) : (b))
00248 
00249 #undef  MIN
00250 #define MIN(a, b)  (((a) < (b)) ? (a) : (b))
00251 
00252 #undef  ABS
00253 #define ABS(a)     (((a) < 0) ? -(a) : (a))
00254 
00255 #define _DBUS_ISASCII(c) ((c) != '\0' && (((c) & ~0x7f) == 0))
00256 
00257 typedef void (* DBusForeachFunction) (void *element,
00258                                       void *data);
00259 
00260 dbus_bool_t _dbus_set_fd_nonblocking (int             fd,
00261                                       DBusError      *error);
00262 
00263 void _dbus_verbose_bytes           (const unsigned char *data,
00264                                     int                  len,
00265                                     int                  offset);
00266 void _dbus_verbose_bytes_of_string (const DBusString    *str,
00267                                     int                  start,
00268                                     int                  len);
00269 
00270 extern const char *_dbus_no_memory_message;
00271 #define _DBUS_SET_OOM(error) dbus_set_error_const ((error), DBUS_ERROR_NO_MEMORY, _dbus_no_memory_message)
00272 
00273 #ifdef DBUS_BUILD_TESTS
00274 /* Memory debugging */
00275 void        _dbus_set_fail_alloc_counter        (int  until_next_fail);
00276 int         _dbus_get_fail_alloc_counter        (void);
00277 void        _dbus_set_fail_alloc_failures       (int  failures_per_failure);
00278 int         _dbus_get_fail_alloc_failures       (void);
00279 dbus_bool_t _dbus_decrement_fail_alloc_counter  (void);
00280 dbus_bool_t _dbus_disable_mem_pools             (void);
00281 int         _dbus_get_malloc_blocks_outstanding (void);
00282 
00283 typedef dbus_bool_t (* DBusTestMemoryFunction)  (void *data);
00284 dbus_bool_t _dbus_test_oom_handling (const char             *description,
00285                                      DBusTestMemoryFunction  func,
00286                                      void                   *data);
00287 #else
00288 #define _dbus_set_fail_alloc_counter(n)
00289 #define _dbus_get_fail_alloc_counter _DBUS_INT_MAX
00290 
00291 /* These are constant expressions so that blocks
00292  * they protect should be optimized away
00293  */
00294 #define _dbus_decrement_fail_alloc_counter() (FALSE)
00295 #define _dbus_disable_mem_pools()            (FALSE)
00296 #define _dbus_get_malloc_blocks_outstanding  (0)
00297 #endif /* !DBUS_BUILD_TESTS */
00298 
00299 typedef void (* DBusShutdownFunction) (void *data);
00300 dbus_bool_t _dbus_register_shutdown_func (DBusShutdownFunction  function,
00301                                           void                 *data);
00302 
00303 extern int _dbus_current_generation;
00304 
00305 /* Thread initializers */
00306 #define _DBUS_LOCK_NAME(name)           _dbus_lock_##name
00307 #define _DBUS_DECLARE_GLOBAL_LOCK(name) extern DBusRMutex *_dbus_lock_##name
00308 #define _DBUS_DEFINE_GLOBAL_LOCK(name)  DBusRMutex        *_dbus_lock_##name
00309 #define _DBUS_LOCK(name)                _dbus_rmutex_lock   (_dbus_lock_##name)
00310 #define _DBUS_UNLOCK(name)              _dbus_rmutex_unlock (_dbus_lock_##name)
00311 
00312 /* 1-5 */
00313 _DBUS_DECLARE_GLOBAL_LOCK (list);
00314 _DBUS_DECLARE_GLOBAL_LOCK (connection_slots);
00315 _DBUS_DECLARE_GLOBAL_LOCK (pending_call_slots);
00316 _DBUS_DECLARE_GLOBAL_LOCK (server_slots);
00317 _DBUS_DECLARE_GLOBAL_LOCK (message_slots);
00318 /* 5-10 */
00319 _DBUS_DECLARE_GLOBAL_LOCK (bus);
00320 _DBUS_DECLARE_GLOBAL_LOCK (bus_datas);
00321 _DBUS_DECLARE_GLOBAL_LOCK (shutdown_funcs);
00322 _DBUS_DECLARE_GLOBAL_LOCK (system_users);
00323 _DBUS_DECLARE_GLOBAL_LOCK (message_cache);
00324 /* 10-14 */
00325 _DBUS_DECLARE_GLOBAL_LOCK (shared_connections);
00326 _DBUS_DECLARE_GLOBAL_LOCK (win_fds);
00327 _DBUS_DECLARE_GLOBAL_LOCK (sid_atom_cache);
00328 _DBUS_DECLARE_GLOBAL_LOCK (machine_uuid);
00329 
00330 #if !DBUS_USE_SYNC
00331 _DBUS_DECLARE_GLOBAL_LOCK (atomic);
00332 #define _DBUS_N_GLOBAL_LOCKS (15)
00333 #else
00334 #define _DBUS_N_GLOBAL_LOCKS (14)
00335 #endif
00336 
00337 dbus_bool_t _dbus_threads_init_debug (void);
00338 
00339 dbus_bool_t   _dbus_address_append_escaped (DBusString       *escaped,
00340                                             const DBusString *unescaped);
00341 
00342 void          _dbus_set_bad_address        (DBusError         *error,
00343                                             const char        *address_problem_type,
00344                                             const char        *address_problem_field,
00345                                             const char        *address_problem_other);
00346 
00347 #define DBUS_UUID_LENGTH_BYTES 16
00348 #define DBUS_UUID_LENGTH_WORDS (DBUS_UUID_LENGTH_BYTES / 4)
00349 #define DBUS_UUID_LENGTH_HEX   (DBUS_UUID_LENGTH_BYTES * 2)
00350 
00355 union DBusGUID
00356 {
00357   dbus_uint32_t as_uint32s[DBUS_UUID_LENGTH_WORDS];     
00358   char as_bytes[DBUS_UUID_LENGTH_BYTES];                
00359 };
00360 
00361 void        _dbus_generate_uuid  (DBusGUID         *uuid);
00362 dbus_bool_t _dbus_uuid_encode    (const DBusGUID   *uuid,
00363                                   DBusString       *encoded);
00364 dbus_bool_t _dbus_read_uuid_file (const DBusString *filename,
00365                                   DBusGUID         *uuid,
00366                                   dbus_bool_t       create_if_not_found,
00367                                   DBusError        *error);
00368 
00369 dbus_bool_t _dbus_get_local_machine_uuid_encoded (DBusString *uuid_str);
00370 
00371 #define _DBUS_PASTE2(a, b) a ## b
00372 #define _DBUS_PASTE(a, b) _DBUS_PASTE2 (a, b)
00373 #define _DBUS_STATIC_ASSERT(expr) \
00374   typedef struct { char _assertion[(expr) ? 1 : -1]; } \
00375   _DBUS_PASTE (_DBUS_STATIC_ASSERT_, __LINE__)
00376 
00377 DBUS_END_DECLS
00378 
00379 #endif /* DBUS_INTERNALS_H */