2003-04-17 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / dbus / dbus-internals.h
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
38 DBUS_BEGIN_DECLS;
39
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);
45
46 #ifdef DBUS_ENABLE_VERBOSE_MODE
47 #  define _dbus_verbose _dbus_verbose_real
48 #  define _dbus_verbose_reset _dbus_verbose_reset_real
49 #else
50 #  ifdef HAVE_ISO_VARARGS
51 #    define _dbus_verbose(...)
52 #  elif defined (HAVE_GNUC_VARARGS)
53 #    define _dbus_verbose(format...)
54 #  else
55 #    error "This compiler does not support varargs macros and thus verbose mode can't be disabled meaningfully"
56 #  endif
57 #  define _dbus_verbose_reset()
58 #endif /* !DBUS_ENABLE_VERBOSE_MODE */
59
60 const char* _dbus_strerror (int error_number);
61
62 #ifdef DBUS_DISABLE_ASSERT
63 #define _dbus_assert(condition)
64 #else
65 void _dbus_real_assert (dbus_bool_t  condition,
66                         const char  *condition_text,
67                         const char  *file,
68                         int          line);
69 #define _dbus_assert(condition)                                         \
70   _dbus_real_assert ((condition) != 0, #condition, __FILE__, __LINE__)
71 #endif /* !DBUS_DISABLE_ASSERT */
72
73 #ifdef DBUS_DISABLE_ASSERT
74 #define _dbus_assert_not_reached(explanation)
75 #else
76 void _dbus_real_assert_not_reached (const char *explanation,
77                                     const char *file,
78                                     int         line) _DBUS_GNUC_NORETURN;
79 #define _dbus_assert_not_reached(explanation)                                   \
80   _dbus_real_assert_not_reached (explanation, __FILE__, __LINE__)
81 #endif /* !DBUS_DISABLE_ASSERT */
82
83 #define _DBUS_N_ELEMENTS(array) ((int) (sizeof ((array)) / sizeof ((array)[0])))
84
85 #define _DBUS_POINTER_TO_INT(pointer) ((long)(pointer))
86 #define _DBUS_INT_TO_POINTER(integer) ((void*)((long)(integer)))
87
88 #define _DBUS_ZERO(object) (memset (&(object), '\0', sizeof ((object))))
89
90 #define _DBUS_STRUCT_OFFSET(struct_type, member)        \
91     ((long) ((unsigned char*) &((struct_type*) 0)->member))
92
93 #define _DBUS_ASSERT_ERROR_IS_SET(error)   _dbus_assert ((error) == NULL || dbus_error_is_set ((error)))
94 #define _DBUS_ASSERT_ERROR_IS_CLEAR(error) _dbus_assert ((error) == NULL || !dbus_error_is_set ((error)))
95
96 /* This alignment thing is from ORBit2 */
97 /* Align a value upward to a boundary, expressed as a number of bytes.
98  * E.g. align to an 8-byte boundary with argument of 8.
99  */
100
101 /*
102  *   (this + boundary - 1)
103  *          &
104  *    ~(boundary - 1)
105  */
106
107 #define _DBUS_ALIGN_VALUE(this, boundary) \
108   (( ((unsigned long)(this)) + (((unsigned long)(boundary)) -1)) & (~(((unsigned long)(boundary))-1)))
109
110 #define _DBUS_ALIGN_ADDRESS(this, boundary) \
111   ((void*)_DBUS_ALIGN_VALUE(this, boundary))
112
113 char*       _dbus_strdup                (const char  *str);
114 dbus_bool_t _dbus_string_array_contains (const char **array,
115                                          const char  *str);
116 char**      _dbus_dup_string_array      (const char **array);
117
118
119 #define _DBUS_INT_MIN   (-_DBUS_INT_MAX - 1)
120 #define _DBUS_INT_MAX   2147483647
121 #define _DBUS_UINT_MAX  0xffffffff
122 #define _DBUS_ONE_KILOBYTE 1024
123 #define _DBUS_ONE_MEGABYTE 1024 * _DBUS_ONE_KILOBYTE
124 #define _DBUS_ONE_HOUR_IN_MILLISECONDS (1000 * 60 * 60)
125 #define _DBUS_USEC_PER_SECOND          (1000000)
126
127 #undef  MAX
128 #define MAX(a, b)  (((a) > (b)) ? (a) : (b))
129
130 #undef  MIN
131 #define MIN(a, b)  (((a) < (b)) ? (a) : (b))
132
133 #undef  ABS
134 #define ABS(a)     (((a) < 0) ? -(a) : (a))
135
136 typedef void (* DBusForeachFunction) (void *element,
137                                       void *data);
138
139 dbus_bool_t _dbus_set_fd_nonblocking (int             fd,
140                                       DBusError      *error);
141
142 void _dbus_verbose_bytes           (const unsigned char *data,
143                                     int                  len);
144 void _dbus_verbose_bytes_of_string (const DBusString    *str,
145                                     int                  start,
146                                     int                  len);
147
148
149 const char* _dbus_type_to_string (int type);
150
151 extern const char _dbus_no_memory_message[];
152 #define _DBUS_SET_OOM(error) dbus_set_error ((error), DBUS_ERROR_NO_MEMORY, _dbus_no_memory_message)
153
154 #ifdef DBUS_BUILD_TESTS
155 /* Memory debugging */
156 void        _dbus_set_fail_alloc_counter        (int  until_next_fail);
157 int         _dbus_get_fail_alloc_counter        (void);
158 void        _dbus_set_fail_alloc_failures       (int  failures_per_failure);
159 int         _dbus_get_fail_alloc_failures       (void);
160 dbus_bool_t _dbus_decrement_fail_alloc_counter  (void);
161 dbus_bool_t _dbus_disable_mem_pools             (void);
162 int         _dbus_get_malloc_blocks_outstanding (void);
163
164 typedef dbus_bool_t (* DBusTestMemoryFunction)  (void *data);
165 dbus_bool_t _dbus_test_oom_handling (const char             *description,
166                                      DBusTestMemoryFunction  func,
167                                      void                   *data);
168 #else
169 #define _dbus_set_fail_alloc_counter(n)
170 #define _dbus_get_fail_alloc_counter _DBUS_INT_MAX
171
172 /* These are constant expressions so that blocks
173  * they protect should be optimized away
174  */
175 #define _dbus_decrement_fail_alloc_counter() (FALSE)
176 #define _dbus_disable_mem_pools()            (FALSE)
177 #define _dbus_get_malloc_blocks_outstanding  (0)
178 #endif /* !DBUS_BUILD_TESTS */
179
180 typedef void (* DBusShutdownFunction) (void *data);
181 dbus_bool_t _dbus_register_shutdown_func (DBusShutdownFunction  function,
182                                           void                 *data);
183
184 extern int _dbus_current_generation;
185
186 /* Thread initializers */
187 #define _DBUS_LOCK_NAME(name)           _dbus_lock_##name
188 #define _DBUS_DECLARE_GLOBAL_LOCK(name) extern DBusMutex  *_dbus_lock_##name
189 #define _DBUS_DEFINE_GLOBAL_LOCK(name)  DBusMutex         *_dbus_lock_##name  
190 #define _DBUS_LOCK(name)                dbus_mutex_lock   (_dbus_lock_##name)
191 #define _DBUS_UNLOCK(name)              dbus_mutex_unlock (_dbus_lock_##name)
192
193 _DBUS_DECLARE_GLOBAL_LOCK (list);
194 _DBUS_DECLARE_GLOBAL_LOCK (connection_slots);
195 _DBUS_DECLARE_GLOBAL_LOCK (server_slots);
196 _DBUS_DECLARE_GLOBAL_LOCK (atomic);
197 _DBUS_DECLARE_GLOBAL_LOCK (message_handler);
198 _DBUS_DECLARE_GLOBAL_LOCK (bus);
199 _DBUS_DECLARE_GLOBAL_LOCK (shutdown_funcs);
200 _DBUS_DECLARE_GLOBAL_LOCK (system_users);
201 #define _DBUS_N_GLOBAL_LOCKS (8)
202
203 dbus_bool_t _dbus_threads_init_debug (void);
204
205 DBUS_END_DECLS;
206
207 #endif /* DBUS_INTERNALS_H */