Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(273)

Side by Side Diff: mojo/public/cpp/bindings/lib/serialization.h

Issue 2738643004: [Not for commit] Test performance impact if string validation checks whether it is utf8.
Patch Set: . Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | mojo/public/cpp/bindings/lib/string_serialization.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_H_ 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_H_
7 7
8 #include <string.h> 8 #include <string.h>
9 9
10 #include "base/strings/string_util.h"
10 #include "mojo/public/cpp/bindings/array_traits_carray.h" 11 #include "mojo/public/cpp/bindings/array_traits_carray.h"
11 #include "mojo/public/cpp/bindings/array_traits_stl.h" 12 #include "mojo/public/cpp/bindings/array_traits_stl.h"
12 #include "mojo/public/cpp/bindings/lib/array_serialization.h" 13 #include "mojo/public/cpp/bindings/lib/array_serialization.h"
13 #include "mojo/public/cpp/bindings/lib/buffer.h" 14 #include "mojo/public/cpp/bindings/lib/buffer.h"
14 #include "mojo/public/cpp/bindings/lib/handle_interface_serialization.h" 15 #include "mojo/public/cpp/bindings/lib/handle_interface_serialization.h"
15 #include "mojo/public/cpp/bindings/lib/map_serialization.h" 16 #include "mojo/public/cpp/bindings/lib/map_serialization.h"
16 #include "mojo/public/cpp/bindings/lib/native_enum_serialization.h" 17 #include "mojo/public/cpp/bindings/lib/native_enum_serialization.h"
17 #include "mojo/public/cpp/bindings/lib/native_struct_serialization.h" 18 #include "mojo/public/cpp/bindings/lib/native_struct_serialization.h"
18 #include "mojo/public/cpp/bindings/lib/string_serialization.h" 19 #include "mojo/public/cpp/bindings/lib/string_serialization.h"
19 #include "mojo/public/cpp/bindings/lib/template_util.h" 20 #include "mojo/public/cpp/bindings/lib/template_util.h"
20 #include "mojo/public/cpp/bindings/map_traits_stl.h" 21 #include "mojo/public/cpp/bindings/map_traits_stl.h"
21 #include "mojo/public/cpp/bindings/string_traits_stl.h" 22 #include "mojo/public/cpp/bindings/string_traits_stl.h"
22 #include "mojo/public/cpp/bindings/string_traits_string16.h" 23 #include "mojo/public/cpp/bindings/string_traits_string16.h"
23 #include "mojo/public/cpp/bindings/string_traits_string_piece.h" 24 #include "mojo/public/cpp/bindings/string_traits_string_piece.h"
24 25
25 namespace mojo { 26 namespace mojo {
26 namespace internal { 27 namespace internal {
27 28
29 inline std::vector<uint8_t> SerializeString(const std::string& value) {
30 SerializationContext context;
31 size_t size = PrepareToSerialize<StringDataView>(value, &context);
32 DCHECK_EQ(size, Align(size));
33
34 std::vector<uint8_t> result(size);
35 if (size == 0)
36 return result;
37
38 void* result_buffer = &result.front();
39 // The serialization logic requires that the buffer is 8-byte aligned. If the
40 // result buffer is not properly aligned, we have to do an extra copy. In
41 // practice, this should never happen for std::vector.
42 bool need_copy = !IsAligned(result_buffer);
43
44 if (need_copy) {
45 // calloc sets the memory to all zero.
46 result_buffer = calloc(size, 1);
47 DCHECK(IsAligned(result_buffer));
48 }
49
50 Buffer buffer;
51 buffer.Initialize(result_buffer, size);
52 typename MojomTypeTraits<StringDataView>::Data* data = nullptr;
53 Serialize<StringDataView>(value, &buffer, &data, &context);
54
55 if (need_copy) {
56 memcpy(&result.front(), result_buffer, size);
57 free(result_buffer);
58 }
59
60 return result;
61 }
62
63 inline bool DeserializeString(bool utf8_check,
64 const std::vector<uint8_t>& input,
65 std::string* output) {
66 using DataType = MojomTypeTraits<StringDataView>::Data;
67
68 void* input_buffer =
69 input.size() == 0
70 ? nullptr
71 : const_cast<void*>(reinterpret_cast<const void*>(&input.front()));
72
73 // Please see comments in StructSerializeImpl.
74 bool need_copy = !IsAligned(input_buffer);
75
76 if (need_copy) {
77 input_buffer = malloc(input.size());
78 DCHECK(IsAligned(input_buffer));
79 memcpy(input_buffer, &input.front(), input.size());
80 }
81
82 ValidationContext validation_context(input_buffer, input.size(), 0, 0);
83 ContainerValidateParams params(0, false, nullptr);
84 bool result = false;
85 if (DataType::Validate(input_buffer, &validation_context, &params)) {
86 DataType* data = reinterpret_cast<DataType*>(input_buffer);
87 base::StringPiece string_piece(data->storage(), data->size());
88 if (!utf8_check || base::IsStringUTF8(string_piece)) {
89 SerializationContext context;
90 result = Deserialize<StringDataView>(data, output, &context);
91 }
92 }
93
94 if (need_copy)
95 free(input_buffer);
96
97 return result;
98 }
99
28 template <typename MojomType, typename DataArrayType, typename UserType> 100 template <typename MojomType, typename DataArrayType, typename UserType>
29 DataArrayType StructSerializeImpl(UserType* input) { 101 DataArrayType StructSerializeImpl(UserType* input) {
30 static_assert(BelongsTo<MojomType, MojomTypeCategory::STRUCT>::value, 102 static_assert(BelongsTo<MojomType, MojomTypeCategory::STRUCT>::value,
31 "Unexpected type."); 103 "Unexpected type.");
32 104
33 SerializationContext context; 105 SerializationContext context;
34 size_t size = PrepareToSerialize<MojomType>(*input, &context); 106 size_t size = PrepareToSerialize<MojomType>(*input, &context);
35 DCHECK_EQ(size, Align(size)); 107 DCHECK_EQ(size, Align(size));
36 108
37 DataArrayType result(size); 109 DataArrayType result(size);
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 if (need_copy) 167 if (need_copy)
96 free(input_buffer); 168 free(input_buffer);
97 169
98 return result; 170 return result;
99 } 171 }
100 172
101 } // namespace internal 173 } // namespace internal
102 } // namespace mojo 174 } // namespace mojo
103 175
104 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_H_ 176 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_H_
OLDNEW
« no previous file with comments | « no previous file | mojo/public/cpp/bindings/lib/string_serialization.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698