OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #ifndef MOJO_PUBLIC_BINDINGS_BINDINGS_H_ |
| 6 #define MOJO_PUBLIC_BINDINGS_BINDINGS_H_ |
| 7 |
| 8 #include <assert.h> |
| 9 #include <stddef.h> |
| 10 #include <stdint.h> |
| 11 #include <stdlib.h> |
| 12 #include <string.h> |
| 13 |
| 14 #include "mojo/public/system/core.h" |
| 15 |
| 16 namespace mojo { |
| 17 |
| 18 //----------------------------------------------------------------------------- |
| 19 |
| 20 struct Message { |
| 21 uint32_t name; |
| 22 uint8_t* data_start; |
| 23 uint8_t* data_end; |
| 24 Handle* handles_start; |
| 25 Handle* handles_end; |
| 26 }; |
| 27 |
| 28 class MessageSender { |
| 29 public: |
| 30 virtual void Send(const Message& message) = 0; |
| 31 }; |
| 32 |
| 33 //----------------------------------------------------------------------------- |
| 34 |
| 35 struct MessageHeader { |
| 36 uint32_t num_bytes; |
| 37 uint32_t name; |
| 38 }; |
| 39 |
| 40 struct StructHeader { |
| 41 uint32_t num_bytes; |
| 42 uint32_t num_fields; |
| 43 }; |
| 44 |
| 45 struct ArrayHeader { |
| 46 uint32_t num_bytes; |
| 47 uint32_t num_elements; |
| 48 }; |
| 49 |
| 50 //----------------------------------------------------------------------------- |
| 51 |
| 52 template <typename T> class Array; |
| 53 |
| 54 namespace internal { |
| 55 template <typename T> class Traits {}; |
| 56 } |
| 57 |
| 58 template <typename T> |
| 59 union StructPointer { |
| 60 uint64_t offset; |
| 61 T* ptr; |
| 62 }; |
| 63 |
| 64 template <typename T> |
| 65 union ArrayPointer { |
| 66 uint64_t offset; |
| 67 Array<T>* ptr; |
| 68 }; |
| 69 |
| 70 template <typename T> |
| 71 struct TypeTraits { |
| 72 typedef T StorageType; |
| 73 |
| 74 static T& ToRef(StorageType& e) { return e; } |
| 75 static T const& ToConstRef(const StorageType& e) { return e; } |
| 76 }; |
| 77 |
| 78 template <typename P> |
| 79 struct TypeTraits<P*> { |
| 80 typedef StructPointer<P> StorageType; |
| 81 |
| 82 static P*& ToRef(StorageType& e) { return e.ptr; } |
| 83 static P* const& ToConstRef(const StorageType& e) { return e.ptr; } |
| 84 }; |
| 85 |
| 86 template <typename T> |
| 87 class Array { |
| 88 public: |
| 89 explicit Array(size_t num_elements) { |
| 90 // TODO: bools should get packed to a single bit? |
| 91 header_.num_bytes = |
| 92 sizeof(*this) + |
| 93 sizeof(typename TypeTraits<T>::StorageType) * (num_elements - 1); |
| 94 header_.num_elements = num_elements; |
| 95 } |
| 96 |
| 97 size_t size() const { return header_.num_elements; } |
| 98 |
| 99 T& at(size_t offset) { |
| 100 return TypeTraits<T>::ToRef(elements_[offset]); |
| 101 } |
| 102 |
| 103 const T& at(size_t offset) const { |
| 104 return TypeTraits<T>::ToConstRef(elements_[offset]); |
| 105 } |
| 106 |
| 107 T& operator[](size_t offset) { |
| 108 return TypeTraits<T>::ToRef(elements_[offset]); |
| 109 } |
| 110 |
| 111 const T& operator[](size_t offset) const { |
| 112 return TypeTraits<T>::ToConstRef(elements_[offset]); |
| 113 } |
| 114 |
| 115 private: |
| 116 friend class internal::Traits<Array<T> >; |
| 117 |
| 118 ArrayHeader header_; |
| 119 typename TypeTraits<T>::StorageType elements_[1]; // Extra elements follow. |
| 120 }; |
| 121 |
| 122 //----------------------------------------------------------------------------- |
| 123 |
| 124 // The following is a cheezy arena allocator. |
| 125 class MessageBuffer { |
| 126 public: |
| 127 MessageBuffer() : ptr_(NULL), size_(0) { |
| 128 } |
| 129 ~MessageBuffer() { free(ptr_); } |
| 130 |
| 131 const uint8_t* data() const { return ptr_; } |
| 132 uint8_t* data() { return ptr_; } |
| 133 |
| 134 size_t size() const { return size_; } |
| 135 |
| 136 uint8_t* AllocBytes(size_t size) { |
| 137 return Grow(size); |
| 138 } |
| 139 |
| 140 template <typename T> |
| 141 T* Alloc() { |
| 142 size_t size = sizeof(T); |
| 143 return new (Grow(size)) T(); |
| 144 } |
| 145 |
| 146 template <typename T> |
| 147 Array<T>* AllocArray(size_t count) { |
| 148 // (count - 1) because Array<T> has reserved space for the first element. |
| 149 size_t size = sizeof(Array<T>) + sizeof(T) * (count - 1); |
| 150 return new (Grow(size)) Array<T>(count); |
| 151 } |
| 152 |
| 153 private: |
| 154 uint8_t* Grow(size_t delta) { |
| 155 // TODO: Align allocations |
| 156 size_t old_size = size_; |
| 157 size_t new_size = old_size + delta; |
| 158 ptr_ = static_cast<uint8_t*>(realloc(ptr_, old_size + delta)); |
| 159 size_ = new_size; |
| 160 uint8_t* result = ptr_ + old_size; |
| 161 memset(result, 0, delta); |
| 162 return result; |
| 163 } |
| 164 |
| 165 uint8_t* ptr_; |
| 166 size_t size_; |
| 167 |
| 168 // NOT IMPLEMENTED |
| 169 MessageBuffer(const MessageBuffer&); |
| 170 void operator=(const MessageBuffer&); |
| 171 }; |
| 172 |
| 173 } // namespace mojo |
| 174 |
| 175 #endif // MOJO_PUBLIC_BINDINGS_BINDINGS_H_ |
OLD | NEW |