V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
slot-set.cc
1 // Copyright 2018 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 #include "src/heap/slot-set.h"
6 
7 namespace v8 {
8 namespace internal {
9 
10 TypedSlots::~TypedSlots() {
11  Chunk* chunk = head_;
12  while (chunk != nullptr) {
13  Chunk* next = chunk->next;
14  delete[] chunk->buffer;
15  delete chunk;
16  chunk = next;
17  }
18  head_ = nullptr;
19  tail_ = nullptr;
20 }
21 
22 void TypedSlots::Insert(SlotType type, uint32_t host_offset, uint32_t offset) {
23  TypedSlot slot = {TypeField::encode(type) | OffsetField::encode(offset),
24  host_offset};
25  Chunk* chunk = EnsureChunk();
26  DCHECK_LT(chunk->count, chunk->capacity);
27  chunk->buffer[chunk->count] = slot;
28  ++chunk->count;
29 }
30 
31 void TypedSlots::Merge(TypedSlots* other) {
32  if (other->head_ == nullptr) {
33  return;
34  }
35  if (head_ == nullptr) {
36  head_ = other->head_;
37  tail_ = other->tail_;
38  } else {
39  tail_->next = other->head_;
40  tail_ = other->tail_;
41  }
42  other->head_ = nullptr;
43  other->tail_ = nullptr;
44 }
45 
46 TypedSlots::Chunk* TypedSlots::EnsureChunk() {
47  if (!head_) {
48  head_ = tail_ = NewChunk(nullptr, kInitialBufferSize);
49  }
50  if (head_->count == head_->capacity) {
51  head_ = NewChunk(head_, NextCapacity(head_->capacity));
52  }
53  return head_;
54 }
55 
56 TypedSlots::Chunk* TypedSlots::NewChunk(Chunk* next, int capacity) {
57  Chunk* chunk = new Chunk;
58  chunk->next = next;
59  chunk->buffer = new TypedSlot[capacity];
60  chunk->capacity = capacity;
61  chunk->count = 0;
62  return chunk;
63 }
64 
65 TypedSlotSet::~TypedSlotSet() { FreeToBeFreedChunks(); }
66 
67 void TypedSlotSet::FreeToBeFreedChunks() {
68  base::MutexGuard guard(&to_be_freed_chunks_mutex_);
69  std::stack<std::unique_ptr<Chunk>> empty;
70  to_be_freed_chunks_.swap(empty);
71 }
72 
73 void TypedSlotSet::ClearInvalidSlots(
74  const std::map<uint32_t, uint32_t>& invalid_ranges) {
75  Chunk* chunk = LoadHead();
76  while (chunk != nullptr) {
77  TypedSlot* buffer = chunk->buffer;
78  int count = chunk->count;
79  for (int i = 0; i < count; i++) {
80  TypedSlot slot = LoadTypedSlot(buffer + i);
81  SlotType type = TypeField::decode(slot.type_and_offset);
82  if (type == CLEARED_SLOT) continue;
83  uint32_t host_offset = slot.host_offset;
84  std::map<uint32_t, uint32_t>::const_iterator upper_bound =
85  invalid_ranges.upper_bound(host_offset);
86  if (upper_bound == invalid_ranges.begin()) continue;
87  // upper_bounds points to the invalid range after the given slot. Hence,
88  // we have to go to the previous element.
89  upper_bound--;
90  DCHECK_LE(upper_bound->first, host_offset);
91  if (upper_bound->second > host_offset) {
92  ClearTypedSlot(buffer + i);
93  }
94  }
95  chunk = LoadNext(chunk);
96  }
97 }
98 
99 } // namespace internal
100 } // namespace v8
Definition: libplatform.h:13