• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 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 V8_EXECUTION_EXTERNAL_POINTER_TABLE_H_
6 #define V8_EXECUTION_EXTERNAL_POINTER_TABLE_H_
7 
8 #include "src/common/external-pointer.h"
9 #include "src/utils/utils.h"
10 
11 namespace v8 {
12 namespace internal {
13 
14 class V8_EXPORT_PRIVATE ExternalPointerTable {
15  public:
16   static const int kExternalPointerTableInitialCapacity = 1024;
17 
ExternalPointerTable()18   ExternalPointerTable()
19       : buffer_(reinterpret_cast<Address*>(
20             calloc(kExternalPointerTableInitialCapacity, sizeof(Address)))),
21         length_(1),
22         capacity_(kExternalPointerTableInitialCapacity),
23         freelist_head_(0) {
24     // Explicitly setup the invalid nullptr entry.
25     STATIC_ASSERT(kNullExternalPointer == 0);
26     buffer_[kNullExternalPointer] = kNullAddress;
27   }
28 
~ExternalPointerTable()29   ~ExternalPointerTable() { ::free(buffer_); }
30 
get(uint32_t index)31   Address get(uint32_t index) const {
32     CHECK_LT(index, length_);
33     return buffer_[index];
34   }
35 
set(uint32_t index,Address value)36   void set(uint32_t index, Address value) {
37     DCHECK_NE(kNullExternalPointer, index);
38     CHECK_LT(index, length_);
39     buffer_[index] = value;
40   }
41 
allocate()42   uint32_t allocate() {
43     uint32_t index = length_++;
44     if (index >= capacity_) {
45       GrowTable(this);
46     }
47     DCHECK_NE(kNullExternalPointer, index);
48     return index;
49   }
50 
free(uint32_t index)51   void free(uint32_t index) {
52     // TODO(v8:10391, saelo): implement simple free list here, i.e. set
53     // buffer_[index] to freelist_head_ and set freelist_head
54     // to index
55     DCHECK_NE(kNullExternalPointer, index);
56   }
57 
58   // Returns true if the entry exists in the table and therefore it can be read.
is_valid_index(uint32_t index)59   bool is_valid_index(uint32_t index) const {
60     // TODO(v8:10391, saelo): also check here if entry is free
61     return index < length_;
62   }
63 
size()64   uint32_t size() const { return length_; }
65 
66   static void GrowTable(ExternalPointerTable* table);
67 
68  private:
69   friend class Isolate;
70 
71   Address* buffer_;
72   uint32_t length_;
73   uint32_t capacity_;
74   uint32_t freelist_head_;
75 };
76 
77 }  // namespace internal
78 }  // namespace v8
79 
80 #endif  // V8_EXECUTION_EXTERNAL_POINTER_TABLE_H_
81