summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authordeanm@google.com <deanm@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-08-07 13:40:16 +0000
committerdeanm@google.com <deanm@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-08-07 13:40:16 +0000
commitd26cd52757991911c111279e900d732ba283dd05 (patch)
tree1ed17eca0119921798175038aa8e3786902ebf57
parentf1f642879ba2c3e7edb3af50d70ea11286f3ae93 (diff)
downloadchromium_src-d26cd52757991911c111279e900d732ba283dd05.zip
chromium_src-d26cd52757991911c111279e900d732ba283dd05.tar.gz
chromium_src-d26cd52757991911c111279e900d732ba283dd05.tar.bz2
Some cross platform changes and general cleanups to Pickle.
- Clean up TrimWriteData, and remove the unneeded VariableLengthBuffer struct. Modify a test to slightly test TrimWriteData, but it probably deserves more. - Remove unneeded includes in pickle_unittest, including windows.h. - According to 3.5 of the C++ standard, CustomHeader could not be used as a template argument, because it had no linkage. This now builds on GCC. git-svn-id: svn://svn.chromium.org/chrome/trunk/src@508 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--base/pickle.cc19
-rw-r--r--base/pickle.h6
-rw-r--r--base/pickle_unittest.cc26
3 files changed, 26 insertions, 25 deletions
diff --git a/base/pickle.cc b/base/pickle.cc
index 0fb1d9c..356d5df 100644
--- a/base/pickle.cc
+++ b/base/pickle.cc
@@ -309,20 +309,21 @@ char* Pickle::BeginWriteData(int length) {
return data_ptr;
}
-void Pickle::TrimWriteData(int length) {
+void Pickle::TrimWriteData(int new_length) {
DCHECK(variable_buffer_offset_ != 0);
- VariableLengthBuffer *buffer = reinterpret_cast<VariableLengthBuffer*>(
+ // Fetch the the variable buffer size
+ int* cur_length = reinterpret_cast<int*>(
reinterpret_cast<char*>(header_) + variable_buffer_offset_);
- DCHECK_GE(buffer->length, length);
-
- int old_length = buffer->length;
- int trimmed_bytes = old_length - length;
- if (trimmed_bytes > 0) {
- header_->payload_size -= trimmed_bytes;
- buffer->length = length;
+ if (new_length < 0 || new_length > *cur_length) {
+ NOTREACHED() << "Invalid length in TrimWriteData.";
+ return;
}
+
+ // Update the payload size and variable buffer size
+ header_->payload_size -= (*cur_length - new_length);
+ *cur_length = new_length;
}
bool Pickle::Resize(size_t new_capacity) {
diff --git a/base/pickle.h b/base/pickle.h
index 7c4a007..da2a485 100644
--- a/base/pickle.h
+++ b/base/pickle.h
@@ -239,12 +239,6 @@ class Pickle {
static const int kPayloadUnit;
private:
- // A buffer of variable length; used internally
- struct VariableLengthBuffer {
- int length;
- char data; // This is variable length.
- };
-
Header* header_;
size_t header_size_; // Supports extra data between header and payload.
// Allocation size of payload (or -1 if allocation is const).
diff --git a/base/pickle_unittest.cc b/base/pickle_unittest.cc
index 6780e21..4c38677 100644
--- a/base/pickle_unittest.cc
+++ b/base/pickle_unittest.cc
@@ -26,14 +26,13 @@
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-#include <windows.h>
-#include <string.h>
+
+#include <string>
#include "base/basictypes.h"
-#include "base/check_handler.h"
#include "base/logging.h"
-#include "base/scoped_ptr.h"
#include "base/pickle.h"
+#include "base/scoped_ptr.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
@@ -94,10 +93,13 @@ TEST(PickleTest, EncodeDecode) {
EXPECT_TRUE(pickle.WriteBool(testbool2));
EXPECT_TRUE(pickle.WriteData(testdata, testdatalen));
- char* dest = pickle.BeginWriteData(testdatalen);
+ // Over allocate BeginWriteData so we can test TrimWriteData.
+ char* dest = pickle.BeginWriteData(testdatalen + 100);
EXPECT_TRUE(dest);
memcpy(dest, testdata, testdatalen);
+ pickle.TrimWriteData(testdatalen);
+
VerifyResult(pickle);
// test copy constructor
@@ -206,11 +208,15 @@ TEST(PickleTest, Resize) {
EXPECT_EQ(cur_payload, pickle.payload_size());
}
-TEST(PickleTest, HeaderPadding) {
- struct CustomHeader : Pickle::Header {
- int blah;
- };
+namespace {
+struct CustomHeader : Pickle::Header {
+ int blah;
+};
+
+} // namespace
+
+TEST(PickleTest, HeaderPadding) {
const uint32 kMagic = 0x12345678;
Pickle pickle(sizeof(CustomHeader));
@@ -235,4 +241,4 @@ TEST(PickleTest, EqualsOperator) {
Pickle copy;
copy = copy_refs_source_buffer;
ASSERT_EQ(source.size(), copy.size());
-} \ No newline at end of file
+}