dbus-internals.h revision d6e1b2adb3d8e51ce1bb47295cef12d9fe1a15a8
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_getpid (), _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_getpid (), _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);
147void*       _dbus_memdup                (const void  *mem,
148                                         size_t       n_bytes);
149dbus_bool_t _dbus_string_array_contains (const char **array,
150                                         const char  *str);
151char**      _dbus_dup_string_array      (const char **array);
152
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)
159#endif
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)
164
165#undef	MAX
166#define MAX(a, b)  (((a) > (b)) ? (a) : (b))
167
168#undef	MIN
169#define MIN(a, b)  (((a) < (b)) ? (a) : (b))
170
171#undef	ABS
172#define ABS(a)	   (((a) < 0) ? -(a) : (a))
173
174typedef void (* DBusForeachFunction) (void *element,
175                                      void *data);
176
177dbus_bool_t _dbus_set_fd_nonblocking (int             fd,
178                                      DBusError      *error);
179
180void _dbus_verbose_bytes           (const unsigned char *data,
181                                    int                  len);
182void _dbus_verbose_bytes_of_string (const DBusString    *str,
183                                    int                  start,
184                                    int                  len);
185
186
187const char* _dbus_type_to_string         (int type);
188const char* _dbus_header_field_to_string (int header_field);
189
190extern const char _dbus_no_memory_message[];
191#define _DBUS_SET_OOM(error) dbus_set_error ((error), DBUS_ERROR_NO_MEMORY, _dbus_no_memory_message)
192
193#ifdef DBUS_BUILD_TESTS
194/* Memory debugging */
195void        _dbus_set_fail_alloc_counter        (int  until_next_fail);
196int         _dbus_get_fail_alloc_counter        (void);
197void        _dbus_set_fail_alloc_failures       (int  failures_per_failure);
198int         _dbus_get_fail_alloc_failures       (void);
199dbus_bool_t _dbus_decrement_fail_alloc_counter  (void);
200dbus_bool_t _dbus_disable_mem_pools             (void);
201int         _dbus_get_malloc_blocks_outstanding (void);
202
203typedef dbus_bool_t (* DBusTestMemoryFunction)  (void *data);
204dbus_bool_t _dbus_test_oom_handling (const char             *description,
205                                     DBusTestMemoryFunction  func,
206                                     void                   *data);
207#else
208#define _dbus_set_fail_alloc_counter(n)
209#define _dbus_get_fail_alloc_counter _DBUS_INT_MAX
210
211/* These are constant expressions so that blocks
212 * they protect should be optimized away
213 */
214#define _dbus_decrement_fail_alloc_counter() (FALSE)
215#define _dbus_disable_mem_pools()            (FALSE)
216#define _dbus_get_malloc_blocks_outstanding  (0)
217#endif /* !DBUS_BUILD_TESTS */
218
219typedef void (* DBusShutdownFunction) (void *data);
220dbus_bool_t _dbus_register_shutdown_func (DBusShutdownFunction  function,
221                                          void                 *data);
222
223extern int _dbus_current_generation;
224
225/* Thread initializers */
226#define _DBUS_LOCK_NAME(name)           _dbus_lock_##name
227#define _DBUS_DECLARE_GLOBAL_LOCK(name) extern DBusMutex  *_dbus_lock_##name
228#define _DBUS_DEFINE_GLOBAL_LOCK(name)  DBusMutex         *_dbus_lock_##name
229#define _DBUS_LOCK(name)                dbus_mutex_lock   (_dbus_lock_##name)
230#define _DBUS_UNLOCK(name)              dbus_mutex_unlock (_dbus_lock_##name)
231
232_DBUS_DECLARE_GLOBAL_LOCK (list);
233_DBUS_DECLARE_GLOBAL_LOCK (connection_slots);
234_DBUS_DECLARE_GLOBAL_LOCK (pending_call_slots);
235_DBUS_DECLARE_GLOBAL_LOCK (server_slots);
236_DBUS_DECLARE_GLOBAL_LOCK (message_slots);
237_DBUS_DECLARE_GLOBAL_LOCK (atomic);
238_DBUS_DECLARE_GLOBAL_LOCK (bus);
239_DBUS_DECLARE_GLOBAL_LOCK (shutdown_funcs);
240_DBUS_DECLARE_GLOBAL_LOCK (system_users);
241#define _DBUS_N_GLOBAL_LOCKS (9)
242
243dbus_bool_t _dbus_threads_init_debug (void);
244
245DBUS_END_DECLS;
246
247#endif /* DBUS_INTERNALS_H */
248