Public Types | Public Member Functions
mem_pool< total_size, block_shift > Class Template Reference

Public Types

using size_type = size_t
 
using shift_type = size_t
 
using integer_pointer = uintptr_t
 

Public Member Functions

 mem_pool (integer_pointer addr) noexcept _testing
 
 ~mem_pool ()=default
 
integer_pointer alloc (size_type size)
 
void free (integer_pointer addr) noexcept
 
bool contains (integer_pointer addr) const noexcept
 
size_type size (integer_pointer addr) const noexcept
 
void clear () noexcept
 
 mem_pool (const mem_pool &)=delete
 
mem_pooloperator= (const mem_pool &)=delete
 
 mem_pool (mem_pool &&) noexcept=delete
 
mem_pooloperator= (mem_pool &&) noexcept=delete
 

Detailed Description

template<size_t total_size, size_t block_shift>
class mem_pool< total_size, block_shift >

*INDENT-OFF*Memory Pool

The VMM has to manage a lot of memory. This includes:

The problem is, some of these memory pools point to pre-allocated space while others point to virtual memory that simply needs to be reserved for memory mapping (classic alloc vs map problem). In all cases, a contiguous memory space needs to be divided up and managed. This memory pool provides a really simple "next fit" algorithm for managing these different memory pools. Clearly there is room for improvement with this algorithm, but it should work for basic allocations. Further optimizations could be done using custom new / delete operators at the class level if needed until we can provide a more complicated algorithm.

Parameters
total_sizetotal size in bytes of the memory pool
block_shiftblock size in bit shifts (i.e. 8 bytes == 3 bits)

Definition at line 81 of file mem_pool.h.

Member Typedef Documentation

◆ size_type

template<size_t total_size, size_t block_shift>
using mem_pool< total_size, block_shift >::size_type = size_t

Definition at line 89 of file mem_pool.h.

◆ shift_type

template<size_t total_size, size_t block_shift>
using mem_pool< total_size, block_shift >::shift_type = size_t

Definition at line 90 of file mem_pool.h.

◆ integer_pointer

template<size_t total_size, size_t block_shift>
using mem_pool< total_size, block_shift >::integer_pointer = uintptr_t

Definition at line 91 of file mem_pool.h.

Constructor & Destructor Documentation

◆ mem_pool() [1/3]

template<size_t total_size, size_t block_shift>
mem_pool< total_size, block_shift >::mem_pool ( integer_pointer  addr)
inlinenoexcept

Constructor

Creates a memory pool with the starting virtual address of addr.

Precondition
expects: addr != 0
Postcondition
ensures: none
Parameters
addrthe starting address of the memory pool

Definition at line 101 of file mem_pool.h.

◆ ~mem_pool()

template<size_t total_size, size_t block_shift>
mem_pool< total_size, block_shift >::~mem_pool ( )
default

Default Destructor

◆ mem_pool() [2/3]

template<size_t total_size, size_t block_shift>
mem_pool< total_size, block_shift >::mem_pool ( const mem_pool< total_size, block_shift > &  )
delete

◆ mem_pool() [3/3]

template<size_t total_size, size_t block_shift>
mem_pool< total_size, block_shift >::mem_pool ( mem_pool< total_size, block_shift > &&  )
deletenoexcept

Member Function Documentation

◆ alloc()

template<size_t total_size, size_t block_shift>
integer_pointer mem_pool< total_size, block_shift >::alloc ( size_type  size)
inline

Allocate Memory

Allocates memory from the memory pool whose size is greater than or equal to size. The memory pool will always be a multiple of 1 << block_shift. Memory allocated will always have an alignment equal to block_shift plus the starting address provided when creating the memory pool. For this reason, if a specific alignment is needed, ensure the start address has this same alignment when creating the memory pool

Precondition
expects: size > 0
expects: size <= total_size
Postcondition
ensures: ret != nullptr
Parameters
sizethe number of bytes to allocate
Returns
the starting address of the

Definition at line 137 of file mem_pool.h.

◆ free()

template<size_t total_size, size_t block_shift>
void mem_pool< total_size, block_shift >::free ( integer_pointer  addr)
inlinenoexcept

Free Memory

Free's previously allocated memory.

Precondition
expects: none
Postcondition
ensures: none
Parameters
addrthe address to free

Definition at line 169 of file mem_pool.h.

◆ contains()

template<size_t total_size, size_t block_shift>
bool mem_pool< total_size, block_shift >::contains ( integer_pointer  addr) const
inlinenoexcept

Contains Address

Returns true if this memory pool contains this address, returns false otherwise.

Precondition
expects: none
Postcondition
ensures: none
Parameters
addrto lookup

Definition at line 196 of file mem_pool.h.

◆ size()

template<size_t total_size, size_t block_shift>
size_type mem_pool< total_size, block_shift >::size ( integer_pointer  addr) const
inlinenoexcept

Allocation Size

Locates and returns the size of previously allocated memory from this pool. Like free, this function will not crash but instead will return 0 given invalid inputs.

Precondition
expects: none
Postcondition
ensures: none
Parameters
addrto lookup

Definition at line 211 of file mem_pool.h.

◆ clear()

template<size_t total_size, size_t block_shift>
void mem_pool< total_size, block_shift >::clear ( )
inlinenoexcept

Clear Memory Pool

This is a very dangerous function, and will effectively run free() on all memory previously allocated.

Precondition
expects: none
Postcondition
ensures: none

Definition at line 235 of file mem_pool.h.

◆ operator=() [1/2]

template<size_t total_size, size_t block_shift>
mem_pool& mem_pool< total_size, block_shift >::operator= ( const mem_pool< total_size, block_shift > &  )
delete

◆ operator=() [2/2]

template<size_t total_size, size_t block_shift>
mem_pool& mem_pool< total_size, block_shift >::operator= ( mem_pool< total_size, block_shift > &&  )
deletenoexcept

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