1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-internals.h random utility stuff (internal to D-BUS implementation)
4 * Copyright (C) 2002, 2003 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.1
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.
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.
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
23 #ifdef DBUS_INSIDE_DBUS_H
24 #error "You can't include dbus-internals.h in the public header dbus.h"
27 #ifndef DBUS_INTERNALS_H
28 #define DBUS_INTERNALS_H
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>
40 void _dbus_warn (const char *format,
41 ...) _DBUS_GNUC_PRINTF (1, 2);
42 void _dbus_verbose_real (const char *format,
43 ...) _DBUS_GNUC_PRINTF (1, 2);
44 void _dbus_verbose_reset_real (void);
46 #if defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
47 #define _DBUS_FUNCTION_NAME __func__
48 #elif defined(__GNUC__)
49 #define _DBUS_FUNCTION_NAME __FUNCTION__
51 #define _DBUS_FUNCTION_NAME "unknown function"
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.
61 * The _DBUS_BOOLEAN_EXPR macro is intended to trigger a gcc warning when
62 * putting assignments in the macro arg
64 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
65 #define _DBUS_BOOLEAN_EXPR(expr) \
67 int _dbus_boolean_var_; \
69 _dbus_boolean_var_ = 1; \
71 _dbus_boolean_var_ = 0; \
74 #define _DBUS_LIKELY(expr) (__builtin_expect (_DBUS_BOOLEAN_EXPR(expr), 1))
75 #define _DBUS_UNLIKELY(expr) (__builtin_expect (_DBUS_BOOLEAN_EXPR(expr), 0))
77 #define _DBUS_LIKELY(expr) (expr)
78 #define _DBUS_UNLIKELY(expr) (expr)
81 #ifdef DBUS_ENABLE_VERBOSE_MODE
82 # define _dbus_verbose _dbus_verbose_real
83 # define _dbus_verbose_reset _dbus_verbose_reset_real
85 # ifdef HAVE_ISO_VARARGS
86 # define _dbus_verbose(...)
87 # elif defined (HAVE_GNUC_VARARGS)
88 # define _dbus_verbose(format...)
90 # error "This compiler does not support varargs macros and thus verbose mode can't be disabled meaningfully"
92 # define _dbus_verbose_reset()
93 #endif /* !DBUS_ENABLE_VERBOSE_MODE */
95 const char* _dbus_strerror (int error_number);
97 #ifdef DBUS_DISABLE_ASSERT
98 #define _dbus_assert(condition)
100 void _dbus_real_assert (dbus_bool_t condition,
101 const char *condition_text,
104 #define _dbus_assert(condition) \
105 _dbus_real_assert ((condition) != 0, #condition, __FILE__, __LINE__)
106 #endif /* !DBUS_DISABLE_ASSERT */
108 #ifdef DBUS_DISABLE_ASSERT
109 #define _dbus_assert_not_reached(explanation)
111 void _dbus_real_assert_not_reached (const char *explanation,
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 */
118 #ifdef DBUS_DISABLE_CHECKS
119 #define _dbus_return_if_fail(condition)
120 #define _dbus_return_val_if_fail(condition, val)
122 extern const char _dbus_return_if_fail_warning_format[];
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__); \
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__); \
138 #endif /* !DBUS_DISABLE_ASSERT */
140 #define _DBUS_N_ELEMENTS(array) ((int) (sizeof ((array)) / sizeof ((array)[0])))
142 #define _DBUS_POINTER_TO_INT(pointer) ((long)(pointer))
143 #define _DBUS_INT_TO_POINTER(integer) ((void*)((long)(integer)))
145 #define _DBUS_ZERO(object) (memset (&(object), '\0', sizeof ((object))))
147 #define _DBUS_STRUCT_OFFSET(struct_type, member) \
148 ((long) ((unsigned char*) &((struct_type*) 0)->member))
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)))
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))
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.
162 * (this + boundary - 1)
167 #define _DBUS_ALIGN_VALUE(this, boundary) \
168 (( ((unsigned long)(this)) + (((unsigned long)(boundary)) -1)) & (~(((unsigned long)(boundary))-1)))
170 #define _DBUS_ALIGN_ADDRESS(this, boundary) \
171 ((void*)_DBUS_ALIGN_VALUE(this, boundary))
173 char* _dbus_strdup (const char *str);
174 void* _dbus_memdup (const void *mem,
176 dbus_bool_t _dbus_string_array_contains (const char **array,
178 char** _dbus_dup_string_array (const char **array);
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)
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)
193 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
196 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
199 #define ABS(a) (((a) < 0) ? -(a) : (a))
201 typedef void (* DBusForeachFunction) (void *element,
204 dbus_bool_t _dbus_set_fd_nonblocking (int fd,
207 void _dbus_verbose_bytes (const unsigned char *data,
209 void _dbus_verbose_bytes_of_string (const DBusString *str,
214 const char* _dbus_type_to_string (int type);
215 const char* _dbus_header_field_to_string (int header_field);
217 extern 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)
220 #ifdef DBUS_BUILD_TESTS
221 /* Memory debugging */
222 void _dbus_set_fail_alloc_counter (int until_next_fail);
223 int _dbus_get_fail_alloc_counter (void);
224 void _dbus_set_fail_alloc_failures (int failures_per_failure);
225 int _dbus_get_fail_alloc_failures (void);
226 dbus_bool_t _dbus_decrement_fail_alloc_counter (void);
227 dbus_bool_t _dbus_disable_mem_pools (void);
228 int _dbus_get_malloc_blocks_outstanding (void);
230 typedef dbus_bool_t (* DBusTestMemoryFunction) (void *data);
231 dbus_bool_t _dbus_test_oom_handling (const char *description,
232 DBusTestMemoryFunction func,
235 #define _dbus_set_fail_alloc_counter(n)
236 #define _dbus_get_fail_alloc_counter _DBUS_INT_MAX
238 /* These are constant expressions so that blocks
239 * they protect should be optimized away
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 */
246 typedef void (* DBusShutdownFunction) (void *data);
247 dbus_bool_t _dbus_register_shutdown_func (DBusShutdownFunction function,
250 extern int _dbus_current_generation;
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)
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)
270 dbus_bool_t _dbus_threads_init_debug (void);
274 #endif /* DBUS_INTERNALS_H */