Public Member Functions | Private Attributes
xpath_allocator Class Reference
Collaboration diagram for xpath_allocator:
Collaboration graph
[legend]

List of all members.

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_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
                }

Member Function Documentation

void* xpath_allocator::allocate ( size_t  size) [inline]

Definition at line 6250 of file pugixml.cpp.

References allocate_nothrow().

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

                {
                        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;
                }
void* xpath_allocator::allocate_nothrow ( size_t  size) [inline]

Definition at line 6217 of file pugixml.cpp.

References _root, _root_size, 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().

                {
                        // 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;
                        }
                }
void* xpath_allocator::reallocate ( void *  ptr,
size_t  old_size,
size_t  new_size 
) [inline]

Definition at line 6267 of file pugixml.cpp.

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().

                {
                        // 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;
                }
void xpath_allocator::release ( ) [inline]

Definition at line 6331 of file pugixml.cpp.

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

Referenced by xpath_query_impl::destroy().

                {
                        xpath_memory_block* cur = _root;
                        assert(cur);

                        while (cur->next)
                        {
                                xpath_memory_block* next = cur->next;

                                xml_memory::deallocate(cur);

                                cur = next;
                        }
                }
void xpath_allocator::revert ( const xpath_allocator state) [inline]

Definition at line 6312 of file pugixml.cpp.

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

Referenced by xpath_allocator_capture::~xpath_allocator_capture().

                {
                        // 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;
                }

Member Data Documentation

Definition at line 6202 of file pugixml.cpp.

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

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:

Generated on Mon Sep 15 2014 01:23:55 for QuickFIX by doxygen 1.7.6.1 written by Dimitri van Heesch, © 1997-2001