Merge 6e994e7545 into 22401bafaf
This commit is contained in:
commit
43c1b7eb8d
466
src/pugixml.cpp
466
src/pugixml.cpp
File diff suppressed because it is too large
Load Diff
@ -275,6 +275,9 @@ namespace pugi
|
|||||||
const int default_double_precision = 17;
|
const int default_double_precision = 17;
|
||||||
const int default_float_precision = 9;
|
const int default_float_precision = 9;
|
||||||
|
|
||||||
|
// memory allocation alignment requirement for memory allocators
|
||||||
|
const size_t memory_allocation_alignment = sizeof(double) > sizeof(void*) ? sizeof(double) : sizeof(void*);
|
||||||
|
|
||||||
// Forward declarations
|
// Forward declarations
|
||||||
struct xml_attribute_struct;
|
struct xml_attribute_struct;
|
||||||
struct xml_node_struct;
|
struct xml_node_struct;
|
||||||
@ -316,6 +319,71 @@ namespace pugi
|
|||||||
It _begin, _end;
|
It _begin, _end;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Interface for pool allocators
|
||||||
|
// 'pugi::memory_allocation_alignment' provides alignment requirements
|
||||||
|
class PUGIXML_CLASS xml_memory_pool
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~xml_memory_pool() {}
|
||||||
|
|
||||||
|
virtual void* allocate(size_t size) = 0;
|
||||||
|
virtual void deallocate(void* ptr) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Non-owning allocate-only fixed size memory pool, deallocation is no-op
|
||||||
|
// Size must be at least PUGIXML_MEMORY_PAGE_SIZE bytes for xml_document construction
|
||||||
|
class PUGIXML_CLASS xml_monotonic_memory_pool : public xml_memory_pool
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
xml_monotonic_memory_pool(void* buffer, size_t size) PUGIXML_NOEXCEPT;
|
||||||
|
|
||||||
|
virtual void* allocate(size_t size) PUGIXML_OVERRIDE;
|
||||||
|
virtual void deallocate(void* ptr) PUGIXML_OVERRIDE;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void* _buffer;
|
||||||
|
size_t _capacity;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Adapter for allocators with std::pmr::memory_resource-like interface
|
||||||
|
// The overhead equals 'pugi::memory_allocation_alignment' bytes per allocation
|
||||||
|
template<typename Resource>
|
||||||
|
class memory_resource_adapter : public xml_memory_pool
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit memory_resource_adapter(Resource* resource) PUGIXML_NOEXCEPT : _resource(resource) {}
|
||||||
|
|
||||||
|
virtual void* allocate(size_t size) PUGIXML_OVERRIDE
|
||||||
|
{
|
||||||
|
void* result = _resource->allocate(size + pugi::memory_allocation_alignment, pugi::memory_allocation_alignment);
|
||||||
|
if (!result) return 0;
|
||||||
|
|
||||||
|
size_t* block_size = reinterpret_cast<size_t*>(result);
|
||||||
|
*block_size = size;
|
||||||
|
|
||||||
|
return static_cast<char*>(result) + pugi::memory_allocation_alignment;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void deallocate(void* ptr) PUGIXML_OVERRIDE
|
||||||
|
{
|
||||||
|
if (!ptr) return;
|
||||||
|
|
||||||
|
size_t size = memory_size(ptr);
|
||||||
|
|
||||||
|
_resource->deallocate(static_cast<char*>(ptr) - pugi::memory_allocation_alignment, size + pugi::memory_allocation_alignment, pugi::memory_allocation_alignment);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static size_t memory_size(void* ptr) PUGIXML_NOEXCEPT
|
||||||
|
{
|
||||||
|
// this function casts pointers through void* to avoid 'cast increases required alignment of target type' warnings
|
||||||
|
size_t* block_size = reinterpret_cast<size_t*>(static_cast<void*>(static_cast<char*>(ptr) - pugi::memory_allocation_alignment));
|
||||||
|
return *block_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
Resource* _resource;
|
||||||
|
};
|
||||||
|
|
||||||
// Writer interface for node printing (see xml_node::print)
|
// Writer interface for node printing (see xml_node::print)
|
||||||
class PUGIXML_CLASS xml_writer
|
class PUGIXML_CLASS xml_writer
|
||||||
{
|
{
|
||||||
@ -1030,20 +1098,28 @@ namespace pugi
|
|||||||
xml_document(const xml_document&);
|
xml_document(const xml_document&);
|
||||||
xml_document& operator=(const xml_document&);
|
xml_document& operator=(const xml_document&);
|
||||||
|
|
||||||
|
xml_memory_pool* _get_memory_pool() const PUGIXML_NOEXCEPT;
|
||||||
|
|
||||||
void _create();
|
void _create();
|
||||||
|
void _create_using_pool(xml_memory_pool* pool);
|
||||||
void _destroy();
|
void _destroy();
|
||||||
|
xml_memory_pool* _destroy_release_pool();
|
||||||
void _move(xml_document& rhs) PUGIXML_NOEXCEPT_IF_NOT_COMPACT;
|
void _move(xml_document& rhs) PUGIXML_NOEXCEPT_IF_NOT_COMPACT;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Default constructor, makes empty document
|
// Default constructor, makes empty document
|
||||||
xml_document();
|
xml_document();
|
||||||
|
|
||||||
|
// Create empty document using supplied memory pool
|
||||||
|
explicit xml_document(xml_memory_pool& pool);
|
||||||
|
|
||||||
// Destructor, invalidates all node/attribute handles to this document
|
// Destructor, invalidates all node/attribute handles to this document
|
||||||
~xml_document();
|
~xml_document();
|
||||||
|
|
||||||
#ifdef PUGIXML_HAS_MOVE
|
#ifdef PUGIXML_HAS_MOVE
|
||||||
// Move semantics support
|
// Move semantics support
|
||||||
xml_document(xml_document&& rhs) PUGIXML_NOEXCEPT_IF_NOT_COMPACT;
|
xml_document(xml_document&& rhs) PUGIXML_NOEXCEPT_IF_NOT_COMPACT;
|
||||||
|
// the operation is exception safe only when objects use the same memory pools
|
||||||
xml_document& operator=(xml_document&& rhs) PUGIXML_NOEXCEPT_IF_NOT_COMPACT;
|
xml_document& operator=(xml_document&& rhs) PUGIXML_NOEXCEPT_IF_NOT_COMPACT;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -1173,7 +1249,9 @@ namespace pugi
|
|||||||
|
|
||||||
xpath_variable* _find(const char_t* name) const;
|
xpath_variable* _find(const char_t* name) const;
|
||||||
|
|
||||||
|
static bool _clone(xpath_variable* var, xpath_variable** out_result, xml_memory_pool* pool);
|
||||||
static bool _clone(xpath_variable* var, xpath_variable** out_result);
|
static bool _clone(xpath_variable* var, xpath_variable** out_result);
|
||||||
|
static void _destroy(xpath_variable* var, xml_memory_pool* pool);
|
||||||
static void _destroy(xpath_variable* var);
|
static void _destroy(xpath_variable* var);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@ -1218,11 +1296,18 @@ namespace pugi
|
|||||||
xpath_query(const xpath_query&);
|
xpath_query(const xpath_query&);
|
||||||
xpath_query& operator=(const xpath_query&);
|
xpath_query& operator=(const xpath_query&);
|
||||||
|
|
||||||
|
void _construct(xml_memory_pool* pool, const char_t* query, xpath_variable_set* variables);
|
||||||
|
xml_memory_pool* _get_memory_pool() const PUGIXML_NOEXCEPT;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Construct a compiled object from XPath expression.
|
// Construct a compiled object from XPath expression.
|
||||||
// If PUGIXML_NO_EXCEPTIONS is not defined, throws xpath_exception on compilation errors.
|
// If PUGIXML_NO_EXCEPTIONS is not defined, throws xpath_exception on compilation errors.
|
||||||
explicit xpath_query(const char_t* query, xpath_variable_set* variables = PUGIXML_NULL);
|
explicit xpath_query(const char_t* query, xpath_variable_set* variables = PUGIXML_NULL);
|
||||||
|
|
||||||
|
// Construct a compiled object from XPath expression using supplied memory pool.
|
||||||
|
// If PUGIXML_NO_EXCEPTIONS is not defined, throws xpath_exception on compilation errors.
|
||||||
|
xpath_query(xml_memory_pool& pool, const char_t* query, xpath_variable_set* variables = 0);
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
xpath_query();
|
xpath_query();
|
||||||
|
|
||||||
@ -1232,6 +1317,8 @@ namespace pugi
|
|||||||
#ifdef PUGIXML_HAS_MOVE
|
#ifdef PUGIXML_HAS_MOVE
|
||||||
// Move semantics support
|
// Move semantics support
|
||||||
xpath_query(xpath_query&& rhs) PUGIXML_NOEXCEPT;
|
xpath_query(xpath_query&& rhs) PUGIXML_NOEXCEPT;
|
||||||
|
// currently move assignment operator cannot properly hanle situation when memory pools are different
|
||||||
|
// object ends up referring to rhs' memory pool
|
||||||
xpath_query& operator=(xpath_query&& rhs) PUGIXML_NOEXCEPT;
|
xpath_query& operator=(xpath_query&& rhs) PUGIXML_NOEXCEPT;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@ -13,6 +13,8 @@ namespace
|
|||||||
int page_allocs = 0;
|
int page_allocs = 0;
|
||||||
int page_deallocs = 0;
|
int page_deallocs = 0;
|
||||||
|
|
||||||
|
size_t memory_allocated = 0;
|
||||||
|
|
||||||
bool is_page(size_t size)
|
bool is_page(size_t size)
|
||||||
{
|
{
|
||||||
return size >= 16384;
|
return size >= 16384;
|
||||||
@ -30,6 +32,93 @@ namespace
|
|||||||
page_deallocs += is_page(memory_size(ptr));
|
page_deallocs += is_page(memory_size(ptr));
|
||||||
memory_deallocate(ptr);
|
memory_deallocate(ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void* null_allocate(size_t)
|
||||||
|
{
|
||||||
|
#ifndef PUGIXML_NO_EXCEPTIONS
|
||||||
|
throw std::runtime_error("null_allocate was invoked");
|
||||||
|
#else
|
||||||
|
return 0;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void null_deallocate(void*)
|
||||||
|
{
|
||||||
|
#ifndef PUGIXML_NO_EXCEPTIONS
|
||||||
|
throw std::runtime_error("null_deallocate was invoked");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
class test_memory_pool : public pugi::xml_memory_pool
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
test_memory_pool(pugi::allocation_function alloc, pugi::deallocation_function dealloc) PUGIXML_NOEXCEPT
|
||||||
|
: _allocate(alloc)
|
||||||
|
, _deallocate(dealloc)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void* allocate(size_t size) PUGIXML_OVERRIDE
|
||||||
|
{
|
||||||
|
return _allocate(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
void deallocate(void* ptr) PUGIXML_OVERRIDE
|
||||||
|
{
|
||||||
|
_deallocate(ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
pugi::allocation_function _allocate;
|
||||||
|
pugi::deallocation_function _deallocate;
|
||||||
|
};
|
||||||
|
|
||||||
|
// class with interface similar to std::pmr::memory_resource
|
||||||
|
class test_memory_resource
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
test_memory_resource(pugi::allocation_function alloc, pugi::deallocation_function dealloc) PUGIXML_NOEXCEPT
|
||||||
|
: _allocate(alloc)
|
||||||
|
, _deallocate(dealloc)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void* allocate(size_t size, size_t alignment)
|
||||||
|
{
|
||||||
|
if (alignment != pugi::memory_allocation_alignment)
|
||||||
|
{
|
||||||
|
#ifndef PUGIXML_NO_EXCEPTIONS
|
||||||
|
throw std::runtime_error("Invalid alignment during allocation");
|
||||||
|
#else
|
||||||
|
return 0;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
memory_allocated += size;
|
||||||
|
return _allocate(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
void deallocate(void* ptr, size_t size, size_t alignment)
|
||||||
|
{
|
||||||
|
if (alignment != pugi::memory_allocation_alignment)
|
||||||
|
{
|
||||||
|
#ifndef PUGIXML_NO_EXCEPTIONS
|
||||||
|
throw std::runtime_error("Invalid alignment during deallocation");
|
||||||
|
#else
|
||||||
|
return;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
memory_allocated -= size;
|
||||||
|
_deallocate(ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
pugi::allocation_function _allocate;
|
||||||
|
pugi::deallocation_function _deallocate;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(memory_custom_memory_management)
|
TEST(memory_custom_memory_management)
|
||||||
@ -76,6 +165,53 @@ TEST(memory_custom_memory_management)
|
|||||||
set_memory_management_functions(old_allocate, old_deallocate);
|
set_memory_management_functions(old_allocate, old_deallocate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(memory_pool_custom_memory_management)
|
||||||
|
{
|
||||||
|
page_allocs = page_deallocs = 0;
|
||||||
|
|
||||||
|
// remember old functions
|
||||||
|
allocation_function old_allocate = get_memory_allocation_function();
|
||||||
|
deallocation_function old_deallocate = get_memory_deallocation_function();
|
||||||
|
|
||||||
|
// replace functions with non-allocating ones
|
||||||
|
set_memory_management_functions(null_allocate, null_deallocate);
|
||||||
|
|
||||||
|
{
|
||||||
|
// create custom memory pool
|
||||||
|
test_memory_pool pool(allocate, deallocate);
|
||||||
|
|
||||||
|
// parse document
|
||||||
|
xml_document doc(pool);
|
||||||
|
|
||||||
|
CHECK(page_allocs == 0 && page_deallocs == 0);
|
||||||
|
|
||||||
|
CHECK(doc.load_string(STR("<node />")));
|
||||||
|
|
||||||
|
CHECK(page_allocs == 1 && page_deallocs == 0);
|
||||||
|
|
||||||
|
// modify document (no new page)
|
||||||
|
CHECK(doc.first_child().set_name(STR("foobars")));
|
||||||
|
CHECK(page_allocs == 1 && page_deallocs == 0);
|
||||||
|
|
||||||
|
// modify document (new page)
|
||||||
|
std::basic_string<char_t> s(65536, 'x');
|
||||||
|
|
||||||
|
CHECK(doc.first_child().set_name(s.c_str()));
|
||||||
|
CHECK(page_allocs == 2 && page_deallocs == 0);
|
||||||
|
|
||||||
|
// modify document (new page, old one should die)
|
||||||
|
s += s;
|
||||||
|
|
||||||
|
CHECK(doc.first_child().set_name(s.c_str()));
|
||||||
|
CHECK(page_allocs == 3 && page_deallocs == 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
CHECK(page_allocs == 3 && page_deallocs == 3);
|
||||||
|
|
||||||
|
// restore old functions
|
||||||
|
set_memory_management_functions(old_allocate, old_deallocate);
|
||||||
|
}
|
||||||
|
|
||||||
TEST(memory_large_allocations)
|
TEST(memory_large_allocations)
|
||||||
{
|
{
|
||||||
page_allocs = page_deallocs = 0;
|
page_allocs = page_deallocs = 0;
|
||||||
@ -140,6 +276,73 @@ TEST(memory_large_allocations)
|
|||||||
set_memory_management_functions(old_allocate, old_deallocate);
|
set_memory_management_functions(old_allocate, old_deallocate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(memory_pool_large_allocations)
|
||||||
|
{
|
||||||
|
page_allocs = page_deallocs = 0;
|
||||||
|
|
||||||
|
// remember old functions
|
||||||
|
allocation_function old_allocate = get_memory_allocation_function();
|
||||||
|
deallocation_function old_deallocate = get_memory_deallocation_function();
|
||||||
|
|
||||||
|
// replace functions with non-allocating ones
|
||||||
|
set_memory_management_functions(null_allocate, null_deallocate);
|
||||||
|
|
||||||
|
{
|
||||||
|
// create custom memory pool
|
||||||
|
test_memory_pool pool(allocate, deallocate);
|
||||||
|
|
||||||
|
xml_document doc(pool);
|
||||||
|
|
||||||
|
CHECK(page_allocs == 0 && page_deallocs == 0);
|
||||||
|
|
||||||
|
// initial fill
|
||||||
|
for (size_t i = 0; i < 128; ++i)
|
||||||
|
{
|
||||||
|
std::basic_string<char_t> s(i * 128, 'x');
|
||||||
|
|
||||||
|
CHECK(doc.append_child(node_pcdata).set_value(s.c_str()));
|
||||||
|
}
|
||||||
|
|
||||||
|
CHECK(page_allocs > 0 && page_deallocs == 0);
|
||||||
|
|
||||||
|
// grow-prune loop
|
||||||
|
while (doc.first_child())
|
||||||
|
{
|
||||||
|
xml_node node;
|
||||||
|
|
||||||
|
// grow
|
||||||
|
for (node = doc.first_child(); node; node = node.next_sibling())
|
||||||
|
{
|
||||||
|
std::basic_string<char_t> s = node.value();
|
||||||
|
|
||||||
|
CHECK(node.set_value((s + s).c_str()));
|
||||||
|
}
|
||||||
|
|
||||||
|
// prune
|
||||||
|
for (node = doc.first_child(); node; )
|
||||||
|
{
|
||||||
|
xml_node next = node.next_sibling().next_sibling();
|
||||||
|
|
||||||
|
node.parent().remove_child(node);
|
||||||
|
|
||||||
|
node = next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
CHECK(page_allocs == page_deallocs + 1); // only one live page left (it waits for new allocations)
|
||||||
|
|
||||||
|
char buffer;
|
||||||
|
CHECK(doc.load_buffer_inplace(&buffer, 0, parse_fragment, get_native_encoding()));
|
||||||
|
|
||||||
|
CHECK(page_allocs == page_deallocs); // no live pages left
|
||||||
|
}
|
||||||
|
|
||||||
|
CHECK(page_allocs == page_deallocs); // everything is freed
|
||||||
|
|
||||||
|
// restore old functions
|
||||||
|
set_memory_management_functions(old_allocate, old_deallocate);
|
||||||
|
}
|
||||||
|
|
||||||
TEST(memory_page_management)
|
TEST(memory_page_management)
|
||||||
{
|
{
|
||||||
page_allocs = page_deallocs = 0;
|
page_allocs = page_deallocs = 0;
|
||||||
@ -197,6 +400,66 @@ TEST(memory_page_management)
|
|||||||
set_memory_management_functions(old_allocate, old_deallocate);
|
set_memory_management_functions(old_allocate, old_deallocate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(memory_resource_page_management)
|
||||||
|
{
|
||||||
|
page_allocs = page_deallocs = 0;
|
||||||
|
|
||||||
|
// remember old functions
|
||||||
|
allocation_function old_allocate = get_memory_allocation_function();
|
||||||
|
deallocation_function old_deallocate = get_memory_deallocation_function();
|
||||||
|
|
||||||
|
// replace functions with non-allocating ones
|
||||||
|
set_memory_management_functions(null_allocate, null_deallocate);
|
||||||
|
|
||||||
|
{
|
||||||
|
// create custom memory pool
|
||||||
|
test_memory_pool pool(allocate, deallocate);
|
||||||
|
|
||||||
|
xml_document doc(pool);
|
||||||
|
|
||||||
|
CHECK(page_allocs == 0 && page_deallocs == 0);
|
||||||
|
|
||||||
|
// initial fill
|
||||||
|
std::vector<xml_node> nodes;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < 4000; ++i)
|
||||||
|
{
|
||||||
|
xml_node node = doc.append_child(STR("n"));
|
||||||
|
CHECK(node);
|
||||||
|
|
||||||
|
nodes.push_back(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
CHECK(page_allocs > 0 && page_deallocs == 0);
|
||||||
|
|
||||||
|
// grow-prune loop
|
||||||
|
size_t offset = 0;
|
||||||
|
size_t prime = 15485863;
|
||||||
|
|
||||||
|
while (nodes.size() > 0)
|
||||||
|
{
|
||||||
|
offset = (offset + prime) % nodes.size();
|
||||||
|
|
||||||
|
doc.remove_child(nodes[offset]);
|
||||||
|
|
||||||
|
nodes[offset] = nodes.back();
|
||||||
|
nodes.pop_back();
|
||||||
|
}
|
||||||
|
|
||||||
|
CHECK(page_allocs == page_deallocs + 1); // only one live page left (it waits for new allocations)
|
||||||
|
|
||||||
|
char buffer;
|
||||||
|
CHECK(doc.load_buffer_inplace(&buffer, 0, parse_fragment, get_native_encoding()));
|
||||||
|
|
||||||
|
CHECK(page_allocs == page_deallocs); // no live pages left
|
||||||
|
}
|
||||||
|
|
||||||
|
CHECK(page_allocs == page_deallocs); // everything is freed
|
||||||
|
|
||||||
|
// restore old functions
|
||||||
|
set_memory_management_functions(old_allocate, old_deallocate);
|
||||||
|
}
|
||||||
|
|
||||||
TEST(memory_string_allocate_increasing)
|
TEST(memory_string_allocate_increasing)
|
||||||
{
|
{
|
||||||
xml_document doc;
|
xml_document doc;
|
||||||
@ -301,3 +564,186 @@ TEST(memory_string_allocate_decreasing_inplace)
|
|||||||
|
|
||||||
CHECK(result == "x");
|
CHECK(result == "x");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(monotonic_memory_pool_management)
|
||||||
|
{
|
||||||
|
page_allocs = page_deallocs = 0;
|
||||||
|
|
||||||
|
// remember old functions
|
||||||
|
allocation_function old_allocate = get_memory_allocation_function();
|
||||||
|
deallocation_function old_deallocate = get_memory_deallocation_function();
|
||||||
|
|
||||||
|
// replace functions with non-allocating ones
|
||||||
|
set_memory_management_functions(null_allocate, null_deallocate);
|
||||||
|
|
||||||
|
const size_t buffer_size = 64 * 1024;
|
||||||
|
void* buffer = old_allocate(buffer_size);
|
||||||
|
|
||||||
|
{
|
||||||
|
// create custom memory pool
|
||||||
|
xml_monotonic_memory_pool pool(buffer, buffer_size);
|
||||||
|
|
||||||
|
// parse document
|
||||||
|
xml_document doc(pool);
|
||||||
|
|
||||||
|
CHECK(page_allocs == 0 && page_deallocs == 0);
|
||||||
|
|
||||||
|
// string length should be odd number in order to detect any memory misalighment issues
|
||||||
|
CHECK(doc.load_string(STR("<node><description>Simple node</description><param name='id' value='123'/>1</node>")));
|
||||||
|
|
||||||
|
CHECK(page_allocs == 0 && page_deallocs == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
old_deallocate(buffer);
|
||||||
|
|
||||||
|
// restore old functions
|
||||||
|
set_memory_management_functions(old_allocate, old_deallocate);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(monotonic_memory_pool_allocation)
|
||||||
|
{
|
||||||
|
// make address not aligned on 8 byte boundary
|
||||||
|
void* buffer = reinterpret_cast<void*>(pugi::memory_allocation_alignment + 3);
|
||||||
|
size_t size = 20;
|
||||||
|
|
||||||
|
xml_monotonic_memory_pool pool(buffer, size);
|
||||||
|
// buffer should start at 8 byte boundary, minimum allocation is 8 bytes
|
||||||
|
void* result = pool.allocate(6);
|
||||||
|
CHECK(result == reinterpret_cast<void*>(16));
|
||||||
|
|
||||||
|
// now capacity should not be enough for memory allocation
|
||||||
|
result = pool.allocate(12);
|
||||||
|
CHECK(result == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(memory_resource_pool_management)
|
||||||
|
{
|
||||||
|
memory_allocated = 0;
|
||||||
|
|
||||||
|
// remember old functions
|
||||||
|
allocation_function old_allocate = get_memory_allocation_function();
|
||||||
|
deallocation_function old_deallocate = get_memory_deallocation_function();
|
||||||
|
|
||||||
|
// replace functions with non-allocating ones
|
||||||
|
set_memory_management_functions(null_allocate, null_deallocate);
|
||||||
|
|
||||||
|
// create custom memory pool
|
||||||
|
test_memory_resource resource(old_allocate, old_deallocate);
|
||||||
|
memory_resource_adapter<test_memory_resource> pool(&resource);
|
||||||
|
|
||||||
|
{
|
||||||
|
// parse document
|
||||||
|
xml_document doc(pool);
|
||||||
|
|
||||||
|
CHECK(memory_allocated == 0);
|
||||||
|
|
||||||
|
CHECK(doc.load_string(STR("<node />")));
|
||||||
|
|
||||||
|
CHECK(memory_allocated != 0);
|
||||||
|
|
||||||
|
// modify document (no new page)
|
||||||
|
CHECK(doc.first_child().set_name(STR("foobars")));
|
||||||
|
|
||||||
|
// modify document (new page)
|
||||||
|
std::basic_string<char_t> s(65536, 'x');
|
||||||
|
|
||||||
|
CHECK(doc.first_child().set_name(s.c_str()));
|
||||||
|
|
||||||
|
// modify document (new page, old one should die)
|
||||||
|
s += s;
|
||||||
|
|
||||||
|
CHECK(doc.first_child().set_name(s.c_str()));
|
||||||
|
}
|
||||||
|
|
||||||
|
CHECK(memory_allocated == 0);
|
||||||
|
|
||||||
|
// restore old functions
|
||||||
|
set_memory_management_functions(old_allocate, old_deallocate);
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef PUGIXML_HAS_MOVE
|
||||||
|
TEST(memory_pool_swap)
|
||||||
|
{
|
||||||
|
memory_allocated = 0;
|
||||||
|
|
||||||
|
// remember old functions
|
||||||
|
allocation_function current_allocate = get_memory_allocation_function();
|
||||||
|
deallocation_function current_deallocate = get_memory_deallocation_function();
|
||||||
|
|
||||||
|
{
|
||||||
|
// create custom memory pool
|
||||||
|
test_memory_resource resource(current_allocate, current_deallocate);
|
||||||
|
memory_resource_adapter<test_memory_resource> pool(&resource);
|
||||||
|
|
||||||
|
// parse document
|
||||||
|
xml_document doc_with_custom_pool(pool);
|
||||||
|
xml_document doc_with_default_pool;
|
||||||
|
|
||||||
|
CHECK(memory_allocated == 0);
|
||||||
|
|
||||||
|
CHECK(doc_with_custom_pool.load_string(STR("<node />")));
|
||||||
|
CHECK(doc_with_custom_pool.first_child().set_name(STR("foobars")));
|
||||||
|
|
||||||
|
CHECK(memory_allocated != 0);
|
||||||
|
|
||||||
|
// move content of the document with custom memory pool
|
||||||
|
doc_with_default_pool = std::move(doc_with_custom_pool);
|
||||||
|
|
||||||
|
// memory from custom pool should become deallocated
|
||||||
|
// which means that memory pool wasn't swapped
|
||||||
|
// and that document is now empty
|
||||||
|
CHECK(memory_allocated == 0);
|
||||||
|
|
||||||
|
// check document with default pool
|
||||||
|
xml_node node = doc_with_default_pool.first_child();
|
||||||
|
CHECK_STRING(node.name(), STR("foobars"));
|
||||||
|
}
|
||||||
|
|
||||||
|
CHECK(memory_allocated == 0);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef PUGIXML_NO_XPATH
|
||||||
|
TEST(memory_pool_management_for_xpath_query)
|
||||||
|
{
|
||||||
|
const char_t* paths = STR("(//location[string(info/references/reference[1]/url)=string(current-url)]/info/references/reference[1])[1]");
|
||||||
|
|
||||||
|
char buffer[5 * 1024];
|
||||||
|
|
||||||
|
xml_monotonic_memory_pool pool(buffer, sizeof(buffer));
|
||||||
|
xpath_query q(pool, paths);
|
||||||
|
CHECK(q.result());
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef PUGIXML_HAS_MOVE
|
||||||
|
TEST(memory_pool_swap_for_xpath_query)
|
||||||
|
{
|
||||||
|
memory_allocated = 0;
|
||||||
|
|
||||||
|
// remember old functions
|
||||||
|
allocation_function current_allocate = get_memory_allocation_function();
|
||||||
|
deallocation_function current_deallocate = get_memory_deallocation_function();
|
||||||
|
|
||||||
|
{
|
||||||
|
// create custom memory pool
|
||||||
|
test_memory_resource resource(current_allocate, current_deallocate);
|
||||||
|
memory_resource_adapter<test_memory_resource> pool(&resource);
|
||||||
|
|
||||||
|
const char_t* paths1 = STR("(//location[string(info/references/reference[1]/url)=string(current-url)]/info/references/reference[1])[1]");
|
||||||
|
const char_t* paths2 = STR("/descendant-or-self::node()/child::para");
|
||||||
|
|
||||||
|
xpath_query query_with_custom_pool(pool, paths1);
|
||||||
|
size_t memory_allocated_for_query_with_custom_pool = memory_allocated;
|
||||||
|
CHECK(memory_allocated_for_query_with_custom_pool != 0);
|
||||||
|
|
||||||
|
xpath_query query_with_default_pool(paths2);
|
||||||
|
CHECK(memory_allocated_for_query_with_custom_pool == memory_allocated);
|
||||||
|
|
||||||
|
// query_with_custom_pool should receive default pool after the move
|
||||||
|
query_with_custom_pool = std::move(query_with_default_pool);
|
||||||
|
CHECK(memory_allocated == 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user