From 13f70ce711630d7f70c0b11fa57639fad95e4afd Mon Sep 17 00:00:00 2001 From: Havoc Pennington Date: Thu, 27 Feb 2003 04:17:44 +0000 Subject: 2003-02-27 Havoc Pennington * dbus/dbus-marshal.c (_dbus_demarshal_int32): rewrite to be much more inlined, using dbus-string-private.h, speeds things up substantially * dbus/dbus-string.c (_dbus_string_free): apply align offset when freeing the string (_dbus_string_steal_data): fix for align offset (undo_alignment): new function --- dbus/Makefile.am | 1 + dbus/dbus-marshal.c | 30 +++++++++++++----------- dbus/dbus-string-private.h | 58 ++++++++++++++++++++++++++++++++++++++++++++++ dbus/dbus-string.c | 41 ++++++++++++++++---------------- 4 files changed, 97 insertions(+), 33 deletions(-) create mode 100644 dbus/dbus-string-private.h (limited to 'dbus') diff --git a/dbus/Makefile.am b/dbus/Makefile.am index 7a064ce8..b0b001af 100644 --- a/dbus/Makefile.am +++ b/dbus/Makefile.am @@ -89,6 +89,7 @@ libdbus_convenience_la_SOURCES= \ dbus-message-builder.h \ dbus-string.c \ dbus-string.h \ + dbus-string-private.h \ dbus-sysdeps.c \ dbus-sysdeps.h diff --git a/dbus/dbus-marshal.c b/dbus/dbus-marshal.c index e29143ea..b199561b 100644 --- a/dbus/dbus-marshal.c +++ b/dbus/dbus-marshal.c @@ -23,6 +23,8 @@ #include "dbus-marshal.h" #include "dbus-internals.h" +#define DBUS_CAN_USE_DBUS_STRING_PRIVATE 1 +#include "dbus-string-private.h" #include @@ -538,20 +540,21 @@ _dbus_demarshal_double (const DBusString *str, */ dbus_int32_t _dbus_demarshal_int32 (const DBusString *str, - int byte_order, - int pos, - int *new_pos) + int byte_order, + int pos, + int *new_pos) { - const char *buffer; - + const DBusRealString *real = (const DBusRealString*) str; + pos = _DBUS_ALIGN_VALUE (pos, sizeof (dbus_int32_t)); - _dbus_string_get_const_data_len (str, &buffer, pos, sizeof (dbus_int32_t)); - if (new_pos) *new_pos = pos + sizeof (dbus_int32_t); - return _dbus_unpack_int32 (byte_order, buffer); + if (byte_order == DBUS_LITTLE_ENDIAN) + return DBUS_INT32_FROM_LE (*(dbus_int32_t*)(real->str + pos)); + else + return DBUS_INT32_FROM_BE (*(dbus_int32_t*)(real->str + pos)); } /** @@ -569,16 +572,17 @@ _dbus_demarshal_uint32 (const DBusString *str, int pos, int *new_pos) { - const char *buffer; - + const DBusRealString *real = (const DBusRealString*) str; + pos = _DBUS_ALIGN_VALUE (pos, sizeof (dbus_uint32_t)); - _dbus_string_get_const_data_len (str, &buffer, pos, sizeof (dbus_uint32_t)); - if (new_pos) *new_pos = pos + sizeof (dbus_uint32_t); - return _dbus_unpack_uint32 (byte_order, buffer); + if (byte_order == DBUS_LITTLE_ENDIAN) + return DBUS_UINT32_FROM_LE (*(dbus_uint32_t*)(real->str + pos)); + else + return DBUS_UINT32_FROM_BE (*(dbus_uint32_t*)(real->str + pos)); } /** diff --git a/dbus/dbus-string-private.h b/dbus/dbus-string-private.h new file mode 100644 index 00000000..a030674b --- /dev/null +++ b/dbus/dbus-string-private.h @@ -0,0 +1,58 @@ +/* -*- mode: C; c-file-style: "gnu" -*- */ +/* dbus-string-private.h String utility class (internal to D-BUS implementation) + * + * Copyright (C) 2002, 2003 Red Hat, Inc. + * + * Licensed under the Academic Free License version 1.2 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#ifndef DBUS_STRING_PRIVATE_H +#define DBUS_STRING_PRIVATE_H + +#include + +#include +#include + +#ifndef DBUS_CAN_USE_DBUS_STRING_PRIVATE +#error "Don't go including dbus-string-private.h for no good reason" +#endif + +DBUS_BEGIN_DECLS; + +/** + * @brief Internals of DBusString. + * + * DBusString internals. DBusString is an opaque objects, it must be + * used via accessor functions. + */ +typedef struct +{ + unsigned char *str; /**< String data, plus nul termination */ + int len; /**< Length without nul */ + int allocated; /**< Allocated size of data */ + int max_length; /**< Max length of this string, without nul byte */ + unsigned int constant : 1; /**< String data is not owned by DBusString */ + unsigned int locked : 1; /**< DBusString has been locked and can't be changed */ + unsigned int invalid : 1; /**< DBusString is invalid (e.g. already freed) */ + unsigned int align_offset : 3; /**< str - align_offset is the actual malloc block */ +} DBusRealString; + +DBUS_END_DECLS; + +#endif /* DBUS_STRING_PRIVATE_H */ diff --git a/dbus/dbus-string.c b/dbus/dbus-string.c index 22d000da..f19a30db 100644 --- a/dbus/dbus-string.c +++ b/dbus/dbus-string.c @@ -25,6 +25,9 @@ #include "dbus-string.h" /* we allow a system header here, for speed/convenience */ #include +#include "dbus-marshal.h" +#define DBUS_CAN_USE_DBUS_STRING_PRIVATE 1 +#include "dbus-string-private.h" /** * @defgroup DBusString string class @@ -63,24 +66,6 @@ * @{ */ -/** - * @brief Internals of DBusString. - * - * DBusString internals. DBusString is an opaque objects, it must be - * used via accessor functions. - */ -typedef struct -{ - unsigned char *str; /**< String data, plus nul termination */ - int len; /**< Length without nul */ - int allocated; /**< Allocated size of data */ - int max_length; /**< Max length of this string, without nul byte */ - unsigned int constant : 1; /**< String data is not owned by DBusString */ - unsigned int locked : 1; /**< DBusString has been locked and can't be changed */ - unsigned int invalid : 1; /**< DBusString is invalid (e.g. already freed) */ - unsigned int align_offset : 3; /**< str - align_offset is the actual malloc block */ -} DBusRealString; - /** * We allocate 1 byte for nul termination, plus 7 bytes for possible * align_offset, so we always need 8 bytes on top of the string's @@ -168,6 +153,20 @@ fixup_alignment (DBusRealString *real) _dbus_assert (_DBUS_ALIGN_ADDRESS (real->str, 8) == real->str); } +static void +undo_alignment (DBusRealString *real) +{ + if (real->align_offset != 0) + { + memmove (real->str - real->align_offset, + real->str, + real->len + 1); + + real->align_offset = 0; + real->str = real->str - real->align_offset; + } +} + /** * Initializes a string. The maximum length may be _DBUS_INT_MAX for * no maximum. The string starts life with zero length. @@ -296,7 +295,7 @@ _dbus_string_free (DBusString *str) if (real->constant) return; - dbus_free (real->str); + dbus_free (real->str - real->align_offset); real->invalid = TRUE; } @@ -577,6 +576,8 @@ _dbus_string_steal_data (DBusString *str, { DBUS_STRING_PREAMBLE (str); _dbus_assert (data_return != NULL); + + undo_alignment (real); *data_return = real->str; @@ -586,6 +587,7 @@ _dbus_string_steal_data (DBusString *str, /* hrm, put it back then */ real->str = *data_return; *data_return = NULL; + fixup_alignment (real); return FALSE; } @@ -2447,7 +2449,6 @@ _dbus_string_zero (DBusString *str) memset (real->str, '\0', real->allocated); } - /** @} */ #ifdef DBUS_BUILD_TESTS -- cgit