Classes | Public Member Functions | Private Member Functions | Private Attributes | List of all members
rapidxml::memory_pool< Ch > Class Template Reference

#include <rapidxml.hpp>

Inheritance diagram for rapidxml::memory_pool< Ch >:
Inheritance graph
[legend]

Classes

struct  header
 

Public Member Functions

xml_attribute< Ch > * allocate_attribute (const Ch *name=0, const Ch *value=0, std::size_t name_size=0, std::size_t value_size=0)
 
xml_node< Ch > * allocate_node (node_type type, const Ch *name=0, const Ch *value=0, std::size_t name_size=0, std::size_t value_size=0)
 
Ch * allocate_string (const Ch *source=0, std::size_t size=0)
 
void clear ()
 
xml_node< Ch > * clone_node (const xml_node< Ch > *source, xml_node< Ch > *result=0)
 
 memory_pool ()
 Constructs empty pool with default allocator functions. More...
 
void set_allocator (alloc_func *af, free_func *ff)
 
 ~memory_pool ()
 

Private Member Functions

char * align (char *ptr)
 
voidallocate_aligned (std::size_t size)
 
char * allocate_raw (std::size_t size)
 
void init ()
 

Private Attributes

alloc_func * m_alloc_func
 
char * m_begin
 
char * m_end
 
free_func * m_free_func
 
char * m_ptr
 
char m_static_memory [RAPIDXML_STATIC_POOL_SIZE]
 

Detailed Description

template<class Ch = char>
class rapidxml::memory_pool< Ch >

This class is used by the parser to create new nodes and attributes, without overheads of dynamic memory allocation. In most cases, you will not need to use this class directly. However, if you need to create nodes manually or modify names/values of nodes, you are encouraged to use memory_pool of relevant xml_document to allocate the memory. Not only is this faster than allocating them by using new operator, but also their lifetime will be tied to the lifetime of document, possibly simplyfing memory management.

Call allocate_node() or allocate_attribute() functions to obtain new nodes or attributes from the pool. You can also call allocate_string() function to allocate strings. Such strings can then be used as names or values of nodes without worrying about their lifetime. Note that there is no free() function – all allocations are freed at once when clear() function is called, or when the pool is destroyed.

It is also possible to create a standalone memory_pool, and use it to allocate nodes, whose lifetime will not be tied to any document.

Pool maintains RAPIDXML_STATIC_POOL_SIZE bytes of statically allocated memory. Until static memory is exhausted, no dynamic memory allocations are done. When static memory is exhausted, pool allocates additional blocks of memory of size RAPIDXML_DYNAMIC_POOL_SIZE each, by using global new[] and delete[] operators. This behaviour can be changed by setting custom allocation routines. Use set_allocator() function to set them.

Allocations for nodes, attributes and strings are aligned at RAPIDXML_ALIGNMENT bytes. This value defaults to the size of pointer on target architecture.

To obtain absolutely top performance from the parser, it is important that all nodes are allocated from a single, contiguous block of memory. Otherwise, cache misses when jumping between two (or more) disjoint blocks of memory can slow down parsing quite considerably. If required, you can tweak RAPIDXML_STATIC_POOL_SIZE, RAPIDXML_DYNAMIC_POOL_SIZE and RAPIDXML_ALIGNMENT to obtain best wasted memory to performance compromise. To do it, define their values before rapidxml.hpp file is included.

Parameters
ChCharacter type of created nodes.

Definition at line 379 of file rapidxml.hpp.

Constructor & Destructor Documentation

template<class Ch = char>
rapidxml::memory_pool< Ch >::memory_pool ( )
inline

Constructs empty pool with default allocator functions.

Definition at line 390 of file rapidxml.hpp.

template<class Ch = char>
rapidxml::memory_pool< Ch >::~memory_pool ( )
inline

Destroys pool and frees all the memory. This causes memory occupied by nodes allocated by the pool to be freed. Nodes allocated from the pool are no longer valid.

Definition at line 400 of file rapidxml.hpp.

Member Function Documentation

template<class Ch = char>
char* rapidxml::memory_pool< Ch >::align ( char *  ptr)
inlineprivate

Definition at line 573 of file rapidxml.hpp.

template<class Ch = char>
void* rapidxml::memory_pool< Ch >::allocate_aligned ( std::size_t  size)
inlineprivate

Definition at line 599 of file rapidxml.hpp.

template<class Ch = char>
xml_attribute<Ch>* rapidxml::memory_pool< Ch >::allocate_attribute ( const Ch *  name = 0,
const Ch *  value = 0,
std::size_t  name_size = 0,
std::size_t  value_size = 0 
)
inline

Allocates a new attribute from the pool, and optionally assigns name and value to it. If the allocation request cannot be accomodated, this function will throw std::bad_alloc. If exceptions are disabled by defining RAPIDXML_NO_EXCEPTIONS, this function will call rapidxml::parse_error_handler() function.

Parameters
nameName to assign to the attribute, or 0 to assign no name.
valueValue to assign to the attribute, or 0 to assign no value.
name_sizeSize of name to assign, or 0 to automatically calculate size from name string.
value_sizeSize of value to assign, or 0 to automatically calculate size from value string.
Returns
Pointer to allocated attribute. This pointer will never be NULL.

Definition at line 447 of file rapidxml.hpp.

template<class Ch = char>
xml_node<Ch>* rapidxml::memory_pool< Ch >::allocate_node ( node_type  type,
const Ch *  name = 0,
const Ch *  value = 0,
std::size_t  name_size = 0,
std::size_t  value_size = 0 
)
inline

Allocates a new node from the pool, and optionally assigns name and value to it. If the allocation request cannot be accomodated, this function will throw std::bad_alloc. If exceptions are disabled by defining RAPIDXML_NO_EXCEPTIONS, this function will call rapidxml::parse_error_handler() function.

Parameters
typeType of node to create.
nameName to assign to the node, or 0 to assign no name.
valueValue to assign to the node, or 0 to assign no value.
name_sizeSize of name to assign, or 0 to automatically calculate size from name string.
value_sizeSize of value to assign, or 0 to automatically calculate size from value string.
Returns
Pointer to allocated node. This pointer will never be NULL.

Definition at line 415 of file rapidxml.hpp.

template<class Ch = char>
char* rapidxml::memory_pool< Ch >::allocate_raw ( std::size_t  size)
inlineprivate

Definition at line 579 of file rapidxml.hpp.

template<class Ch = char>
Ch* rapidxml::memory_pool< Ch >::allocate_string ( const Ch *  source = 0,
std::size_t  size = 0 
)
inline

Allocates a char array of given size from the pool, and optionally copies a given string to it. If the allocation request cannot be accomodated, this function will throw std::bad_alloc. If exceptions are disabled by defining RAPIDXML_NO_EXCEPTIONS, this function will call rapidxml::parse_error_handler() function.

Parameters
sourceString to initialize the allocated memory with, or 0 to not initialize it.
sizeNumber of characters to allocate, or zero to calculate it automatically from source string length; if size is 0, source string must be specified and null terminated.
Returns
Pointer to allocated char array. This pointer will never be NULL.

Definition at line 476 of file rapidxml.hpp.

template<class Ch = char>
void rapidxml::memory_pool< Ch >::clear ( void  )
inline

Clears the pool. This causes memory occupied by nodes allocated by the pool to be freed. Any nodes or strings allocated from the pool will no longer be valid.

Definition at line 525 of file rapidxml.hpp.

template<class Ch = char>
xml_node<Ch>* rapidxml::memory_pool< Ch >::clone_node ( const xml_node< Ch > *  source,
xml_node< Ch > *  result = 0 
)
inline

Clones an xml_node and its hierarchy of child nodes and attributes. Nodes and attributes are allocated from this memory pool. Names and values are not cloned, they are shared between the clone and the source. Result node can be optionally specified as a second parameter, in which case its contents will be replaced with cloned source node. This is useful when you want to clone entire document.

Parameters
sourceNode to clone.
resultNode to put results in, or 0 to automatically allocate result node
Returns
Pointer to cloned node. This pointer will never be NULL.

Definition at line 497 of file rapidxml.hpp.

template<class Ch = char>
void rapidxml::memory_pool< Ch >::init ( )
inlineprivate

Definition at line 566 of file rapidxml.hpp.

template<class Ch = char>
void rapidxml::memory_pool< Ch >::set_allocator ( alloc_func *  af,
free_func *  ff 
)
inline

Sets or resets the user-defined memory allocation functions for the pool. This can only be called when no memory is allocated from the pool yet, otherwise results are undefined. Allocation function must not return invalid pointer on failure. It should either throw, stop the program, or use longjmp() function to pass control to other place of program. If it returns invalid pointer, results are undefined.

User defined allocation functions must have the following forms:

void *allocate(std::size_t size);
void free(void *pointer);

Parameters
afAllocation function, or 0 to restore default function
ffFree function, or 0 to restore default function

Definition at line 552 of file rapidxml.hpp.

Member Data Documentation

template<class Ch = char>
alloc_func* rapidxml::memory_pool< Ch >::m_alloc_func
private

Definition at line 637 of file rapidxml.hpp.

template<class Ch = char>
char* rapidxml::memory_pool< Ch >::m_begin
private

Definition at line 633 of file rapidxml.hpp.

template<class Ch = char>
char* rapidxml::memory_pool< Ch >::m_end
private

Definition at line 635 of file rapidxml.hpp.

template<class Ch = char>
free_func* rapidxml::memory_pool< Ch >::m_free_func
private

Definition at line 638 of file rapidxml.hpp.

template<class Ch = char>
char* rapidxml::memory_pool< Ch >::m_ptr
private

Definition at line 634 of file rapidxml.hpp.

template<class Ch = char>
char rapidxml::memory_pool< Ch >::m_static_memory[RAPIDXML_STATIC_POOL_SIZE]
private

Definition at line 636 of file rapidxml.hpp.


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


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:50:40