dbus-internals.h revision c9cd648f115759176d35508e9007dc5520a0dd3f
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 1.2
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#ifdef DBUS_ENABLE_VERBOSE_MODE
55#  define _dbus_verbose _dbus_verbose_real
56#  define _dbus_verbose_reset _dbus_verbose_reset_real
57#else
58#  ifdef HAVE_ISO_VARARGS
59#    define _dbus_verbose(...)
60#  elif defined (HAVE_GNUC_VARARGS)
61#    define _dbus_verbose(format...)
62#  else
63#    error "This compiler does not support varargs macros and thus verbose mode can't be disabled meaningfully"
64#  endif
65#  define _dbus_verbose_reset()
66#endif /* !DBUS_ENABLE_VERBOSE_MODE */
67
68const char* _dbus_strerror (int error_number);
69
70#ifdef DBUS_DISABLE_ASSERT
71#define _dbus_assert(condition)
72#else
73void _dbus_real_assert (dbus_bool_t  condition,
74                        const char  *condition_text,
75                        const char  *file,
76                        int          line);
77#define _dbus_assert(condition)                                         \
78  _dbus_real_assert ((condition) != 0, #condition, __FILE__, __LINE__)
79#endif /* !DBUS_DISABLE_ASSERT */
80
81#ifdef DBUS_DISABLE_ASSERT
82#define _dbus_assert_not_reached(explanation)
83#else
84void _dbus_real_assert_not_reached (const char *explanation,
85                                    const char *file,
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 */
90
91#ifdef DBUS_DISABLE_CHECKS
92#define _dbus_return_if_fail(condition)
93#define _dbus_return_val_if_fail(condition, val)
94#else
95extern const char _dbus_return_if_fail_warning_format[];
96
97#define _dbus_return_if_fail(condition) do {                            \
98  if (!(condition)) {                                                   \
99    _dbus_warn (_dbus_return_if_fail_warning_format,                    \
100                _DBUS_FUNCTION_NAME, #condition, __FILE__, __LINE__);   \
101    return;                                                             \
102  } } while (0)
103
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__);   \
108    return (val);                                                       \
109  } } while (0)
110
111#endif /* !DBUS_DISABLE_ASSERT */
112
113#define _DBUS_N_ELEMENTS(array) ((int) (sizeof ((array)) / sizeof ((array)[0])))
114
115#define _DBUS_POINTER_TO_INT(pointer) ((long)(pointer))
116#define _DBUS_INT_TO_POINTER(integer) ((void*)((long)(integer)))
117
118#define _DBUS_ZERO(object) (memset (&(object), '\0', sizeof ((object))))
119
120#define _DBUS_STRUCT_OFFSET(struct_type, member)	\
121    ((long) ((unsigned char*) &((struct_type*) 0)->member))
122
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)))
125
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))
128
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.
132 */
133
134/*
135 *   (this + boundary - 1)
136 *          &
137 *    ~(boundary - 1)
138 */
139
140#define _DBUS_ALIGN_VALUE(this, boundary) \
141  (( ((unsigned long)(this)) + (((unsigned long)(boundary)) -1)) & (~(((unsigned long)(boundary))-1)))
142
143#define _DBUS_ALIGN_ADDRESS(this, boundary) \
144  ((void*)_DBUS_ALIGN_VALUE(this, boundary))
145
146char*       _dbus_strdup                (const char  *str);
147dbus_bool_t _dbus_string_array_contains (const char **array,
148                                         const char  *str);
149char**      _dbus_dup_string_array      (const char **array);
150
151#define _DBUS_INT_MIN	 (-_DBUS_INT_MAX - 1)
152#define _DBUS_INT_MAX	 2147483647
153#define _DBUS_UINT_MAX	 0xffffffff
154#ifdef DBUS_HAVE_INT64
155#define _DBUS_INT64_MAX	 DBUS_INT64_CONSTANT (9223372036854775807)
156#define _DBUS_UINT64_MAX DBUS_UINT64_CONSTANT (0xffffffffffffffff)
157#endif
158#define _DBUS_ONE_KILOBYTE 1024
159#define _DBUS_ONE_MEGABYTE 1024 * _DBUS_ONE_KILOBYTE
160#define _DBUS_ONE_HOUR_IN_MILLISECONDS (1000 * 60 * 60)
161#define _DBUS_USEC_PER_SECOND          (1000000)
162
163#undef	MAX
164#define MAX(a, b)  (((a) > (b)) ? (a) : (b))
165
166#undef	MIN
167#define MIN(a, b)  (((a) < (b)) ? (a) : (b))
168
169#undef	ABS
170#define ABS(a)	   (((a) < 0) ? -(a) : (a))
171
172typedef void (* DBusForeachFunction) (void *element,
173                                      void *data);
174
175dbus_bool_t _dbus_set_fd_nonblocking (int             fd,
176                                      DBusError      *error);
177
178void _dbus_verbose_bytes           (const unsigned char *data,
179                                    int                  len);
180void _dbus_verbose_bytes_of_string (const DBusString    *str,
181                                    int                  start,
182                                    int                  len);
183
184
185const char* _dbus_type_to_string (int type);
186
187extern const char _dbus_no_memory_message[];
188#define _DBUS_SET_OOM(error) dbus_set_error ((error), DBUS_ERROR_NO_MEMORY, _dbus_no_memory_message)
189
190#ifdef DBUS_BUILD_TESTS
191/* Memory debugging */
192void        _dbus_set_fail_alloc_counter        (int  until_next_fail);
193int         _dbus_get_fail_alloc_counter        (void);
194void        _dbus_set_fail_alloc_failures       (int  failures_per_failure);
195int         _dbus_get_fail_alloc_failures       (void);
196dbus_bool_t _dbus_decrement_fail_alloc_counter  (void);
197dbus_bool_t _dbus_disable_mem_pools             (void);
198int         _dbus_get_malloc_blocks_outstanding (void);
199
200typedef dbus_bool_t (* DBusTestMemoryFunction)  (void *data);
201dbus_bool_t _dbus_test_oom_handling (const char             *description,
202                                     DBusTestMemoryFunction  func,
203                                     void                   *data);
204#else
205#define _dbus_set_fail_alloc_counter(n)
206#define _dbus_get_fail_alloc_counter _DBUS_INT_MAX
207
208/* These are constant expressions so that blocks
209 * they protect should be optimized away
210 */
211#define _dbus_decrement_fail_alloc_counter() (FALSE)
212#define _dbus_disable_mem_pools()            (FALSE)
213#define _dbus_get_malloc_blocks_outstanding  (0)
214#endif /* !DBUS_BUILD_TESTS */
215
216typedef void (* DBusShutdownFunction) (void *data);
217dbus_bool_t _dbus_register_shutdown_func (DBusShutdownFunction  function,
218                                          void                 *data);
219
220extern int _dbus_current_generation;
221
222/* Thread initializers */
223#define _DBUS_LOCK_NAME(name)           _dbus_lock_##name
224#define _DBUS_DECLARE_GLOBAL_LOCK(name) extern DBusMutex  *_dbus_lock_##name
225#define _DBUS_DEFINE_GLOBAL_LOCK(name)  DBusMutex         *_dbus_lock_##name
226#define _DBUS_LOCK(name)                dbus_mutex_lock   (_dbus_lock_##name)
227#define _DBUS_UNLOCK(name)              dbus_mutex_unlock (_dbus_lock_##name)
228
229_DBUS_DECLARE_GLOBAL_LOCK (list);
230_DBUS_DECLARE_GLOBAL_LOCK (connection_slots);
231_DBUS_DECLARE_GLOBAL_LOCK (server_slots);
232_DBUS_DECLARE_GLOBAL_LOCK (atomic);
233_DBUS_DECLARE_GLOBAL_LOCK (message_handler);
234_DBUS_DECLARE_GLOBAL_LOCK (bus);
235_DBUS_DECLARE_GLOBAL_LOCK (shutdown_funcs);
236_DBUS_DECLARE_GLOBAL_LOCK (system_users);
237#define _DBUS_N_GLOBAL_LOCKS (8)
238
239dbus_bool_t _dbus_threads_init_debug (void);
240
241DBUS_END_DECLS;
242
243#endif /* DBUS_INTERNALS_H */
244