mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 09:00:22 +00:00
AK: Remove clear() from FixedArray and fixate its allocation guarantees
FixedArray always *almost* had the following allocation guarantees: There is (possibly) one allocation in the constructor and one (or more) deallocation(s) in the destructor. No other operation allocates or deallocates. With this removal of the public clear() method, which nobody except the test used anyways, those guarantees are now completely true and furthermore fixated with an explanatory comment.
This commit is contained in:
parent
9bf2d0b718
commit
1d144ed6fc
Notes:
sideshowbarker
2024-07-17 21:00:30 +09:00
Author: https://github.com/kleinesfilmroellchen Commit: https://github.com/SerenityOS/serenity/commit/1d144ed6fcc Pull-request: https://github.com/SerenityOS/serenity/pull/11844 Reviewed-by: https://github.com/bgianfo Reviewed-by: https://github.com/linusg
2 changed files with 3 additions and 8 deletions
|
@ -14,6 +14,8 @@
|
||||||
|
|
||||||
namespace AK {
|
namespace AK {
|
||||||
|
|
||||||
|
// FixedArray is an Array with a size only known at run-time.
|
||||||
|
// It guarantees to only allocate when being constructed, and to only deallocate when being destructed.
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class FixedArray {
|
class FixedArray {
|
||||||
public:
|
public:
|
||||||
|
@ -68,17 +70,13 @@ public:
|
||||||
FixedArray<T>& operator=(FixedArray<T>&&) = delete;
|
FixedArray<T>& operator=(FixedArray<T>&&) = delete;
|
||||||
|
|
||||||
~FixedArray()
|
~FixedArray()
|
||||||
{
|
|
||||||
clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
void clear()
|
|
||||||
{
|
{
|
||||||
if (!m_elements)
|
if (!m_elements)
|
||||||
return;
|
return;
|
||||||
for (size_t i = 0; i < m_size; ++i)
|
for (size_t i = 0; i < m_size; ++i)
|
||||||
m_elements[i].~T();
|
m_elements[i].~T();
|
||||||
kfree_sized(m_elements, sizeof(T) * m_size);
|
kfree_sized(m_elements, sizeof(T) * m_size);
|
||||||
|
// NOTE: should prevent use-after-free early
|
||||||
m_size = 0;
|
m_size = 0;
|
||||||
m_elements = nullptr;
|
m_elements = nullptr;
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,4 @@ TEST_CASE(ints)
|
||||||
EXPECT_EQ(ints[0], 0);
|
EXPECT_EQ(ints[0], 0);
|
||||||
EXPECT_EQ(ints[1], 1);
|
EXPECT_EQ(ints[1], 1);
|
||||||
EXPECT_EQ(ints[2], 2);
|
EXPECT_EQ(ints[2], 2);
|
||||||
|
|
||||||
ints.clear();
|
|
||||||
EXPECT_EQ(ints.size(), 0u);
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue