dbus-internals.h revision 72c3ed063016e0fb288bd916651d09e2d6885ed0
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
201#define _DBUS_ISASCII(c) ((c) != '\0' && (((c) & ~0x7f) == 0))
202
203typedef void (* DBusForeachFunction) (void *element,
204                                      void *data);
205
206dbus_bool_t _dbus_set_fd_nonblocking (int             fd,
207                                      DBusError      *error);
208
209void _dbus_verbose_bytes           (const unsigned char *data,
210                                    int                  len);
211void _dbus_verbose_bytes_of_string (const DBusString    *str,
212                                    int                  start,
213                                    int                  len);
214
215
216const char* _dbus_type_to_string         (int type);
217const char* _dbus_header_field_to_string (int header_field);
218
219extern const char _dbus_no_memory_message[];
220#define _DBUS_SET_OOM(error) dbus_set_error_const ((error), DBUS_ERROR_NO_MEMORY, _dbus_no_memory_message)
221
222#ifdef DBUS_BUILD_TESTS
223/* Memory debugging */
224void        _dbus_set_fail_alloc_counter        (int  until_next_fail);
225int         _dbus_get_fail_alloc_counter        (void);
226void        _dbus_set_fail_alloc_failures       (int  failures_per_failure);
227int         _dbus_get_fail_alloc_failures       (void);
228dbus_bool_t _dbus_decrement_fail_alloc_counter  (void);
229dbus_bool_t _dbus_disable_mem_pools             (void);
230int         _dbus_get_malloc_blocks_outstanding (void);
231
232typedef dbus_bool_t (* DBusTestMemoryFunction)  (void *data);
233dbus_bool_t _dbus_test_oom_handling (const char             *description,
234                                     DBusTestMemoryFunction  func,
235                                     void                   *data);
236#else
237#define _dbus_set_fail_alloc_counter(n)
238#define _dbus_get_fail_alloc_counter _DBUS_INT_MAX
239
240/* These are constant expressions so that blocks
241 * they protect should be optimized away
242 */
243#define _dbus_decrement_fail_alloc_counter() (FALSE)
244#define _dbus_disable_mem_pools()            (FALSE)
245#define _dbus_get_malloc_blocks_outstanding  (0)
246#endif /* !DBUS_BUILD_TESTS */
247
248typedef void (* DBusShutdownFunction) (void *data);
249dbus_bool_t _dbus_register_shutdown_func (DBusShutdownFunction  function,
250                                          void                 *data);
251
252extern int _dbus_current_generation;
253
254/* Thread initializers */
255#define _DBUS_LOCK_NAME(name)           _dbus_lock_##name
256#define _DBUS_DECLARE_GLOBAL_LOCK(name) extern DBusMutex  *_dbus_lock_##name
257#define _DBUS_DEFINE_GLOBAL_LOCK(name)  DBusMutex         *_dbus_lock_##name
258#define _DBUS_LOCK(name)                dbus_mutex_lock   (_dbus_lock_##name)
259#define _DBUS_UNLOCK(name)              dbus_mutex_unlock (_dbus_lock_##name)
260
261_DBUS_DECLARE_GLOBAL_LOCK (list);
262_DBUS_DECLARE_GLOBAL_LOCK (connection_slots);
263_DBUS_DECLARE_GLOBAL_LOCK (pending_call_slots);
264_DBUS_DECLARE_GLOBAL_LOCK (server_slots);
265_DBUS_DECLARE_GLOBAL_LOCK (message_slots);
266_DBUS_DECLARE_GLOBAL_LOCK (atomic);
267_DBUS_DECLARE_GLOBAL_LOCK (bus);
268_DBUS_DECLARE_GLOBAL_LOCK (shutdown_funcs);
269_DBUS_DECLARE_GLOBAL_LOCK (system_users);
270_DBUS_DECLARE_GLOBAL_LOCK (message_cache);
271#define _DBUS_N_GLOBAL_LOCKS (10)
272
273dbus_bool_t _dbus_threads_init_debug (void);
274
275DBUS_END_DECLS
276
277#endif /* DBUS_INTERNALS_H */
278