From 9f68dec787de7620f1152a2cd5c1c17aecc55b50 Mon Sep 17 00:00:00 2001 From: "arseny.kapoulkine" Date: Mon, 13 Sep 2010 07:53:53 +0000 Subject: XPath: Allocator refactoring: xpath_allocator now only consists of allocator state and can be copied git-svn-id: http://pugixml.googlecode.com/svn/trunk@719 99668b35-9821-0410-8761-19e4c4f06640 --- src/pugixml.cpp | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index c1e586d..e974794 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -5633,28 +5633,28 @@ namespace pugi } #endif - const size_t xpath_memory_block_size = 4096; - class xpath_allocator { struct memory_block { memory_block* next; - char data[xpath_memory_block_size]; + char data[4096]; }; memory_block* _root; size_t _root_size; - - memory_block _first; public: static xpath_allocator* create() { - void* memory = global_allocate(sizeof(xpath_allocator)); + void* memory = global_allocate(sizeof(xpath_allocator) + sizeof(memory_block)); + if (!memory) return 0; - return new (memory) xpath_allocator(); + memory_block* root = reinterpret_cast(static_cast(memory) + 1); + root->next = 0; + + return new (memory) xpath_allocator(root); } static void destroy(void* ptr) @@ -5665,9 +5665,9 @@ namespace pugi xpath_allocator* alloc = static_cast(ptr); memory_block* cur = alloc->_root; - memory_block* first = &alloc->_first; + assert(cur); - while (cur != first) + while (cur->next) { memory_block* next = cur->next; @@ -5680,19 +5680,18 @@ namespace pugi global_deallocate(alloc); } - xpath_allocator() + xpath_allocator(memory_block* root, size_t root_size = 0): _root(root), _root_size(root_size) { - _root = &_first; - _root_size = 0; - _first.next = 0; } void* allocate(size_t size) { + const size_t block_capacity = sizeof(_root->data); + // 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 <= xpath_memory_block_size) + if (_root_size + size <= block_capacity) { void* buf = _root->data + _root_size; _root_size += size; @@ -5700,7 +5699,7 @@ namespace pugi } else { - size_t block_data_size = (size > xpath_memory_block_size) ? size : xpath_memory_block_size; + size_t block_data_size = (size > block_capacity) ? size : block_capacity; size_t block_size = block_data_size + offsetof(memory_block, data); memory_block* block = static_cast(global_allocate(block_size)); -- cgit v1.2.3