From 4f48a8d4b910039d6a0001e36222bccace510e21 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Mon, 19 Sep 2011 17:08:31 +0100 Subject: [PATCH 3/9] DBusMessageIter: only set up the type writer when we open the signature This removes the only place where _dbus_type_writer_add_types and friends were needed, except for inside DBusMessage (when realigning). --- dbus/dbus-message.c | 46 ++++++++++++++++++++++++++++++++++------------ 1 files changed, 34 insertions(+), 12 deletions(-) diff --git a/dbus/dbus-message.c b/dbus/dbus-message.c index 5d7f62c..4a4593a 100644 --- a/dbus/dbus-message.c +++ b/dbus/dbus-message.c @@ -81,7 +81,7 @@ struct DBusMessageRealIter dbus_uint32_t sig_refcount : 8; /**< depth of open_signature() */ union { - DBusTypeWriter writer; /**< writer */ + DBusTypeWriter writer; /**< writer, only valid if sig_refcount >= 1 */ DBusTypeReader reader; /**< reader */ } u; /**< the type writer or reader that does all the work */ }; @@ -692,6 +692,8 @@ _dbus_message_iter_check (DBusMessageRealIter *iter) { if (iter->sig_refcount > 0) { + _DBUS_ASSERT_TYPE_WRITER (&iter->u.writer); + if (iter->u.writer.byte_order != byte_order) { _dbus_warn_check_failed ("dbus message changed byte order since append iterator was created\n"); @@ -2304,14 +2306,11 @@ dbus_message_iter_init_append (DBusMessage *message, _dbus_message_iter_init_common (message, real, DBUS_MESSAGE_ITER_TYPE_WRITER); - /* We create the signature string and point iterators at it "on demand" + /* We create the signature string and init the writer with it "on demand" * when a value is actually appended. That means that init() never fails * due to OOM. */ - _dbus_type_writer_init_types_delayed (&real->u.writer, - _dbus_header_get_byte_order (&message->header), - &message->body, - _dbus_string_get_length (&message->body)); + _dbus_type_writer_clear (&real->u.writer); } /** @@ -2331,9 +2330,10 @@ _dbus_message_iter_open_signature (DBusMessageRealIter *real) _dbus_assert (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER); - if (real->u.writer.type_str != NULL) + if (real->sig_refcount > 0) { - _dbus_assert (real->sig_refcount > 0); + _DBUS_ASSERT_TYPE_WRITER (&real->u.writer); + _dbus_assert (real->u.writer.type_str != NULL); real->sig_refcount += 1; return TRUE; } @@ -2379,8 +2379,13 @@ _dbus_message_iter_open_signature (DBusMessageRealIter *real) real->sig_refcount = 1; - _dbus_type_writer_add_types (&real->u.writer, - str, _dbus_string_get_length (str)); + _dbus_type_writer_init (&real->u.writer, + _dbus_header_get_byte_order (&real->message->header), + str, + _dbus_string_get_length (str), + &real->message->body, + _dbus_string_get_length (&real->message->body)); + _DBUS_ASSERT_TYPE_WRITER (&real->u.writer); return TRUE; } @@ -2403,6 +2408,7 @@ _dbus_message_iter_close_signature (DBusMessageRealIter *real) _dbus_assert (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER); _dbus_assert (real->u.writer.type_str != NULL); _dbus_assert (real->sig_refcount > 0); + _DBUS_ASSERT_TYPE_WRITER (&real->u.writer); real->sig_refcount -= 1; @@ -2421,7 +2427,7 @@ _dbus_message_iter_close_signature (DBusMessageRealIter *real) &v_STRING)) retval = FALSE; - _dbus_type_writer_remove_types (&real->u.writer); + _dbus_type_writer_clear (&real->u.writer); _dbus_string_free (str); dbus_free (str); @@ -2443,6 +2449,7 @@ _dbus_message_iter_abandon_signature (DBusMessageRealIter *real) _dbus_assert (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER); _dbus_assert (real->u.writer.type_str != NULL); _dbus_assert (real->sig_refcount > 0); + _DBUS_ASSERT_TYPE_WRITER (&real->u.writer); real->sig_refcount -= 1; @@ -2452,7 +2459,7 @@ _dbus_message_iter_abandon_signature (DBusMessageRealIter *real) str = real->u.writer.type_str; - _dbus_type_writer_remove_types (&real->u.writer); + _dbus_type_writer_clear (&real->u.writer); _dbus_string_free (str); dbus_free (str); } @@ -2576,6 +2583,8 @@ dbus_message_iter_append_basic (DBusMessageIter *iter, if (!_dbus_message_iter_open_signature (real)) return FALSE; + _DBUS_ASSERT_TYPE_WRITER (&real->u.writer); + if (type == DBUS_TYPE_UNIX_FD) { #ifdef HAVE_UNIX_FD_PASSING @@ -2680,6 +2689,10 @@ dbus_message_iter_append_fixed_array (DBusMessageIter *iter, _dbus_return_val_if_fail (n_elements <= DBUS_MAXIMUM_ARRAY_LENGTH / _dbus_type_get_alignment (element_type), FALSE); + /* if the caller has opened a container, then this must be true */ + _dbus_return_val_if_fail (real->sig_refcount > 0, FALSE); + /* sig_refcount > 0 guarantees this, so it's an assert, not a check */ + _DBUS_ASSERT_TYPE_WRITER (&real->u.writer); #ifndef DBUS_DISABLE_CHECKS if (element_type == DBUS_TYPE_BOOLEAN) @@ -2755,6 +2768,8 @@ dbus_message_iter_open_container (DBusMessageIter *iter, if (!_dbus_message_iter_open_signature (real)) return FALSE; + _DBUS_ASSERT_TYPE_WRITER (&real->u.writer); + *real_sub = *real; if (contained_signature != NULL) @@ -2799,8 +2814,15 @@ dbus_message_iter_close_container (DBusMessageIter *iter, _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE); _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE); + /* if the caller has opened a container, then this must be true */ + _dbus_return_val_if_fail (real->sig_refcount > 0, FALSE); + /* sig_refcount > 0 guarantees this, so it's an assert, not a check */ + _DBUS_ASSERT_TYPE_WRITER (&real->u.writer); + _dbus_return_val_if_fail (_dbus_message_iter_append_check (real_sub), FALSE); _dbus_return_val_if_fail (real_sub->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE); + _dbus_return_val_if_fail (real_sub->sig_refcount > 0, FALSE); + _DBUS_ASSERT_TYPE_WRITER (&real_sub->u.writer); ret = _dbus_type_writer_unrecurse (&real->u.writer, &real_sub->u.writer); -- 1.7.6.3