1 // Copyright 2021 the V8 project 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 INCLUDE_V8_MEMORY_SPAN_H_
6 #define INCLUDE_V8_MEMORY_SPAN_H_
7 
8 #include <stddef.h>
9 
10 #include "v8config.h"  // NOLINT(build/include_directory)
11 
12 namespace v8 {
13 
14 /**
15  * Points to an unowned continous buffer holding a known number of elements.
16  *
17  * This is similar to std::span (under consideration for C++20), but does not
18  * require advanced C++ support. In the (far) future, this may be replaced with
19  * or aliased to std::span.
20  *
21  * To facilitate future migration, this class exposes a subset of the interface
22  * implemented by std::span.
23  */
24 template <typename T>
25 class V8_EXPORT MemorySpan {
26  public:
27   /** The default constructor creates an empty span. */
28   constexpr MemorySpan() = default;
29 
MemorySpan(T * data,size_t size)30   constexpr MemorySpan(T* data, size_t size) : data_(data), size_(size) {}
31 
32   /** Returns a pointer to the beginning of the buffer. */
data()33   constexpr T* data() const { return data_; }
34   /** Returns the number of elements that the buffer holds. */
size()35   constexpr size_t size() const { return size_; }
36 
37  private:
38   T* data_ = nullptr;
39   size_t size_ = 0;
40 };
41 
42 }  // namespace v8
43 #endif  // INCLUDE_V8_MEMORY_SPAN_H_
44