integrated attributes into the placement new

This commit is contained in:
Lee Thomason 2012-02-06 18:18:11 -08:00
parent 455c9d4dd0
commit 43f59307cc
3 changed files with 25 additions and 28 deletions

View File

@ -17,6 +17,9 @@ static const char CR = CARRIAGE_RETURN;
static const char SINGLE_QUOTE = '\''; static const char SINGLE_QUOTE = '\'';
static const char DOUBLE_QUOTE = '\"'; static const char DOUBLE_QUOTE = '\"';
#define DELETE_NODE( node ) { MemPool* pool = node->memPool; node->~XMLNode(); pool->Free( node ); }
#define DELETE_ATTRIBUTE( attrib ) { MemPool* pool = attrib->memPool; attrib->~XMLAttribute(); pool->Free( attrib ); }
struct Entity { struct Entity {
const char* pattern; const char* pattern;
int length; int length;
@ -260,12 +263,7 @@ void XMLNode::ClearChildren()
XMLNode* node = firstChild; XMLNode* node = firstChild;
Unlink( node ); Unlink( node );
//delete node; DELETE_NODE( node );
// placement new!
MemPool* pool = node->memPool;
node->~XMLNode();
pool->Free( node );
// fixme: memory never free'd.
} }
firstChild = lastChild = 0; firstChild = lastChild = 0;
} }
@ -347,10 +345,7 @@ char* XMLNode::ParseDeep( char* p )
p = node->ParseDeep( p ); p = node->ParseDeep( p );
// FIXME: is it the correct closing element? // FIXME: is it the correct closing element?
if ( node->IsClosingElement() ) { if ( node->IsClosingElement() ) {
//delete node; DELETE_NODE( node );
MemPool* pool = node->memPool;
node->~XMLNode(); // fixme linked list memory not free
pool->Free( node );
return p; return p;
} }
this->InsertEndChild( node ); this->InsertEndChild( node );
@ -444,7 +439,7 @@ XMLElement::~XMLElement()
XMLAttribute* attribute = rootAttribute; XMLAttribute* attribute = rootAttribute;
while( attribute ) { while( attribute ) {
XMLAttribute* next = attribute->next; XMLAttribute* next = attribute->next;
delete attribute; DELETE_ATTRIBUTE( attribute );
attribute = next; attribute = next;
} }
} }
@ -465,11 +460,12 @@ char* XMLElement::ParseAttributes( char* p, bool* closedElement )
// attribute. // attribute.
if ( XMLBase::IsAlpha( *p ) ) { if ( XMLBase::IsAlpha( *p ) ) {
XMLAttribute* attrib = new XMLAttribute( this ); XMLAttribute* attrib = new (document->attributePool.Alloc() ) XMLAttribute( this );
attrib->memPool = &document->attributePool;
p = attrib->ParseDeep( p ); p = attrib->ParseDeep( p );
if ( !p ) { if ( !p ) {
delete attrib; DELETE_ATTRIBUTE( attrib );
document->SetError( XMLDocument::ERROR_PARSING_ATTRIBUTE, start, p ); document->SetError( XMLDocument::ERROR_PARSING_ATTRIBUTE, start, p );
return 0; return 0;
} }

View File

@ -206,8 +206,8 @@ public:
root = chunk; root = chunk;
} }
void Trace( const char* name ) { void Trace( const char* name ) {
printf( "Mempool %s watermark=%d current=%d size=%d nAlloc=%d blocks=%d\n", printf( "Mempool %s watermark=%d [%dk] current=%d size=%d nAlloc=%d blocks=%d\n",
name, maxAllocs, currentAllocs, SIZE, nAllocs, blockPtrs.Size() ); name, maxAllocs, maxAllocs*SIZE/1024, currentAllocs, SIZE, nAllocs, blockPtrs.Size() );
} }
private: private:
@ -419,6 +419,7 @@ private:
StrPair name; StrPair name;
StrPair value; StrPair value;
XMLAttribute* next; XMLAttribute* next;
MemPool* memPool;
}; };

View File

@ -28,19 +28,19 @@ int main( int argc, const char* argv )
} }
#endif #endif
{ {
static const char* test[] = { //"<element />", static const char* test[] = { "<element />",
//"<element></element>", "<element></element>",
//"<element><subelement/></element>", "<element><subelement/></element>",
//"<element><subelement></subelement></element>", "<element><subelement></subelement></element>",
// "<element><subelement><subsub/></subelement></element>", "<element><subelement><subsub/></subelement></element>",
//"<!--comment beside elements--><element><subelement></subelement></element>", "<!--comment beside elements--><element><subelement></subelement></element>",
//"<!--comment beside elements, this time with spaces--> \n <element> <subelement> \n </subelement> </element>", "<!--comment beside elements, this time with spaces--> \n <element> <subelement> \n </subelement> </element>",
//"<element attrib1='foo' attrib2=\"bar\" ></element>", "<element attrib1='foo' attrib2=\"bar\" ></element>",
//"<element attrib1='foo' attrib2=\"bar\" ><subelement attrib3='yeehaa' /></element>", "<element attrib1='foo' attrib2=\"bar\" ><subelement attrib3='yeehaa' /></element>",
//"<element>Text inside element.</element>", "<element>Text inside element.</element>",
//"<element><b></b></element>", "<element><b></b></element>",
//"<element>Text inside and <b>bolded</b> in the element.</element>", "<element>Text inside and <b>bolded</b> in the element.</element>",
//"<outer><element>Text inside and <b>bolded</b> in the element.</element></outer>", "<outer><element>Text inside and <b>bolded</b> in the element.</element></outer>",
"<element>This &amp; That.</element>", "<element>This &amp; That.</element>",
"<element attrib='This&lt;That' />", "<element attrib='This&lt;That' />",
0 0