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