Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
xml_allocator Struct Reference
Inheritance diagram for xml_allocator:

Public Member Functions

 xml_allocator (xml_memory_page *root)
xml_memory_pageallocate_page (size_t data_size)
void * allocate_memory_oob (size_t size, xml_memory_page *&out_page)
void * allocate_memory (size_t size, xml_memory_page *&out_page)
void * allocate_object (size_t size, xml_memory_page *&out_page)
void deallocate_memory (void *ptr, size_t size, xml_memory_page *page)
char_t * allocate_string (size_t length)
void deallocate_string (char_t *string)
bool reserve ()

Static Public Member Functions

static void deallocate_page (xml_memory_page *page)

Public Attributes

xml_memory_page_root
size_t _busy_size

Detailed Description

Definition at line 510 of file pugixml.cc.

Constructor & Destructor Documentation

◆ xml_allocator()

xml_allocator::xml_allocator ( xml_memory_page * root)
inline

Definition at line 512 of file pugixml.cc.

512 : _root(root), _busy_size(root->busy_size)
513 {
514 #ifdef PUGIXML_COMPACT
515 _hash = 0;
516 #endif
517 }
xml_memory_page * _root
Definition pugixml.cc:702
size_t _busy_size
Definition pugixml.cc:703
size_t busy_size
Definition pugixml.cc:486

Referenced by xml_document_struct::xml_document_struct().

Member Function Documentation

◆ allocate_memory()

void * xml_allocator::allocate_memory ( size_t size,
xml_memory_page *& out_page )
inline

Definition at line 544 of file pugixml.cc.

545 {
546 if (PUGI__UNLIKELY(_busy_size + size > xml_memory_page_size))
547 return allocate_memory_oob(size, out_page);
548
549 void* buf = reinterpret_cast<char*>(_root) + sizeof(xml_memory_page) + _busy_size;
550
551 _busy_size += size;
552
553 out_page = _root;
554
555 return buf;
556 }
#define PUGI__UNLIKELY(cond)
Definition pugixml.cc:96
void * allocate_memory_oob(size_t size, xml_memory_page *&out_page)
Definition pugixml.cc:710

Referenced by allocate_object(), and allocate_string().

◆ allocate_memory_oob()

PUGI__FN_NO_INLINE void * xml_allocator::allocate_memory_oob ( size_t size,
xml_memory_page *& out_page )

Definition at line 710 of file pugixml.cc.

711 {
712 const size_t large_allocation_threshold = xml_memory_page_size / 4;
713
714 xml_memory_page* page = allocate_page(size <= large_allocation_threshold ? xml_memory_page_size : size);
715 out_page = page;
716
717 if (!page) return 0;
718
719 if (size <= large_allocation_threshold)
720 {
721 _root->busy_size = _busy_size;
722
723 // insert page at the end of linked list
724 page->prev = _root;
725 _root->next = page;
726 _root = page;
727
728 _busy_size = size;
729 }
730 else
731 {
732 // insert page before the end of linked list, so that it is deleted as soon as possible
733 // the last page is not deleted even if it's empty (see deallocate_memory)
734 assert(_root->prev);
735
736 page->prev = _root->prev;
737 page->next = _root;
738
739 _root->prev->next = page;
740 _root->prev = page;
741
742 page->busy_size = size;
743 }
744
745 return reinterpret_cast<char*>(page) + sizeof(xml_memory_page);
746 }
xml_memory_page * allocate_page(size_t data_size)
Definition pugixml.cc:519
xml_memory_page * prev
Definition pugixml.cc:483
xml_memory_page * next
Definition pugixml.cc:484

Referenced by allocate_memory().

◆ allocate_object()

void * xml_allocator::allocate_object ( size_t size,
xml_memory_page *& out_page )
inline

Definition at line 590 of file pugixml.cc.

591 {
592 return allocate_memory(size, out_page);
593 }
void * allocate_memory(size_t size, xml_memory_page *&out_page)
Definition pugixml.cc:544

Referenced by allocate_attribute(), and allocate_node().

◆ allocate_page()

xml_memory_page * xml_allocator::allocate_page ( size_t data_size)
inline

Definition at line 519 of file pugixml.cc.

520 {
521 size_t size = sizeof(xml_memory_page) + data_size;
522
523 // allocate block with some alignment, leaving memory for worst-case padding
524 void* memory = xml_memory::allocate(size);
525 if (!memory) return 0;
526
527 // prepare page structure
528 xml_memory_page* page = xml_memory_page::construct(memory);
529 assert(page);
530
531 assert(this == _root->allocator);
532 page->allocator = this;
533
534 return page;
535 }
static allocation_function allocate
Definition pugixml.cc:199
static xml_memory_page * construct(void *memory)
Definition pugixml.cc:462
xml_allocator * allocator
Definition pugixml.cc:481

Referenced by allocate_memory_oob().

◆ allocate_string()

char_t * xml_allocator::allocate_string ( size_t length)
inline

Definition at line 640 of file pugixml.cc.

641 {
642 static const size_t max_encoded_offset = (1 << 16) * xml_memory_block_alignment;
643
644 PUGI__STATIC_ASSERT(xml_memory_page_size <= max_encoded_offset);
645
646 // allocate memory for string and header block
647 size_t size = sizeof(xml_memory_string_header) + length * sizeof(char_t);
648
649 // round size up to block alignment boundary
650 size_t full_size = (size + (xml_memory_block_alignment - 1)) & ~(xml_memory_block_alignment - 1);
651
652 xml_memory_page* page;
653 xml_memory_string_header* header = static_cast<xml_memory_string_header*>(allocate_memory(full_size, page));
654
655 if (!header) return 0;
656
657 // setup header
658 ptrdiff_t page_offset = reinterpret_cast<char*>(header) - reinterpret_cast<char*>(page) - sizeof(xml_memory_page);
659
660 assert(page_offset % xml_memory_block_alignment == 0);
661 assert(page_offset >= 0 && static_cast<size_t>(page_offset) < max_encoded_offset);
662 header->page_offset = static_cast<uint16_t>(static_cast<size_t>(page_offset) / xml_memory_block_alignment);
663
664 // full_size == 0 for large strings that occupy the whole page
665 assert(full_size % xml_memory_block_alignment == 0);
666 assert(full_size < max_encoded_offset || (page->busy_size == full_size && page_offset == 0));
667 header->full_size = static_cast<uint16_t>(full_size < max_encoded_offset ? full_size / xml_memory_block_alignment : 0);
668
669 // round-trip through void* to avoid 'cast increases required alignment of target type' warning
670 // header is guaranteed a pointer-sized alignment, which should be enough for char_t
671 return static_cast<char_t*>(static_cast<void*>(header + 1));
672 }
PUGIXML_CHAR char_t
Definition pugixml.hpp:137
#define PUGI__STATIC_ASSERT(cond)
Definition pugixml.cc:100

Referenced by strcpy_insitu().

◆ deallocate_memory()

void xml_allocator::deallocate_memory ( void * ptr,
size_t size,
xml_memory_page * page )
inline

Definition at line 596 of file pugixml.cc.

597 {
598 if (page == _root) page->busy_size = _busy_size;
599
600 assert(ptr >= reinterpret_cast<char*>(page) + sizeof(xml_memory_page) && ptr < reinterpret_cast<char*>(page) + sizeof(xml_memory_page) + page->busy_size);
601 (void)!ptr;
602
603 page->freed_size += size;
604 assert(page->freed_size <= page->busy_size);
605
606 if (page->freed_size == page->busy_size)
607 {
608 if (page->next == 0)
609 {
610 assert(_root == page);
611
612 // top page freed, just reset sizes
613 page->busy_size = 0;
614 page->freed_size = 0;
615
616 #ifdef PUGIXML_COMPACT
617 // reset compact state to maximize efficiency
618 page->compact_string_base = 0;
619 page->compact_shared_parent = 0;
620 page->compact_page_marker = 0;
621 #endif
622
623 _busy_size = 0;
624 }
625 else
626 {
627 assert(_root != page);
628 assert(page->prev);
629
630 // remove from the list
631 page->prev->next = page->next;
632 page->next->prev = page->prev;
633
634 // deallocate
635 deallocate_page(page);
636 }
637 }
638 }
static void deallocate_page(xml_memory_page *page)
Definition pugixml.cc:537
size_t freed_size
Definition pugixml.cc:487

Referenced by deallocate_string(), destroy_attribute(), and destroy_node().

◆ deallocate_page()

void xml_allocator::deallocate_page ( xml_memory_page * page)
inlinestatic

Definition at line 537 of file pugixml.cc.

538 {
540 }
static deallocation_function deallocate
Definition pugixml.cc:200

Referenced by deallocate_memory().

◆ deallocate_string()

void xml_allocator::deallocate_string ( char_t * string)
inline

Definition at line 674 of file pugixml.cc.

675 {
676 // this function casts pointers through void* to avoid 'cast increases required alignment of target type' warnings
677 // we're guaranteed the proper (pointer-sized) alignment on the input string if it was allocated via allocate_string
678
679 // get header
680 xml_memory_string_header* header = static_cast<xml_memory_string_header*>(static_cast<void*>(string)) - 1;
681 assert(header);
682
683 // deallocate
684 size_t page_offset = sizeof(xml_memory_page) + header->page_offset * xml_memory_block_alignment;
685 xml_memory_page* page = reinterpret_cast<xml_memory_page*>(static_cast<void*>(reinterpret_cast<char*>(header) - page_offset));
686
687 // if full_size == 0 then this string occupies the whole page
688 size_t full_size = header->full_size == 0 ? page->busy_size : header->full_size * xml_memory_block_alignment;
689
690 deallocate_memory(header, full_size, page);
691 }
void deallocate_memory(void *ptr, size_t size, xml_memory_page *page)
Definition pugixml.cc:596

Referenced by destroy_attribute(), destroy_node(), and strcpy_insitu().

◆ reserve()

bool xml_allocator::reserve ( )
inline

Definition at line 693 of file pugixml.cc.

694 {
695 #ifdef PUGIXML_COMPACT
696 return _hash->reserve();
697 #else
698 return true;
699 #endif
700 }

Referenced by append_new_attribute(), append_new_node(), and strcpy_insitu().

Member Data Documentation

◆ _busy_size

size_t xml_allocator::_busy_size

Definition at line 703 of file pugixml.cc.

Referenced by allocate_memory(), allocate_memory_oob(), deallocate_memory(), and xml_allocator().

◆ _root

xml_memory_page* xml_allocator::_root

The documentation for this struct was generated from the following file: