2003-02-16 Anders Carlsson <andersca@codefactory.se>
[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  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
37 DBUS_BEGIN_DECLS;
38
39 void _dbus_warn         (const char *format,
40                          ...);
41 void _dbus_verbose_real (const char *format,
42                          ...);
43
44
45 #ifdef DBUS_ENABLE_VERBOSE_MODE
46 #  define _dbus_verbose _dbus_verbose_real
47 #else
48 #  ifdef HAVE_ISO_VARARGS
49 #    define _dbus_verbose(...)
50 #  elif defined (HAVE_GNUC_VARARGS)
51 #    define _dbus_verbose(format...)
52 #  else
53 #    error "This compiler does not support varargs macros and thus verbose mode can't be disabled meaningfully"
54 #  endif
55 #endif /* !DBUS_ENABLE_VERBOSE_MODE */
56
57 const char* _dbus_strerror (int error_number);
58
59 DBusResultCode _dbus_result_from_errno (int error_number);
60
61 #ifdef DBUS_DISABLE_ASSERT
62 #define _dbus_assert(condition)
63 #else
64 #define _dbus_assert(condition)                                         \
65 do {                                                                    \
66   if (!(condition))                                                     \
67     {                                                                   \
68       _dbus_warn ("Assertion failed \"%s\" file \"%s\" line %d\n",      \
69                   #condition, __FILE__, __LINE__);                      \
70       _dbus_abort ();                                                   \
71     }                                                                   \
72 } while (0)
73 #endif /* !DBUS_DISABLE_ASSERT */
74
75 #ifdef DBUS_DISABLE_ASSERT
76 #define _dbus_assert_not_reached(explanation)
77 #else
78 #define _dbus_assert_not_reached(explanation)                                   \
79 do {                                                                            \
80     _dbus_warn ("File \"%s\" line %d should not have been reached: %s\n",       \
81                __FILE__, __LINE__, (explanation));                              \
82     _dbus_abort ();                                                             \
83 } while (0)
84 #endif /* !DBUS_DISABLE_ASSERT */
85
86 #define _DBUS_N_ELEMENTS(array) ((int) (sizeof ((array)) / sizeof ((array)[0])))
87
88 #define _DBUS_POINTER_TO_INT(pointer) ((long)(pointer))
89 #define _DBUS_INT_TO_POINTER(integer) ((void*)((long)(integer)))
90
91 #define _DBUS_ZERO(object) (memset (&(object), '\0', sizeof ((object))))
92
93 #define _DBUS_STRUCT_OFFSET(struct_type, member)        \
94     ((long) ((unsigned char*) &((struct_type*) 0)->member))
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
115 #define _DBUS_INT_MIN   (-_DBUS_INT_MAX - 1)
116 #define _DBUS_INT_MAX   2147483647
117 #define _DBUS_MAX_SUN_PATH_LENGTH 99
118 #define _DBUS_ONE_KILOBYTE 1024
119 #define _DBUS_ONE_MEGABYTE 1024 * _DBUS_ONE_KILOBYTE
120
121 #undef  MAX
122 #define MAX(a, b)  (((a) > (b)) ? (a) : (b))
123
124 #undef  MIN
125 #define MIN(a, b)  (((a) < (b)) ? (a) : (b))
126
127 #undef  ABS
128 #define ABS(a)     (((a) < 0) ? -(a) : (a))
129
130 typedef void (* DBusForeachFunction) (void *element,
131                                       void *data);
132
133 dbus_bool_t _dbus_set_fd_nonblocking (int             fd,
134                                       DBusResultCode *result);
135
136 void _dbus_verbose_bytes           (const unsigned char *data,
137                                     int                  len);
138 void _dbus_verbose_bytes_of_string (const DBusString    *str,
139                                     int                  start,
140                                     int                  len);
141
142
143 const char* _dbus_type_to_string (int type);
144
145 #ifdef DBUS_BUILD_TESTS
146 /* Memory debugging */
147 void        _dbus_set_fail_alloc_counter       (int  until_next_fail);
148 int         _dbus_get_fail_alloc_counter       (void);
149 dbus_bool_t _dbus_decrement_fail_alloc_counter (void);
150 #else
151 #define _dbus_set_fail_alloc_counter(n)
152 #define _dbus_get_fail_alloc_counter _DBUS_INT_MAX
153 #define _dbus_decrement_fail_alloc_counter() FALSE
154 #endif /* !DBUS_BUILD_TESTS */
155
156 DBUS_END_DECLS;
157
158 #endif /* DBUS_INTERNALS_H */