Public Member Functions | Private Attributes | List of all members
xpath_allocator Class Reference
Collaboration diagram for xpath_allocator:
Collaboration graph
[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.

6210  : _root(root), _root_size(root_size)
6211  {
6212  #ifdef PUGIXML_NO_EXCEPTIONS
6213  error_handler = 0;
6214  #endif
6215  }

Member Function Documentation

◆ allocate()

void* xpath_allocator::allocate ( size_t  size)
inline

Definition at line 6250 of file pugixml.cpp.

6251  {
6252  void* result = allocate_nothrow(size);
6253 
6254  if (!result)
6255  {
6256  #ifdef PUGIXML_NO_EXCEPTIONS
6257  assert(error_handler);
6258  longjmp(*error_handler, 1);
6259  #else
6260  throw std::bad_alloc();
6261  #endif
6262  }
6263 
6264  return result;
6265  }

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.

6218  {
6219  // align size so that we're able to store pointers in subsequent blocks
6220  size = (size + sizeof(void*) - 1) & ~(sizeof(void*) - 1);
6221 
6222  if (_root_size + size <= _root->capacity)
6223  {
6224  void* buf = _root->data + _root_size;
6225  _root_size += size;
6226  return buf;
6227  }
6228  else
6229  {
6230  // make sure we have at least 1/4th of the page free after allocation to satisfy subsequent allocation requests
6231  size_t block_capacity_base = sizeof(_root->data);
6232  size_t block_capacity_req = size + block_capacity_base / 4;
6233  size_t block_capacity = (block_capacity_base > block_capacity_req) ? block_capacity_base : block_capacity_req;
6234 
6235  size_t block_size = block_capacity + offsetof(xpath_memory_block, data);
6236 
6237  xpath_memory_block* block = static_cast<xpath_memory_block*>(xml_memory::allocate(block_size));
6238  if (!block) return 0;
6239 
6240  block->next = _root;
6241  block->capacity = block_capacity;
6242 
6243  _root = block;
6244  _root_size = size;
6245 
6246  return block->data;
6247  }
6248  }

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.

6268  {
6269  // align size so that we're able to store pointers in subsequent blocks
6270  old_size = (old_size + sizeof(void*) - 1) & ~(sizeof(void*) - 1);
6271  new_size = (new_size + sizeof(void*) - 1) & ~(sizeof(void*) - 1);
6272 
6273  // we can only reallocate the last object
6274  assert(ptr == 0 || static_cast<char*>(ptr) + old_size == _root->data + _root_size);
6275 
6276  // adjust root size so that we have not allocated the object at all
6277  bool only_object = (_root_size == old_size);
6278 
6279  if (ptr) _root_size -= old_size;
6280 
6281  // allocate a new version (this will obviously reuse the memory if possible)
6282  void* result = allocate(new_size);
6283  assert(result);
6284 
6285  // we have a new block
6286  if (result != ptr && ptr)
6287  {
6288  // copy old data
6289  assert(new_size >= old_size);
6290  memcpy(result, ptr, old_size);
6291 
6292  // free the previous page if it had no other objects
6293  if (only_object)
6294  {
6295  assert(_root->data == result);
6296  assert(_root->next);
6297 
6298  xpath_memory_block* next = _root->next->next;
6299 
6300  if (next)
6301  {
6302  // deallocate the whole page, unless it was the first one
6304  _root->next = next;
6305  }
6306  }
6307  }
6308 
6309  return result;
6310  }

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

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

◆ release()

void xpath_allocator::release ( )
inline

Definition at line 6331 of file pugixml.cpp.

6332  {
6333  xpath_memory_block* cur = _root;
6334  assert(cur);
6335 
6336  while (cur->next)
6337  {
6338  xpath_memory_block* next = cur->next;
6339 
6341 
6342  cur = next;
6343  }
6344  }

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.

6313  {
6314  // free all new pages
6315  xpath_memory_block* cur = _root;
6316 
6317  while (cur != state._root)
6318  {
6319  xpath_memory_block* next = cur->next;
6320 
6322 
6323  cur = next;
6324  }
6325 
6326  // restore state
6327  _root = state._root;
6328  _root_size = state._root_size;
6329  }

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:
xpath_memory_block::data
char data[4096]
Definition: pugixml.cpp:6197
xpath_allocator::_root
xpath_memory_block * _root
Definition: pugixml.cpp:6202
xpath_allocator::_root_size
size_t _root_size
Definition: pugixml.cpp:6203
xpath_memory_block::next
xpath_memory_block * next
Definition: pugixml.cpp:6188
xml_memory_management_function_storage::deallocate
static deallocation_function deallocate
Definition: pugixml.cpp:164
xml_memory_management_function_storage::allocate
static allocation_function allocate
Definition: pugixml.cpp:163
xpath_memory_block
Definition: pugixml.cpp:6186
xpath_allocator::allocate_nothrow
void * allocate_nothrow(size_t size)
Definition: pugixml.cpp:6217
xpath_allocator::allocate
void * allocate(size_t size)
Definition: pugixml.cpp:6250
xpath_memory_block::capacity
size_t capacity
Definition: pugixml.cpp:6189

Generated on Wed Apr 29 2020 19:41:30 for QuickFIX by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2001