Index: mojo/public/libs/message/message_builder.cc |
diff --git a/mojo/public/libs/message/message_builder.cc b/mojo/public/libs/message/message_builder.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..5f4b6a02e28d4853fe999846dc8a870b616aa427 |
--- /dev/null |
+++ b/mojo/public/libs/message/message_builder.cc |
@@ -0,0 +1,189 @@ |
+// Copyright 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "mojo/public/libs/message/message_builder.h" |
+ |
+#include <assert.h> |
+#include <stdlib.h> |
+#include <string.h> |
+#include <limits> |
viettrungluu
2013/09/13 16:40:53
nit: I think we usually separate C and C++ headers
|
+ |
+#include "mojo/public/libs/message/message.h" |
+ |
+namespace mojo { |
+namespace { |
+ |
+// Aligns 'value' by rounding it up to the next multiple of 'alignment' |
viettrungluu
2013/09/13 16:40:53
nit: needs punctuation
|
+uint32_t AlignInt(uint32_t value, uint32_t alignment) { |
+ return value + (alignment - (value % alignment)) % alignment; |
viettrungluu
2013/09/13 16:40:53
The second |% alignment| is redundant.
|
+} |
+ |
+} // namespace |
+ |
+MessageBuilder::MessageBuilder() { |
+} |
+ |
+MessageBuilder::~MessageBuilder() { |
+} |
+ |
+bool MessageBuilder::Initialize(uint32_t type) { |
+ uint32_t message_header[2] = { |
+ type, |
viettrungluu
2013/09/13 16:40:53
I guess this code is written assuming a little-end
|
+ 0 // Length in bytes to be filled out in Finish. |
+ }; |
+ return AppendRawBytes(&message_header, sizeof(message_header)); |
+} |
+ |
+bool MessageBuilder::Append(uint16_t name, int8_t value) { |
+ return AppendU32(name, kMessageFieldType_Int8, |
+ static_cast<uint32_t>(value)); |
+} |
+ |
+bool MessageBuilder::Append(uint16_t name, int16_t value) { |
+ return AppendU32(name, kMessageFieldType_Int16, |
+ static_cast<uint32_t>(value)); |
+} |
+ |
+bool MessageBuilder::Append(uint16_t name, int32_t value) { |
+ return AppendU32(name, kMessageFieldType_Int32, |
+ static_cast<uint32_t>(value)); |
+} |
+ |
+bool MessageBuilder::Append(uint16_t name, int64_t value) { |
+ return AppendU64(name, kMessageFieldType_Int64, |
+ static_cast<uint64_t>(value)); |
+} |
+ |
+bool MessageBuilder::Append(uint16_t name, uint8_t value) { |
+ return AppendU32(name, kMessageFieldType_Uint8, |
+ static_cast<uint32_t>(value)); |
+} |
+ |
+bool MessageBuilder::Append(uint16_t name, uint16_t value) { |
+ return AppendU32(name, kMessageFieldType_Uint16, |
+ static_cast<uint32_t>(value)); |
+} |
+ |
+bool MessageBuilder::Append(uint16_t name, uint32_t value) { |
+ return AppendU32(name, kMessageFieldType_Uint32, value); |
+} |
+ |
+bool MessageBuilder::Append(uint16_t name, uint64_t value) { |
+ return AppendU64(name, kMessageFieldType_Uint32, value); |
+} |
+ |
+bool MessageBuilder::Append(uint16_t name, const std::string& value) { |
+ return AppendArray(name, kMessageFieldType_String, value.data(), |
+ value.size()); |
+} |
+ |
+bool MessageBuilder::Append(uint16_t name, const void* bytes, |
+ size_t num_bytes) { |
+ return AppendArray(name, kMessageFieldType_Bytes, bytes, num_bytes); |
+} |
+ |
+bool MessageBuilder::Append(uint16_t name, Handle handle) { |
+ size_t index = handles_.size(); |
+ |
+ if (!AppendU32(name, kMessageFieldType_Handle, index)) |
+ return false; |
+ |
+ handles_.push_back(handle); |
+ |
+ // TODO(darin): Should we even worry about memory allocation failure? |
+ return handles_.size() == index + 1; |
+} |
+ |
+bool MessageBuilder::Append(uint16_t name, const Message& message) { |
+ if (!AppendArray(name, kMessageFieldType_Message, &message.data()[0], |
+ message.data().size())) |
+ return false; |
+ |
+ size_t size_before = handles_.size(); |
+ handles_.insert(handles_.end(), |
+ message.handles().begin(), |
+ message.handles().end()); |
+ |
+ // TODO(darin): Should we even worry about memory allocation failure? |
+ return handles_.size() == size_before + message.handles().size(); |
+} |
+ |
+bool MessageBuilder::Finish(Message* message) { |
+ if (data_.empty()) // Initialize was not called! |
+ return false; |
+ |
+ uint32_t payload_size = data_.size() - 2U*sizeof(uint32_t); |
+ assert(payload_size < data_.size()); |
+ |
+ // Stamp payload size into the message. |
+ reinterpret_cast<uint32_t*>(&data_[0])[1] = payload_size; |
viettrungluu
2013/09/13 16:40:53
I think this violates strict aliasing, so you shou
|
+ |
+ message->data_.swap(data_); |
+ message->handles_.swap(handles_); |
+ return true; |
+} |
+ |
+bool MessageBuilder::AppendU32(uint16_t name, MessageFieldType field_type, |
+ uint32_t value) { |
+ return AppendHeader(name, field_type) && |
+ AppendRawBytes(&value, sizeof(value)); |
+} |
+ |
+bool MessageBuilder::AppendU64(uint16_t name, MessageFieldType field_type, |
+ uint64_t value) { |
+ // Insert padding to achieve 8-byte alignment. |
+ return AppendHeader(name, field_type) && |
+ AppendPadding(4U) && |
+ AppendRawBytes(&value, sizeof(value)); |
+} |
+ |
+bool MessageBuilder::AppendArray(uint16_t name, MessageFieldType field_type, |
+ const void* bytes, size_t num_bytes) { |
+ if (num_bytes > std::numeric_limits<uint32_t>::max()) |
+ return false; |
+ |
+ // Append padding to achieve 8-byte alignment. |
+ uint32_t size = static_cast<uint32_t>(num_bytes); |
+ uint32_t aligned_size = AlignInt(size, 8U); |
+ |
+ return AppendHeader(name, field_type) && |
+ AppendRawBytes(&size, sizeof(size)) && |
+ AppendRawBytes(bytes, size) && |
+ AppendPadding(aligned_size - size); |
+} |
+ |
+bool MessageBuilder::AppendHeader(uint16_t name, MessageFieldType field_type) { |
+ assert(data_.size() >= 8U); // Initialize should have been called. |
+ |
+ uint32_t header = static_cast<uint32_t>(name) << 16 | |
+ static_cast<uint32_t>(field_type); |
+ return AppendRawBytes(&header, sizeof(header)); |
+} |
+ |
+bool MessageBuilder::AppendPadding(size_t num_bytes) { |
+ if (num_bytes == 0) |
+ return true; |
+ assert(num_bytes <= 7); |
+ |
+ const uint8_t kNulls[7] = { 0 }; |
+ return AppendRawBytes(kNulls, num_bytes); |
+} |
+ |
+bool MessageBuilder::AppendRawBytes(const void* bytes, size_t num_bytes) { |
+ if (!num_bytes) |
+ return true; |
+ |
+ const uint8_t* data_start = static_cast<const uint8_t*>(bytes); |
+ const uint8_t* data_end = data_start + num_bytes; |
+ |
+ size_t size_before = data_.size(); |
+ |
+ // TODO(darin): Call reserve() here? |
+ data_.insert(data_.end(), data_start, data_end); |
+ |
+ // TODO(darin): Should we even worry about memory allocation failure? |
+ return data_.size() == size_before + num_bytes; |
+} |
+ |
+} // namespace mojo |