nlib
nn::nlib::exi::ExiAllocator Class Referencefinal

Allocator used by the XML parser. The user can also use it. More...

#include "nn/nlib/exi/ExiAllocator.h"

Static Public Member Functions

static bool Test (size_t nbytes) noexcept
 Tests whether nBytes of memory can be allocated. More...
 
static size_t GetPos () noexcept
 Returns the amount of memory that has already been allocated. More...
 
static bool IsOutOfMemory () noexcept
 Checks whether the previous attempt at memory allocation failed. More...
 
static ExiAllocatorEx GetAllocator () noexcept
 Returns the ExiAllocatorEx object for every thread.
 
Initialization and Finalization
static errno_t Init (void *p, size_t size) noexcept
 Initializes an allocator. More...
 
static void * Finalize () noexcept
 Finalizes allocator use. More...
 
static void Reset () noexcept
 Resets an allocator. Returns the allocator to the state it had immediately after initialization.
 
Memory Allocation and Deallocation
static void * Alloc (size_t nbytes) noexcept
 Allocates nBytes of memory from the allocator. More...
 
static void * Realloc (void *ptr, size_t from_nbytes, size_t nbytes) noexcept
 Sometimes called when expanding the amount of memory that has been allocated. More...
 
static void Dealloc (void *p) noexcept
 Called when memory is deallocated. More...
 

Detailed Description

Allocator used by the XML parser. The user can also use it.

Description
The XML parser runs within the memory range initially provided by the user. This practice helps prevent situations like overall system instability when invalid data (such as excessively long element names) is provided. You must initialize the allocator in separate memory regions for each thread. Within a particular thread, be careful when using functions such as ExiAllocator::Reset because memory is allocated for both the XML reader and the XML writer from the same memory space.
Below is sample code for initialization and finalization.
unsigned char buf[SIZE];
// Must be initialized in separate memory regions for each thread.
ExiAllocator::Init(&buf[0], SIZE);
// Use the XML parser.

Definition at line 69 of file ExiAllocator.h.

Member Function Documentation

◆ Alloc()

nn::nlib::exi::ExiAllocator::Alloc ( size_t  nbytes)
staticnoexcept

Allocates nBytes of memory from the allocator.

Parameters
[in]nbytesMemory size to allocate.
Returns
Address of the allocated memory. NULL if process fails.

◆ Dealloc()

nn::nlib::exi::ExiAllocator::Dealloc ( void *  p)
staticnoexcept

Called when memory is deallocated.

Parameters
[in]pStarting address of the memory to deallocate.

◆ Finalize()

nn::nlib::exi::ExiAllocator::Finalize ( )
staticnoexcept

Finalizes allocator use.

Returns
Pointer to the memory block that was provided during initialization.
Description
Finalizes allocator use and returns to the pre-initialization state. You must call this for each thread that uses the exi library.

◆ GetPos()

nn::nlib::exi::ExiAllocator::GetPos ( )
staticnoexcept

Returns the amount of memory that has already been allocated.

Returns
Amount of memory that has already been allocated.

◆ Init()

nn::nlib::exi::ExiAllocator::Init ( void *  p,
size_t  size 
)
staticnoexcept

Initializes an allocator.

Parameters
[in]pPointer to the start of the memory block.
[in]sizeMemory block size.
Returns
Returns 0 on success.
Description
Allocates memory to an allocator and initializes it. Memory must be allocated separately and initialized for each thread.
The exi library runs using the memory that was allocated for each thread. Consequently, it is not possible to use the same XML parser instance from multiple threads.

◆ IsOutOfMemory()

nn::nlib::exi::ExiAllocator::IsOutOfMemory ( )
staticnoexcept

Checks whether the previous attempt at memory allocation failed.

Returns
Returns true if memory allocation failed.

◆ Realloc()

nn::nlib::exi::ExiAllocator::Realloc ( void *  ptr,
size_t  from_nbytes,
size_t  nbytes 
)
staticnoexcept

Sometimes called when expanding the amount of memory that has been allocated.

Parameters
[in]ptrStarting address of the memory to expand.
[in]from_nbytesMemory size allocated prior to expansion.
[in]nbytesMemory size after expansion.
Returns
Address of the allocated memory. NULL if process fails.
Description
Unlike with the standard C realloc function, pass this function the amount of memory currently allocated.

◆ Test()

nn::nlib::exi::ExiAllocator::Test ( size_t  nbytes)
staticnoexcept

Tests whether nBytes of memory can be allocated.

Parameters
[in]nbytesMemory size to attempt to allocate.
Returns
Returns false if allocation is not possible.
Description
If it returns false, the subsequent call to IsOutOfMemory() returns true. Used to determine in advance whether Alloc() will fail.

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