| Index: mojo/public/bindings/bindings.h
|
| diff --git a/mojo/public/bindings/bindings.h b/mojo/public/bindings/bindings.h
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..4b7663e6873e1f9d7790947a9a4d8e7f2eb25ae5
|
| --- /dev/null
|
| +++ b/mojo/public/bindings/bindings.h
|
| @@ -0,0 +1,175 @@
|
| +// Copyright 2013 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#ifndef MOJO_PUBLIC_BINDINGS_BINDINGS_H_
|
| +#define MOJO_PUBLIC_BINDINGS_BINDINGS_H_
|
| +
|
| +#include <assert.h>
|
| +#include <stddef.h>
|
| +#include <stdint.h>
|
| +#include <stdlib.h>
|
| +#include <string.h>
|
| +
|
| +#include "mojo/public/system/core.h"
|
| +
|
| +namespace mojo {
|
| +
|
| +//-----------------------------------------------------------------------------
|
| +
|
| +struct Message {
|
| + uint32_t name;
|
| + uint8_t* data_start;
|
| + uint8_t* data_end;
|
| + Handle* handles_start;
|
| + Handle* handles_end;
|
| +};
|
| +
|
| +class MessageSender {
|
| + public:
|
| + virtual void Send(const Message& message) = 0;
|
| +};
|
| +
|
| +//-----------------------------------------------------------------------------
|
| +
|
| +struct MessageHeader {
|
| + uint32_t num_bytes;
|
| + uint32_t name;
|
| +};
|
| +
|
| +struct StructHeader {
|
| + uint32_t num_bytes;
|
| + uint32_t num_fields;
|
| +};
|
| +
|
| +struct ArrayHeader {
|
| + uint32_t num_bytes;
|
| + uint32_t num_elements;
|
| +};
|
| +
|
| +//-----------------------------------------------------------------------------
|
| +
|
| +template <typename T> class Array;
|
| +
|
| +namespace internal {
|
| +template <typename T> class Traits {};
|
| +}
|
| +
|
| +template <typename T>
|
| +union StructPointer {
|
| + uint64_t offset;
|
| + T* ptr;
|
| +};
|
| +
|
| +template <typename T>
|
| +union ArrayPointer {
|
| + uint64_t offset;
|
| + Array<T>* ptr;
|
| +};
|
| +
|
| +template <typename T>
|
| +struct TypeTraits {
|
| + typedef T StorageType;
|
| +
|
| + static T& ToRef(StorageType& e) { return e; }
|
| + static T const& ToConstRef(const StorageType& e) { return e; }
|
| +};
|
| +
|
| +template <typename P>
|
| +struct TypeTraits<P*> {
|
| + typedef StructPointer<P> StorageType;
|
| +
|
| + static P*& ToRef(StorageType& e) { return e.ptr; }
|
| + static P* const& ToConstRef(const StorageType& e) { return e.ptr; }
|
| +};
|
| +
|
| +template <typename T>
|
| +class Array {
|
| + public:
|
| + explicit Array(size_t num_elements) {
|
| + // TODO: bools should get packed to a single bit?
|
| + header_.num_bytes =
|
| + sizeof(*this) +
|
| + sizeof(typename TypeTraits<T>::StorageType) * (num_elements - 1);
|
| + header_.num_elements = num_elements;
|
| + }
|
| +
|
| + size_t size() const { return header_.num_elements; }
|
| +
|
| + T& at(size_t offset) {
|
| + return TypeTraits<T>::ToRef(elements_[offset]);
|
| + }
|
| +
|
| + const T& at(size_t offset) const {
|
| + return TypeTraits<T>::ToConstRef(elements_[offset]);
|
| + }
|
| +
|
| + T& operator[](size_t offset) {
|
| + return TypeTraits<T>::ToRef(elements_[offset]);
|
| + }
|
| +
|
| + const T& operator[](size_t offset) const {
|
| + return TypeTraits<T>::ToConstRef(elements_[offset]);
|
| + }
|
| +
|
| + private:
|
| + friend class internal::Traits<Array<T> >;
|
| +
|
| + ArrayHeader header_;
|
| + typename TypeTraits<T>::StorageType elements_[1]; // Extra elements follow.
|
| +};
|
| +
|
| +//-----------------------------------------------------------------------------
|
| +
|
| +// The following is a cheezy arena allocator.
|
| +class MessageBuffer {
|
| + public:
|
| + MessageBuffer() : ptr_(NULL), size_(0) {
|
| + }
|
| + ~MessageBuffer() { free(ptr_); }
|
| +
|
| + const uint8_t* data() const { return ptr_; }
|
| + uint8_t* data() { return ptr_; }
|
| +
|
| + size_t size() const { return size_; }
|
| +
|
| + uint8_t* AllocBytes(size_t size) {
|
| + return Grow(size);
|
| + }
|
| +
|
| + template <typename T>
|
| + T* Alloc() {
|
| + size_t size = sizeof(T);
|
| + return new (Grow(size)) T();
|
| + }
|
| +
|
| + template <typename T>
|
| + Array<T>* AllocArray(size_t count) {
|
| + // (count - 1) because Array<T> has reserved space for the first element.
|
| + size_t size = sizeof(Array<T>) + sizeof(T) * (count - 1);
|
| + return new (Grow(size)) Array<T>(count);
|
| + }
|
| +
|
| + private:
|
| + uint8_t* Grow(size_t delta) {
|
| + // TODO: Align allocations
|
| + size_t old_size = size_;
|
| + size_t new_size = old_size + delta;
|
| + ptr_ = static_cast<uint8_t*>(realloc(ptr_, old_size + delta));
|
| + size_ = new_size;
|
| + uint8_t* result = ptr_ + old_size;
|
| + memset(result, 0, delta);
|
| + return result;
|
| + }
|
| +
|
| + uint8_t* ptr_;
|
| + size_t size_;
|
| +
|
| + // NOT IMPLEMENTED
|
| + MessageBuffer(const MessageBuffer&);
|
| + void operator=(const MessageBuffer&);
|
| +};
|
| +
|
| +} // namespace mojo
|
| +
|
| +#endif // MOJO_PUBLIC_BINDINGS_BINDINGS_H_
|
|
|