summaryrefslogtreecommitdiffstats
path: root/dbus/dbus-string-util.c
diff options
context:
space:
mode:
authorRyan Lortie <desrt@desrt.ca>2007-09-20 00:13:35 -0400
committerRyan Lortie <desrt@desrt.ca>2007-09-20 00:13:35 -0400
commit8c6b0ab3f7e437362112eeaf83a566475b85d27c (patch)
treea8e56fe093b9597bf69c1b4c9144ba8490a1c3da /dbus/dbus-string-util.c
parent44ed90c10d435f1a12c2b4b1a601040fa585ce38 (diff)
Add support for compacting DBusStrings to release wasted memory.
2007-09-19 Ryan Lortie <desrt@desrt.ca> * dbus/dbus-string.[ch] (compact, _dbus_string_compact, _dbus_string_lock): new compact function to free up allocated memory that is no longer used. * dbus/dbus-message.c (load_message): call _dbus_string_compact on the message loader buffer. * dbus/dbus-transport-socket.c (do_reading, do_writing): call _dbus_string_compact on the incoming/outgoing "encoded" buffers. * dbus/dbus-string-util.c (_dbus_string_test): add a few tests for string compacting.
Diffstat (limited to 'dbus/dbus-string-util.c')
-rw-r--r--dbus/dbus-string-util.c56
1 files changed, 56 insertions, 0 deletions
diff --git a/dbus/dbus-string-util.c b/dbus/dbus-string-util.c
index cee02c4c..492c5289 100644
--- a/dbus/dbus-string-util.c
+++ b/dbus/dbus-string-util.c
@@ -790,6 +790,62 @@ _dbus_string_test (void)
_dbus_string_free (&str);
_dbus_string_free (&line);
}
+
+ {
+ if (!_dbus_string_init (&str))
+ _dbus_assert_not_reached ("no memory");
+
+ for (i = 0; i < 10000; i++)
+ if (!_dbus_string_append (&str, "abcdefghijklmnopqrstuvwxyz"))
+ _dbus_assert_not_reached ("no memory");
+
+ if (!_dbus_string_set_length (&str, 10))
+ _dbus_assert_not_reached ("failed to set length");
+
+ /* actually compact */
+ if (!_dbus_string_compact (&str, 2048))
+ _dbus_assert_not_reached ("failed to compact after set_length");
+
+ /* peek inside to make sure it worked */
+ if (((DBusRealString *)&str)->allocated > 30)
+ _dbus_assert_not_reached ("compacting string didn't do anything");
+
+ if (!_dbus_string_equal_c_str (&str, "abcdefghij"))
+ _dbus_assert_not_reached ("unexpected content after compact");
+
+ /* compact nothing */
+ if (!_dbus_string_compact (&str, 2048))
+ _dbus_assert_not_reached ("failed to compact 2nd time");
+
+ if (!_dbus_string_equal_c_str (&str, "abcdefghij"))
+ _dbus_assert_not_reached ("unexpected content after 2nd compact");
+
+ /* and make sure it still works...*/
+ if (!_dbus_string_append (&str, "123456"))
+ _dbus_assert_not_reached ("failed to append after compact");
+
+ if (!_dbus_string_equal_c_str (&str, "abcdefghij123456"))
+ _dbus_assert_not_reached ("unexpected content after append");
+
+ /* after growing automatically, this should do nothing */
+ if (!_dbus_string_compact (&str, 20000))
+ _dbus_assert_not_reached ("failed to compact after grow");
+
+ /* but this one will do something */
+ if (!_dbus_string_compact (&str, 0))
+ _dbus_assert_not_reached ("failed to compact after grow");
+
+ if (!_dbus_string_equal_c_str (&str, "abcdefghij123456"))
+ _dbus_assert_not_reached ("unexpected content");
+
+ if (!_dbus_string_append (&str, "!@#$%"))
+ _dbus_assert_not_reached ("failed to append after compact");
+
+ if (!_dbus_string_equal_c_str (&str, "abcdefghij123456!@#$%"))
+ _dbus_assert_not_reached ("unexpected content");
+
+ _dbus_string_free (&str);
+ }
return TRUE;
}