Shaka Player Embedded
byte_buffer.h
Go to the documentation of this file.
1 // Copyright 2016 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef SHAKA_EMBEDDED_MAPPING_BYTE_BUFFER_H_
16 #define SHAKA_EMBEDDED_MAPPING_BYTE_BUFFER_H_
17 
18 #include <cstring>
19 #include <string>
20 
25 
26 namespace shaka {
27 
34  public:
35  static std::string name() {
36  return "arraybuffer";
37  }
38 
39  ByteBuffer();
40  ByteBuffer(const uint8_t* data, size_t data_size);
41  ~ByteBuffer() override;
42 
43  ByteBuffer(const ByteBuffer&) = delete;
45  ByteBuffer& operator=(const ByteBuffer& other) = delete;
47 
48 
50  const uint8_t* data() const {
51  return ptr_;
52  }
53 
55  size_t size() const {
56  return size_;
57  }
58 
60  void Clear();
61 
71  void SetFromDynamicBuffer(const util::DynamicBuffer& other);
72 
74  void SetFromBuffer(const void* buffer, size_t size);
75 
76 
77  bool TryConvert(Handle<JsValue> value) override;
78  ReturnVal<JsValue> ToJsValue() const override;
79  void Trace(memory::HeapTracer* tracer) const override;
80 
82  ReturnVal<JsValue> ToJsValue(proto::ValueType kind) const;
83 
84  private:
86  void ClearFields();
87 
94  void ClearAndAllocateBuffer(size_t size);
95 
96  // Both |buffer_| and |ptr_| point to the same data block. |buffer_| and
97  // |own_ptr_| are mutable to allow ToJsValue to create a new ArrayBuffer to
98  // take ownership of |ptr_|.
99  mutable WeakJsPtr<JsObject> buffer_;
100  uint8_t* ptr_ = nullptr;
101  size_t size_ = 0;
102  // Whether we own |ptr_|, this may be slightly different from
103  // |buffer_.empty()| since the ArrayBuffer may be destroyed before we
104  // are during a GC run.
105  mutable bool own_ptr_ = false;
106 };
107 
108 inline bool operator==(const ByteBuffer& lhs, const ByteBuffer& rhs) {
109  return lhs.size() == rhs.size() &&
110  memcmp(lhs.data(), rhs.data(), lhs.size()) == 0;
111 }
112 inline bool operator!=(const ByteBuffer& lhs, const ByteBuffer& rhs) {
113  return !(lhs == rhs);
114 }
115 
116 } // namespace shaka
117 
118 namespace std {
119 
120 template <>
121 struct hash<shaka::ByteBuffer> {
122  size_t operator()(const shaka::ByteBuffer& buffer) const {
123  // Introduce some noise to make small buffers have a more distributed hash.
124  uint64_t ret = 0xacbdcfd0e1f20304;
125 
126  const uint8_t* ptr = buffer.data();
127  for (size_t count = buffer.size(); count > 0; ptr++, count--) {
128  // Rotate the number to make the order of bytes matter and so we affect
129  // the whole number.
130  ret = (ret << 8) | (ret >> 56);
131 
132  // "insert" the data into the hash.
133  ret ^= *ptr;
134  }
135 
136  // Intentionally truncate on 32-bit platforms.
137  return static_cast<size_t>(ret);
138  }
139 };
140 
141 } // namespace std
142 
143 #endif // SHAKA_EMBEDDED_MAPPING_BYTE_BUFFER_H_
void SetFromDynamicBuffer(const util::DynamicBuffer &other)
Definition: byte_buffer.cc:72
ReturnVal< JsValue > ToJsValue() const override
Definition: byte_buffer.cc:123
const uint8_t * data() const
Definition: byte_buffer.h:50
bool operator==(const optional< A > &lhs, const optional< B > &rhs)
Definition: optional.h:207
bool operator!=(const optional< A > &lhs, const optional< B > &rhs)
Definition: optional.h:214
ByteBuffer & operator=(const ByteBuffer &other)=delete
void SetFromBuffer(const void *buffer, size_t size)
Definition: byte_buffer.cc:77
static std::string name()
Definition: byte_buffer.h:35
size_t size() const
Definition: byte_buffer.h:55
void Trace(memory::HeapTracer *tracer) const override
Definition: byte_buffer.cc:240
size_t operator()(const shaka::ByteBuffer &buffer) const
Definition: byte_buffer.h:122
~ByteBuffer() override
Definition: byte_buffer.cc:49
bool TryConvert(Handle< JsValue > value) override
Definition: byte_buffer.cc:82