aboutsummaryrefslogtreecommitdiff
path: root/deps/v8/src/utils/detachable-vector.h
blob: 232e89f18398ad080b706f3f91b85db0902ae96a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2017 the V8 project 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 V8_UTILS_DETACHABLE_VECTOR_H_
#define V8_UTILS_DETACHABLE_VECTOR_H_

#include <stddef.h>

#include <algorithm>

#include "src/base/logging.h"
#include "src/base/macros.h"

namespace v8 {
namespace internal {

class V8_EXPORT_PRIVATE DetachableVectorBase {
 public:
  // Clear our reference to the backing store. Does not delete it!
  void detach() {
    data_ = nullptr;
    capacity_ = 0;
    size_ = 0;
  }

  void pop_back() { --size_; }
  size_t capacity() const { return capacity_; }
  size_t size() const { return size_; }
  bool empty() const { return size_ == 0; }

  static const size_t kMinimumCapacity;
  static const size_t kDataOffset;
  static const size_t kCapacityOffset;
  static const size_t kSizeOffset;

 protected:
  void* data_ = nullptr;
  size_t capacity_ = 0;
  size_t size_ = 0;
};

// This class wraps an array and provides a few of the common member
// functions for accessing the data. Two extra methods are also provided: free()
// and detach(), which allow for manual control of the backing store. This is
// currently required for use in the HandleScopeImplementer. Any other class
// should just use a std::vector.
template <typename T>
class DetachableVector : public DetachableVectorBase {
 public:
  DetachableVector() = default;
  ~DetachableVector() { delete[] data(); }

  void push_back(const T& value) {
    if (size_ == capacity_) {
      size_t new_capacity = std::max(kMinimumCapacity, 2 * capacity_);
      Resize(new_capacity);
    }

    data()[size_] = value;
    ++size_;
  }

  // Free the backing store and clear our reference to it.
  void free() {
    delete[] data();
    data_ = nullptr;
    capacity_ = 0;
    size_ = 0;
  }

  T& at(size_t i) const {
    DCHECK_LT(i, size_);
    return data()[i];
  }
  T& back() const { return at(size_ - 1); }
  T& front() const { return at(0); }

  void shrink_to_fit() {
    size_t new_capacity = std::max(size_, kMinimumCapacity);
    if (new_capacity < capacity_ / 2) {
      Resize(new_capacity);
    }
  }

 private:
  T* data() const { return static_cast<T*>(data_); }

  void Resize(size_t new_capacity) {
    DCHECK_LE(size_, new_capacity);
    T* new_data_ = new T[new_capacity];

    std::copy(data(), data() + size_, new_data_);
    delete[] data();

    data_ = new_data_;
    capacity_ = new_capacity;
  }
};

}  // namespace internal
}  // namespace v8

#endif  // V8_UTILS_DETACHABLE_VECTOR_H_