5 #include "src/heap/slot-set.h" 10 TypedSlots::~TypedSlots() {
12 while (chunk !=
nullptr) {
13 Chunk* next = chunk->next;
14 delete[] chunk->buffer;
22 void TypedSlots::Insert(SlotType type,
uint32_t host_offset,
uint32_t offset) {
23 TypedSlot slot = {TypeField::encode(type) | OffsetField::encode(offset),
25 Chunk* chunk = EnsureChunk();
26 DCHECK_LT(chunk->count, chunk->capacity);
27 chunk->buffer[chunk->count] = slot;
31 void TypedSlots::Merge(TypedSlots* other) {
32 if (other->head_ ==
nullptr) {
35 if (head_ ==
nullptr) {
39 tail_->next = other->head_;
42 other->head_ =
nullptr;
43 other->tail_ =
nullptr;
46 TypedSlots::Chunk* TypedSlots::EnsureChunk() {
48 head_ = tail_ = NewChunk(
nullptr, kInitialBufferSize);
50 if (head_->count == head_->capacity) {
51 head_ = NewChunk(head_, NextCapacity(head_->capacity));
56 TypedSlots::Chunk* TypedSlots::NewChunk(Chunk* next,
int capacity) {
57 Chunk* chunk =
new Chunk;
59 chunk->buffer =
new TypedSlot[capacity];
60 chunk->capacity = capacity;
65 TypedSlotSet::~TypedSlotSet() { FreeToBeFreedChunks(); }
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);
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;
90 DCHECK_LE(upper_bound->first, host_offset);
91 if (upper_bound->second > host_offset) {
92 ClearTypedSlot(buffer +
i);
95 chunk = LoadNext(chunk);