Skip to content

Commit

Permalink
AK: Rename adopt() to adopt_ref()
Browse files Browse the repository at this point in the history
This makes it more symmetrical with adopt_own() (which is used to
create a NonnullOwnPtr from the result of a naked new.)
  • Loading branch information
awesomekling committed Apr 23, 2021
1 parent b3db01e commit b91c493
Show file tree
Hide file tree
Showing 228 changed files with 461 additions and 461 deletions.
6 changes: 3 additions & 3 deletions AK/ByteBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,20 +292,20 @@ inline void ByteBufferImpl::zero_fill()

inline NonnullRefPtr<ByteBufferImpl> ByteBufferImpl::create_uninitialized(size_t size)
{
return ::adopt(*new ByteBufferImpl(size));
return ::adopt_ref(*new ByteBufferImpl(size));
}

inline NonnullRefPtr<ByteBufferImpl> ByteBufferImpl::create_zeroed(size_t size)
{
auto buffer = ::adopt(*new ByteBufferImpl(size));
auto buffer = ::adopt_ref(*new ByteBufferImpl(size));
if (size != 0)
__builtin_memset(buffer->data(), 0, size);
return buffer;
}

inline NonnullRefPtr<ByteBufferImpl> ByteBufferImpl::copy(const void* data, size_t size)
{
return ::adopt(*new ByteBufferImpl(data, size));
return ::adopt_ref(*new ByteBufferImpl(data, size));
}

}
Expand Down
2 changes: 1 addition & 1 deletion AK/MappedFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Result<NonnullRefPtr<MappedFile>, OSError> MappedFile::map(const String& path)
if (ptr == MAP_FAILED)
return OSError(errno);

return adopt(*new MappedFile(ptr, size));
return adopt_ref(*new MappedFile(ptr, size));
}

MappedFile::MappedFile(void* ptr, size_t size)
Expand Down
4 changes: 2 additions & 2 deletions AK/NonnullRefPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ class NonnullRefPtr {
};

template<typename T>
inline NonnullRefPtr<T> adopt(T& object)
inline NonnullRefPtr<T> adopt_ref(T& object)
{
return NonnullRefPtr<T>(NonnullRefPtr<T>::Adopt, object);
}
Expand All @@ -335,5 +335,5 @@ inline void swap(NonnullRefPtr<T>& a, NonnullRefPtr<U>& b)

}

using AK::adopt;
using AK::adopt_ref;
using AK::NonnullRefPtr;
2 changes: 1 addition & 1 deletion AK/StringImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ NonnullRefPtr<StringImpl> StringImpl::create_uninitialized(size_t length, char*&
VERIFY(length);
void* slot = kmalloc(allocation_size_for_stringimpl(length));
VERIFY(slot);
auto new_stringimpl = adopt(*new (slot) StringImpl(ConstructWithInlineBuffer, length));
auto new_stringimpl = adopt_ref(*new (slot) StringImpl(ConstructWithInlineBuffer, length));
buffer = const_cast<char*>(new_stringimpl->characters());
buffer[length] = '\0';
return new_stringimpl;
Expand Down
28 changes: 14 additions & 14 deletions AK/TestSuite.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,25 +239,25 @@ using AK::TestSuite;
#define __TESTCASE_FUNC(x) __test_##x
#define __TESTCASE_TYPE(x) __TestCase_##x

#define TEST_CASE(x) \
static void __TESTCASE_FUNC(x)(); \
struct __TESTCASE_TYPE(x) { \
__TESTCASE_TYPE(x) \
() { TestSuite::the().add_case(adopt(*new TestCase(#x, __TESTCASE_FUNC(x), false))); } \
}; \
static struct __TESTCASE_TYPE(x) __TESTCASE_TYPE(x); \
#define TEST_CASE(x) \
static void __TESTCASE_FUNC(x)(); \
struct __TESTCASE_TYPE(x) { \
__TESTCASE_TYPE(x) \
() { TestSuite::the().add_case(adopt_ref(*new TestCase(#x, __TESTCASE_FUNC(x), false))); } \
}; \
static struct __TESTCASE_TYPE(x) __TESTCASE_TYPE(x); \
static void __TESTCASE_FUNC(x)()

#define __BENCHMARK_FUNC(x) __benchmark_##x
#define __BENCHMARK_TYPE(x) __BenchmarkCase_##x

#define BENCHMARK_CASE(x) \
static void __BENCHMARK_FUNC(x)(); \
struct __BENCHMARK_TYPE(x) { \
__BENCHMARK_TYPE(x) \
() { TestSuite::the().add_case(adopt(*new TestCase(#x, __BENCHMARK_FUNC(x), true))); } \
}; \
static struct __BENCHMARK_TYPE(x) __BENCHMARK_TYPE(x); \
#define BENCHMARK_CASE(x) \
static void __BENCHMARK_FUNC(x)(); \
struct __BENCHMARK_TYPE(x) { \
__BENCHMARK_TYPE(x) \
() { TestSuite::the().add_case(adopt_ref(*new TestCase(#x, __BENCHMARK_FUNC(x), true))); } \
}; \
static struct __BENCHMARK_TYPE(x) __BENCHMARK_TYPE(x); \
static void __BENCHMARK_FUNC(x)()

#define TEST_MAIN(x) \
Expand Down
8 changes: 4 additions & 4 deletions AK/Tests/TestIntrusiveList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ using IntrusiveRefPtrList = IntrusiveList<IntrusiveRefPtrItem, RefPtr<IntrusiveR

TEST_CASE(intrusive_ref_ptr_no_ref_leaks)
{
auto item = adopt(*new IntrusiveRefPtrItem());
auto item = adopt_ref(*new IntrusiveRefPtrItem());
EXPECT_EQ(1u, item->ref_count());
IntrusiveRefPtrList ref_list;

Expand All @@ -73,7 +73,7 @@ TEST_CASE(intrusive_ref_ptr_no_ref_leaks)

TEST_CASE(intrusive_ref_ptr_clear)
{
auto item = adopt(*new IntrusiveRefPtrItem());
auto item = adopt_ref(*new IntrusiveRefPtrItem());
EXPECT_EQ(1u, item->ref_count());
IntrusiveRefPtrList ref_list;

Expand All @@ -86,7 +86,7 @@ TEST_CASE(intrusive_ref_ptr_clear)

TEST_CASE(intrusive_ref_ptr_destructor)
{
auto item = adopt(*new IntrusiveRefPtrItem());
auto item = adopt_ref(*new IntrusiveRefPtrItem());
EXPECT_EQ(1u, item->ref_count());

{
Expand All @@ -107,7 +107,7 @@ using IntrusiveNonnullRefPtrList = IntrusiveList<IntrusiveNonnullRefPtrItem, Non

TEST_CASE(intrusive_nonnull_ref_ptr_intrusive)
{
auto item = adopt(*new IntrusiveNonnullRefPtrItem());
auto item = adopt_ref(*new IntrusiveNonnullRefPtrItem());
EXPECT_EQ(1u, item->ref_count());
IntrusiveNonnullRefPtrList nonnull_ref_list;

Expand Down
10 changes: 5 additions & 5 deletions AK/Tests/TestNonnullRefPtr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct Object : public RefCounted<Object> {

TEST_CASE(basics)
{
auto object = adopt(*new Object);
auto object = adopt_ref(*new Object);
EXPECT(object.ptr() != nullptr);
EXPECT_EQ(object->ref_count(), 1u);
object->ref();
Expand All @@ -33,7 +33,7 @@ TEST_CASE(basics)

TEST_CASE(assign_reference)
{
auto object = adopt(*new Object);
auto object = adopt_ref(*new Object);
EXPECT_EQ(object->ref_count(), 1u);
object = *object;
EXPECT_EQ(object->ref_count(), 1u);
Expand All @@ -45,8 +45,8 @@ TEST_CASE(assign_owner_of_self)
RefPtr<Object> parent;
};

auto parent = adopt(*new Object);
auto child = adopt(*new Object);
auto parent = adopt_ref(*new Object);
auto child = adopt_ref(*new Object);
child->parent = move(parent);

child = *child->parent;
Expand All @@ -55,7 +55,7 @@ TEST_CASE(assign_owner_of_self)

TEST_CASE(swap_with_self)
{
auto object = adopt(*new Object);
auto object = adopt_ref(*new Object);
swap(object, object);
EXPECT_EQ(object->ref_count(), 1u);
}
Expand Down
18 changes: 9 additions & 9 deletions AK/Tests/TestRefPtr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ size_t SelfAwareObject::num_destroyed = 0;

TEST_CASE(basics)
{
RefPtr<Object> object = adopt(*new Object);
RefPtr<Object> object = adopt_ref(*new Object);
EXPECT(object.ptr() != nullptr);
EXPECT_EQ(object->ref_count(), 1u);
object->ref();
Expand All @@ -45,23 +45,23 @@ TEST_CASE(basics)

TEST_CASE(assign_reference)
{
RefPtr<Object> object = adopt(*new Object);
RefPtr<Object> object = adopt_ref(*new Object);
EXPECT_EQ(object->ref_count(), 1u);
object = *object;
EXPECT_EQ(object->ref_count(), 1u);
}

TEST_CASE(assign_ptr)
{
RefPtr<Object> object = adopt(*new Object);
RefPtr<Object> object = adopt_ref(*new Object);
EXPECT_EQ(object->ref_count(), 1u);
object = object.ptr();
EXPECT_EQ(object->ref_count(), 1u);
}

TEST_CASE(copy_move_ref)
{
RefPtr<Object2> object = adopt(*new Object2);
RefPtr<Object2> object = adopt_ref(*new Object2);
EXPECT_EQ(object->ref_count(), 1u);
{
auto object2 = object;
Expand All @@ -84,8 +84,8 @@ TEST_CASE(copy_move_ref)

TEST_CASE(swap)
{
RefPtr<Object> object_a = adopt(*new Object);
RefPtr<Object> object_b = adopt(*new Object);
RefPtr<Object> object_a = adopt_ref(*new Object);
RefPtr<Object> object_b = adopt_ref(*new Object);
auto* ptr_a = object_a.ptr();
auto* ptr_b = object_b.ptr();
swap(object_a, object_b);
Expand All @@ -97,7 +97,7 @@ TEST_CASE(swap)

TEST_CASE(assign_moved_self)
{
RefPtr<Object> object = adopt(*new Object);
RefPtr<Object> object = adopt_ref(*new Object);
EXPECT_EQ(object->ref_count(), 1u);
#ifdef __clang__
# pragma clang diagnostic push
Expand All @@ -112,7 +112,7 @@ TEST_CASE(assign_moved_self)

TEST_CASE(assign_copy_self)
{
RefPtr<Object> object = adopt(*new Object);
RefPtr<Object> object = adopt_ref(*new Object);
EXPECT_EQ(object->ref_count(), 1u);

#ifdef __clang__
Expand All @@ -129,7 +129,7 @@ TEST_CASE(assign_copy_self)

TEST_CASE(self_observers)
{
RefPtr<SelfAwareObject> object = adopt(*new SelfAwareObject);
RefPtr<SelfAwareObject> object = adopt_ref(*new SelfAwareObject);
EXPECT_EQ(object->ref_count(), 1u);
EXPECT_EQ(object->m_has_one_ref_left, false);
EXPECT_EQ(SelfAwareObject::num_destroyed, 0u);
Expand Down
4 changes: 2 additions & 2 deletions AK/Tests/TestWeakPtr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ TEST_CASE(basic_weak)
WeakPtr<SimpleWeakable> weak2;

{
auto simple = adopt(*new SimpleWeakable);
auto simple = adopt_ref(*new SimpleWeakable);
weak1 = simple;
weak2 = simple;
EXPECT_EQ(weak1.is_null(), false);
Expand All @@ -54,7 +54,7 @@ TEST_CASE(weakptr_move)
WeakPtr<SimpleWeakable> weak2;

{
auto simple = adopt(*new SimpleWeakable);
auto simple = adopt_ref(*new SimpleWeakable);
weak1 = simple;
weak2 = move(weak1);
EXPECT_EQ(weak1.is_null(), true);
Expand Down
2 changes: 1 addition & 1 deletion AK/WeakPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ inline WeakPtr<U> Weakable<T>::make_weak_ptr() const
// There is a small chance that we create a new WeakLink and throw
// it away because another thread beat us to it. But the window is
// pretty small and the overhead isn't terrible.
m_link.assign_if_null(adopt(*new WeakLink(const_cast<T&>(static_cast<const T&>(*this)))));
m_link.assign_if_null(adopt_ref(*new WeakLink(const_cast<T&>(static_cast<const T&>(*this)))));
}

WeakPtr<U> weak_ptr(m_link);
Expand Down
2 changes: 1 addition & 1 deletion AK/Weakable.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class WeakLink : public RefCounted<WeakLink> {
if (!(m_consumers.fetch_add(1u << 1, AK::MemoryOrder::memory_order_acquire) & 1u)) {
T* ptr = (T*)m_ptr.load(AK::MemoryOrder::memory_order_acquire);
if (ptr && ptr->try_ref())
ref = adopt(*ptr);
ref = adopt_ref(*ptr);
}
m_consumers.fetch_sub(1u << 1, AK::MemoryOrder::memory_order_release);
}
Expand Down
4 changes: 2 additions & 2 deletions Documentation/SmartPointers.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ Objects can only be held by `RefPtr` if they meet certain criteria. Specifically

To make a class `T` reference-counted, you can simply make it inherit from `RefCounted<T>`. This will add all the necessary pieces to `T`.

**Note:** When constructing an object that derives from `RefCounted`, the reference count starts out at 1 (since 0 would mean that the object has no owners and should be deleted.) The object must therefore be "adopted" by someone who takes responsibility of that 1. This is done through the global `adopt()` function:
**Note:** When constructing an object that derives from `RefCounted`, the reference count starts out at 1 (since 0 would mean that the object has no owners and should be deleted.) The object must therefore be "adopted" by someone who takes responsibility of that 1. This is done through the global `adopt_ref()` function:

```cpp
class Bar : public RefCounted<Bar> {
...
};

RefPtr<Bar> our_object = adopt(*new Bar);
RefPtr<Bar> our_object = adopt_ref(*new Bar);
RefPtr<Bar> another_owner = our_object;
```
Expand Down
2 changes: 1 addition & 1 deletion Kernel/Devices/Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Device : public File {
template<typename AsyncRequestType, typename... Args>
NonnullRefPtr<AsyncRequestType> make_request(Args&&... args)
{
auto request = adopt(*new AsyncRequestType(*this, forward<Args>(args)...));
auto request = adopt_ref(*new AsyncRequestType(*this, forward<Args>(args)...));
ScopedSpinLock lock(m_requests_lock);
bool was_empty = m_requests.is_empty();
m_requests.append(request);
Expand Down
2 changes: 1 addition & 1 deletion Kernel/Devices/HID/I8042Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Kernel {

UNMAP_AFTER_INIT NonnullRefPtr<I8042Controller> I8042Controller::initialize()
{
return adopt(*new I8042Controller());
return adopt_ref(*new I8042Controller());
}

RefPtr<MouseDevice> I8042Controller::mouse() const
Expand Down
2 changes: 1 addition & 1 deletion Kernel/Devices/HID/PS2KeyboardDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ void PS2KeyboardDevice::handle_irq(const RegisterState&)

UNMAP_AFTER_INIT RefPtr<PS2KeyboardDevice> PS2KeyboardDevice::try_to_initialize(const I8042Controller& ps2_controller)
{
auto device = adopt(*new PS2KeyboardDevice(ps2_controller));
auto device = adopt_ref(*new PS2KeyboardDevice(ps2_controller));
if (device->initialize())
return device;
return nullptr;
Expand Down
2 changes: 1 addition & 1 deletion Kernel/Devices/HID/PS2MouseDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ void PS2MouseDevice::set_sample_rate(u8 rate)

UNMAP_AFTER_INIT RefPtr<PS2MouseDevice> PS2MouseDevice::try_to_initialize(const I8042Controller& ps2_controller)
{
auto device = adopt(*new PS2MouseDevice(ps2_controller));
auto device = adopt_ref(*new PS2MouseDevice(ps2_controller));
if (device->initialize())
return device;
return nullptr;
Expand Down
2 changes: 1 addition & 1 deletion Kernel/Devices/HID/VMWareMouseDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ UNMAP_AFTER_INIT RefPtr<VMWareMouseDevice> VMWareMouseDevice::try_to_initialize(
return {};
if (!VMWareBackdoor::the()->vmmouse_is_absolute())
return {};
auto device = adopt(*new VMWareMouseDevice(ps2_controller));
auto device = adopt_ref(*new VMWareMouseDevice(ps2_controller));
if (device->initialize())
return device;
return {};
Expand Down
2 changes: 1 addition & 1 deletion Kernel/FileSystem/AnonymousFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class AnonymousFile final : public File {
public:
static NonnullRefPtr<AnonymousFile> create(NonnullRefPtr<AnonymousVMObject> vmobject)
{
return adopt(*new AnonymousFile(move(vmobject)));
return adopt_ref(*new AnonymousFile(move(vmobject)));
}

virtual ~AnonymousFile() override;
Expand Down
2 changes: 1 addition & 1 deletion Kernel/FileSystem/Custody.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Custody : public RefCounted<Custody> {
public:
static NonnullRefPtr<Custody> create(Custody* parent, const StringView& name, Inode& inode, int mount_flags)
{
return adopt(*new Custody(parent, name, inode, mount_flags));
return adopt_ref(*new Custody(parent, name, inode, mount_flags));
}

~Custody();
Expand Down
Loading

0 comments on commit b91c493

Please sign in to comment.