dbus-internals.h revision 43605a6f4e78a8c28afb4b1e924dff0301e0e95c
1/* -*- mode: C; c-file-style: "gnu" -*- */ 2/* dbus-internals.h random utility stuff (internal to D-BUS implementation) 3 * 4 * Copyright (C) 2002, 2003 Red Hat, Inc. 5 * 6 * Licensed under the Academic Free License version 2.1 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; either version 2 of the License, or 11 * (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with this program; if not, write to the Free Software 20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 * 22 */ 23#ifdef DBUS_INSIDE_DBUS_H 24#error "You can't include dbus-internals.h in the public header dbus.h" 25#endif 26 27#ifndef DBUS_INTERNALS_H 28#define DBUS_INTERNALS_H 29 30#include <config.h> 31 32#include <dbus/dbus-memory.h> 33#include <dbus/dbus-types.h> 34#include <dbus/dbus-errors.h> 35#include <dbus/dbus-sysdeps.h> 36#include <dbus/dbus-threads.h> 37 38DBUS_BEGIN_DECLS; 39 40void _dbus_warn (const char *format, 41 ...) _DBUS_GNUC_PRINTF (1, 2); 42void _dbus_verbose_real (const char *format, 43 ...) _DBUS_GNUC_PRINTF (1, 2); 44void _dbus_verbose_reset_real (void); 45 46#if defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) 47#define _DBUS_FUNCTION_NAME __func__ 48#elif defined(__GNUC__) 49#define _DBUS_FUNCTION_NAME __FUNCTION__ 50#else 51#define _DBUS_FUNCTION_NAME "unknown function" 52#endif 53 54/* 55 * (code from GLib) 56 * 57 * The _DBUS_LIKELY and _DBUS_UNLIKELY macros let the programmer give hints to 58 * the compiler about the expected result of an expression. Some compilers 59 * can use this information for optimizations. 60 * 61 * The _DBUS_BOOLEAN_EXPR macro is intended to trigger a gcc warning when 62 * putting assignments in the macro arg 63 */ 64#if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__) 65#define _DBUS_BOOLEAN_EXPR(expr) \ 66 __extension__ ({ \ 67 int _dbus_boolean_var_; \ 68 if (expr) \ 69 _dbus_boolean_var_ = 1; \ 70 else \ 71 _dbus_boolean_var_ = 0; \ 72 _dbus_boolean_var_; \ 73}) 74#define _DBUS_LIKELY(expr) (__builtin_expect (_DBUS_BOOLEAN_EXPR(expr), 1)) 75#define _DBUS_UNLIKELY(expr) (__builtin_expect (_DBUS_BOOLEAN_EXPR(expr), 0)) 76#else 77#define _DBUS_LIKELY(expr) (expr) 78#define _DBUS_UNLIKELY(expr) (expr) 79#endif 80 81#ifdef DBUS_ENABLE_VERBOSE_MODE 82# define _dbus_verbose _dbus_verbose_real 83# define _dbus_verbose_reset _dbus_verbose_reset_real 84#else 85# ifdef HAVE_ISO_VARARGS 86# define _dbus_verbose(...) 87# elif defined (HAVE_GNUC_VARARGS) 88# define _dbus_verbose(format...) 89# else 90# error "This compiler does not support varargs macros and thus verbose mode can't be disabled meaningfully" 91# endif 92# define _dbus_verbose_reset() 93#endif /* !DBUS_ENABLE_VERBOSE_MODE */ 94 95const char* _dbus_strerror (int error_number); 96 97#ifdef DBUS_DISABLE_ASSERT 98#define _dbus_assert(condition) 99#else 100void _dbus_real_assert (dbus_bool_t condition, 101 const char *condition_text, 102 const char *file, 103 int line); 104#define _dbus_assert(condition) \ 105 _dbus_real_assert ((condition) != 0, #condition, __FILE__, __LINE__) 106#endif /* !DBUS_DISABLE_ASSERT */ 107 108#ifdef DBUS_DISABLE_ASSERT 109#define _dbus_assert_not_reached(explanation) 110#else 111void _dbus_real_assert_not_reached (const char *explanation, 112 const char *file, 113 int line) _DBUS_GNUC_NORETURN; 114#define _dbus_assert_not_reached(explanation) \ 115 _dbus_real_assert_not_reached (explanation, __FILE__, __LINE__) 116#endif /* !DBUS_DISABLE_ASSERT */ 117 118#ifdef DBUS_DISABLE_CHECKS 119#define _dbus_return_if_fail(condition) 120#define _dbus_return_val_if_fail(condition, val) 121#else 122extern const char _dbus_return_if_fail_warning_format[]; 123 124#define _dbus_return_if_fail(condition) do { \ 125 if (!(condition)) { \ 126 _dbus_warn (_dbus_return_if_fail_warning_format, \ 127 _dbus_getpid (), _DBUS_FUNCTION_NAME, #condition, __FILE__, __LINE__); \ 128 return; \ 129 } } while (0) 130 131#define _dbus_return_val_if_fail(condition, val) do { \ 132 if (!(condition)) { \ 133 _dbus_warn (_dbus_return_if_fail_warning_format, \ 134 _dbus_getpid (), _DBUS_FUNCTION_NAME, #condition, __FILE__, __LINE__); \ 135 return (val); \ 136 } } while (0) 137 138#endif /* !DBUS_DISABLE_ASSERT */ 139 140#define _DBUS_N_ELEMENTS(array) ((int) (sizeof ((array)) / sizeof ((array)[0]))) 141 142#define _DBUS_POINTER_TO_INT(pointer) ((long)(pointer)) 143#define _DBUS_INT_TO_POINTER(integer) ((void*)((long)(integer))) 144 145#define _DBUS_ZERO(object) (memset (&(object), '\0', sizeof ((object)))) 146 147#define _DBUS_STRUCT_OFFSET(struct_type, member) \ 148 ((long) ((unsigned char*) &((struct_type*) 0)->member)) 149 150#define _DBUS_ASSERT_ERROR_IS_SET(error) _dbus_assert ((error) == NULL || dbus_error_is_set ((error))) 151#define _DBUS_ASSERT_ERROR_IS_CLEAR(error) _dbus_assert ((error) == NULL || !dbus_error_is_set ((error))) 152 153#define _dbus_return_if_error_is_set(error) _dbus_return_if_fail ((error) == NULL || !dbus_error_is_set ((error))) 154#define _dbus_return_val_if_error_is_set(error, val) _dbus_return_val_if_fail ((error) == NULL || !dbus_error_is_set ((error)), (val)) 155 156/* This alignment thing is from ORBit2 */ 157/* Align a value upward to a boundary, expressed as a number of bytes. 158 * E.g. align to an 8-byte boundary with argument of 8. 159 */ 160 161/* 162 * (this + boundary - 1) 163 * & 164 * ~(boundary - 1) 165 */ 166 167#define _DBUS_ALIGN_VALUE(this, boundary) \ 168 (( ((unsigned long)(this)) + (((unsigned long)(boundary)) -1)) & (~(((unsigned long)(boundary))-1))) 169 170#define _DBUS_ALIGN_ADDRESS(this, boundary) \ 171 ((void*)_DBUS_ALIGN_VALUE(this, boundary)) 172 173char* _dbus_strdup (const char *str); 174void* _dbus_memdup (const void *mem, 175 size_t n_bytes); 176dbus_bool_t _dbus_string_array_contains (const char **array, 177 const char *str); 178char** _dbus_dup_string_array (const char **array); 179 180#define _DBUS_INT_MIN (-_DBUS_INT_MAX - 1) 181#define _DBUS_INT_MAX 2147483647 182#define _DBUS_UINT_MAX 0xffffffff 183#ifdef DBUS_HAVE_INT64 184#define _DBUS_INT64_MAX DBUS_INT64_CONSTANT (9223372036854775807) 185#define _DBUS_UINT64_MAX DBUS_UINT64_CONSTANT (0xffffffffffffffff) 186#endif 187#define _DBUS_ONE_KILOBYTE 1024 188#define _DBUS_ONE_MEGABYTE 1024 * _DBUS_ONE_KILOBYTE 189#define _DBUS_ONE_HOUR_IN_MILLISECONDS (1000 * 60 * 60) 190#define _DBUS_USEC_PER_SECOND (1000000) 191 192#undef MAX 193#define MAX(a, b) (((a) > (b)) ? (a) : (b)) 194 195#undef MIN 196#define MIN(a, b) (((a) < (b)) ? (a) : (b)) 197 198#undef ABS 199#define ABS(a) (((a) < 0) ? -(a) : (a)) 200 201typedef void (* DBusForeachFunction) (void *element, 202 void *data); 203 204dbus_bool_t _dbus_set_fd_nonblocking (int fd, 205 DBusError *error); 206 207void _dbus_verbose_bytes (const unsigned char *data, 208 int len); 209void _dbus_verbose_bytes_of_string (const DBusString *str, 210 int start, 211 int len); 212 213 214const char* _dbus_type_to_string (int type); 215const char* _dbus_header_field_to_string (int header_field); 216 217extern const char _dbus_no_memory_message[]; 218#define _DBUS_SET_OOM(error) dbus_set_error_const ((error), DBUS_ERROR_NO_MEMORY, _dbus_no_memory_message) 219 220#ifdef DBUS_BUILD_TESTS 221/* Memory debugging */ 222void _dbus_set_fail_alloc_counter (int until_next_fail); 223int _dbus_get_fail_alloc_counter (void); 224void _dbus_set_fail_alloc_failures (int failures_per_failure); 225int _dbus_get_fail_alloc_failures (void); 226dbus_bool_t _dbus_decrement_fail_alloc_counter (void); 227dbus_bool_t _dbus_disable_mem_pools (void); 228int _dbus_get_malloc_blocks_outstanding (void); 229 230typedef dbus_bool_t (* DBusTestMemoryFunction) (void *data); 231dbus_bool_t _dbus_test_oom_handling (const char *description, 232 DBusTestMemoryFunction func, 233 void *data); 234#else 235#define _dbus_set_fail_alloc_counter(n) 236#define _dbus_get_fail_alloc_counter _DBUS_INT_MAX 237 238/* These are constant expressions so that blocks 239 * they protect should be optimized away 240 */ 241#define _dbus_decrement_fail_alloc_counter() (FALSE) 242#define _dbus_disable_mem_pools() (FALSE) 243#define _dbus_get_malloc_blocks_outstanding (0) 244#endif /* !DBUS_BUILD_TESTS */ 245 246typedef void (* DBusShutdownFunction) (void *data); 247dbus_bool_t _dbus_register_shutdown_func (DBusShutdownFunction function, 248 void *data); 249 250extern int _dbus_current_generation; 251 252/* Thread initializers */ 253#define _DBUS_LOCK_NAME(name) _dbus_lock_##name 254#define _DBUS_DECLARE_GLOBAL_LOCK(name) extern DBusMutex *_dbus_lock_##name 255#define _DBUS_DEFINE_GLOBAL_LOCK(name) DBusMutex *_dbus_lock_##name 256#define _DBUS_LOCK(name) dbus_mutex_lock (_dbus_lock_##name) 257#define _DBUS_UNLOCK(name) dbus_mutex_unlock (_dbus_lock_##name) 258 259_DBUS_DECLARE_GLOBAL_LOCK (list); 260_DBUS_DECLARE_GLOBAL_LOCK (connection_slots); 261_DBUS_DECLARE_GLOBAL_LOCK (pending_call_slots); 262_DBUS_DECLARE_GLOBAL_LOCK (server_slots); 263_DBUS_DECLARE_GLOBAL_LOCK (message_slots); 264_DBUS_DECLARE_GLOBAL_LOCK (atomic); 265_DBUS_DECLARE_GLOBAL_LOCK (bus); 266_DBUS_DECLARE_GLOBAL_LOCK (shutdown_funcs); 267_DBUS_DECLARE_GLOBAL_LOCK (system_users); 268#define _DBUS_N_GLOBAL_LOCKS (9) 269 270dbus_bool_t _dbus_threads_init_debug (void); 271 272DBUS_END_DECLS; 273 274#endif /* DBUS_INTERNALS_H */ 275