OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mojo/system/message_in_transit.h" | 5 #include "mojo/system/message_in_transit.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include <new> | 9 #include <new> |
10 | 10 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 MessageInTransit* MessageInTransit::Create(Type type, | 44 MessageInTransit* MessageInTransit::Create(Type type, |
45 Subtype subtype, | 45 Subtype subtype, |
46 const void* bytes, | 46 const void* bytes, |
47 uint32_t num_bytes, | 47 uint32_t num_bytes, |
48 uint32_t num_handles) { | 48 uint32_t num_handles) { |
49 DCHECK_LE(num_bytes, kMaxMessageNumBytes); | 49 DCHECK_LE(num_bytes, kMaxMessageNumBytes); |
50 DCHECK_LE(num_handles, kMaxMessageNumHandles); | 50 DCHECK_LE(num_handles, kMaxMessageNumHandles); |
51 | 51 |
52 const size_t data_size = num_bytes; | 52 const size_t data_size = num_bytes; |
53 const size_t size_with_header = sizeof(MessageInTransit) + data_size; | 53 const size_t size_with_header = sizeof(MessageInTransit) + data_size; |
54 const size_t size_with_header_and_padding = | 54 const size_t buffer_size = RoundUpMessageAlignment(size_with_header); |
55 RoundUpMessageAlignment(size_with_header); | |
56 | 55 |
57 char* buffer = static_cast<char*>( | 56 char* buffer = static_cast<char*>(base::AlignedAlloc(buffer_size, |
58 base::AlignedAlloc(size_with_header_and_padding, kMessageAlignment)); | 57 kMessageAlignment)); |
59 // The buffer consists of the header (a |MessageInTransit|, constructed using | 58 // The buffer consists of the header (a |MessageInTransit|, constructed using |
60 // a placement new), followed by the data, followed by padding (of zeros). | 59 // a placement new), followed by the data, followed by padding (of zeros). |
61 MessageInTransit* rv = new (buffer) MessageInTransit( | 60 MessageInTransit* rv = new (buffer) MessageInTransit( |
62 static_cast<uint32_t>(data_size), type, subtype, num_bytes, num_handles); | 61 static_cast<uint32_t>(data_size), type, subtype, num_bytes, num_handles); |
63 memcpy(buffer + sizeof(MessageInTransit), bytes, num_bytes); | 62 memcpy(buffer + sizeof(MessageInTransit), bytes, num_bytes); |
64 memset(buffer + size_with_header, 0, | 63 memset(buffer + size_with_header, 0, buffer_size - size_with_header); |
65 size_with_header_and_padding - size_with_header); | |
66 return rv; | 64 return rv; |
67 } | 65 } |
68 | 66 |
| 67 MessageInTransit* MessageInTransit::Clone() const { |
| 68 size_t buffer_size = main_buffer_size(); |
| 69 char* buffer = static_cast<char*>(base::AlignedAlloc(buffer_size, |
| 70 kMessageAlignment)); |
| 71 memcpy(buffer, main_buffer(), buffer_size); |
| 72 return reinterpret_cast<MessageInTransit*>(buffer); |
| 73 } |
| 74 |
69 void MessageInTransit::Destroy() { | 75 void MessageInTransit::Destroy() { |
70 // No need to call the destructor, since we're POD. | 76 // No need to call the destructor, since we're POD. |
71 base::AlignedFree(this); | 77 base::AlignedFree(this); |
72 } | 78 } |
73 | 79 |
74 MessageInTransit::MessageInTransit(uint32_t data_size, | 80 MessageInTransit::MessageInTransit(uint32_t data_size, |
75 Type type, | 81 Type type, |
76 Subtype subtype, | 82 Subtype subtype, |
77 uint32_t num_bytes, | 83 uint32_t num_bytes, |
78 uint32_t num_handles) | 84 uint32_t num_handles) |
79 : header_(data_size, type, subtype, kInvalidEndpointId, kInvalidEndpointId, | 85 : header_(data_size, type, subtype, kInvalidEndpointId, kInvalidEndpointId, |
80 num_bytes, num_handles) { | 86 num_bytes, num_handles) { |
81 DCHECK_GE(header()->data_size, header()->num_bytes); | 87 DCHECK_GE(header()->data_size, header()->num_bytes); |
82 } | 88 } |
83 | 89 |
84 } // namespace system | 90 } // namespace system |
85 } // namespace mojo | 91 } // namespace mojo |
OLD | NEW |