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
3 // found in the LICENSE file.
5 #include "mojo/public/bindings/lib/bindings_serialization.h"
9 #include "mojo/public/bindings/lib/bindings_internal.h"
14 size_t Align(size_t size) {
15 const size_t kAlignment = 8;
16 return size + (kAlignment - (size % kAlignment)) % kAlignment;
19 void EncodePointer(const void* ptr, uint64_t* offset) {
25 const char* p_obj = reinterpret_cast<const char*>(ptr);
26 const char* p_slot = reinterpret_cast<const char*>(offset);
27 assert(p_obj > p_slot);
29 *offset = static_cast<uint64_t>(p_obj - p_slot);
32 const void* DecodePointerRaw(const uint64_t* offset) {
35 return reinterpret_cast<const char*>(offset) + *offset;
38 bool ValidatePointer(const void* ptr, const Message& message) {
39 const uint8_t* data = static_cast<const uint8_t*>(ptr);
40 if (reinterpret_cast<ptrdiff_t>(data) % 8 != 0)
43 const uint8_t* data_start = message.data();
44 const uint8_t* data_end = data_start + message.data_num_bytes();
46 return data >= data_start && data < data_end;
49 void EncodeHandle(Handle* handle, std::vector<Handle>* handles) {
50 if (handle->is_valid()) {
51 handles->push_back(*handle);
52 handle->set_value(static_cast<MojoHandle>(handles->size() - 1));
54 // Encode -1 to mean the invalid handle.
55 handle->set_value(static_cast<MojoHandle>(-1));
59 bool DecodeHandle(Handle* handle, std::vector<Handle>* handles) {
60 // Decode -1 to mean the invalid handle.
61 if (handle->value() == static_cast<MojoHandle>(-1)) {
65 if (handle->value() >= handles->size())
67 // Just leave holes in the vector so we don't screw up other indices.
68 *handle = FetchAndReset(&handles->at(handle->value()));
72 } // namespace internal