Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch PROJECTILE to use PagedEntityContainer<PROJECTILE> as backing storage #3675

Merged
merged 3 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions lib/framework/paged_entity_container.h
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,8 @@ class PagedEntityContainer
template<bool IsConst2>
friend class IteratorImpl;

friend class PagedEntityContainer;

PageIndex _pageIdx;
ParentContainerType& _c;
};
Expand Down Expand Up @@ -626,13 +628,46 @@ class PagedEntityContainer
return end();
}

// Tries to look up the relevant page index for `x` and return an iterator to it.
// Returns `end()` if not found.
iterator find(T& x)
{
auto* addr = reinterpret_cast<AlignedElementStorage*>(&x);
assert(is_properly_aligned_ptr(addr));
if (empty())
{
return end();
}
for (size_t i = 0, end = _pages.size(); i != end; ++i)
{
Page& p = _pages[i];
auto* storage = p.storage();
// If the address is inside the current page, calculate the index from raw offset.
if (addr >= storage && addr <= &storage[p.max_valid_index()])
{
// Pointer subtraction of yields the difference between
// array subscripts in the current case.
const ptrdiff_t idx = addr - storage;
return iterator(*this, {i, static_cast<size_t>(idx)});
}
}
return end();
}

const_iterator find(const T& x) const
{
return const_iterator(const_cast<PagedEntityContainer<T>*>(this)->find(const_cast<T&>(x)));
}

void erase(const_iterator it)
{
assert(&it._c == this);
erase(it.index());
}

void erase(iterator it)
{
assert(&it._c == this);
erase(it.index());
}

Expand Down Expand Up @@ -746,6 +781,11 @@ class PagedEntityContainer
return { INVALID_PAGE_IDX, INVALID_SLOT_IDX };
}

static bool is_properly_aligned_ptr(void* addr)
{
return (reinterpret_cast<uintptr_t>(addr) % alignof(T)) == 0;
}

std::vector<Page> _pages;
SlotIndexType _maxIndex = INVALID_SLOT_IDX;
size_t _size = 0;
Expand Down
Loading
Loading