xpath_allocator Class Reference
Collaboration diagram for xpath_allocator:
[legend]

## Public Member Functions

| xpath_allocator (xpath_memory_block *root, size_t root_size=0)
void * | allocate_nothrow (size_t size)
void * | allocate (size_t size)
void * | reallocate (void *ptr, size_t old_size, size_t new_size)
void | revert (const xpath_allocator &state)
void | release ()

## Private Attributes

xpath_memory_block * | _root
size_t | _root_size

## Detailed Description

Definition at line 6200 of file pugixml.cpp.

## Constructor & Destructor Documentation

## xpath_allocator()

xpath_allocator::xpath_allocator | ( | xpath_memory_block * | root,
|  | size_t | root_size = `0`
| ) |  |

inline

Definition at line 6210 of file pugixml.cpp.

: _root(root), _root_size(root_size)
{
#ifdef PUGIXML_NO_EXCEPTIONS
error_handler = 0;
#endif
}
xpath_allocator::_root
xpath_memory_block * _root
Definition pugixml.cpp:6202
xpath_allocator::_root_size
size_t _root_size
Definition pugixml.cpp:6203

## Member Function Documentation

## allocate()

void * xpath_allocator::allocate | ( | size_t | size | ) |

inline

Definition at line 6250 of file pugixml.cpp.

{
void* result = allocate_nothrow(size);
if (!result)
{
#ifdef PUGIXML_NO_EXCEPTIONS
assert(error_handler);
longjmp(*error_handler, 1);
#else
throw std::bad_alloc();
#endif
}
return result;
}
xpath_allocator::allocate_nothrow
void * allocate_nothrow(size_t size)
Definition pugixml.cpp:6217

References allocate_nothrow().

Referenced by convert_number_to_string(), xpath_string::duplicate_string(), xpath_ast_node::eval_string_concat(), and reallocate().

## allocate_nothrow()

void * xpath_allocator::allocate_nothrow | ( | size_t | size | ) |

inline

Definition at line 6217 of file pugixml.cpp.

{
// align size so that we're able to store pointers in subsequent blocks
size = (size + sizeof(void*) - 1) & ~(sizeof(void*) - 1);
if (_root_size + size <= _root->capacity)
{
void* buf = _root->data + _root_size;
_root_size += size;
return buf;
}
else
{
// make sure we have at least 1/4th of the page free after allocation to satisfy subsequent allocation requests
size_t block_capacity_base = sizeof(_root->data);
size_t block_capacity_req = size + block_capacity_base / 4;
size_t block_capacity = (block_capacity_base > block_capacity_req) ? block_capacity_base : block_capacity_req;
size_t block_size = block_capacity + offsetof(xpath_memory_block, data);
xpath_memory_block* block = static_cast<xpath_memory_block*>(xml_memory::allocate(block_size));
if (!block) return 0;
block->next = _root;
block->capacity = block_capacity;
_root = block;
_root_size = size;
return block->data;
}
}
xml_memory_management_function_storage::allocate
static allocation_function allocate
Definition pugixml.cpp:163
xpath_memory_block
Definition pugixml.cpp:6187
xpath_memory_block::capacity
size_t capacity
Definition pugixml.cpp:6189
xpath_memory_block::next
xpath_memory_block * next
Definition pugixml.cpp:6188
xpath_memory_block::data
char data[4096]
Definition pugixml.cpp:6197

References _root, _root_size, xml_memory_management_function_storage< T >::allocate, xpath_memory_block::capacity, xpath_memory_block::data, and xpath_memory_block::next.

Referenced by xpath_parser::alloc_node(), xpath_parser::alloc_string(), and allocate().

## reallocate()

void * xpath_allocator::reallocate | ( | void * | ptr,
|  | size_t | old_size,
|  | size_t | new_size
| ) |  |

inline

Definition at line 6267 of file pugixml.cpp.

{
// align size so that we're able to store pointers in subsequent blocks
old_size = (old_size + sizeof(void*) - 1) & ~(sizeof(void*) - 1);
new_size = (new_size + sizeof(void*) - 1) & ~(sizeof(void*) - 1);
// we can only reallocate the last object
assert(ptr == 0 || static_cast<char*>(ptr) + old_size == _root->data + _root_size);
// adjust root size so that we have not allocated the object at all
bool only_object = (_root_size == old_size);
if (ptr) _root_size -= old_size;
// allocate a new version (this will obviously reuse the memory if possible)
void* result = allocate(new_size);
assert(result);
// we have a new block
if (result != ptr && ptr)
{
// copy old data
assert(new_size >= old_size);
memcpy(result, ptr, old_size);
// free the previous page if it had no other objects
if (only_object)
{
assert(_root->data == result);
assert(_root->next);
xpath_memory_block* next = _root->next->next;
if (next)
{
// deallocate the whole page, unless it was the first one
xml_memory::deallocate(_root->next);
_root->next = next;
}
}
}
return result;
}
xpath_allocator::allocate
void * allocate(size_t size)
Definition pugixml.cpp:6250
xml_memory_management_function_storage::deallocate
static deallocation_function deallocate
Definition pugixml.cpp:164

References _root, _root_size, allocate(), xpath_memory_block::data, xml_memory_management_function_storage< T >::deallocate, and xpath_memory_block::next.

Referenced by xpath_node_set_raw::append(), xpath_string::append(), and xpath_node_set_raw::push_back().

## release()

void xpath_allocator::release | ( |  | ) |

inline

Definition at line 6331 of file pugixml.cpp.

{
xpath_memory_block* cur = _root;
assert(cur);
while (cur->next)
{
xpath_memory_block* next = cur->next;
xml_memory::deallocate(cur);
cur = next;
}
}

References _root, xml_memory_management_function_storage< T >::deallocate, and xpath_memory_block::next.

Referenced by xpath_query_impl::destroy(), and xpath_stack_data::~xpath_stack_data().

## revert()

void xpath_allocator::revert | ( | const xpath_allocator & | state | ) |

inline

Definition at line 6312 of file pugixml.cpp.

{
// free all new pages
xpath_memory_block* cur = _root;
while (cur != state._root)
{
xpath_memory_block* next = cur->next;
xml_memory::deallocate(cur);
cur = next;
}
// restore state
_root = state._root;
_root_size = state._root_size;
}

References _root, _root_size, xml_memory_management_function_storage< T >::deallocate, and xpath_memory_block::next.

Referenced by xpath_allocator_capture::~xpath_allocator_capture().

## Member Data Documentation

## _root

xpath_memory_block* xpath_allocator::_root

private

Definition at line 6202 of file pugixml.cpp.

Referenced by allocate_nothrow(), reallocate(), release(), and revert().

## _root_size

size_t xpath_allocator::_root_size

private

Definition at line 6203 of file pugixml.cpp.

Referenced by allocate_nothrow(), reallocate(), and revert().

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

- pugixml.cpp