summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.travis.yml1
-rw-r--r--src/pugixml.cpp883
-rw-r--r--tests/test_dom_modify.cpp5
-rw-r--r--tests/test_memory.cpp45
4 files changed, 786 insertions, 148 deletions
diff --git a/.travis.yml b/.travis.yml
index e52453e..e30a179 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -5,4 +5,5 @@ compiler:
env:
- DEFINES=standard
- DEFINES=PUGIXML_WCHAR_MODE
+ - DEFINES=PUGIXML_COMPACT
script: make test defines=$DEFINES -j2
diff --git a/src/pugixml.cpp b/src/pugixml.cpp
index b8847a4..52ddf4c 100644
--- a/src/pugixml.cpp
+++ b/src/pugixml.cpp
@@ -259,6 +259,144 @@ PUGI__NS_BEGIN
PUGI__NS_END
#endif
+#ifdef PUGIXML_COMPACT
+PUGI__NS_BEGIN
+ class compact_hash_table
+ {
+ public:
+ compact_hash_table(): _items(0), _capacity(0), _count(0)
+ {
+ }
+
+ void clear()
+ {
+ if (_items)
+ {
+ xml_memory::deallocate(_items);
+ _items = 0;
+ _capacity = 0;
+ _count = 0;
+ }
+ }
+
+ void** find(const void* key)
+ {
+ assert(key);
+
+ if (_capacity == 0) return 0;
+
+ size_t hashmod = _capacity - 1;
+ size_t bucket = hash(key) & hashmod;
+
+ for (size_t probe = 0; probe <= hashmod; ++probe)
+ {
+ item_t& probe_item = _items[bucket];
+
+ if (probe_item.key == key)
+ return &probe_item.value;
+
+ if (probe_item.key == 0)
+ return 0;
+
+ // hash collision, quadratic probing
+ bucket = (bucket + probe + 1) & hashmod;
+ }
+
+ assert(!"Hash table is full");
+ return 0;
+ }
+
+ void** insert(const void* key)
+ {
+ assert(key);
+ assert(_count < _capacity * 3 / 4);
+
+ size_t hashmod = _capacity - 1;
+ size_t bucket = hash(key) & hashmod;
+
+ for (size_t probe = 0; probe <= hashmod; ++probe)
+ {
+ item_t& probe_item = _items[bucket];
+
+ if (probe_item.key == 0)
+ {
+ probe_item.key = key;
+ _count++;
+ return &probe_item.value;
+ }
+
+ if (probe_item.key == key)
+ return &probe_item.value;
+
+ // hash collision, quadratic probing
+ bucket = (bucket + probe + 1) & hashmod;
+ }
+
+ assert(!"Hash table is full");
+ return 0;
+ }
+
+ bool reserve()
+ {
+ if (_count + 16 >= _capacity * 3 / 4)
+ return rehash();
+
+ return true;
+ }
+
+ private:
+ struct item_t
+ {
+ const void* key;
+ void* value;
+ };
+
+ item_t* _items;
+ size_t _capacity;
+
+ size_t _count;
+
+ bool rehash()
+ {
+ compact_hash_table rt;
+ rt._capacity = (_capacity == 0) ? 32 : _capacity * 2;
+ rt._items = static_cast<item_t*>(xml_memory::allocate(sizeof(item_t) * rt._capacity));
+
+ if (!rt._items)
+ return false;
+
+ memset(rt._items, 0, sizeof(item_t) * rt._capacity);
+
+ for (size_t i = 0; i < _capacity; ++i)
+ if (_items[i].key)
+ *rt.insert(_items[i].key) = _items[i].value;
+
+ if (_items)
+ xml_memory::deallocate(_items);
+
+ _capacity = rt._capacity;
+ _items = rt._items;
+
+ return true;
+ }
+
+ static unsigned int hash(const void* key)
+ {
+ unsigned int h = static_cast<unsigned int>(reinterpret_cast<uintptr_t>(key));
+
+ // MurmurHash3 32-bit finalizer
+ h ^= h >> 16;
+ h *= 0x85ebca6bu;
+ h ^= h >> 13;
+ h *= 0xc2b2ae35u;
+ h ^= h >> 16;
+
+ return h;
+ }
+ };
+PUGI__NS_END
+#endif
+
PUGI__NS_BEGIN
static const size_t xml_memory_page_size =
#ifdef PUGIXML_MEMORY_PAGE_SIZE
@@ -268,14 +406,22 @@ PUGI__NS_BEGIN
#endif
;
- static const uintptr_t xml_memory_page_alignment = 64;
+ static const uintptr_t xml_memory_page_alignment = 32;
static const uintptr_t xml_memory_page_pointer_mask = ~(xml_memory_page_alignment - 1);
- static const uintptr_t xml_memory_page_contents_shared_mask = 32;
- static const uintptr_t xml_memory_page_name_allocated_mask = 16;
- static const uintptr_t xml_memory_page_value_allocated_mask = 8;
+
+ // extra metadata bits for xml_node_struct
+ static const uintptr_t xml_memory_page_contents_shared_mask = 16;
+ static const uintptr_t xml_memory_page_contents_allocated_mask = 8;
static const uintptr_t xml_memory_page_type_mask = 7;
+
+ // extra metadata bits for xml_attribute_struct
+ static const uintptr_t xml_memory_page_name_allocated_mask = 2;
+ static const uintptr_t xml_memory_page_value_allocated_mask = 1;
+
+ // combined masks for string uniqueness
static const uintptr_t xml_memory_page_name_allocated_or_shared_mask = xml_memory_page_name_allocated_mask | xml_memory_page_contents_shared_mask;
static const uintptr_t xml_memory_page_value_allocated_or_shared_mask = xml_memory_page_value_allocated_mask | xml_memory_page_contents_shared_mask;
+ static const uintptr_t xml_memory_page_contents_allocated_or_shared_mask = xml_memory_page_contents_allocated_mask | xml_memory_page_contents_shared_mask;
#define PUGI__NODETYPE(n) static_cast<xml_node_type>(((n)->header & impl::xml_memory_page_type_mask) + 1)
@@ -293,6 +439,11 @@ PUGI__NS_BEGIN
result->busy_size = 0;
result->freed_size = 0;
+ #ifdef PUGIXML_COMPACT
+ result->compact_string_base = 0;
+ result->compact_shared_parent = 0;
+ #endif
+
return result;
}
@@ -303,6 +454,11 @@ PUGI__NS_BEGIN
size_t busy_size;
size_t freed_size;
+
+ #ifdef PUGIXML_COMPACT
+ char_t* compact_string_base;
+ void* compact_shared_parent;
+ #endif
};
struct xml_memory_string_header
@@ -315,6 +471,9 @@ PUGI__NS_BEGIN
{
xml_allocator(xml_memory_page* root): _root(root), _busy_size(root->busy_size)
{
+ #ifdef PUGIXML_COMPACT
+ _hash = 0;
+ #endif
}
xml_memory_page* allocate_page(size_t data_size)
@@ -447,8 +606,21 @@ PUGI__NS_BEGIN
deallocate_memory(header, full_size, page);
}
+ bool reserve()
+ {
+ #ifdef PUGIXML_COMPACT
+ return _hash->reserve();
+ #else
+ return true;
+ #endif
+ }
+
xml_memory_page* _root;
size_t _busy_size;
+
+ #ifdef PUGIXML_COMPACT
+ compact_hash_table* _hash;
+ #endif
};
PUGI__FN_NO_INLINE void* xml_allocator::allocate_memory_oob(size_t size, xml_memory_page*& out_page)
@@ -491,6 +663,338 @@ PUGI__NS_BEGIN
}
PUGI__NS_END
+#ifdef PUGIXML_COMPACT
+PUGI__NS_BEGIN
+ static const uintptr_t compact_alignment_log2 = 2;
+ static const uintptr_t compact_alignment = 1 << compact_alignment_log2;
+
+ class compact_header
+ {
+ public:
+ compact_header(xml_memory_page* page, unsigned int flags)
+ {
+ PUGI__STATIC_ASSERT(sizeof(xml_memory_page) + xml_memory_page_size <= (1 << (16 + compact_alignment_log2)));
+
+ ptrdiff_t page_offset = (reinterpret_cast<char*>(this) - reinterpret_cast<char*>(page)) >> compact_alignment_log2;
+ assert(page_offset >= 0 && page_offset < (1 << 16));
+
+ this->page0 = static_cast<unsigned char>(page_offset);
+ this->page1 = static_cast<unsigned char>(page_offset >> 8);
+ this->flags = static_cast<unsigned char>(flags);
+ }
+
+ void operator&=(uintptr_t modflags)
+ {
+ flags &= modflags;
+ }
+
+ void operator|=(uintptr_t modflags)
+ {
+ flags |= modflags;
+ }
+
+ operator uintptr_t() const
+ {
+ return reinterpret_cast<uintptr_t>(get_page()) | flags;
+ }
+
+ xml_memory_page* get_page() const
+ {
+ unsigned int page_offset = page0 + (page1 << 8);
+
+ return const_cast<xml_memory_page*>(reinterpret_cast<const xml_memory_page*>(reinterpret_cast<const char*>(this) - (page_offset << compact_alignment_log2)));
+ }
+
+ private:
+ unsigned char page0, page1;
+ unsigned char flags;
+ };
+
+ PUGI__FN xml_memory_page* compact_get_page(const void* object, int header_offset)
+ {
+ const compact_header* header = reinterpret_cast<const compact_header*>(static_cast<const char*>(object) - header_offset);
+
+ return header->get_page();
+ }
+
+ template <int header_offset, typename T> PUGI__FN_NO_INLINE T* compact_get_value(const void* object)
+ {
+ return static_cast<T*>(*compact_get_page(object, header_offset)->allocator->_hash->find(object));
+ }
+
+ template <int header_offset, typename T> PUGI__FN_NO_INLINE void compact_set_value(const void* object, T* value)
+ {
+ *compact_get_page(object, header_offset)->allocator->_hash->insert(object) = value;
+ }
+
+ template <typename T, int header_offset, int start = -126> class compact_pointer
+ {
+ public:
+ compact_pointer(): _data(0)
+ {
+ }
+
+ void operator=(const compact_pointer& rhs)
+ {
+ *this = rhs + 0;
+ }
+
+ void operator=(T* value)
+ {
+ if (value)
+ {
+ ptrdiff_t offset = ((reinterpret_cast<char*>(value) - reinterpret_cast<char*>(this) + int(compact_alignment - 1)) >> compact_alignment_log2) - start;
+
+ if (static_cast<uintptr_t>(offset) <= 253)
+ _data = static_cast<unsigned char>(offset + 1);
+ else
+ {
+ compact_set_value<header_offset>(this, value);
+
+ _data = 255;
+ }
+ }
+ else
+ _data = 0;
+ }
+
+ operator T*() const
+ {
+ if (_data)
+ {
+ if (_data < 255)
+ {
+ uintptr_t base = reinterpret_cast<uintptr_t>(this) & ~(compact_alignment - 1);
+
+ return reinterpret_cast<T*>(base + ((_data - (1 - start)) << compact_alignment_log2));
+ }
+ else
+ return compact_get_value<header_offset, T>(this);
+ }
+ else
+ return 0;
+ }
+
+ T* operator->() const
+ {
+ return operator T*();
+ }
+
+ private:
+ unsigned char _data;
+ };
+
+ template <typename T, int header_offset> class compact_pointer_parent
+ {
+ public:
+ compact_pointer_parent(): _data0(0), _data1(0)
+ {
+ }
+
+ void operator=(const compact_pointer_parent& rhs)
+ {
+ *this = rhs + 0;
+ }
+
+ void operator=(T* value)
+ {
+ if (value)
+ {
+ ptrdiff_t offset = ((reinterpret_cast<char*>(value) - reinterpret_cast<char*>(this) + int(compact_alignment - 1)) >> compact_alignment_log2) + 65533;
+
+ if (static_cast<uintptr_t>(offset) <= 65533)
+ {
+ _data0 = static_cast<unsigned char>(offset + 1);
+ _data1 = static_cast<unsigned char>((offset + 1) >> 8);
+ }
+ else
+ {
+ xml_memory_page* page = compact_get_page(this, header_offset);
+
+ if (page->compact_shared_parent == 0)
+ page->compact_shared_parent = value;
+
+ if (page->compact_shared_parent == value)
+ {
+ _data0 = 254;
+ _data1 = 255;
+ }
+ else
+ {
+ compact_set_value<header_offset>(this, value);
+
+ _data0 = 255;
+ _data1 = 255;
+ }
+ }
+ }
+ else
+ {
+ _data0 = 0;
+ _data1 = 0;
+ }
+ }
+
+ operator T*() const
+ {
+ int data = _data0 + (_data1 << 8);
+
+ if (data)
+ {
+ if (data < 65534)
+ {
+ uintptr_t base = reinterpret_cast<uintptr_t>(this) & ~(compact_alignment - 1);
+
+ return reinterpret_cast<T*>(base + ((data - (1 + 65533)) << compact_alignment_log2));
+ }
+ else if (data == 65534)
+ return static_cast<T*>(compact_get_page(this, header_offset)->compact_shared_parent);
+ else
+ return compact_get_value<header_offset, T>(this);
+ }
+ else
+ return 0;
+ }
+
+ T* operator->() const
+ {
+ return operator T*();
+ }
+
+ private:
+ unsigned char _data0;
+ unsigned char _data1;
+ };
+
+ template <int header_offset> class compact_string
+ {
+ public:
+ compact_string(): _data0(0), _data1(0), _data2(0)
+ {
+ }
+
+ void operator=(const compact_string& rhs)
+ {
+ *this = rhs + 0;
+ }
+
+ void operator=(char_t* value)
+ {
+ if (value)
+ {
+ xml_memory_page* page = compact_get_page(this, header_offset);
+
+ if (page->compact_string_base == 0)
+ page->compact_string_base = value;
+
+ ptrdiff_t offset = value - page->compact_string_base;
+
+ if (static_cast<uintptr_t>(offset) >= 16777213)
+ {
+ compact_set_value<header_offset>(this, value);
+
+ offset = 16777214;
+ }
+
+ _data0 = static_cast<unsigned char>(offset + 1);
+ _data1 = static_cast<unsigned char>((offset + 1) >> 8);
+ _data2 = static_cast<unsigned char>((offset + 1) >> 16);
+ }
+ else
+ {
+ _data0 = 0;
+ _data1 = 0;
+ _data2 = 0;
+ }
+ }
+
+ operator char_t*() const
+ {
+ unsigned int data = _data0 + (_data1 << 8) + (_data2 << 16);
+
+ if (data)
+ {
+ xml_memory_page* page = compact_get_page(this, header_offset);
+
+ if (data < 16777215)
+ return page->compact_string_base + (data - 1);
+ else
+ return compact_get_value<header_offset, char_t>(this);
+ }
+ else
+ return 0;
+ }
+
+ private:
+ unsigned char _data0;
+ unsigned char _data1;
+ unsigned char _data2;
+ };
+
+PUGI__NS_END
+#endif
+
+#ifdef PUGIXML_COMPACT
+namespace pugi
+{
+ /// A 'name=value' XML attribute structure.
+ struct xml_attribute_struct
+ {
+ /// Default ctor
+ xml_attribute_struct(impl::xml_memory_page* page): header(page, 0)
+ {
+ PUGI__STATIC_ASSERT(sizeof(xml_attribute_struct) == 12);
+ }
+
+ impl::compact_header header;
+
+ unsigned char padding;
+
+ impl::compact_string<4> name; ///< Pointer to attribute name.
+ impl::compact_string<7> value; ///< Pointer to attribute value.
+
+ impl::compact_pointer<xml_attribute_struct, 10> prev_attribute_c; ///< Previous attribute (cyclic list)
+ impl::compact_pointer<xml_attribute_struct, 11, 0> next_attribute; ///< Next attribute
+ };
+
+ /// An XML document tree node.
+ struct xml_node_struct
+ {
+ /// Default ctor
+ /// \param type - node type
+ xml_node_struct(impl::xml_memory_page* page, xml_node_type type): header(page, type - 1)
+ {
+ PUGI__STATIC_ASSERT(sizeof(xml_node_struct) == 12);
+ }
+
+ impl::compact_header header;
+
+ impl::compact_string<3> contents; ///< Pointer to element name.
+
+ impl::compact_pointer_parent<xml_node_struct, 6> parent; ///< Pointer to parent
+
+ impl::compact_pointer<xml_node_struct, 8, 0> first_child; ///< First child
+
+ impl::compact_pointer<xml_node_struct, 9> prev_sibling_c; ///< Left brother (cyclic list)
+ impl::compact_pointer<xml_node_struct, 10, 0> next_sibling; ///< Right brother
+
+ impl::compact_pointer<xml_attribute_struct, 11, 0> first_attribute; ///< First attribute
+ };
+
+ struct xml_node_pi_struct: xml_node_struct
+ {
+ xml_node_pi_struct(impl::xml_memory_page* page): xml_node_struct(page, node_pi), pi_header(page, 0)
+ {
+ PUGI__STATIC_ASSERT(sizeof(xml_node_pi_struct) == 20);
+ }
+
+ impl::compact_header pi_header;
+ impl::compact_string<3> pi_value;
+
+ unsigned char padding[2];
+ };
+}
+#else
namespace pugi
{
/// A 'name=value' XML attribute structure.
@@ -503,7 +1007,7 @@ namespace pugi
uintptr_t header;
- char_t* name; ///< Pointer to attribute name.
+ char_t* name; ///< Pointer to attribute name.
char_t* value; ///< Pointer to attribute value.
xml_attribute_struct* prev_attribute_c; ///< Previous attribute (cyclic list)
@@ -515,7 +1019,7 @@ namespace pugi
{
/// Default ctor
/// \param type - node type
- xml_node_struct(impl::xml_memory_page* page, xml_node_type type): header(reinterpret_cast<uintptr_t>(page) | (type - 1)), parent(0), name(0), value(0), first_child(0), prev_sibling_c(0), next_sibling(0), first_attribute(0)
+ xml_node_struct(impl::xml_memory_page* page, xml_node_type type): header(reinterpret_cast<uintptr_t>(page) | (type - 1)), parent(0), contents(0), first_child(0), prev_sibling_c(0), next_sibling(0), first_attribute(0)
{
}
@@ -523,8 +1027,7 @@ namespace pugi
xml_node_struct* parent; ///< Pointer to parent
- char_t* name; ///< Pointer to element name.
- char_t* value; ///< Pointer to any associated string data.
+ char_t* contents; ///< Pointer to element name/value
xml_node_struct* first_child; ///< First child
@@ -533,7 +1036,18 @@ namespace pugi
xml_attribute_struct* first_attribute; ///< First attribute
};
+
+ struct xml_node_pi_struct: xml_node_struct
+ {
+ xml_node_pi_struct(impl::xml_memory_page* page): xml_node_struct(page, node_pi), pi_header(reinterpret_cast<uintptr_t>(page)), pi_value(0)
+ {
+ }
+
+ uintptr_t pi_header;
+ char_t* pi_value;
+ };
}
+#endif
PUGI__NS_BEGIN
struct xml_extra_buffer
@@ -546,11 +1060,18 @@ PUGI__NS_BEGIN
{
xml_document_struct(xml_memory_page* page): xml_node_struct(page, node_document), xml_allocator(page), buffer(0), extra_buffers(0)
{
+ #ifdef PUGIXML_COMPACT
+ _hash = &hash;
+ #endif
}
const char_t* buffer;
xml_extra_buffer* extra_buffers;
+
+ #ifdef PUGIXML_COMPACT
+ compact_hash_table hash;
+ #endif
};
inline xml_allocator& get_allocator(const xml_node_struct* node)
@@ -580,10 +1101,20 @@ PUGI__NS_BEGIN
inline xml_node_struct* allocate_node(xml_allocator& alloc, xml_node_type type)
{
- xml_memory_page* page;
- void* memory = alloc.allocate_memory(sizeof(xml_node_struct), page);
+ if (type != node_pi)
+ {
+ xml_memory_page* page;
+ void* memory = alloc.allocate_memory(sizeof(xml_node_struct), page);
+
+ return new (memory) xml_node_struct(page, type);
+ }
+ else
+ {
+ xml_memory_page* page;
+ void* memory = alloc.allocate_memory(sizeof(xml_node_pi_struct), page);
- return new (memory) xml_node_struct(page, type);
+ return new (memory) xml_node_pi_struct(page);
+ }
}
inline void destroy_attribute(xml_attribute_struct* a, xml_allocator& alloc)
@@ -600,8 +1131,7 @@ PUGI__NS_BEGIN
{
uintptr_t header = n->header;
- if (header & impl::xml_memory_page_name_allocated_mask) alloc.deallocate_string(n->name);
- if (header & impl::xml_memory_page_value_allocated_mask) alloc.deallocate_string(n->value);
+ if (header & impl::xml_memory_page_contents_allocated_mask) alloc.deallocate_string(n->contents);
for (xml_attribute_struct* attr = n->first_attribute; attr; )
{
@@ -793,6 +1323,8 @@ PUGI__NS_BEGIN
PUGI__FN_NO_INLINE xml_node_struct* append_new_node(xml_node_struct* node, xml_allocator& alloc, xml_node_type type = node_element)
{
+ if (!alloc.reserve()) return 0;
+
xml_node_struct* child = allocate_node(alloc, type);
if (!child) return 0;
@@ -803,6 +1335,8 @@ PUGI__NS_BEGIN
PUGI__FN_NO_INLINE xml_attribute_struct* append_new_attribute(xml_node_struct* node, xml_allocator& alloc)
{
+ if (!alloc.reserve()) return 0;
+
xml_attribute_struct* attr = allocate_attribute(alloc);
if (!attr) return 0;
@@ -1749,7 +2283,8 @@ PUGI__NS_BEGIN
return target_length >= length && (target_length < reuse_threshold || target_length - length < target_length / 2);
}
- PUGI__FN bool strcpy_insitu(char_t*& dest, uintptr_t& header, uintptr_t header_mask, const char_t* source)
+ template <typename String, typename Header>
+ PUGI__FN bool strcpy_insitu(String& dest, Header& header, uintptr_t header_mask, const char_t* source)
{
assert(header);
@@ -1779,6 +2314,8 @@ PUGI__NS_BEGIN
{
xml_allocator* alloc = reinterpret_cast<xml_memory_page*>(header & xml_memory_page_pointer_mask)->allocator;
+ if (!alloc->reserve()) return false;
+
// allocate new buffer
char_t* buf = alloc->allocate_string(source_length + 1);
if (!buf) return false;
@@ -2441,14 +2978,14 @@ PUGI__NS_BEGIN
if (PUGI__OPTSET(parse_comments))
{
PUGI__PUSHNODE(node_comment); // Append a new node on the tree.
- cursor->value = s; // Save the offset.
+ cursor->contents = s; // Save the offset.
}
if (PUGI__OPTSET(parse_eol) && PUGI__OPTSET(parse_comments))
{
s = strconv_comment(s, endch);
- if (!s) PUGI__THROW_ERROR(status_bad_comment, cursor->value);
+ if (!s) PUGI__THROW_ERROR(status_bad_comment, cursor->contents);
}
else
{
@@ -2474,13 +3011,13 @@ PUGI__NS_BEGIN
if (PUGI__OPTSET(parse_cdata))
{
PUGI__PUSHNODE(node_cdata); // Append a new node on the tree.
- cursor->value = s; // Save the offset.
+ cursor->contents = s; // Save the offset.
if (PUGI__OPTSET(parse_eol))
{
s = strconv_cdata(s, endch);
- if (!s) PUGI__THROW_ERROR(status_bad_cdata, cursor->value);
+ if (!s) PUGI__THROW_ERROR(status_bad_cdata, cursor->contents);
}
else
{
@@ -2524,7 +3061,7 @@ PUGI__NS_BEGIN
PUGI__PUSHNODE(node_doctype);
- cursor->value = mark;
+ cursor->contents = mark;
}
}
else if (*s == 0 && endch == '-') PUGI__THROW_ERROR(status_bad_comment, s);
@@ -2568,7 +3105,7 @@ PUGI__NS_BEGIN
PUGI__PUSHNODE(node_pi);
}
- cursor->name = target;
+ cursor->contents = target;
PUGI__ENDSEG();
@@ -2602,7 +3139,8 @@ PUGI__NS_BEGIN
else
{
// store value and step over >
- cursor->value = value;
+ static_cast<xml_node_pi_struct*>(cursor)->pi_value = value;
+
PUGI__POPNODE();
PUGI__ENDSEG();
@@ -2647,7 +3185,7 @@ PUGI__NS_BEGIN
{
PUGI__PUSHNODE(node_element); // Append a new node to the tree.
- cursor->name = s;
+ cursor->contents = s;
PUGI__SCANWHILE_UNROLL(PUGI__IS_CHARTYPE(ss, ct_symbol)); // Scan for a terminator.
PUGI__ENDSEG(); // Save char in 'ch', terminate & step over.
@@ -2757,7 +3295,7 @@ PUGI__NS_BEGIN
{
++s;
- char_t* name = cursor->name;
+ char_t* name = cursor->contents;
if (!name) PUGI__THROW_ERROR(status_end_element_mismatch, s);
while (PUGI__IS_CHARTYPE(*s, ct_symbol))
@@ -2828,7 +3366,7 @@ PUGI__NS_BEGIN
if (cursor->parent || PUGI__OPTSET(parse_fragment))
{
PUGI__PUSHNODE(node_pcdata); // Append a new node on the tree.
- cursor->value = s; // Save the offset.
+ cursor->contents = s; // Save the offset.
s = strconv_pcdata(s);
@@ -2890,7 +3428,7 @@ PUGI__NS_BEGIN
return make_parse_result(PUGI__OPTSET(parse_fragment) ? status_ok : status_no_document_element);
// get last child of the root before parsing
- xml_node_struct* last_root_child = root->first_child ? root->first_child->prev_sibling_c : 0;
+ xml_node_struct* last_root_child = root->first_child ? root->first_child->prev_sibling_c + 0 : 0;
// create parser on stack
xml_parser parser(alloc_);
@@ -2918,7 +3456,7 @@ PUGI__NS_BEGIN
return make_parse_result(status_unrecognized_tag, length - 1);
// check if there are any element nodes parsed
- xml_node_struct* first_root_child_parsed = last_root_child ? last_root_child->next_sibling : root->first_child;
+ xml_node_struct* first_root_child_parsed = last_root_child ? last_root_child->next_sibling + 0 : root->first_child;
if (!PUGI__OPTSET(parse_fragment) && !has_element_node_siblings(first_root_child_parsed))
return make_parse_result(status_no_document_element, length - 1);
@@ -3471,7 +4009,7 @@ PUGI__NS_BEGIN
PUGI__FN bool node_output_start(xml_buffered_writer& writer, xml_node_struct* node, unsigned int flags)
{
const char_t* default_name = PUGIXML_TEXT(":anonymous");
- const char_t* name = node->name ? node->name : default_name;
+ const char_t* name = node->contents ? node->contents : default_name;
writer.write('<');
writer.write_string(name);
@@ -3500,7 +4038,7 @@ PUGI__NS_BEGIN
{
writer.write('>');
- const char_t* value = first->value ? first->value : PUGIXML_TEXT("");
+ const char_t* value = first->contents ? first->contents + 0 : PUGIXML_TEXT("");
if (PUGI__NODETYPE(first) == node_pcdata)
text_output(writer, value, ctx_special_pcdata, flags);
@@ -3525,7 +4063,7 @@ PUGI__NS_BEGIN
PUGI__FN void node_output_end(xml_buffered_writer& writer, xml_node_struct* node, unsigned int flags)
{
const char_t* default_name = PUGIXML_TEXT(":anonymous");
- const char_t* name = node->name ? node->name : default_name;
+ const char_t* name = node->contents ? node->contents : default_name;
writer.write('<', '/');
writer.write_string(name);
@@ -3543,28 +4081,28 @@ PUGI__NS_BEGIN
switch (PUGI__NODETYPE(node))
{
case node_pcdata:
- text_output(writer, node->value ? node->value : PUGIXML_TEXT(""), ctx_special_pcdata, flags);
+ text_output(writer, node->contents ? node->contents + 0 : PUGIXML_TEXT(""), ctx_special_pcdata, flags);
if ((flags & format_raw) == 0) writer.write('\n');
break;
case node_cdata:
- text_output_cdata(writer, node->value ? node->value : PUGIXML_TEXT(""));
+ text_output_cdata(writer, node->contents ? node->contents + 0 : PUGIXML_TEXT(""));
if ((flags & format_raw) == 0) writer.write('\n');
break;
case node_comment:
- node_output_comment(writer, node->value ? node->value : PUGIXML_TEXT(""));
+ node_output_comment(writer, node->contents ? node->contents + 0 : PUGIXML_TEXT(""));
if ((flags & format_raw) == 0) writer.write('\n');
break;
case node_pi:
writer.write('<', '?');
- writer.write_string(node->name ? node->name : default_name);
+ writer.write_string(node->contents ? node->contents : default_name);
- if (node->value)
+ if (static_cast<xml_node_pi_struct*>(node)->pi_value)
{
writer.write(' ');
- writer.write_string(node->value);
+ writer.write_string(static_cast<xml_node_pi_struct*>(node)->pi_value);
}
writer.write('?', '>');
@@ -3573,7 +4111,7 @@ PUGI__NS_BEGIN
case node_declaration:
writer.write('<', '?');
- writer.write_string(node->name ? node->name : default_name);
+ writer.write_string(node->contents ? node->contents : default_name);
node_output_attributes(writer, node, flags);
writer.write('?', '>');
if ((flags & format_raw) == 0) writer.write('\n');
@@ -3583,10 +4121,10 @@ PUGI__NS_BEGIN
writer.write('<', '!', 'D', 'O', 'C');
writer.write('T', 'Y', 'P', 'E');
- if (node->value)
+ if (node->contents)
{
writer.write(' ');
- writer.write_string(node->value);
+ writer.write_string(node->contents);
}
writer.write('>');
@@ -3720,7 +4258,8 @@ PUGI__NS_BEGIN
return true;
}
- PUGI__FN void node_copy_string(char_t*& dest, uintptr_t& header, uintptr_t header_mask, char_t* source, uintptr_t& source_header, xml_allocator* alloc)
+ template <typename String, typename Header>
+ PUGI__FN void node_copy_string(String& dest, Header& header, uintptr_t header_mask, char_t* source, Header& source_header, xml_allocator* alloc)
{
assert(!dest && (header & header_mask) == 0);
@@ -3741,8 +4280,15 @@ PUGI__NS_BEGIN
PUGI__FN void node_copy_contents(xml_node_struct* dn, xml_node_struct* sn, xml_allocator* shared_alloc)
{
- node_copy_string(dn->name, dn->header, xml_memory_page_name_allocated_mask, sn->name, sn->header, shared_alloc);
- node_copy_string(dn->value, dn->header, xml_memory_page_value_allocated_mask, sn->value, sn->header, shared_alloc);
+ node_copy_string(dn->contents, dn->header, xml_memory_page_contents_allocated_mask, sn->contents, sn->header, shared_alloc);
+
+ if (PUGI__NODETYPE(dn) == node_pi)
+ {
+ xml_node_pi_struct* dnp = static_cast<xml_node_pi_struct*>(dn);
+ xml_node_pi_struct* snp = static_cast<xml_node_pi_struct*>(sn);
+
+ node_copy_string(dnp->pi_value, dnp->pi_header, xml_memory_page_contents_allocated_mask, snp->pi_value, snp->pi_header, shared_alloc);
+ }
for (xml_attribute_struct* sa = sn->first_attribute; sa; sa = sa->next_attribute)
{
@@ -3926,7 +4472,8 @@ PUGI__NS_BEGIN
#endif
// set value with conversion functions
- PUGI__FN bool set_value_buffer(char_t*& dest, uintptr_t& header, uintptr_t header_mask, char (&buf)[128])
+ template <typename String, typename Header>
+ PUGI__FN bool set_value_buffer(String& dest, Header& header, uintptr_t header_mask, char (&buf)[128])
{
#ifdef PUGIXML_WCHAR_MODE
char_t wbuf[128];
@@ -3938,7 +4485,8 @@ PUGI__NS_BEGIN
#endif
}
- PUGI__FN bool set_value_convert(char_t*& dest, uintptr_t& header, uintptr_t header_mask, int value)
+ template <typename String, typename Header>
+ PUGI__FN bool set_value_convert(String& dest, Header& header, uintptr_t header_mask, int value)
{
char buf[128];
sprintf(buf, "%d", value);
@@ -3946,7 +4494,8 @@ PUGI__NS_BEGIN
return set_value_buffer(dest, header, header_mask, buf);
}
- PUGI__FN bool set_value_convert(char_t*& dest, uintptr_t& header, uintptr_t header_mask, unsigned int value)
+ template <typename String, typename Header>
+ PUGI__FN bool set_value_convert(String& dest, Header& header, uintptr_t header_mask, unsigned int value)
{
char buf[128];
sprintf(buf, "%u", value);
@@ -3954,7 +4503,8 @@ PUGI__NS_BEGIN
return set_value_buffer(dest, header, header_mask, buf);
}
- PUGI__FN bool set_value_convert(char_t*& dest, uintptr_t& header, uintptr_t header_mask, double value)
+ template <typename String, typename Header>
+ PUGI__FN bool set_value_convert(String& dest, Header& header, uintptr_t header_mask, double value)
{
char buf[128];
sprintf(buf, "%g", value);
@@ -3962,13 +4512,15 @@ PUGI__NS_BEGIN
return set_value_buffer(dest, header, header_mask, buf);
}
- PUGI__FN bool set_value_convert(char_t*& dest, uintptr_t& header, uintptr_t header_mask, bool value)
+ template <typename String, typename Header>
+ PUGI__FN bool set_value_convert(String& dest, Header& header, uintptr_t header_mask, bool value)
{
return strcpy_insitu(dest, header, header_mask, value ? PUGIXML_TEXT("true") : PUGIXML_TEXT("false"));
}
#ifdef PUGIXML_HAS_LONG_LONG
- PUGI__FN bool set_value_convert(char_t*& dest, uintptr_t& header, uintptr_t header_mask, long long value)
+ template <typename String, typename Header>
+ PUGI__FN bool set_value_convert(String& dest, Header& header, uintptr_t header_mask, long long value)
{
char buf[128];
sprintf(buf, "%lld", value);
@@ -3976,7 +4528,8 @@ PUGI__NS_BEGIN
return set_value_buffer(dest, header, header_mask, buf);
}
- PUGI__FN bool set_value_convert(char_t*& dest, uintptr_t& header, uintptr_t header_mask, unsigned long long value)
+ template <typename String, typename Header>
+ PUGI__FN bool set_value_convert(String& dest, Header& header, uintptr_t header_mask, unsigned long long value)
{
char buf[128];
sprintf(buf, "%llu", value);
@@ -4329,6 +4882,20 @@ PUGI__NS_BEGIN
return res;
}
+
+ inline bool has_name(xml_node_struct* node)
+ {
+ static const bool result[] = { false, false, true, false, false, false, true, true, false };
+
+ return result[PUGI__NODETYPE(node)];
+ }
+
+ inline bool has_value(xml_node_struct* node)
+ {
+ static const bool result[] = { false, false, false, true, true, true, false, false, true };
+
+ return result[PUGI__NODETYPE(node)];
+ }
PUGI__NS_END
namespace pugi
@@ -4461,38 +5028,38 @@ namespace pugi
PUGI__FN int xml_attribute::as_int(int def) const
{
- return impl::get_value_int(_attr ? _attr->value : 0, def);
+ return impl::get_value_int(_attr ? _attr->value + 0 : 0, def);
}
PUGI__FN unsigned int xml_attribute::as_uint(unsigned int def) const
{
- return impl::get_value_uint(_attr ? _attr->value : 0, def);
+ return impl::get_value_uint(_attr ? _attr->value + 0 : 0, def);
}
PUGI__FN double xml_attribute::as_double(double def) const
{
- return impl::get_value_double(_attr ? _attr->value : 0, def);
+ return impl::get_value_double(_attr ? _attr->value + 0 : 0, def);
}
PUGI__FN float xml_attribute::as_float(float def) const
{
- return impl::get_value_float(_attr ? _attr->value : 0, def);
+ return impl::get_value_float(_attr ? _attr->value + 0 : 0, def);
}
PUGI__FN bool xml_attribute::as_bool(bool def) const
{
- return impl::get_value_bool(_attr ? _attr->value : 0, def);
+ return impl::get_value_bool(_attr ? _attr->value + 0 : 0, def);
}
#ifdef PUGIXML_HAS_LONG_LONG
PUGI__FN long long xml_attribute::as_llong(long long def) const
{
- return impl::get_value_llong(_attr ? _attr->value : 0, def);
+ return impl::get_value_llong(_attr ? _attr->value + 0 : 0, def);
}
PUGI__FN unsigned long long xml_attribute::as_ullong(unsigned long long def) const
{
- return impl::get_value_ullong(_attr ? _attr->value : 0, def);
+ return impl::get_value_ullong(_attr ? _attr->value + 0 : 0, def);
}
#endif
@@ -4503,12 +5070,12 @@ namespace pugi
PUGI__FN const char_t* xml_attribute::name() const
{
- return (_attr && _attr->name) ? _attr->name : PUGIXML_TEXT("");
+ return (_attr && _attr->name) ? _attr->name + 0 : PUGIXML_TEXT("");
}
PUGI__FN const char_t* xml_attribute::value() const
{
- return (_attr && _attr->value) ? _attr->value : PUGIXML_TEXT("");
+ return (_attr && _attr->value) ? _attr->value + 0 : PUGIXML_TEXT("");
}
PUGI__FN size_t xml_attribute::hash_value() const
@@ -4659,7 +5226,7 @@ namespace pugi
PUGI__FN xml_node::iterator xml_node::begin() const
{
- return iterator(_root ? _root->first_child : 0, _root);
+ return iterator(_root ? _root->first_child + 0 : 0, _root);
}
PUGI__FN xml_node::iterator xml_node::end() const
@@ -4669,7 +5236,7 @@ namespace pugi
PUGI__FN xml_node::attribute_iterator xml_node::attributes_begin() const
{
- return attribute_iterator(_root ? _root->first_attribute : 0, _root);
+ return attribute_iterator(_root ? _root->first_attribute + 0 : 0, _root);
}
PUGI__FN xml_node::attribute_iterator xml_node::attributes_end() const
@@ -4729,7 +5296,7 @@ namespace pugi
PUGI__FN const char_t* xml_node::name() const
{
- return (_root && _root->name) ? _root->name : PUGIXML_TEXT("");
+ return (_root && impl::has_name(_root) && _root->contents) ? _root->contents + 0 : PUGIXML_TEXT("");
}
PUGI__FN xml_node_type xml_node::type() const
@@ -4739,7 +5306,16 @@ namespace pugi
PUGI__FN const char_t* xml_node::value() const
{
- return (_root && _root->value) ? _root->value : PUGIXML_TEXT("");
+ if (_root)
+ {
+ if (impl::has_value(_root) && _root->contents)
+ return _root->contents;
+
+ if (PUGI__NODETYPE(_root) == node_pi && static_cast<xml_node_pi_struct*>(_root)->pi_value)
+ return static_cast<xml_node_pi_struct*>(_root)->pi_value;
+ }
+
+ return PUGIXML_TEXT("");
}
PUGI__FN xml_node xml_node::child(const char_t* name_) const
@@ -4747,7 +5323,7 @@ namespace pugi
if (!_root) return xml_node();
for (xml_node_struct* i = _root->first_child; i; i = i->next_sibling)
- if (i->name && impl::strequal(name_, i->name)) return xml_node(i);
+ if (impl::has_name(i) && i->contents && impl::strequal(name_, i->contents)) return xml_node(i);
return xml_node();
}
@@ -4768,7 +5344,7 @@ namespace pugi
if (!_root) return xml_node();
for (xml_node_struct* i = _root->next_sibling; i; i = i->next_sibling)
- if (i->name && impl::strequal(name_, i->name)) return xml_node(i);
+ if (impl::has_name(i) && i->contents && impl::strequal(name_, i->contents)) return xml_node(i);
return xml_node();
}
@@ -4783,7 +5359,7 @@ namespace pugi
if (!_root) return xml_node();
for (xml_node_struct* i = _root->prev_sibling_c; i->next_sibling; i = i->prev_sibling_c)
- if (i->name && impl::strequal(name_, i->name)) return xml_node(i);
+ if (impl::has_name(i) && i->contents && impl::strequal(name_, i->contents)) return xml_node(i);
return xml_node();
}
@@ -4816,8 +5392,8 @@ namespace pugi
if (!_root) return PUGIXML_TEXT("");
for (xml_node_struct* i = _root->first_child; i; i = i->next_sibling)
- if (i->value && impl::is_text_node(i))
- return i->value;
+ if (impl::has_value(i) && i->contents && impl::is_text_node(i))
+ return i->contents;
return PUGIXML_TEXT("");
}
@@ -4854,7 +5430,7 @@ namespace pugi
case node_pi:
case node_declaration:
case node_element:
- return impl::strcpy_insitu(_root->name, _root->header, impl::xml_memory_page_name_allocated_mask, rhs);
+ return impl::strcpy_insitu(_root->contents, _root->header, impl::xml_memory_page_contents_allocated_mask, rhs);
default:
return false;
@@ -4866,11 +5442,17 @@ namespace pugi
switch (type())
{
case node_pi:
+ {
+ xml_node_pi_struct* pn = static_cast<xml_node_pi_struct*>(_root);
+
+ return impl::strcpy_insitu(pn->pi_value, pn->pi_header, impl::xml_memory_page_contents_allocated_mask, rhs);
+ }
+
case node_cdata:
case node_pcdata:
case node_comment:
case node_doctype:
- return impl::strcpy_insitu(_root->value, _root->header, impl::xml_memory_page_value_allocated_mask, rhs);
+ return impl::strcpy_insitu(_root->contents, _root->header, impl::xml_memory_page_contents_allocated_mask, rhs);
default:
return false;
@@ -4881,7 +5463,10 @@ namespace pugi
{
if (!impl::allow_insert_attribute(type())) return xml_attribute();
- xml_attribute a(impl::allocate_attribute(impl::get_allocator(_root)));
+ impl::xml_allocator& alloc = impl::get_allocator(_root);
+ if (!alloc.reserve()) return xml_attribute();
+
+ xml_attribute a(impl::allocate_attribute(alloc));
if (!a) return xml_attribute();
impl::append_attribute(a._attr, _root);
@@ -4895,7 +5480,10 @@ namespace pugi
{
if (!impl::allow_insert_attribute(type())) return xml_attribute();
- xml_attribute a(impl::allocate_attribute(impl::get_allocator(_root)));
+ impl::xml_allocator& alloc = impl::get_allocator(_root);
+ if (!alloc.reserve()) return xml_attribute();
+
+ xml_attribute a(impl::allocate_attribute(alloc));
if (!a) return xml_attribute();
impl::prepend_attribute(a._attr, _root);
@@ -4910,7 +5498,10 @@ namespace pugi
if (!impl::allow_insert_attribute(type())) return xml_attribute();
if (!attr || !impl::is_attribute_of(attr._attr, _root)) return xml_attribute();
- xml_attribute a(impl::allocate_attribute(impl::get_allocator(_root)));
+ impl::xml_allocator& alloc = impl::get_allocator(_root);
+ if (!alloc.reserve()) return xml_attribute();
+
+ xml_attribute a(impl::allocate_attribute(alloc));
if (!a) return xml_attribute();
impl::insert_attribute_after(a._attr, attr._attr, _root);
@@ -4925,7 +5516,10 @@ namespace pugi
if (!impl::allow_insert_attribute(type())) return xml_attribute();
if (!attr || !impl::is_attribute_of(attr._attr, _root)) return xml_attribute();
- xml_attribute a(impl::allocate_attribute(impl::get_allocator(_root)));
+ impl::xml_allocator& alloc = impl::get_allocator(_root);
+ if (!alloc.reserve()) return xml_attribute();
+
+ xml_attribute a(impl::allocate_attribute(alloc));
if (!a) return xml_attribute();
impl::insert_attribute_before(a._attr, attr._attr, _root);
@@ -4979,7 +5573,10 @@ namespace pugi
{
if (!impl::allow_insert_child(type(), type_)) return xml_node();
- xml_node n(impl::allocate_node(impl::get_allocator(_root), type_));
+ impl::xml_allocator& alloc = impl::get_allocator(_root);
+ if (!alloc.reserve()) return xml_node();
+
+ xml_node n(impl::allocate_node(alloc, type_));
if (!n) return xml_node();
impl::append_node(n._root, _root);
@@ -4992,8 +5589,11 @@ namespace pugi
PUGI__FN xml_node xml_node::prepend_child(xml_node_type type_)
{
if (!impl::allow_insert_child(type(), type_)) return xml_node();
+
+ impl::xml_allocator& alloc = impl::get_allocator(_root);
+ if (!alloc.reserve()) return xml_node();
- xml_node n(impl::allocate_node(impl::get_allocator(_root), type_));
+ xml_node n(impl::allocate_node(alloc, type_));
if (!n) return xml_node();
impl::prepend_node(n._root, _root);
@@ -5007,8 +5607,11 @@ namespace pugi
{
if (!impl::allow_insert_child(type(), type_)) return xml_node();
if (!node._root || node._root->parent != _root) return xml_node();
+
+ impl::xml_allocator& alloc = impl::get_allocator(_root);
+ if (!alloc.reserve()) return xml_node();
- xml_node n(impl::allocate_node(impl::get_allocator(_root), type_));
+ xml_node n(impl::allocate_node(alloc, type_));
if (!n) return xml_node();
impl::insert_node_before(n._root, node._root);
@@ -5022,8 +5625,11 @@ namespace pugi
{
if (!impl::allow_insert_child(type(), type_)) return xml_node();
if (!node._root || node._root->parent != _root) return xml_node();
+
+ impl::xml_allocator& alloc = impl::get_allocator(_root);
+ if (!alloc.reserve()) return xml_node();
- xml_node n(impl::allocate_node(impl::get_allocator(_root), type_));
+ xml_node n(impl::allocate_node(alloc, type_));
if (!n) return xml_node();
impl::insert_node_after(n._root, node._root);
@@ -5074,7 +5680,10 @@ namespace pugi
xml_node_type type_ = proto.type();
if (!impl::allow_insert_child(type(), type_)) return xml_node();
- xml_node n(impl::allocate_node(impl::get_allocator(_root), type_));
+ impl::xml_allocator& alloc = impl::get_allocator(_root);
+ if (!alloc.reserve()) return xml_node();
+
+ xml_node n(impl::allocate_node(alloc, type_));
if (!n) return xml_node();
impl::append_node(n._root, _root);
@@ -5088,7 +5697,10 @@ namespace pugi
xml_node_type type_ = proto.type();
if (!impl::allow_insert_child(type(), type_)) return xml_node();
- xml_node n(impl::allocate_node(impl::get_allocator(_root), type_));
+ impl::xml_allocator& alloc = impl::get_allocator(_root);
+ if (!alloc.reserve()) return xml_node();
+
+ xml_node n(impl::allocate_node(alloc, type_));
if (!n) return xml_node();
impl::prepend_node(n._root, _root);
@@ -5103,7 +5715,10 @@ namespace pugi
if (!impl::allow_insert_child(type(), type_)) return xml_node();
if (!node._root || node._root->parent != _root) return xml_node();
- xml_node n(impl::allocate_node(impl::get_allocator(_root), type_));
+ impl::xml_allocator& alloc = impl::get_allocator(_root);
+ if (!alloc.reserve()) return xml_node();
+
+ xml_node n(impl::allocate_node(alloc, type_));
if (!n) return xml_node();
impl::insert_node_after(n._root, node._root);
@@ -5118,7 +5733,10 @@ namespace pugi
if (!impl::allow_insert_child(type(), type_)) return xml_node();
if (!node._root || node._root->parent != _root) return xml_node();
- xml_node n(impl::allocate_node(impl::get_allocator(_root), type_));
+ impl::xml_allocator& alloc = impl::get_allocator(_root);
+ if (!alloc.reserve()) return xml_node();
+
+ xml_node n(impl::allocate_node(alloc, type_));
if (!n) return xml_node();
impl::insert_node_before(n._root, node._root);
@@ -5131,6 +5749,9 @@ namespace pugi
{
if (!impl::allow_move(*this, moved)) return xml_node();
+ impl::xml_allocator& alloc = impl::get_allocator(_root);
+ if (!alloc.reserve()) return xml_node();
+
// disable document_buffer_order optimization since moving nodes around changes document order without changing buffer pointers
impl::get_document(_root).header |= impl::xml_memory_page_contents_shared_mask;
@@ -5144,6 +5765,9 @@ namespace pugi
{
if (!impl::allow_move(*this, moved)) return xml_node();
+ impl::xml_allocator& alloc = impl::get_allocator(_root);
+ if (!alloc.reserve()) return xml_node();
+
// disable document_buffer_order optimization since moving nodes around changes document order without changing buffer pointers
impl::get_document(_root).header |= impl::xml_memory_page_contents_shared_mask;
@@ -5159,6 +5783,9 @@ namespace pugi
if (!node._root || node._root->parent != _root) return xml_node();
if (moved._root == node._root) return xml_node();
+ impl::xml_allocator& alloc = impl::get_allocator(_root);
+ if (!alloc.reserve()) return xml_node();
+
// disable document_buffer_order optimization since moving nodes around changes document order without changing buffer pointers
impl::get_document(_root).header |= impl::xml_memory_page_contents_shared_mask;
@@ -5174,6 +5801,9 @@ namespace pugi
if (!node._root || node._root->parent != _root) return xml_node();
if (moved._root == node._root) return xml_node();
+ impl::xml_allocator& alloc = impl::get_allocator(_root);
+ if (!alloc.reserve()) return xml_node();
+
// disable document_buffer_order optimization since moving nodes around changes document order without changing buffer pointers
impl::get_document(_root).header |= impl::xml_memory_page_contents_shared_mask;
@@ -5193,8 +5823,11 @@ namespace pugi
if (!_root || !a._attr) return false;
if (!impl::is_attribute_of(a._attr, _root)) return false;
+ impl::xml_allocator& alloc = impl::get_allocator(_root);
+ if (!alloc.reserve()) return false;
+
impl::remove_attribute(a._attr, _root);
- impl::destroy_attribute(a._attr, impl::get_allocator(_root));
+ impl::destroy_attribute(a._attr, alloc);
return true;
}
@@ -5208,8 +5841,11 @@ namespace pugi
{
if (!_root || !n._root || n._root->parent != _root) return false;
+ impl::xml_allocator& alloc = impl::get_allocator(_root);
+ if (!alloc.reserve()) return false;
+
impl::remove_node(n._root);
- impl::destroy_node(n._root, impl::get_allocator(_root));
+ impl::destroy_node(n._root, alloc);
return true;
}
@@ -5233,15 +5869,15 @@ namespace pugi
if (!extra) return impl::make_parse_result(status_out_of_memory);
// save name; name of the root has to be NULL before parsing - otherwise closing node mismatches will not be detected at the top level
- char_t* rootname = _root->name;
- _root->name = 0;
+ char_t* rootname = _root->contents;
+ _root->contents = 0;
// parse
char_t* buffer = 0;
xml_parse_result res = impl::load_buffer_impl(doc, _root, const_cast<void*>(contents), size, options, encoding, false, false, &buffer);
// restore name
- _root->name = rootname;
+ _root->contents = rootname;
// add extra buffer to the list
extra->buffer = buffer;
@@ -5256,10 +5892,10 @@ namespace pugi
if (!_root) return xml_node();
for (xml_node_struct* i = _root->first_child; i; i = i->next_sibling)
- if (i->name && impl::strequal(name_, i->name))
+ if (impl::has_name(i) && i->contents && impl::strequal(name_, i->contents))
{
for (xml_attribute_struct* a = i->first_attribute; a; a = a->next_attribute)
- if (a->name && impl::strequal(attr_name, a->name) && impl::strequal(attr_value, a->value ? a->value : PUGIXML_TEXT("")))
+ if (a->name && impl::strequal(attr_name, a->name) && impl::strequal(attr_value, a->value ? a->value + 0 : PUGIXML_TEXT("")))
return xml_node(i);
}
@@ -5272,7 +5908,7 @@ namespace pugi
for (xml_node_struct* i = _root->first_child; i; i = i->next_sibling)
for (xml_attribute_struct* a = i->first_attribute; a; a = a->next_attribute)
- if (a->name && impl::strequal(attr_name, a->name) && impl::strequal(attr_value, a->value ? a->value : PUGIXML_TEXT("")))
+ if (a->name && impl::strequal(attr_name, a->name) && impl::strequal(attr_value, a->value ? a->value + 0 : PUGIXML_TEXT("")))
return xml_node(i);
return xml_node();
@@ -5334,7 +5970,7 @@ namespace pugi
{
for (xml_node_struct* j = found._root->first_child; j; j = j->next_sibling)
{
- if (j->name && impl::strequalrange(j->name, path_segment, static_cast<size_t>(path_segment_end - path_segment)))
+ if (impl::has_name(j) && j->contents && impl::strequalrange(j->contents, path_segment, static_cast<size_t>(path_segment_end - path_segment)))
{
xml_node subsearch = xml_node(j).first_element_by_path(next_segment, delimiter);
@@ -5438,25 +6074,10 @@ namespace pugi
// we can determine the offset reliably only if there is exactly once parse buffer
if (!doc.buffer || doc.extra_buffers) return -1;
- switch (type())
- {
- case node_document:
- return 0;
-
- case node_element:
- case node_declaration:
- case node_pi:
- return _root->name && (_root->header & impl::xml_memory_page_name_allocated_or_shared_mask) == 0 ? _root->name - doc.buffer : -1;
-
- case node_pcdata:
- case node_cdata:
- case node_comment:
- case node_doctype:
- return _root->value && (_root->header & impl::xml_memory_page_value_allocated_or_shared_mask) == 0 ? _root->value - doc.buffer : -1;
+ // document node always has an offset of 0
+ if (_root == &doc) return 0;
- default:
- return -1;
- }
+ return _root->contents && (_root->header & impl::xml_memory_page_contents_allocated_or_shared_mask) == 0 ? _root->contents - doc.buffer : -1;
}
#ifdef __BORLANDC__
@@ -5521,49 +6142,49 @@ namespace pugi
{
xml_node_struct* d = _data();
- return (d && d->value) ? d->value : PUGIXML_TEXT("");
+ return (d && d->contents) ? d->contents + 0 : PUGIXML_TEXT("");
}
PUGI__FN const char_t* xml_text::as_string(const char_t* def) const
{
xml_node_struct* d = _data();
- return (d && d->value) ? d->value : def;
+ return (d && d->contents) ? d->contents : def;
}
PUGI__FN int xml_text::as_int(int def) const
{
xml_node_struct* d = _data();
- return impl::get_value_int(d ? d->value : 0, def);
+ return impl::get_value_int(d ? d->contents + 0 : 0, def);
}
PUGI__FN unsigned int xml_text::as_uint(unsigned int def) const
{
xml_node_struct* d = _data();
- return impl::get_value_uint(d ? d->value : 0, def);
+ return impl::get_value_uint(d ? d->contents + 0 : 0, def);
}
PUGI__FN double xml_text::as_double(double def) const
{
xml_node_struct* d = _data();
- return impl::get_value_double(d ? d->value : 0, def);
+ return impl::get_value_double(d ? d->contents + 0 : 0, def);
}
PUGI__FN float xml_text::as_float(float def) const
{
xml_node_struct* d = _data();
- return impl::get_value_float(d ? d->value : 0, def);
+ return impl::get_value_float(d ? d->contents + 0 : 0, def);
}
PUGI__FN bool xml_text::as_bool(bool def) const
{
xml_node_struct* d = _data();
- return impl::get_value_bool(d ? d->value : 0, def);
+ return impl::get_value_bool(d ? d->contents + 0 : 0, def);
}
#ifdef PUGIXML_HAS_LONG_LONG
@@ -5571,14 +6192,14 @@ namespace pugi
{
xml_node_struct* d = _data();
- return impl::get_value_llong(d ? d->value : 0, def);
+ return impl::get_value_llong(d ? d->contents + 0 : 0, def);
}
PUGI__FN unsigned long long xml_text::as_ullong(unsigned long long def) const
{
xml_node_struct* d = _data();
- return impl::get_value_ullong(d ? d->value : 0, def);
+ return impl::get_value_ullong(d ? d->contents + 0 : 0, def);
}
#endif
@@ -5586,35 +6207,35 @@ namespace pugi
{
xml_node_struct* dn = _data_new();
- return dn ? impl::strcpy_insitu(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false;
+ return dn ? impl::strcpy_insitu(dn->contents, dn->header, impl::xml_memory_page_contents_allocated_mask, rhs) : false;
}
PUGI__FN bool xml_text::set(int rhs)
{
xml_node_struct* dn = _data_new();
- return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false;
+ return dn ? impl::set_value_convert(dn->contents, dn->header, impl::xml_memory_page_contents_allocated_mask, rhs) : false;
}
PUGI__FN bool xml_text::set(unsigned int rhs)
{
xml_node_struct* dn = _data_new();
- return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false;
+ return dn ? impl::set_value_convert(dn->contents, dn->header, impl::xml_memory_page_contents_allocated_mask, rhs) : false;
}
PUGI__FN bool xml_text::set(double rhs)
{
xml_node_struct* dn = _data_new();
- return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false;
+ return dn ? impl::set_value_convert(dn->contents, dn->header, impl::xml_memory_page_contents_allocated_mask, rhs) : false;
}
PUGI__FN bool xml_text::set(bool rhs)
{
xml_node_struct* dn = _data_new();
- return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false;
+ return dn ? impl::set_value_convert(dn->contents, dn->header, impl::xml_memory_page_contents_allocated_mask, rhs) : false;
}
#ifdef PUGIXML_HAS_LONG_LONG
@@ -5622,14 +6243,14 @@ namespace pugi
{
xml_node_struct* dn = _data_new();
- return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false;
+ return dn ? impl::set_value_convert(dn->contents, dn->header, impl::xml_memory_page_contents_allocated_mask, rhs) : false;
}
PUGI__FN bool xml_text::set(unsigned long long rhs)
{
xml_node_struct* dn = _data_new();
- return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false;
+ return dn ? impl::set_value_convert(dn->contents, dn->header, impl::xml_memory_page_contents_allocated_mask, rhs) : false;
}
#endif
@@ -6008,6 +6629,11 @@ namespace pugi
page = next;
}
+ #ifdef PUGIXML_COMPACT
+ // destroy hash table
+ static_cast<impl::xml_document_struct*>(_root)->hash.clear();
+ #endif
+
_root = 0;
}
@@ -6977,8 +7603,7 @@ PUGI__NS_BEGIN
{
if ((get_document(node).header & xml_memory_page_contents_shared_mask) == 0)
{
- if (node->name && (node->header & impl::xml_memory_page_name_allocated_or_shared_mask) == 0) return node->name;
- if (node->value && (node->header & impl::xml_memory_page_value_allocated_or_shared_mask) == 0) return node->value;
+ if (node->contents && (node->header & impl::xml_memory_page_contents_allocated_or_shared_mask) == 0) return node->contents;
}
return 0;
@@ -8649,7 +9274,7 @@ PUGI__NS_BEGIN
{
assert(a);
- const char_t* name = a->name ? a->name : PUGIXML_TEXT("");
+ const char_t* name = a->name ? a->name + 0 : PUGIXML_TEXT("");
switch (_test)
{
@@ -8694,7 +9319,7 @@ PUGI__NS_BEGIN
switch (_test)
{
case nodetest_name:
- if (type == node_element && n->name && strequal(n->name, _data.nodetest))
+ if (type == node_element && n->contents && strequal(n->contents, _data.nodetest))
{
ns.push_back(xml_node(n), alloc);
return true;
@@ -8730,7 +9355,7 @@ PUGI__NS_BEGIN
break;
case nodetest_pi:
- if (type == node_pi && n->name && strequal(n->name, _data.nodetest))
+ if (type == node_pi && n->contents && strequal(n->contents, _data.nodetest))
{
ns.push_back(xml_node(n), alloc);
return true;
@@ -8746,7 +9371,7 @@ PUGI__NS_BEGIN
break;
case nodetest_all_in_namespace:
- if (type == node_element && n->name && starts_with(n->name, _data.nodetest))
+ if (type == node_element && n->contents && starts_with(n->contents, _data.nodetest))
{
ns.push_back(xml_node(n), alloc);
return true;
diff --git a/tests/test_dom_modify.cpp b/tests/test_dom_modify.cpp
index 7863718..9c9109b 100644
--- a/tests/test_dom_modify.cpp
+++ b/tests/test_dom_modify.cpp
@@ -1310,6 +1310,11 @@ TEST(dom_node_copy_copyless)
// the document is parsed in-place so there should only be 1 page worth of allocations
test_runner::_memory_fail_threshold = 32768 + 128;
+#ifdef PUGIXML_COMPACT
+ // ... and some space for hash table
+ test_runner::_memory_fail_threshold += 2048;
+#endif
+
xml_document doc;
CHECK(doc.load_buffer_inplace(&datacopy[0], datacopy.size() * sizeof(char_t), parse_full));
diff --git a/tests/test_memory.cpp b/tests/test_memory.cpp
index bd80ca1..5edfd65 100644
--- a/tests/test_memory.cpp
+++ b/tests/test_memory.cpp
@@ -1,30 +1,37 @@
#include "common.hpp"
#include "writer_string.hpp"
+#include "allocator.hpp"
#include <string>
namespace
{
- int allocate_count = 0;
- int deallocate_count = 0;
+ int page_allocs = 0;
+ int page_deallocs = 0;
+
+ bool is_page(size_t size)
+ {
+ return size >= 8192;
+ }
void* allocate(size_t size)
{
- ++allocate_count;
- return new char[size];
+ void* ptr = memory_allocate(size);
+ page_allocs += is_page(memory_size(ptr));
+ return ptr;
}
void deallocate(void* ptr)
{
- ++deallocate_count;
- delete[] reinterpret_cast<char*>(ptr);
+ page_deallocs += is_page(memory_size(ptr));
+ memory_deallocate(ptr);
}
}
TEST(memory_custom_memory_management)
{
- allocate_count = deallocate_count = 0;
+ page_allocs = page_deallocs = 0;
// remember old functions
allocation_function old_allocate = get_memory_allocation_function();
@@ -37,30 +44,30 @@ TEST(memory_custom_memory_management)
// parse document
xml_document doc;
- CHECK(allocate_count == 0 && deallocate_count == 0);
+ CHECK(page_allocs == 0 && page_deallocs == 0);
CHECK(doc.load_string(STR("<node />")));
- CHECK(allocate_count == 2 && deallocate_count == 0);
+ CHECK(page_allocs == 1 && page_deallocs == 0);
// modify document (no new page)
CHECK(doc.first_child().set_name(STR("foobars")));
- CHECK(allocate_count == 2 && deallocate_count == 0);
+ CHECK(page_allocs == 1 && page_deallocs == 0);
// modify document (new page)
std::basic_string<pugi::char_t> s(65536, 'x');
CHECK(doc.first_child().set_name(s.c_str()));
- CHECK(allocate_count == 3 && deallocate_count == 0);
+ 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(allocate_count == 4 && deallocate_count == 1);
+ CHECK(page_allocs == 3 && page_deallocs == 1);
}
- CHECK(allocate_count == 4 && deallocate_count == 4);
+ CHECK(page_allocs == 3 && page_deallocs == 3);
// restore old functions
set_memory_management_functions(old_allocate, old_deallocate);
@@ -68,7 +75,7 @@ TEST(memory_custom_memory_management)
TEST(memory_large_allocations)
{
- allocate_count = deallocate_count = 0;
+ page_allocs = page_deallocs = 0;
// remember old functions
allocation_function old_allocate = get_memory_allocation_function();
@@ -80,7 +87,7 @@ TEST(memory_large_allocations)
{
xml_document doc;
- CHECK(allocate_count == 0 && deallocate_count == 0);
+ CHECK(page_allocs == 0 && page_deallocs == 0);
// initial fill
for (size_t i = 0; i < 128; ++i)
@@ -90,7 +97,7 @@ TEST(memory_large_allocations)
CHECK(doc.append_child(node_pcdata).set_value(s.c_str()));
}
- CHECK(allocate_count > 0 && deallocate_count == 0);
+ CHECK(page_allocs > 0 && page_deallocs == 0);
// grow-prune loop
while (doc.first_child())
@@ -116,15 +123,15 @@ TEST(memory_large_allocations)
}
}
- CHECK(allocate_count == deallocate_count + 1); // only one live page left (it waits for new allocations)
+ 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(allocate_count == deallocate_count); // no live pages left
+ CHECK(page_allocs == page_deallocs); // no live pages left
}
- CHECK(allocate_count == deallocate_count); // everything is freed
+ CHECK(page_allocs == page_deallocs); // everything is freed
// restore old functions
set_memory_management_functions(old_allocate, old_deallocate);