nlib
nn::nlib::heap::CachedHeap Class Referencefinal

Thread-specific cache class whose use is paired with CentralHeap. More...

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

Public Member Functions

void * Alloc (size_t n) noexcept
 Allocates a memory region of the specified size (in bytes). More...
 
void * Alloc (size_t n, size_t algn) noexcept
 Allocates memory with a specific alignment. More...
 
size_t GetAllocSize (const void *p) noexcept
 Returns the size of memory actually allocated for the p parameter.
 
errno_t Free (void *p) noexcept
 Frees a memory region. More...
 
errno_t FreeWithSize (void *p, size_t size) noexcept
 Frees memory that was allocated with a specific size. More...
 
errno_t Realloc (void *ptr, size_t size, void **p) noexcept
 Reallocates the specified memory to have the specified size. More...
 
Basic Member Functions
 CachedHeap () noexcept
 Instantiates the object with default parameters (default constructor).
 
 ~CachedHeap () noexcept
 Destructor. Calls Finalize.
 
CachedHeapassign (CachedHeap &rhs, move_tag)
 Assigns the object by using swap for a move.
 
 CachedHeap (CachedHeap &rhs, move_tag)
 Instantiates the object by using swap for a move.
 
 CachedHeap (CachedHeap &&rhs)
 Instantiates the object (move constructor). This function is useful when using C++11.
 
CachedHeapoperator= (CachedHeap &&rhs)
 Move assignment operator. This function is useful when using C++11.
 
void swap (CachedHeap &rhs) noexcept
 Swaps the contents of an object.
 
Adding Metadata to Allocated Memory

Some CachedHeap instantiation settings allow user applications to add metadata.

errno_t SetMark1 (void *ptr, uint16_t mark1) noexcept
 Adds data to allocated memory. More...
 
errno_t SetMark2 (void *ptr, uint16_t mark2) noexcept
 Adds data to allocated memory. More...
 
errno_t GetMark (const void *ptr, uint16_t *mark1, uint16_t *mark2) noexcept
 Gets data that was added to allocated memory. More...
 
void * GetObjPtr (void *raw_ptr) noexcept
 Gets an object pointer from a pointer recognized by CentralHeap. More...
 
CachedHeap Initialization and Finalization

Use CentralHeap::MakeCache for initialization.

void ReleaseAllCache () noexcept
 Corresponds to nmalloc_clear_tls.
 
void Finalize () noexcept
 Corresponds to nmalloc_finalize_tls.
 
Error Checking
bool CheckCache () noexcept
 Checks the status of the cache. More...
 
 operator bool () const
 Returns true if the cache was initialized using CentralHeap::MakeCache.
 

Detailed Description

Thread-specific cache class whose use is paired with CentralHeap.

Description
Used to implement nmalloc, which uses thread-local storage to maintain CachedHeap objects for each thread.
By preparing a CachedHeap object for each thread, and using CachedHeap to handle the allocation and freeing of memory, the need for mutual exclusion can be reduced and the ability arises to allocate, or free, memory at high-speed in a multi-threaded environment. It is also possible to free and return memory that was allocated in CachedHeap in one thread to the CachedHeap in another thread, assuming the CentralHeap is the same.

Definition at line 19 of file CachedHeap.h.

Member Function Documentation

nn::nlib::heap::CachedHeap::Alloc ( size_t  n)
noexcept

Allocates a memory region of the specified size (in bytes).

Parameters
[in]nSize of the memory block to be allocated, in bytes.
Returns
If successful, returns a pointer to the allocated memory block. NULL if process fails.
nn::nlib::heap::CachedHeap::Alloc ( size_t  n,
size_t  algn 
)
noexcept

Allocates memory with a specific alignment.

Parameters
[in]nSize of the memory block to be allocated, in bytes.
[in]algnAlignment of the memory block to allocate.
Returns
If successful, returns a pointer to the allocated memory block. NULL if process fails.
Description
The value passed to algn must be a power of two that is less than or equal to CentralHeap::kPageSize.
nn::nlib::heap::CachedHeap::CheckCache ( )
noexcept

Checks the status of the cache.

Returns
Returns true if the cache is in a "normal" state.
nn::nlib::heap::CachedHeap::Free ( void *  p)
noexcept

Frees a memory region.

Parameters
[in]pSpecifies a pointer to the memory block to free.
Returns
Returns 0 if the memory was freed without issue. Any other value indicates that an issue was detected (For example, p may never have been allocated).
nn::nlib::heap::CachedHeap::FreeWithSize ( void *  p,
size_t  size 
)
noexcept

Frees memory that was allocated with a specific size.

Parameters
[in]pSpecifies a pointer to the memory block to free.
[in]sizeSpecifies the size that was allocated to the p parameter.
Returns
Returns 0 if the memory was freed without issue. Any other value indicates that an issue was detected (For example, p may never have been allocated).
nn::nlib::heap::CachedHeap::GetMark ( const void *  ptr,
uint16_t *  mark1,
uint16_t *  mark2 
)
noexcept

Gets data that was added to allocated memory.

Parameters
[in]ptrSpecifies a pointer to an allocated object.
[out]mark1Pointer to the location storing the first piece of 16-bit additional data. Can specify NULL.
[out]mark2Pointer to the location storing the second piece of 16-bit additional data. Can specify NULL.
Return values
0Success.
EINVALWhen ptr was NULL.
EFAULTCorrupted data was detected.
Description
Specified Option Behavior
NMALLOC_HEAPOPTION_CHECK_0 Sets mark1 and mark2 to 0.
NMALLOC_HEAPOPTION_CHECK_1 Sets mark1 and mark2 to the additional data that was set.
nn::nlib::heap::CachedHeap::GetObjPtr ( void *  raw_ptr)
noexcept

Gets an object pointer from a pointer recognized by CentralHeap.

Parameters
[in]raw_ptrSpecifies a pointer recognized by CentralHeap.
Returns
Returns an object pointer.
Description
Specified Option Behavior
NMALLOC_HEAPOPTION_CHECK_0 Same pointer as raw_ptr.
NMALLOC_HEAPOPTION_CHECK_1 Object pointer.
nn::nlib::heap::CachedHeap::Realloc ( void *  ptr,
size_t  size,
void **  p 
)
noexcept

Reallocates the specified memory to have the specified size.

Parameters
[in]ptrSpecifies a pointer to the memory you want to reallocate.
[in]sizeSpecifies the size after changing (in bytes).
[out]pSpecifies a pointer to the reallocated memory.
Returns
Returns 0 if memory reallocation was successful. Any other value indicates that an issue was detected (For example, ptr may never have been allocated).
Description
Unlike the standard realloc function, the ptr parameter cannot be set to NULL, and the size parameter cannot be set to 0.
nn::nlib::heap::CachedHeap::SetMark1 ( void *  ptr,
uint16_t  mark1 
)
noexcept

Adds data to allocated memory.

Parameters
[in]ptrSpecifies a pointer to an allocated object.
[in]mark1Specifies the first piece of 16-bit additional data.
Return values
0Success.
EINVALWhen ptr was NULL.
Description
Specified Option Behavior
NMALLOC_HEAPOPTION_CHECK_0 Returns 0 without doing anything.
NMALLOC_HEAPOPTION_CHECK_1 Sets additional data.
nn::nlib::heap::CachedHeap::SetMark2 ( void *  ptr,
uint16_t  mark2 
)
noexcept

Adds data to allocated memory.

Parameters
[in]ptrSpecifies a pointer to an allocated object.
[in]mark2Specifies the second piece of 16-bit additional data.
Return values
0Success.
EINVALWhen ptr was NULL.
EFAULTCorrupted data was detected.
Description
Specified Option Behavior
NMALLOC_HEAPOPTION_CHECK_0 Returns 0 without doing anything.
NMALLOC_HEAPOPTION_CHECK_1 Sets additional data.

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