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 1.2
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"
54 #ifdef DBUS_ENABLE_VERBOSE_MODE
55 # define _dbus_verbose _dbus_verbose_real
56 # define _dbus_verbose_reset _dbus_verbose_reset_real
58 # ifdef HAVE_ISO_VARARGS
59 # define _dbus_verbose(...)
60 # elif defined (HAVE_GNUC_VARARGS)
61 # define _dbus_verbose(format...)
63 # error "This compiler does not support varargs macros and thus verbose mode can't be disabled meaningfully"
65 # define _dbus_verbose_reset()
66 #endif /* !DBUS_ENABLE_VERBOSE_MODE */
68 const char* _dbus_strerror (int error_number);
70 #ifdef DBUS_DISABLE_ASSERT
71 #define _dbus_assert(condition)
73 void _dbus_real_assert (dbus_bool_t condition,
74 const char *condition_text,
77 #define _dbus_assert(condition) \
78 _dbus_real_assert ((condition) != 0, #condition, __FILE__, __LINE__)
79 #endif /* !DBUS_DISABLE_ASSERT */
81 #ifdef DBUS_DISABLE_ASSERT
82 #define _dbus_assert_not_reached(explanation)
84 void _dbus_real_assert_not_reached (const char *explanation,
86 int line) _DBUS_GNUC_NORETURN;
87 #define _dbus_assert_not_reached(explanation) \
88 _dbus_real_assert_not_reached (explanation, __FILE__, __LINE__)
89 #endif /* !DBUS_DISABLE_ASSERT */
91 #ifdef DBUS_DISABLE_CHECKS
92 #define _dbus_return_if_fail(condition)
93 #define _dbus_return_val_if_fail(condition, val)
95 extern const char _dbus_return_if_fail_warning_format[];
97 #define _dbus_return_if_fail(condition) do { \
99 _dbus_warn (_dbus_return_if_fail_warning_format, \
100 _DBUS_FUNCTION_NAME, #condition, __FILE__, __LINE__); \
104 #define _dbus_return_val_if_fail(condition, val) do { \
105 if (!(condition)) { \
106 _dbus_warn (_dbus_return_if_fail_warning_format, \
107 _DBUS_FUNCTION_NAME, #condition, __FILE__, __LINE__); \
111 #endif /* !DBUS_DISABLE_ASSERT */
113 #define _DBUS_N_ELEMENTS(array) ((int) (sizeof ((array)) / sizeof ((array)[0])))
115 #define _DBUS_POINTER_TO_INT(pointer) ((long)(pointer))
116 #define _DBUS_INT_TO_POINTER(integer) ((void*)((long)(integer)))
118 #define _DBUS_ZERO(object) (memset (&(object), '\0', sizeof ((object))))
120 #define _DBUS_STRUCT_OFFSET(struct_type, member) \
121 ((long) ((unsigned char*) &((struct_type*) 0)->member))
123 #define _DBUS_ASSERT_ERROR_IS_SET(error) _dbus_assert ((error) == NULL || dbus_error_is_set ((error)))
124 #define _DBUS_ASSERT_ERROR_IS_CLEAR(error) _dbus_assert ((error) == NULL || !dbus_error_is_set ((error)))
126 #define _dbus_return_if_error_is_set(error) _dbus_return_if_fail ((error) == NULL || !dbus_error_is_set ((error)))
127 #define _dbus_return_val_if_error_is_set(error, val) _dbus_return_val_if_fail ((error) == NULL || !dbus_error_is_set ((error)), (val))
129 /* This alignment thing is from ORBit2 */
130 /* Align a value upward to a boundary, expressed as a number of bytes.
131 * E.g. align to an 8-byte boundary with argument of 8.
135 * (this + boundary - 1)
140 #define _DBUS_ALIGN_VALUE(this, boundary) \
141 (( ((unsigned long)(this)) + (((unsigned long)(boundary)) -1)) & (~(((unsigned long)(boundary))-1)))
143 #define _DBUS_ALIGN_ADDRESS(this, boundary) \
144 ((void*)_DBUS_ALIGN_VALUE(this, boundary))
146 char* _dbus_strdup (const char *str);
147 void* _dbus_memdup (const void *mem,
149 dbus_bool_t _dbus_string_array_contains (const char **array,
151 char** _dbus_dup_string_array (const char **array);
153 #define _DBUS_INT_MIN (-_DBUS_INT_MAX - 1)
154 #define _DBUS_INT_MAX 2147483647
155 #define _DBUS_UINT_MAX 0xffffffff
156 #ifdef DBUS_HAVE_INT64
157 #define _DBUS_INT64_MAX DBUS_INT64_CONSTANT (9223372036854775807)
158 #define _DBUS_UINT64_MAX DBUS_UINT64_CONSTANT (0xffffffffffffffff)
160 #define _DBUS_ONE_KILOBYTE 1024
161 #define _DBUS_ONE_MEGABYTE 1024 * _DBUS_ONE_KILOBYTE
162 #define _DBUS_ONE_HOUR_IN_MILLISECONDS (1000 * 60 * 60)
163 #define _DBUS_USEC_PER_SECOND (1000000)
166 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
169 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
172 #define ABS(a) (((a) < 0) ? -(a) : (a))
174 typedef void (* DBusForeachFunction) (void *element,
177 dbus_bool_t _dbus_set_fd_nonblocking (int fd,
180 void _dbus_verbose_bytes (const unsigned char *data,
182 void _dbus_verbose_bytes_of_string (const DBusString *str,
187 const char* _dbus_type_to_string (int type);
189 extern const char _dbus_no_memory_message[];
190 #define _DBUS_SET_OOM(error) dbus_set_error ((error), DBUS_ERROR_NO_MEMORY, _dbus_no_memory_message)
192 #ifdef DBUS_BUILD_TESTS
193 /* Memory debugging */
194 void _dbus_set_fail_alloc_counter (int until_next_fail);
195 int _dbus_get_fail_alloc_counter (void);
196 void _dbus_set_fail_alloc_failures (int failures_per_failure);
197 int _dbus_get_fail_alloc_failures (void);
198 dbus_bool_t _dbus_decrement_fail_alloc_counter (void);
199 dbus_bool_t _dbus_disable_mem_pools (void);
200 int _dbus_get_malloc_blocks_outstanding (void);
202 typedef dbus_bool_t (* DBusTestMemoryFunction) (void *data);
203 dbus_bool_t _dbus_test_oom_handling (const char *description,
204 DBusTestMemoryFunction func,
207 #define _dbus_set_fail_alloc_counter(n)
208 #define _dbus_get_fail_alloc_counter _DBUS_INT_MAX
210 /* These are constant expressions so that blocks
211 * they protect should be optimized away
213 #define _dbus_decrement_fail_alloc_counter() (FALSE)
214 #define _dbus_disable_mem_pools() (FALSE)
215 #define _dbus_get_malloc_blocks_outstanding (0)
216 #endif /* !DBUS_BUILD_TESTS */
218 typedef void (* DBusShutdownFunction) (void *data);
219 dbus_bool_t _dbus_register_shutdown_func (DBusShutdownFunction function,
222 extern int _dbus_current_generation;
224 /* Thread initializers */
225 #define _DBUS_LOCK_NAME(name) _dbus_lock_##name
226 #define _DBUS_DECLARE_GLOBAL_LOCK(name) extern DBusMutex *_dbus_lock_##name
227 #define _DBUS_DEFINE_GLOBAL_LOCK(name) DBusMutex *_dbus_lock_##name
228 #define _DBUS_LOCK(name) dbus_mutex_lock (_dbus_lock_##name)
229 #define _DBUS_UNLOCK(name) dbus_mutex_unlock (_dbus_lock_##name)
231 _DBUS_DECLARE_GLOBAL_LOCK (list);
232 _DBUS_DECLARE_GLOBAL_LOCK (connection_slots);
233 _DBUS_DECLARE_GLOBAL_LOCK (server_slots);
234 _DBUS_DECLARE_GLOBAL_LOCK (message_slots);
235 _DBUS_DECLARE_GLOBAL_LOCK (atomic);
236 _DBUS_DECLARE_GLOBAL_LOCK (bus);
237 _DBUS_DECLARE_GLOBAL_LOCK (shutdown_funcs);
238 _DBUS_DECLARE_GLOBAL_LOCK (system_users);
239 #define _DBUS_N_GLOBAL_LOCKS (8)
241 dbus_bool_t _dbus_threads_init_debug (void);
245 #endif /* DBUS_INTERNALS_H */