GCSIM
Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
heap_manager Class Reference

manages the memory on the heap. More...

#include <heap-manager.hpp>

Public Member Functions

 heap_manager (size_t hm_thread_count, size_t gc_thread_count=1)
 creates the instance of the heap manager.
 
 ~heap_manager ()=default
 deletes the instance of the heap manager.
 
 heap_manager (const heap_manager &)=delete
 deleted copy constructor.
 
heap_manageroperator= (const heap_manager &)=delete
 deleted assignment operator.
 
 heap_manager (heap_manager &&)=delete
 deleted move constructor.
 
heap_manageroperator= (heap_manager &&)=delete
 deleted move assignment operator.
 
headerallocate (uint32_t bytes)
 tries to allocate memory on the heap.
 
void add_root (std::string key, std::unique_ptr< root_set_base > base)
 adds new root to a root-set-table.
 
root_set_baseget_root (const std::string &key)
 getter for the root from the root-set-table.
 
void remove_root (const std::string &key)
 removes root from the root-set-table.
 
void clear_roots () noexcept
 removes all roots from the root-set-table.
 
void collect_garbage ()
 starts the garbage collection.
 

Private Member Functions

bool should_run_gc () const noexcept
 checks if enough time has passed since last garbage collection.
 
void periodic_gc_loop (std::stop_token stop_token)
 periodic garbage collection loop.
 
size_t get_segment_category_index (size_t segment_index) const noexcept
 getter for the index of the segment based on object size category.
 
segmentget_segment (size_t segment_index)
 getter for the segment based on index.
 
int find_suitable_segment (uint32_t bytes) noexcept
 finds a segment that can store required bytes.
 
headerallocate_from_segment (size_t segment_index, uint32_t bytes)
 allocates object on the heap segment.
 
void coalesce_segment (size_t segment_index)
 merges free blocks on the segment.
 
void coalesce_segments ()
 merges free blocks of segments.
 

Private Attributes

std::mutex segment_locks [TOTAL_SEGMENTS]
 locks for heap segments.
 
std::mutex root_set_mutex
 locks the root-set-table.
 
heap heap_memory
 segmented memory for object allocation.
 
segment_free_memory_table free_memory_table
 table containing the linked lists of free memory for each segment.
 
root_set_table root_set
 table containing the roots.
 
thread_pool heap_manager_thread_pool
 thread pool for coalescing segments.
 
garbage_collector gc
 gc for heap cleanup.
 
std::atomic< bool > gc_in_progress {false}
 indicates whether gc is currently running.
 
std::atomic< size_t > last_small_segment {SMALL_OBJECT_SEGMENTS - 1}
 small object segment that was used last, default to last.
 
std::atomic< size_t > last_medium_segment {SMALL_OBJECT_SEGMENTS + MEDIUM_OBJECT_SEGMENTS - 1}
 medium object segment that was used last, default to last.
 
std::atomic< size_t > last_large_segment {SMALL_OBJECT_SEGMENTS + MEDIUM_OBJECT_SEGMENTS + LARGE_OBJECT_SEGMENTS - 1}
 large object segment that was used last, default to last.
 
std::atomic< uint64_t > last_gc_time_ms
 last time garbage collection was done.
 
std::jthread gc_timer_thread
 background gc thread.
 

Static Private Attributes

static constexpr std::chrono::milliseconds MIN_GC_INTERVAL {100}
 minimum time between GC runs.
 
static constexpr std::chrono::seconds PERIODIC_GC_INTERVAL {1}
 periodic gc interval.
 

Detailed Description

manages the memory on the heap.

Constructor & Destructor Documentation

◆ heap_manager() [1/3]

heap_manager::heap_manager ( size_t  hm_thread_count,
size_t  gc_thread_count = 1 
)

creates the instance of the heap manager.

Parameters
gc_thread_count- size of gc thread pool, defaults to 1.

initializes the segments on the heap, initializes free memory tables.

◆ ~heap_manager()

heap_manager::~heap_manager ( )
default

deletes the instance of the heap manager.

◆ heap_manager() [2/3]

heap_manager::heap_manager ( const heap_manager )
delete

deleted copy constructor.

◆ heap_manager() [3/3]

heap_manager::heap_manager ( heap_manager &&  )
delete

deleted move constructor.

Member Function Documentation

◆ add_root()

void heap_manager::add_root ( std::string  key,
std::unique_ptr< root_set_base base 
)

adds new root to a root-set-table.

Parameters
key- name of the root.
base- element of the root-set-table.

◆ allocate()

header * heap_manager::allocate ( uint32_t  bytes)

tries to allocate memory on the heap.

Parameters
bytes- number of bytes that need to be allocated.
Returns
pointer to header of the object if allocation is successful, nullptr otherwise.

◆ allocate_from_segment()

header * heap_manager::allocate_from_segment ( size_t  segment_index,
uint32_t  bytes 
)
private

allocates object on the heap segment.

Parameters
segment_index- index of the segment.
bytes- required memory.
Returns
pointer to the header of the object.

◆ clear_roots()

void heap_manager::clear_roots ( )
noexcept

removes all roots from the root-set-table.

◆ coalesce_segment()

void heap_manager::coalesce_segment ( size_t  segment_index)
private

merges free blocks on the segment.

Parameters
segment_index- index of the segment.

◆ coalesce_segments()

void heap_manager::coalesce_segments ( )
private

merges free blocks of segments.

Warning
must be called during the STW, after gc finishes collecting.

◆ collect_garbage()

void heap_manager::collect_garbage ( )

starts the garbage collection.

"Stop the world", mark & sweep collection and coalescing of segments.

Warning
can be called by client, but it may be expensive if called frequently.

◆ find_suitable_segment()

int heap_manager::find_suitable_segment ( uint32_t  bytes)
privatenoexcept

finds a segment that can store required bytes.

Parameters
bytes- number of bytes that need to be allocated.
Returns
index of the segment if segment can allocate enough bytes, -1 otherwise.

◆ get_root()

root_set_base * heap_manager::get_root ( const std::string &  key)

getter for the root from the root-set-table.

Parameters
key- name of the root.
Returns
pointer to a root.

◆ get_segment()

segment & heap_manager::get_segment ( size_t  segment_index)
private

getter for the segment based on index.

Parameters
segment_index- index of the segment.
Returns
reference to a segment.

◆ get_segment_category_index()

size_t heap_manager::get_segment_category_index ( size_t  segment_index) const
privatenoexcept

getter for the index of the segment based on object size category.

Parameters
segment_index- index of the segment 0 to (n-1).
Returns
index of the segment in an object category.

◆ operator=() [1/2]

heap_manager & heap_manager::operator= ( const heap_manager )
delete

deleted assignment operator.

◆ operator=() [2/2]

heap_manager & heap_manager::operator= ( heap_manager &&  )
delete

deleted move assignment operator.

◆ periodic_gc_loop()

void heap_manager::periodic_gc_loop ( std::stop_token  stop_token)
private

periodic garbage collection loop.

Parameters
stop_token- token for stopping periodic gc.

◆ remove_root()

void heap_manager::remove_root ( const std::string &  key)

removes root from the root-set-table.

Parameters
key- const reference to a name of the root-set-table.

◆ should_run_gc()

bool heap_manager::should_run_gc ( ) const
privatenoexcept

checks if enough time has passed since last garbage collection.

Returns
true if gc should run, false otherwise.

Member Data Documentation

◆ free_memory_table

segment_free_memory_table heap_manager::free_memory_table
private

table containing the linked lists of free memory for each segment.

◆ gc

garbage_collector heap_manager::gc
private

gc for heap cleanup.

◆ gc_in_progress

std::atomic<bool> heap_manager::gc_in_progress {false}
private

indicates whether gc is currently running.

◆ gc_timer_thread

std::jthread heap_manager::gc_timer_thread
private

background gc thread.

◆ heap_manager_thread_pool

thread_pool heap_manager::heap_manager_thread_pool
private

thread pool for coalescing segments.

◆ heap_memory

heap heap_manager::heap_memory
private

segmented memory for object allocation.

◆ last_gc_time_ms

std::atomic<uint64_t> heap_manager::last_gc_time_ms
private

last time garbage collection was done.

◆ last_large_segment

std::atomic<size_t> heap_manager::last_large_segment {SMALL_OBJECT_SEGMENTS + MEDIUM_OBJECT_SEGMENTS + LARGE_OBJECT_SEGMENTS - 1}
private

large object segment that was used last, default to last.

◆ last_medium_segment

std::atomic<size_t> heap_manager::last_medium_segment {SMALL_OBJECT_SEGMENTS + MEDIUM_OBJECT_SEGMENTS - 1}
private

medium object segment that was used last, default to last.

◆ last_small_segment

std::atomic<size_t> heap_manager::last_small_segment {SMALL_OBJECT_SEGMENTS - 1}
private

small object segment that was used last, default to last.

◆ MIN_GC_INTERVAL

constexpr std::chrono::milliseconds heap_manager::MIN_GC_INTERVAL {100}
staticconstexprprivate

minimum time between GC runs.

◆ PERIODIC_GC_INTERVAL

constexpr std::chrono::seconds heap_manager::PERIODIC_GC_INTERVAL {1}
staticconstexprprivate

periodic gc interval.

◆ root_set

root_set_table heap_manager::root_set
private

table containing the roots.

◆ root_set_mutex

std::mutex heap_manager::root_set_mutex
private

locks the root-set-table.

◆ segment_locks

std::mutex heap_manager::segment_locks[TOTAL_SEGMENTS]
private

locks for heap segments.


The documentation for this class was generated from the following files: