D-Bus 1.4.6
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(...)
00112 #  elif defined (HAVE_GNUC_VARARGS)
00113 #    define _dbus_verbose(format...)
00114 #  else
00115 static void _dbus_verbose(const char * x,...) {;}
00116 #  endif
00117 #  define _dbus_verbose_reset()
00118 #  define _dbus_is_verbose() FALSE 
00119 #endif /* !DBUS_ENABLE_VERBOSE_MODE */
00120 
00121 const char* _dbus_strerror (int error_number);
00122 
00123 #ifdef DBUS_DISABLE_ASSERT
00124 #define _dbus_assert(condition)
00125 #else
00126 void _dbus_real_assert (dbus_bool_t  condition,
00127                         const char  *condition_text,
00128                         const char  *file,
00129                         int          line,
00130                         const char  *func);
00131 #define _dbus_assert(condition)                                         \
00132   _dbus_real_assert ((condition) != 0, #condition, __FILE__, __LINE__, _DBUS_FUNCTION_NAME)
00133 #endif /* !DBUS_DISABLE_ASSERT */
00134 
00135 #ifdef DBUS_DISABLE_ASSERT
00136 #define _dbus_assert_not_reached(explanation)
00137 #else
00138 void _dbus_real_assert_not_reached (const char *explanation,
00139                                     const char *file,
00140                                     int         line) _DBUS_GNUC_NORETURN;
00141 #define _dbus_assert_not_reached(explanation)                                   \
00142   _dbus_real_assert_not_reached (explanation, __FILE__, __LINE__)
00143 #endif /* !DBUS_DISABLE_ASSERT */
00144 
00145 #ifdef DBUS_DISABLE_CHECKS
00146 #define _dbus_return_if_fail(condition)
00147 #define _dbus_return_val_if_fail(condition, val)
00148 #else
00149 
00150 extern const char *_dbus_return_if_fail_warning_format;
00151 
00152 #define _dbus_return_if_fail(condition) do {                                       \
00153    _dbus_assert ((*(const char*)_DBUS_FUNCTION_NAME) != '_');                      \
00154   if (!(condition)) {                                                              \
00155     _dbus_warn_check_failed (_dbus_return_if_fail_warning_format,                  \
00156                              _DBUS_FUNCTION_NAME, #condition, __FILE__, __LINE__); \
00157     return;                                                                        \
00158   } } while (0)
00159 
00160 #define _dbus_return_val_if_fail(condition, val) 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 (val);                                                                       \
00166   } } while (0)
00167 
00168 #endif /* !DBUS_DISABLE_ASSERT */
00169 
00170 #define _DBUS_N_ELEMENTS(array) ((int) (sizeof ((array)) / sizeof ((array)[0])))
00171 
00172 #define _DBUS_POINTER_TO_INT(pointer) ((intptr_t)(pointer))
00173 #define _DBUS_INT_TO_POINTER(integer) ((void*)((intptr_t)(integer)))
00174 
00175 #define _DBUS_ZERO(object) (memset (&(object), '\0', sizeof ((object))))
00176 
00177 #define _DBUS_STRUCT_OFFSET(struct_type, member)        \
00178     ((intptr_t) ((unsigned char*) &((struct_type*) 0)->member))
00179 
00180 #ifdef DBUS_DISABLE_CHECKS
00181 /* this is an assert and not an error, but in the typical --disable-checks case (you're trying
00182  * to really minimize code size), disabling these assertions makes sense.
00183  */
00184 #define _DBUS_ASSERT_ERROR_IS_SET(error)
00185 #define _DBUS_ASSERT_ERROR_IS_CLEAR(error)
00186 #else
00187 #define _DBUS_ASSERT_ERROR_IS_SET(error)   _dbus_assert ((error) == NULL || dbus_error_is_set ((error)))
00188 #define _DBUS_ASSERT_ERROR_IS_CLEAR(error) _dbus_assert ((error) == NULL || !dbus_error_is_set ((error)))
00189 #endif
00190 
00191 #define _dbus_return_if_error_is_set(error) _dbus_return_if_fail ((error) == NULL || !dbus_error_is_set ((error)))
00192 #define _dbus_return_val_if_error_is_set(error, val) _dbus_return_val_if_fail ((error) == NULL || !dbus_error_is_set ((error)), (val))
00193 
00194 /* This alignment thing is from ORBit2 */
00195 /* Align a value upward to a boundary, expressed as a number of bytes.
00196  * E.g. align to an 8-byte boundary with argument of 8.
00197  */
00198 
00199 /*
00200  *   (this + boundary - 1)
00201  *          &
00202  *    ~(boundary - 1)
00203  */
00204 
00205 #define _DBUS_ALIGN_VALUE(this, boundary) \
00206   (( ((uintptr_t)(this)) + (((uintptr_t)(boundary)) -1)) & (~(((uintptr_t)(boundary))-1)))
00207 
00208 #define _DBUS_ALIGN_ADDRESS(this, boundary) \
00209   ((void*)_DBUS_ALIGN_VALUE(this, boundary))
00210 
00211 
00212 char*       _dbus_strdup                (const char  *str);
00213 void*       _dbus_memdup                (const void  *mem,
00214                                          size_t       n_bytes);
00215 dbus_bool_t _dbus_string_array_contains (const char **array,
00216                                          const char  *str);
00217 char**      _dbus_dup_string_array      (const char **array);
00218 
00219 #define _DBUS_INT16_MIN  ((dbus_int16_t) 0x8000)
00220 #define _DBUS_INT16_MAX  ((dbus_int16_t) 0x7fff)
00221 #define _DBUS_UINT16_MAX ((dbus_uint16_t)0xffff)
00222 #define _DBUS_INT32_MIN  ((dbus_int32_t) 0x80000000)
00223 #define _DBUS_INT32_MAX  ((dbus_int32_t) 0x7fffffff)
00224 #define _DBUS_UINT32_MAX ((dbus_uint32_t)0xffffffff)
00225 /* using 32-bit here is sort of bogus */
00226 #define _DBUS_INT_MIN    _DBUS_INT32_MIN
00227 #define _DBUS_INT_MAX    _DBUS_INT32_MAX
00228 #define _DBUS_UINT_MAX   _DBUS_UINT32_MAX
00229 #ifdef DBUS_HAVE_INT64
00230 #define _DBUS_INT64_MAX  DBUS_INT64_CONSTANT  (0x7fffffffffffffff)
00231 #define _DBUS_UINT64_MAX DBUS_UINT64_CONSTANT (0xffffffffffffffff)
00232 #endif
00233 #define _DBUS_ONE_KILOBYTE 1024
00234 #define _DBUS_ONE_MEGABYTE 1024 * _DBUS_ONE_KILOBYTE
00235 #define _DBUS_ONE_HOUR_IN_MILLISECONDS (1000 * 60 * 60)
00236 #define _DBUS_USEC_PER_SECOND          (1000000)
00237 
00238 #undef  MAX
00239 #define MAX(a, b)  (((a) > (b)) ? (a) : (b))
00240 
00241 #undef  MIN
00242 #define MIN(a, b)  (((a) < (b)) ? (a) : (b))
00243 
00244 #undef  ABS
00245 #define ABS(a)     (((a) < 0) ? -(a) : (a))
00246 
00247 #define _DBUS_ISASCII(c) ((c) != '\0' && (((c) & ~0x7f) == 0))
00248 
00249 typedef void (* DBusForeachFunction) (void *element,
00250                                       void *data);
00251 
00252 dbus_bool_t _dbus_set_fd_nonblocking (int             fd,
00253                                       DBusError      *error);
00254 
00255 void _dbus_verbose_bytes           (const unsigned char *data,
00256                                     int                  len,
00257                                     int                  offset);
00258 void _dbus_verbose_bytes_of_string (const DBusString    *str,
00259                                     int                  start,
00260                                     int                  len);
00261 
00262 const char* _dbus_header_field_to_string (int header_field);
00263 
00264 extern const char *_dbus_no_memory_message;
00265 #define _DBUS_SET_OOM(error) dbus_set_error_const ((error), DBUS_ERROR_NO_MEMORY, _dbus_no_memory_message)
00266 
00267 #ifdef DBUS_BUILD_TESTS
00268 /* Memory debugging */
00269 void        _dbus_set_fail_alloc_counter        (int  until_next_fail);
00270 int         _dbus_get_fail_alloc_counter        (void);
00271 void        _dbus_set_fail_alloc_failures       (int  failures_per_failure);
00272 int         _dbus_get_fail_alloc_failures       (void);
00273 dbus_bool_t _dbus_decrement_fail_alloc_counter  (void);
00274 dbus_bool_t _dbus_disable_mem_pools             (void);
00275 int         _dbus_get_malloc_blocks_outstanding (void);
00276 
00277 typedef dbus_bool_t (* DBusTestMemoryFunction)  (void *data);
00278 dbus_bool_t _dbus_test_oom_handling (const char             *description,
00279                                      DBusTestMemoryFunction  func,
00280                                      void                   *data);
00281 #else
00282 #define _dbus_set_fail_alloc_counter(n)
00283 #define _dbus_get_fail_alloc_counter _DBUS_INT_MAX
00284 
00285 /* These are constant expressions so that blocks
00286  * they protect should be optimized away
00287  */
00288 #define _dbus_decrement_fail_alloc_counter() (FALSE)
00289 #define _dbus_disable_mem_pools()            (FALSE)
00290 #define _dbus_get_malloc_blocks_outstanding  (0)
00291 #endif /* !DBUS_BUILD_TESTS */
00292 
00293 typedef void (* DBusShutdownFunction) (void *data);
00294 dbus_bool_t _dbus_register_shutdown_func (DBusShutdownFunction  function,
00295                                           void                 *data);
00296 
00297 extern int _dbus_current_generation;
00298 
00299 /* Thread initializers */
00300 #define _DBUS_LOCK_NAME(name)           _dbus_lock_##name
00301 #define _DBUS_DECLARE_GLOBAL_LOCK(name) extern DBusMutex  *_dbus_lock_##name
00302 #define _DBUS_DEFINE_GLOBAL_LOCK(name)  DBusMutex         *_dbus_lock_##name  
00303 #define _DBUS_LOCK(name)                _dbus_mutex_lock   (_dbus_lock_##name)
00304 #define _DBUS_UNLOCK(name)              _dbus_mutex_unlock (_dbus_lock_##name)
00305 
00306 /* 1-5 */
00307 _DBUS_DECLARE_GLOBAL_LOCK (list);
00308 _DBUS_DECLARE_GLOBAL_LOCK (connection_slots);
00309 _DBUS_DECLARE_GLOBAL_LOCK (pending_call_slots);
00310 _DBUS_DECLARE_GLOBAL_LOCK (server_slots);
00311 _DBUS_DECLARE_GLOBAL_LOCK (message_slots);
00312 /* 5-10 */
00313 _DBUS_DECLARE_GLOBAL_LOCK (bus);
00314 _DBUS_DECLARE_GLOBAL_LOCK (bus_datas);
00315 _DBUS_DECLARE_GLOBAL_LOCK (shutdown_funcs);
00316 _DBUS_DECLARE_GLOBAL_LOCK (system_users);
00317 _DBUS_DECLARE_GLOBAL_LOCK (message_cache);
00318 /* 10-14 */
00319 _DBUS_DECLARE_GLOBAL_LOCK (shared_connections);
00320 _DBUS_DECLARE_GLOBAL_LOCK (win_fds);
00321 _DBUS_DECLARE_GLOBAL_LOCK (sid_atom_cache);
00322 _DBUS_DECLARE_GLOBAL_LOCK (machine_uuid);
00323 
00324 #if !DBUS_USE_SYNC
00325 _DBUS_DECLARE_GLOBAL_LOCK (atomic);
00326 #define _DBUS_N_GLOBAL_LOCKS (15)
00327 #else
00328 #define _DBUS_N_GLOBAL_LOCKS (14)
00329 #endif
00330 
00331 dbus_bool_t _dbus_threads_init_debug (void);
00332 
00333 dbus_bool_t   _dbus_address_append_escaped (DBusString       *escaped,
00334                                             const DBusString *unescaped);
00335 
00336 void          _dbus_set_bad_address        (DBusError         *error,
00337                                             const char        *address_problem_type,
00338                                             const char        *address_problem_field,
00339                                             const char        *address_problem_other);
00340 
00341 #define DBUS_UUID_LENGTH_BYTES 16
00342 #define DBUS_UUID_LENGTH_WORDS (DBUS_UUID_LENGTH_BYTES / 4)
00343 #define DBUS_UUID_LENGTH_HEX   (DBUS_UUID_LENGTH_BYTES * 2)
00344 
00349 union DBusGUID
00350 {
00351   dbus_uint32_t as_uint32s[DBUS_UUID_LENGTH_WORDS];     
00352   char as_bytes[DBUS_UUID_LENGTH_BYTES];                
00353 };
00354 
00355 void        _dbus_generate_uuid  (DBusGUID         *uuid);
00356 dbus_bool_t _dbus_uuid_encode    (const DBusGUID   *uuid,
00357                                   DBusString       *encoded);
00358 dbus_bool_t _dbus_read_uuid_file (const DBusString *filename,
00359                                   DBusGUID         *uuid,
00360                                   dbus_bool_t       create_if_not_found,
00361                                   DBusError        *error);
00362 
00363 dbus_bool_t _dbus_get_local_machine_uuid_encoded (DBusString *uuid_str);
00364 
00365 DBUS_END_DECLS
00366 
00367 #endif /* DBUS_INTERNALS_H */