1 From 467a2e307462b02865b35397c572b6048c2934e2 Mon Sep 17 00:00:00 2001
2 From: Aleksandar Kanchev <kanchev@itestra.com>
3 Date: Mon, 9 Jul 2012 18:09:04 +0200
4 Subject: [PATCH] DBusMessage: add support for custom marshaling
6 Add functions to support querying and manipulating the message body and
7 signature. This is useful for code generators, which can generate custom
8 marshaling functions based on a given IDL. Those functions tend to be
9 optimized and faster than the generic iterator based marshaling.
11 dbus/dbus-message.c | 143 +++++++++++++++++++++++++++++++++++----------------
12 dbus/dbus-message.h | 13 +++++
13 dbus/dbus-string.c | 16 ++++++
14 dbus/dbus-string.h | 4 ++
15 4 files changed, 132 insertions(+), 44 deletions(-)
17 diff --git a/dbus/dbus-message.c b/dbus/dbus-message.c
18 index 24ef5ac..fbacfba 100644
19 --- a/dbus/dbus-message.c
20 +++ b/dbus/dbus-message.c
21 @@ -356,50 +356,6 @@ set_or_delete_string_field (DBusMessage *message,
26 -/* Probably we don't need to use this */
28 - * Sets the signature of the message, i.e. the arguments in the
29 - * message payload. The signature includes only "in" arguments for
30 - * #DBUS_MESSAGE_TYPE_METHOD_CALL and only "out" arguments for
31 - * #DBUS_MESSAGE_TYPE_METHOD_RETURN, so is slightly different from
32 - * what you might expect (it does not include the signature of the
33 - * entire C++-style method).
35 - * The signature is a string made up of type codes such as
36 - * #DBUS_TYPE_INT32. The string is terminated with nul (nul is also
37 - * the value of #DBUS_TYPE_INVALID). The macros such as
38 - * #DBUS_TYPE_INT32 evaluate to integers; to assemble a signature you
39 - * may find it useful to use the string forms, such as
40 - * #DBUS_TYPE_INT32_AS_STRING.
42 - * An "unset" or #NULL signature is considered the same as an empty
43 - * signature. In fact dbus_message_get_signature() will never return
46 - * @param message the message
47 - * @param signature the type signature or #NULL to unset
48 - * @returns #FALSE if no memory
51 -_dbus_message_set_signature (DBusMessage *message,
52 - const char *signature)
54 - _dbus_return_val_if_fail (message != NULL, FALSE);
55 - _dbus_return_val_if_fail (!message->locked, FALSE);
56 - _dbus_return_val_if_fail (signature == NULL ||
57 - _dbus_check_is_valid_signature (signature));
58 - /* can't delete the signature if you have a message body */
59 - _dbus_return_val_if_fail (_dbus_string_get_length (&message->body) == 0 ||
62 - return set_or_delete_string_field (message,
63 - DBUS_HEADER_FIELD_SIGNATURE,
64 - DBUS_TYPE_SIGNATURE,
71 * We cache some DBusMessage to reduce the overhead of allocating
72 @@ -3370,6 +3326,47 @@ dbus_message_get_sender (DBusMessage *message)
76 + * Sets the signature of the message, i.e. the arguments in the
77 + * message payload. The signature includes only "in" arguments for
78 + * #DBUS_MESSAGE_TYPE_METHOD_CALL and only "out" arguments for
79 + * #DBUS_MESSAGE_TYPE_METHOD_RETURN, so is slightly different from
80 + * what you might expect (it does not include the signature of the
81 + * entire C++-style method).
83 + * The signature is a string made up of type codes such as
84 + * #DBUS_TYPE_INT32. The string is terminated with nul (nul is also
85 + * the value of #DBUS_TYPE_INVALID). The macros such as
86 + * #DBUS_TYPE_INT32 evaluate to integers; to assemble a signature you
87 + * may find it useful to use the string forms, such as
88 + * #DBUS_TYPE_INT32_AS_STRING.
90 + * An "unset" or #NULL signature is considered the same as an empty
91 + * signature. In fact dbus_message_get_signature() will never return
94 + * @param message the message
95 + * @param signature the type signature or #NULL to unset
96 + * @returns #FALSE if no memory
99 +dbus_message_set_signature (DBusMessage *message,
100 + const char *signature)
102 + _dbus_return_val_if_fail (message != NULL, FALSE);
103 + _dbus_return_val_if_fail (!message->locked, FALSE);
104 + _dbus_return_val_if_fail (signature == NULL ||
105 + _dbus_check_is_valid_signature (signature), FALSE);
106 + /* can't delete the signature if you have a message body */
107 + _dbus_return_val_if_fail (_dbus_string_get_length (&message->body) == 0 ||
108 + signature != NULL, FALSE);
110 + return set_or_delete_string_field (message,
111 + DBUS_HEADER_FIELD_SIGNATURE,
112 + DBUS_TYPE_SIGNATURE,
117 * Gets the type signature of the message, i.e. the arguments in the
118 * message payload. The signature includes only "in" arguments for
119 * #DBUS_MESSAGE_TYPE_METHOD_CALL and only "out" arguments for
120 @@ -4519,6 +4516,64 @@ dbus_message_type_to_string (int type)
124 + * Returns pointer to the buffer used to store the message body.
126 + * @param message the message
127 + * @return pointer to the message body memory
130 +dbus_message_get_body (DBusMessage *message) {
131 + _dbus_return_val_if_fail (message != NULL, NULL);
133 + return _dbus_string_get_data(&(message->body));
137 + * Adjust the length of the message body buffer. The memory will be reallocated
138 + * if the new length is bigger than the already allocated size.
140 + * @see dbus_message_get_body_allocated
141 + * @param message the message
142 + * @param length the new length of the body
143 + * @return #TRUE if successful
146 +dbus_message_set_body_length (DBusMessage *message,
148 + _dbus_return_val_if_fail (message != NULL, FALSE);
149 + _dbus_return_val_if_fail (length >= 0, FALSE);
151 + return _dbus_string_set_length(&(message->body), length);
155 + * Gets the length of the message body buffer.
157 + * @param message the message
158 + * @param length the new length of the body
159 + * @return the length of the body buffer
162 +dbus_message_get_body_length (DBusMessage *message) {
163 + _dbus_return_val_if_fail (message != NULL, 0);
165 + return _dbus_string_get_length(&(message->body));
169 + * Gets the allocated memory size used to hold the message body.
171 + * @param message the message
172 + * @return size of the allocated message body memory
175 +dbus_message_get_body_allocated (DBusMessage *message) {
176 + _dbus_return_val_if_fail (message != NULL, 0);
178 + return _dbus_string_get_allocated(&(message->body));
182 * Turn a DBusMessage into the marshalled form as described in the D-Bus
185 diff --git a/dbus/dbus-message.h b/dbus/dbus-message.h
186 index 5500492..55388ac 100644
187 --- a/dbus/dbus-message.h
188 +++ b/dbus/dbus-message.h
189 @@ -138,6 +138,9 @@ dbus_bool_t dbus_message_set_sender (DBusMessage *message,
191 const char* dbus_message_get_sender (DBusMessage *message);
193 +dbus_bool_t dbus_message_set_signature (DBusMessage *message,
194 + const char *signature);
196 const char* dbus_message_get_signature (DBusMessage *message);
198 void dbus_message_set_no_reply (DBusMessage *message,
199 @@ -264,6 +267,16 @@ void dbus_message_iter_abandon_container (DBusMessageIter *iter,
200 DBusMessageIter *sub);
203 +char* dbus_message_get_body (DBusMessage *message);
205 +dbus_bool_t dbus_message_set_body_length (DBusMessage *message,
208 +int dbus_message_get_body_length (DBusMessage *message);
210 +int dbus_message_get_body_allocated (DBusMessage *message);
213 void dbus_message_lock (DBusMessage *message);
216 diff --git a/dbus/dbus-string.c b/dbus/dbus-string.c
217 index e2eb93b..d8628ba 100644
218 --- a/dbus/dbus-string.c
219 +++ b/dbus/dbus-string.c
220 @@ -848,6 +848,22 @@ _dbus_string_get_length (const DBusString *str)
222 #endif /* !_dbus_string_get_length */
224 +/* Only have the function if we don't have the macro */
225 +#ifndef _dbus_string_get_allocated
227 + * Gets the allocated length of a string (not including nul termination).
229 + * @returns the allocated length.
232 +_dbus_string_get_allocated(const DBusString *str)
234 + DBUS_CONST_STRING_PREAMBLE (str);
236 + return real->allocated;
238 +#endif /* !_dbus_string_get_allocated */
241 * Makes a string longer by the given number of bytes. Checks whether
242 * adding additional_length to the current length would overflow an
243 diff --git a/dbus/dbus-string.h b/dbus/dbus-string.h
244 index 2f1ed31..490aea6 100644
245 --- a/dbus/dbus-string.h
246 +++ b/dbus/dbus-string.h
247 @@ -62,6 +62,7 @@ struct DBusString
249 #define _dbus_string_get_data(s) ((char*)(((DBusString*)(s))->dummy1))
250 #define _dbus_string_get_length(s) (((DBusString*)(s))->dummy2)
251 +#define _dbus_string_get_allocated(s) (((DBusString*)(s))->dummy3 - _DBUS_STRING_ALLOCATION_PADDING)
252 #define _dbus_string_set_byte(s, i, b) ((((unsigned char*)(((DBusString*)(s))->dummy1))[(i)]) = (unsigned char) (b))
253 #define _dbus_string_get_byte(s, i) (((const unsigned char*)(((DBusString*)(s))->dummy1))[(i)])
254 #define _dbus_string_get_const_data(s) ((const char*)(((DBusString*)(s))->dummy1))
255 @@ -131,6 +132,9 @@ void _dbus_string_copy_to_buffer_with_nul (const DBusString *str,
256 #ifndef _dbus_string_get_length
257 int _dbus_string_get_length (const DBusString *str);
258 #endif /* !_dbus_string_get_length */
259 +#ifndef _dbus_string_get_allocated
260 +int _dbus_string_get_allocated (const DBusString *str);
261 +#endif /* !_dbus_string_get_allocated */
263 dbus_bool_t _dbus_string_lengthen (DBusString *str,
264 int additional_length);