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

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

Issue 2604953003: Remove internal uses of mojo::Array and mojo::WTFArray. (Closed)
Patch Set: Created 3 years, 11 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
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 "mojo/public/cpp/bindings/array_traits_carray.h" 10 #include "mojo/public/cpp/bindings/array_traits_carray.h"
(...skipping 24 matching lines...) Expand all
35 size_t size = PrepareToSerialize<MojomType>(*input, &context); 35 size_t size = PrepareToSerialize<MojomType>(*input, &context);
36 DCHECK_EQ(size, Align(size)); 36 DCHECK_EQ(size, Align(size));
37 37
38 DataArrayType result(size); 38 DataArrayType result(size);
39 if (size == 0) 39 if (size == 0)
40 return result; 40 return result;
41 41
42 void* result_buffer = &result.front(); 42 void* result_buffer = &result.front();
43 // The serialization logic requires that the buffer is 8-byte aligned. If the 43 // The serialization logic requires that the buffer is 8-byte aligned. If the
44 // result buffer is not properly aligned, we have to do an extra copy. In 44 // result buffer is not properly aligned, we have to do an extra copy. In
45 // practice, this should never happen for mojo::Array (backed by std::vector). 45 // practice, this should never happen for std::vector.
46 bool need_copy = !IsAligned(result_buffer); 46 bool need_copy = !IsAligned(result_buffer);
47 47
48 if (need_copy) { 48 if (need_copy) {
49 // calloc sets the memory to all zero. 49 // calloc sets the memory to all zero.
50 result_buffer = calloc(size, 1); 50 result_buffer = calloc(size, 1);
51 DCHECK(IsAligned(result_buffer)); 51 DCHECK(IsAligned(result_buffer));
52 } 52 }
53 53
54 Buffer buffer; 54 Buffer buffer;
55 buffer.Initialize(result_buffer, size); 55 buffer.Initialize(result_buffer, size);
56 typename MojomTypeTraits<MojomType>::Data* data = nullptr; 56 typename MojomTypeTraits<MojomType>::Data* data = nullptr;
57 Serialize<MojomType>(*input, &buffer, &data, &context); 57 Serialize<MojomType>(*input, &buffer, &data, &context);
58 58
59 if (need_copy) { 59 if (need_copy) {
60 memcpy(&result.front(), result_buffer, size); 60 memcpy(&result.front(), result_buffer, size);
61 free(result_buffer); 61 free(result_buffer);
62 } 62 }
63 63
64 return result; 64 return result;
65 } 65 }
66 66
67 template <typename MojomType, typename DataArrayType, typename UserType> 67 template <typename MojomType, typename DataArrayType, typename UserType>
68 bool StructDeserializeImpl(const DataArrayType& input, UserType* output) { 68 bool StructDeserializeImpl(const DataArrayType& input, UserType* output) {
69 static_assert(BelongsTo<MojomType, MojomTypeCategory::STRUCT>::value, 69 static_assert(BelongsTo<MojomType, MojomTypeCategory::STRUCT>::value,
70 "Unexpected type."); 70 "Unexpected type.");
71 using DataType = typename MojomTypeTraits<MojomType>::Data; 71 using DataType = typename MojomTypeTraits<MojomType>::Data;
72 72
73 if (input.is_null()) 73 // TODO(sammc): Use DataArrayType::empty() once WTF::Vector::empty() exists.
74 return false;
75
76 void* input_buffer = 74 void* input_buffer =
77 input.empty() 75 input.size() == 0
78 ? nullptr 76 ? nullptr
79 : const_cast<void*>(reinterpret_cast<const void*>(&input.front())); 77 : const_cast<void*>(reinterpret_cast<const void*>(&input.front()));
80 78
81 // Please see comments in StructSerializeImpl. 79 // Please see comments in StructSerializeImpl.
82 bool need_copy = !IsAligned(input_buffer); 80 bool need_copy = !IsAligned(input_buffer);
83 81
84 if (need_copy) { 82 if (need_copy) {
85 input_buffer = malloc(input.size()); 83 input_buffer = malloc(input.size());
86 DCHECK(IsAligned(input_buffer)); 84 DCHECK(IsAligned(input_buffer));
87 memcpy(input_buffer, &input.front(), input.size()); 85 memcpy(input_buffer, &input.front(), input.size());
(...skipping 10 matching lines...) Expand all
98 if (need_copy) 96 if (need_copy)
99 free(input_buffer); 97 free(input_buffer);
100 98
101 return result; 99 return result;
102 } 100 }
103 101
104 } // namespace internal 102 } // namespace internal
105 } // namespace mojo 103 } // namespace mojo
106 104
107 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_H_ 105 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698