Modify C++ memory Allocation for TinyXml2
The XMLRead parse example used the already provided PHP extension XmlReader. For a challenge, this was changed so that the inner workings called PHP libxml2 API functions in the same way that XmlReader used them for current node property values. Total time turned out to be a little slower still. Although the "User" time spent was reduced, more was time spent in System calls, according to the PHP timer function "getrusage".
I gave up on using calls to the libxml2 API, to embed the very simple "tinyxml2.cpp" and "tinyxml2.h" code. This code was modified to manage memory from PHP request memory allocations. Otherwise the extension methods uses unmodified "Original code by Lee Thomason (www.grinninglizard.com)".
The wrapper class that mediates the XML parse functionality, previously using libxml2, or via XmlReader, was easily simplified, as the XMLDocument class from tinyxml2, was easy to directly embed. Its DOM tree properties can be directly traversed. The performance improvement was enough to decide to adopt this version and give this class its own sub-folder, as "tinyxml".
The tinyxml XMLDocument class is directly embedded in the XmlWrap class, and XmlWrap is itself embedded into the zend_object* structure of the Wcc_XmlRead class.
How about dynamically allocated nodes created by in the tinyxml2 code?
The tinyxml2 code doesn't require C++ STL code, and a STL style allocator class isn't going to work.
The PHP request memory calls emalloc and efree. Below is how tinyxml2 allocates and frees it's dynamic node objects, using its base MemPool class, and template MemPoolT. Memory pool techniques same small sized node items, which can be freed in one block.
class MemPool
{
public:
MemPool() {}
virtual ~MemPool() {}
virtual size_t ItemSize() const = 0;
virtual void* Alloc() = 0;
virtual void Free( void* ) = 0;
virtual void SetTracked() = 0;
};
template< size_t ITEM_SIZE >
class MemPoolT : public MemPool
{//... Here be details (dragons)...
}
A MemPoolT<size#>::Item is a privately declared union type, with a single linked list pointer for saving freed blocks for reuse.
// Details of MemPoolT - Before allocated, Item is a fixed size of size# characters.
union Item {
Item* next;
char itemData[static_cast<size_t>(ITEM_SIZE)];
};
// Intended as private,
enum { ITEMS_PER_BLOCK = (4 * 1024) / ITEM_SIZE };
// So a block is just that big
struct Block {
Item items[ITEMS_PER_BLOCK];
};
// Then there is a number of blocks, which allows to dynamic expansion
// So this is where most of the dynamic memory is managed.
// Note that stores an array of pointers to Block, 10 is initial capacity, initial size is zero.
// capacity will double if size overruns capacity.
// DynArray uses new T[], and delete[]
DynArray< Block*, 10 > _blockPtrs;
// linked list of previously freed Item / nodes, which could be from any block
Item* _root;
// and tracking work
size_t _currentAllocs;
size_t _nAllocs;
size_t _maxAllocs;
size_t _nUntracked;
// Each instance of XMLNode - XMLText, XMLComment, XMLDeclaration, XMLUnknown, XMLElement, XMLDocument
// XMLAttribute
class XMLDocument : public XMLNode {// has private memory pools
MemPoolT< sizeof(XMLElement) > _elementPool;
MemPoolT< sizeof(XMLAttribute) > _attributePool;
MemPoolT< sizeof(XMLText) > _textPool;
MemPoolT< sizeof(XMLComment) > _commentPool;
// ....
// includes a private pointer to its own MemPool
class XMLNode {
private:
MemPool* _memPool;
// A string from _start to _end, NEEDS_FLUSH or NEEDS_DELETE
// If needs delete[], was allocated with new[len+1] eg: by SetStr
class StrPair {
private:
int _flags;
char* _start;
char* _end;
}
// so a number of classes need their own operators for new[] and delete[].
// MemPoolT<#>::Block need new and deallocate.
### Replacement of operators new, new\[\], delete, delete\[\] doesn't work in single C++ binary.
Just declaring versions of these so called replacement operators to use emalloc and efree, will not work with them in a single binary. I read misleading descriptions, that it would be done for a "single translation unit", but this failed at every attempt. Maybe this could be done by using a linker to another "translation unit" where replacement versions are defined, as this "replacement" is during a "linker" process.
In this instance it is easier to change the declaration of "struct Block", used in the template MemPoolT<ITEM_SIZE> to be an instance of PHPAlloc, an empty struct which just its own operator new and delete methods, which are inherited.
```C++
class PHPAlloc {
public:
void* operator new(size_t size)
{
//zend_printf("PHPAlloc new\n");
return emalloc(size);
}
void operator delete(void* ptr)
{
//zend_printf("PHPAlloc delete\n");
efree(ptr);
}
};
// So easy to do.
struct Block : public PHPAlloc {
Item items[ITEMS_PER_BLOCK];
};
Several direct calls to new T[], were changed into emalloc calls, and delete changed to efree. All the other memory allocations go through the MemPoolT<> allocators.
Switching to PHP request memory allocation is a significant part of C++ code importation.