Files
sdk/runtime/vm/heap/weak_table_test.cc
T
Ryan Macnak 584fd79bc8 [vm, gc] Allow immediates in weak tables, and simplify serialization id tracking.
Allow weak tables to contain immediate objects (Smis) by adjusting the sentinel value for an empty entry to a value which is never a valid Smi nor heap object. Immediates go into the old-space table, since they also don't need to be updated after a scavenge.

Remove the clustered serializer's separate Smi id table.

Change-Id: Ia34ebe92be17f6568f0d4e5090e9abf5e77dadb3
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/168620
Reviewed-by: Régis Crelier <regis@google.com>
Commit-Queue: Ryan Macnak <rmacnak@google.com>
2020-10-21 21:17:54 +00:00

63 lines
2.2 KiB
C++

// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "platform/globals.h"
#include "platform/assert.h"
#include "vm/globals.h"
#include "vm/heap/heap.h"
#include "vm/heap/weak_table.h"
#include "vm/unit_test.h"
namespace dart {
ISOLATE_UNIT_TEST_CASE(WeakTables) {
const Object& old_obj = Object::Handle(String::New("old", Heap::kOld));
const Object& new_obj = Object::Handle(String::New("new", Heap::kNew));
const Object& imm_obj = Object::Handle(Smi::New(0));
// Initially absent.
Heap* heap = thread->heap();
const intptr_t kNoValue = WeakTable::kNoValue;
EXPECT_EQ(kNoValue, heap->GetObjectId(old_obj.raw()));
EXPECT_EQ(kNoValue, heap->GetObjectId(new_obj.raw()));
EXPECT_EQ(kNoValue, heap->GetObjectId(imm_obj.raw()));
// Found after insert.
heap->SetObjectId(old_obj.raw(), 100);
heap->SetObjectId(new_obj.raw(), 200);
heap->SetObjectId(imm_obj.raw(), 300);
EXPECT_EQ(100, heap->GetObjectId(old_obj.raw()));
EXPECT_EQ(200, heap->GetObjectId(new_obj.raw()));
EXPECT_EQ(300, heap->GetObjectId(imm_obj.raw()));
// Found after update.
heap->SetObjectId(old_obj.raw(), 400);
heap->SetObjectId(new_obj.raw(), 500);
heap->SetObjectId(imm_obj.raw(), 600);
EXPECT_EQ(400, heap->GetObjectId(old_obj.raw()));
EXPECT_EQ(500, heap->GetObjectId(new_obj.raw()));
EXPECT_EQ(600, heap->GetObjectId(imm_obj.raw()));
// Found after GC.
GCTestHelper::CollectNewSpace();
EXPECT_EQ(400, heap->GetObjectId(old_obj.raw()));
EXPECT_EQ(500, heap->GetObjectId(new_obj.raw()));
EXPECT_EQ(600, heap->GetObjectId(imm_obj.raw()));
// Found after GC.
GCTestHelper::CollectOldSpace();
EXPECT_EQ(400, heap->GetObjectId(old_obj.raw()));
EXPECT_EQ(500, heap->GetObjectId(new_obj.raw()));
EXPECT_EQ(600, heap->GetObjectId(imm_obj.raw()));
// Absent after reset.
heap->ResetObjectIdTable();
EXPECT_EQ(kNoValue, heap->GetObjectId(old_obj.raw()));
EXPECT_EQ(kNoValue, heap->GetObjectId(new_obj.raw()));
EXPECT_EQ(kNoValue, heap->GetObjectId(imm_obj.raw()));
}
} // namespace dart