nlib
nn::nlib::heap::CentralHeap Class Referencefinal

Central heap class whose use is paired with CachedHeap. It is used to implement nmalloc. More...

#include "nn/nlib/heap/CentralHeap.h"

Public Member Functions

void * Alloc (size_t n) noexcept
 Allocates a memory block. More...
 
void * Alloc (size_t n, size_t algn) noexcept
 Allocates a memory block with a specific alignment. More...
 
errno_t Free (void *p) noexcept
 Frees memory. More...
 
errno_t FreeWithSize (void *p, size_t size) noexcept
 Frees a memory region of an allocated size you specify. More...
 
size_t GetAllocSize (const void *p) noexcept
 Returns the size of memory actually allocated to p.
 
errno_t Realloc (void *ptr, size_t size, void **p) noexcept
 Reallocates the memory that has been allocated with its size specified. More...
 
errno_t WalkAllocatedPtrs (nmalloc_heapwalk_callback func, void *user_ptr) noexcept
 For more information, see nmalloc_walk_allocated_ptrs.
 
Basic Member Functions
 CentralHeap () noexcept
 Instantiates the object with default parameters (default constructor).
 
 ~CentralHeap () noexcept
 Destructor.
 
Initialization and Finalization
errno_t Init (void *start, size_t size, uint32_t heap_option) noexcept
 Initializes the heap. More...
 
void Finalize () noexcept
 Finalizes use of the heap and changes the object's status to what it was prior to initialization.
 
bool MakeCache (CachedHeap *ptr) noexcept
 Initializes CachedHeap and associates it with this CentralHeap. More...
 

Static Public Attributes

static const size_t kPageSize
 Page size of the heap (not related to the operating system page size).
 

Detailed Description

Central heap class whose use is paired with CachedHeap. It is used to implement nmalloc.

Description
It can be used to create CachedHeap instances (thread-specific heaps). CachedHeap allocates memory from and returns memory to the central heap. Memory allocation and freeing by user applications are generally performed using CachedHeap. Memory can also be allocated directly from the CentralHeap, however, this decreases efficiency.

Definition at line 31 of file CentralHeap.h.

Member Function Documentation

◆ Alloc() [1/2]

nn::nlib::heap::CentralHeap::Alloc ( size_t  n)
inlinenoexcept

Allocates a memory block.

Parameters
[in]nSpecifies the memory block size.
Returns
Returns the pointer for the allocated memory. NULL if process fails.

Definition at line 43 of file CentralHeap.h.

◆ Alloc() [2/2]

nn::nlib::heap::CentralHeap::Alloc ( size_t  n,
size_t  algn 
)
noexcept

Allocates a memory block with a specific alignment.

Parameters
[in]nSpecifies the memory block size.
[in]algnSpecifies the alignment.
Returns
Returns the pointer for the allocated memory. NULL if process fails.
The alignment must be a power of two.

◆ Free()

nn::nlib::heap::CentralHeap::Free ( void *  p)
noexcept

Frees memory.

Parameters
[in]pSpecifies a pointer to the memory block to free.
Returns
Returns 0 on success. Any nonzero value indicates that an error has occurred (for example, the pointer wasn't in the heap).

◆ FreeWithSize()

nn::nlib::heap::CentralHeap::FreeWithSize ( void *  p,
size_t  size 
)
noexcept

Frees a memory region of an allocated size you specify.

Parameters
[in]pA pointer to the memory block you want to free.
[in]sizeThe size that has been allocated to p.
Returns
Returns 0 if the memory was successfully freed. Any other value indicates that an issue was detected (For example, p may never have been allocated).

◆ Init()

nn::nlib::heap::CentralHeap::Init ( void *  start,
size_t  size,
uint32_t  heap_option 
)
noexcept

Initializes the heap.

Parameters
[in]startSpecifies a pointer to the beginning of the heap.
[in]sizeSpecifies the size of the heap. It must be a multiple of kPageSize.
[in]heap_optionSpecifies the heap-instantiation options. See NMallocSettings::heap_option.
Returns
0 Indicates that initialization was successful. Any other return value indicates failure.
Description
If a start is not aligned to kPageSize, the region is truncated to the memory boundary before use.

◆ MakeCache()

nn::nlib::heap::CentralHeap::MakeCache ( CachedHeap ptr)
noexcept

Initializes CachedHeap and associates it with this CentralHeap.

Parameters
[out]ptrSpecifies a pointer to the CachedHeap object to initialize.
Returns
Returns true on success.
Description
When this function is called, the central heap allocates a cache region and associates itself with CachedHeap. Thereafter, memory can be allocated and freed using CachedHeap.

◆ Realloc()

nn::nlib::heap::CentralHeap::Realloc ( void *  ptr,
size_t  size,
void **  p 
)
noexcept

Reallocates the memory that has been allocated with its size specified.

Parameters
[in]ptrA pointer to the memory you want to reallocate.
[in]sizeThe size, in bytes, after the change.
[out]pA pointer to the reallocated memory.
Returns
Returns 0 if the memory has been successfully reallocated. Any other value indicates that an issue was detected (For example, no memory has never been allocated for ptr).
Description
Note that, unlike the standard realloc function, the ptr parameter cannot be set to NULL, and the size parameter cannot be set to 0.

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