26#ifndef TEMPTABLE_STORAGE_H
27#define TEMPTABLE_STORAGE_H
190 static constexpr size_t ALIGN_TO =
alignof(
void *);
349 : m_storage(const_cast<
Storage *>(storage)),
350 m_element(const_cast<
Element *>(element)) {}
357 assert(m_storage !=
nullptr || element ==
nullptr);
358 m_element =
const_cast<Element *
>(element);
367 return !(*
this == rhs);
371 assert(m_storage !=
nullptr);
372 assert(*
this != m_storage->end());
375 if (m_storage->element_last_on_page(m_element)) {
376 Page *next_page = *m_storage->element_next_page_ptr(m_element);
377 if (next_page ==
nullptr) {
379 *
this = m_storage->end();
382 m_element = m_storage->first_possible_element_on_page(next_page);
384 m_element = m_storage->next_element(m_element);
386 }
while (m_storage->element_deleted(m_element));
392 assert(m_storage !=
nullptr);
393 assert(*
this != m_storage->begin());
399 if (m_element ==
nullptr) {
400 m_element = m_storage->back();
401 }
else if (m_storage->element_first_on_page(m_element)) {
403 Page *prev_page = *m_storage->element_prev_page_ptr(m_element);
404 assert(prev_page !=
nullptr);
405 m_element = m_storage->last_possible_element_on_page(prev_page);
406 assert(m_storage->element_last_on_page(m_element));
408 m_element = m_storage->prev_element(m_element);
410 }
while (m_storage->element_deleted(m_element));
427 *
this = std::move(other);
436 rhs.m_allocator =
nullptr;
439 rhs.m_element_size = 0;
442 rhs.m_bytes_used_per_element = 0;
445 rhs.m_number_of_elements_per_page = 0;
448 rhs.m_number_of_elements = 0;
451 rhs.m_first_page =
nullptr;
454 rhs.m_last_page =
nullptr;
457 rhs.m_last_element =
nullptr;
592 m_allocator->deallocate(
static_cast<uint8_t *
>(page_to_free),
602 Iterator next_element_position = position;
603 ++next_element_position;
614 return next_element_position;
697 return reinterpret_cast<Page **
>(
static_cast<uint8_t *
>(element) -
703 return reinterpret_cast<Page **
>(
static_cast<uint8_t *
>(element) +
708 assert(element !=
nullptr);
714 assert(element !=
nullptr);
721 assert(
page !=
nullptr);
722 return static_cast<uint8_t *
>(
page) +
sizeof(
Page *);
727 assert(
page !=
nullptr);
728 return static_cast<uint8_t *
>(
page) +
sizeof(
Page *) +
733 assert(
page !=
nullptr);
734 return reinterpret_cast<Page **
>(
page);
738 assert(
page !=
nullptr);
741 return reinterpret_cast<Page **
>(
742 static_cast<uint8_t *
>(
page) +
sizeof(
Page *) +
Custom memory allocator.
Definition: allocator.h:353
Iterator over a Storage object.
Definition: storage.h:50
bool operator!=(const Iterator &rhs) const
Compare with another iterator.
Definition: storage.h:366
Element * m_element
Current element.
Definition: storage.h:111
Iterator(const Iterator &)=default
Copy-construct from another iterator.
Iterator & operator++()
Advance the iterator one element forward.
Definition: storage.h:370
Iterator()
Default constructor.
Definition: storage.h:345
Iterator & operator=(const Iterator &)=default
Copy-assign from another iterator.
Element * operator*() const
Dereference the iterator to the element it points to.
Definition: storage.h:352
bool operator==(const Iterator &rhs) const
Compare with another iterator.
Definition: storage.h:362
Storage * m_storage
Storage over which the iterator operates.
Definition: storage.h:108
Iterator & operator--()
Recede the iterator one element backwards.
Definition: storage.h:391
Iterator & operator=(const Element *element)
Assign a new position within the same Storage object.
Definition: storage.h:356
Storage container.
Definition: storage.h:42
Element * back()
Get the last element.
Definition: storage.h:510
Allocator< uint8_t > * m_allocator
Allocator to use for allocating new pages.
Definition: storage.h:317
void Page
Type used for pages.
Definition: storage.h:47
size_t element_size() const
Get the element size.
Definition: storage.h:506
size_t page_size() const
Calculate the size of a page.
Definition: storage.h:646
Storage(const Storage &)=delete
Copy constructing is disabled, too expensive and not necessary.
Page ** element_prev_page_ptr(Element *first) const
Get the previous page.
Definition: storage.h:695
static constexpr uint8_t ELEMENT_LAST_ON_PAGE
Flag that denotes an element is the last element on a page.
Definition: storage.h:196
Element * m_last_element
Last used element in the last page of the storage.
Definition: storage.h:340
Element * last_possible_element_on_page(Page *page) const
Get the last possible element of a page (not the last occupied).
Definition: storage.h:725
Iterator end() const
Get an iterator, positioned after the last element.
Definition: storage.h:484
size_t size() const
Get the number of elements in the storage.
Definition: storage.h:508
size_t m_number_of_elements_per_page
Maximum number of elements a page can store.
Definition: storage.h:327
Page * m_last_page
Last page of the storage.
Definition: storage.h:336
bool element_last_on_page(Element *element) const
Check if element is the last on its page.
Definition: storage.h:671
Iterator begin() const
Get an iterator, positioned on the first element.
Definition: storage.h:464
size_t number_of_elements_per_page() const
A simple getter.
Definition: storage.h:642
Page ** page_next_page_ptr(Page *page) const
Get a pointer inside a page to the place denoting the next page.
Definition: storage.h:737
Element * next_element(Element *element) const
Get the next element of a given element on the same page.
Definition: storage.h:713
Page * m_first_page
First page of the storage.
Definition: storage.h:333
Element * allocate_back()
Allocate space for one more element at the end and return a pointer to it.
Definition: storage.h:512
static constexpr size_t ALIGN_TO
Align elements to this number of bytes.
Definition: storage.h:190
size_t m_bytes_used_per_element
Number of bytes used per element.
Definition: storage.h:324
void clear()
Delete all elements in the storage.
Definition: storage.h:617
static constexpr uint8_t ELEMENT_DELETED
Flag that denotes an element is deleted.
Definition: storage.h:200
Element * first_possible_element_on_page(Page *page) const
Get the first element of a page.
Definition: storage.h:719
Storage(Allocator< uint8_t > *allocator)
Constructor.
Definition: storage.h:415
static constexpr size_t META_BYTES_PER_ELEMENT
Extra bytes per element for element metadata.
Definition: storage.h:204
Page ** page_prev_page_ptr(Page *page) const
Get a pointer inside a page to the place denoting the previous page.
Definition: storage.h:732
bool element_first_on_page(Element *element) const
Check if element is the first on its page.
Definition: storage.h:658
size_t m_element_size
Element size in bytes.
Definition: storage.h:320
Storage & operator=(const Storage &)=delete
Copy assignment is disabled, too expensive and not necessary.
static constexpr size_t META_BYTES_PER_PAGE
Extra bytes per page for page metadata.
Definition: storage.h:208
~Storage()
Destructor.
Definition: storage.h:462
Iterator erase(const Iterator &position)
Delete the element pointed to by position.
Definition: storage.h:601
static constexpr uint8_t ELEMENT_FIRST_ON_PAGE
Flag that denotes an element is the first element on a page.
Definition: storage.h:193
bool element_deleted(Element *element) const
Check if element is deleted.
Definition: storage.h:683
void Element
Type used for elements.
Definition: storage.h:45
uint8_t * element_meta(Element *element) const
Get a pointer to element's meta byte(s).
Definition: storage.h:654
Page ** element_next_page_ptr(Element *last) const
Get the next page.
Definition: storage.h:701
void deallocate_back()
Destroy the last element.
Definition: storage.h:568
size_t m_number_of_elements
Number of elements in the container.
Definition: storage.h:330
Element * prev_element(Element *element) const
Get the previous element of a given element on the same page.
Definition: storage.h:707
const char * p
Definition: ctype-mb.cc:1234
int page
Definition: ctype-mb.cc:1233
Fido Client Authentication nullptr
Definition: fido_client_plugin.cc:221
Definition: allocator.h:44
constexpr size_t STORAGE_PAGE_SIZE
Storage page size.
Definition: constants.h:68
TempTable custom allocator.