You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by an...@locus.apache.org on 2000/06/08 00:49:44 UTC
cvs commit: xml-xerces/c/src/dom DOM_NamedNodeMap.cpp DOM_NamedNodeMap.hpp DOM_Node.cpp ElementImpl.cpp ElementImpl.hpp
andyh 00/06/07 15:49:43
Modified: c/src/dom DOM_NamedNodeMap.cpp DOM_NamedNodeMap.hpp
DOM_Node.cpp ElementImpl.cpp ElementImpl.hpp
Log:
Memory usage reduction: DOM NamedNodeMaps for attributes are allocated
only for elements that actually have attributes. By Joe Polastre.
Revision Changes Path
1.8 +50 -16 xml-xerces/c/src/dom/DOM_NamedNodeMap.cpp
Index: DOM_NamedNodeMap.cpp
===================================================================
RCS file: /home/cvs/xml-xerces/c/src/dom/DOM_NamedNodeMap.cpp,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- DOM_NamedNodeMap.cpp 2000/03/11 03:19:12 1.7
+++ DOM_NamedNodeMap.cpp 2000/06/07 22:49:40 1.8
@@ -56,6 +56,10 @@
/*
* $Log: DOM_NamedNodeMap.cpp,v $
+ * Revision 1.8 2000/06/07 22:49:40 andyh
+ * Memory usage reduction: DOM NamedNodeMaps for attributes are allocated
+ * only for elements that actually have attributes. By Joe Polastre.
+ *
* Revision 1.7 2000/03/11 03:19:12 chchou
* Fix bug # 19, add const keyword to API
*
@@ -87,32 +91,49 @@
#include "DOM_Node.hpp"
#include "DOM_NamedNodeMap.hpp"
#include "NamedNodeMapImpl.hpp"
+#include "ElementImpl.hpp"
+
+const unsigned short DOM_NamedNodeMap::NNM_ELEMENT = 0;
+const unsigned short DOM_NamedNodeMap::NNM_OTHER = 1;
+
DOM_NamedNodeMap::DOM_NamedNodeMap()
{
- fImpl = null;
+ fImpl = 0;
+ flagElem = NNM_OTHER;
};
DOM_NamedNodeMap::DOM_NamedNodeMap(const DOM_NamedNodeMap & other)
{
this->fImpl = other.fImpl;
- NamedNodeMapImpl::addRef(fImpl);
+ this->flagElem = other.flagElem;
+ (other.flagElem == NNM_ELEMENT) ? NodeImpl::addRef((NodeImpl *)fImpl) :
+ NamedNodeMapImpl::addRef((NamedNodeMapImpl *)fImpl);
};
DOM_NamedNodeMap::DOM_NamedNodeMap(NamedNodeMapImpl *impl)
{
- fImpl = impl;
- NamedNodeMapImpl::addRef(fImpl);
+ fImpl = impl;
+ flagElem = NNM_OTHER;
+ if (impl != null)
+ NamedNodeMapImpl::addRef((NamedNodeMapImpl *)fImpl);
};
+DOM_NamedNodeMap::DOM_NamedNodeMap(NodeImpl *impl)
+{
+ fImpl = impl;
+ flagElem = NNM_ELEMENT;
+ NodeImpl::addRef((NodeImpl *)fImpl);
+}
DOM_NamedNodeMap::~DOM_NamedNodeMap()
{
- NamedNodeMapImpl::removeRef(fImpl);
+ (flagElem == NNM_OTHER) ? NamedNodeMapImpl::removeRef((NamedNodeMapImpl *)fImpl) :
+ NodeImpl::removeRef((NodeImpl *)fImpl);
};
bool DOM_NamedNodeMap::operator == (const DOM_NamedNodeMap &other) const
@@ -143,9 +164,13 @@
{
if (this->fImpl != other.fImpl)
{
- NamedNodeMapImpl::removeRef(fImpl);
+ // update reference counts and change pointers
+ (flagElem == NNM_OTHER) ? NamedNodeMapImpl::removeRef((NamedNodeMapImpl *)fImpl) : NodeImpl::removeRef((NodeImpl *)fImpl);
+
this->fImpl = other.fImpl;
- NamedNodeMapImpl::addRef(fImpl);
+ this->flagElem = other.flagElem;
+
+ (flagElem == NNM_OTHER) ? NamedNodeMapImpl::addRef((NamedNodeMapImpl *)fImpl) : NodeImpl::addRef((NodeImpl *)fImpl);
}
return *this;
};
@@ -153,39 +178,46 @@
DOM_NamedNodeMap & DOM_NamedNodeMap::operator = (const DOM_NullPtr *other)
{
- NamedNodeMapImpl::removeRef(fImpl);
+
+ (flagElem == NNM_OTHER) ? NamedNodeMapImpl::removeRef((NamedNodeMapImpl *)fImpl) : NodeImpl::removeRef((NodeImpl *)fImpl);
this->fImpl = 0;
+ this->flagElem = NNM_OTHER;
return *this;
};
DOM_Node DOM_NamedNodeMap::getNamedItem(const DOMString &name) const
{
- return DOM_Node(fImpl->getNamedItem(name));
+ return (flagElem == NNM_OTHER) ? DOM_Node(((NamedNodeMapImpl *)fImpl)->getNamedItem(name)) :
+ DOM_Node(((ElementImpl *)fImpl)->NNM_getNamedItem(name));
};
DOM_Node DOM_NamedNodeMap::setNamedItem(DOM_Node arg)
{
- return DOM_Node(fImpl->setNamedItem(arg.fImpl));
+ return (flagElem == NNM_OTHER) ? DOM_Node(((NamedNodeMapImpl *)fImpl)->setNamedItem(arg.fImpl)) :
+ DOM_Node(((ElementImpl *)fImpl)->NNM_setNamedItem(arg.fImpl));
};
DOM_Node DOM_NamedNodeMap::removeNamedItem(const DOMString &name)
{
- return DOM_Node(fImpl->removeNamedItem(name));
+ return (flagElem == NNM_OTHER) ? DOM_Node(((NamedNodeMapImpl *)fImpl)->removeNamedItem(name)) :
+ DOM_Node(((ElementImpl *)fImpl)->NNM_removeNamedItem(name));
};
DOM_Node DOM_NamedNodeMap::item(unsigned int index) const
{
- return DOM_Node(fImpl->item(index));
+ return (flagElem == NNM_OTHER) ? DOM_Node(((NamedNodeMapImpl *)fImpl)->item(index)) :
+ DOM_Node(((ElementImpl *)fImpl)->NNM_item(index));
};
unsigned int DOM_NamedNodeMap::getLength() const
{
- return fImpl->getLength();
+ return (flagElem == NNM_OTHER) ? ((NamedNodeMapImpl *)fImpl)->getLength() :
+ ((ElementImpl *)fImpl)->NNM_getLength();
};
@@ -194,16 +226,19 @@
DOM_Node DOM_NamedNodeMap::getNamedItemNS(const DOMString &namespaceURI,
const DOMString &localName)
{
- return DOM_Node(fImpl->getNamedItemNS(namespaceURI, localName));
+ return (flagElem == NNM_OTHER) ? DOM_Node(((NamedNodeMapImpl *)fImpl)->getNamedItemNS(namespaceURI, localName)) :
+ DOM_Node(((ElementImpl *)fImpl)->NNM_getNamedItemNS(namespaceURI, localName));
}
DOM_Node DOM_NamedNodeMap::setNamedItemNS(DOM_Node arg)
{
- return DOM_Node(fImpl->setNamedItemNS(arg.fImpl));
+ return (flagElem == NNM_OTHER) ? DOM_Node(((NamedNodeMapImpl *)fImpl)->setNamedItemNS(arg.fImpl)) :
+ DOM_Node(((ElementImpl *)fImpl)->NNM_setNamedItemNS(arg.fImpl));
}
DOM_Node DOM_NamedNodeMap::removeNamedItemNS(const DOMString &namespaceURI,
const DOMString &localName)
{
- return DOM_Node(fImpl->removeNamedItemNS(namespaceURI, localName));
+ return (flagElem == NNM_OTHER) ? DOM_Node(((NamedNodeMapImpl *)fImpl)->removeNamedItemNS(namespaceURI, localName)) :
+ DOM_Node(((ElementImpl *)fImpl)->NNM_removeNamedItemNS(namespaceURI, localName));
}
1.13 +9 -1 xml-xerces/c/src/dom/DOM_NamedNodeMap.hpp
Index: DOM_NamedNodeMap.hpp
===================================================================
RCS file: /home/cvs/xml-xerces/c/src/dom/DOM_NamedNodeMap.hpp,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- DOM_NamedNodeMap.hpp 2000/03/11 03:19:13 1.12
+++ DOM_NamedNodeMap.hpp 2000/06/07 22:49:40 1.13
@@ -56,6 +56,10 @@
/*
* $Log: DOM_NamedNodeMap.hpp,v $
+ * Revision 1.13 2000/06/07 22:49:40 andyh
+ * Memory usage reduction: DOM NamedNodeMaps for attributes are allocated
+ * only for elements that actually have attributes. By Joe Polastre.
+ *
* Revision 1.12 2000/03/11 03:19:13 chchou
* Fix bug # 19, add const keyword to API
*
@@ -124,7 +128,11 @@
*/
class CDOM_EXPORT DOM_NamedNodeMap {
private:
- NamedNodeMapImpl *fImpl;
+ void *fImpl;
+ short flagElem;
+
+ static const unsigned short NNM_ELEMENT;
+ static const unsigned short NNM_OTHER;
public:
/** @name Constructors and assignment operator */
@@ -384,6 +392,7 @@
protected:
DOM_NamedNodeMap(NamedNodeMapImpl *impl);
+ DOM_NamedNodeMap(NodeImpl *impl);
friend class DOM_DocumentType;
friend class DOM_Node;
1.6 +6 -1 xml-xerces/c/src/dom/DOM_Node.cpp
Index: DOM_Node.cpp
===================================================================
RCS file: /home/cvs/xml-xerces/c/src/dom/DOM_Node.cpp,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- DOM_Node.cpp 2000/03/02 19:53:56 1.5
+++ DOM_Node.cpp 2000/06/07 22:49:40 1.6
@@ -56,6 +56,10 @@
/*
* $Log: DOM_Node.cpp,v $
+ * Revision 1.6 2000/06/07 22:49:40 andyh
+ * Memory usage reduction: DOM NamedNodeMaps for attributes are allocated
+ * only for elements that actually have attributes. By Joe Polastre.
+ *
* Revision 1.5 2000/03/02 19:53:56 roddey
* This checkin includes many changes done while waiting for the
* 1.1.0 code to be finished. I can't list them all here, but a list is
@@ -239,7 +243,7 @@
DOM_NamedNodeMap DOM_Node::getAttributes() const
{
- return DOM_NamedNodeMap(fImpl->getAttributes());
+ return (fImpl->getAttributes() == null) ? DOM_NamedNodeMap(fImpl) : DOM_NamedNodeMap(fImpl->getAttributes());
};
1.22 +102 -7 xml-xerces/c/src/dom/ElementImpl.cpp
Index: ElementImpl.cpp
===================================================================
RCS file: /home/cvs/xml-xerces/c/src/dom/ElementImpl.cpp,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -r1.21 -r1.22
--- ElementImpl.cpp 2000/04/27 02:52:43 1.21
+++ ElementImpl.cpp 2000/06/07 22:49:41 1.22
@@ -55,7 +55,7 @@
*/
/*
- * $Id: ElementImpl.cpp,v 1.21 2000/04/27 02:52:43 lehors Exp $
+ * $Id: ElementImpl.cpp,v 1.22 2000/06/07 22:49:41 andyh Exp $
*/
#include "DeepNodeListImpl.hpp"
@@ -73,7 +73,7 @@
: ChildAndParentNode(ownerDoc)
{
name = eName.clone();
- attributes = new NamedNodeMapImpl(this);
+ attributes = null;
};
@@ -81,9 +81,11 @@
: ChildAndParentNode(other)
{
name = other.name.clone();
+ attributes = null;
if (deep)
cloneChildren(other);
- attributes = other.attributes->cloneMap(this);
+ if (other.attributes != null)
+ attributes = other.attributes->cloneMap(this);
};
@@ -109,7 +111,8 @@
*/
void ElementImpl::setOwnerDocument(DocumentImpl *doc) {
ChildAndParentNode::setOwnerDocument(doc);
- attributes->setOwnerDocument(doc);
+ if (attributes != null)
+ attributes->setOwnerDocument(doc);
}
@@ -134,7 +137,7 @@
AttrImpl *ElementImpl::getAttributeNode(const DOMString &nam)
{
- return (AttrImpl *)(attributes->getNamedItem(nam));
+ return (attributes == null) ? null : (AttrImpl *)(attributes->getNamedItem(nam));
};
@@ -212,6 +215,8 @@
AttrImpl* newAttr = (AttrImpl*)getAttributeNode(nam);
if (!newAttr)
{
+ if (attributes == null)
+ attributes = new NamedNodeMapImpl(this);
newAttr = (AttrImpl*)ownerDocument->createAttribute(nam);
attributes->setNamedItem(newAttr);
}
@@ -232,6 +237,8 @@
if (!(newAttr->isAttrImpl()))
throw DOM_DOMException(DOM_DOMException::WRONG_DOCUMENT_ERR, null);
+ if (attributes == null)
+ attributes = new NamedNodeMapImpl(this);
AttrImpl *oldAttr =
(AttrImpl *) attributes->getNamedItem(newAttr->getName());
// This will throw INUSE if necessary
@@ -285,6 +292,8 @@
(AttrImpl *) ownerDocument->createAttributeNS(fNamespaceURI,
qualifiedName);
newAttr->setNodeValue(fValue);
+ if (attributes == null)
+ attributes = new NamedNodeMapImpl(this);
AttrImpl *oldAttr = (AttrImpl *)attributes->setNamedItem(newAttr);
if (oldAttr) {
@@ -328,8 +337,9 @@
if (newAttr -> getOwnerDocument() != this -> getOwnerDocument())
throw DOM_DOMException(DOM_DOMException::WRONG_DOCUMENT_ERR, null);
- AttrImpl *oldAttr = (AttrImpl *) attributes->getNamedItemNS(
- newAttr->getNamespaceURI(), newAttr->getLocalName());
+ if (attributes == null)
+ attributes = new NamedNodeMapImpl(this);
+ AttrImpl *oldAttr = (AttrImpl *) attributes->getNamedItemNS(newAttr->getNamespaceURI(), newAttr->getLocalName());
// This will throw INUSE if necessary
attributes->setNamedItemNS(newAttr);
@@ -353,3 +363,88 @@
return new DeepNodeListImpl(this,fNamespaceURI, fLocalName);
}
+// DOM_NamedNodeMap UTILITIES
+NamedNodeMapImpl *ElementImpl::NNM_cloneMap(NodeImpl *ownerNode)
+{
+ return (getAttributes() == null) ? null : ownerNode->getAttributes()->cloneMap(ownerNode);
+}
+
+int ElementImpl::NNM_findNamePoint(const DOMString &name)
+{
+ return (getAttributes() == null) ? -1 : getAttributes()->findNamePoint(name);
+}
+
+unsigned int ElementImpl::NNM_getLength()
+{
+ return (getAttributes() == null) ? 0 : getAttributes()->getLength();
+}
+
+NodeImpl *ElementImpl::NNM_getNamedItem(const DOMString &name)
+{
+ return (getAttributes() == null) ? null : getAttributes()->getNamedItem(name);
+}
+
+NodeImpl *ElementImpl::NNM_item(unsigned int index)
+{
+ return (getAttributes() == null) ? null : getAttributes()->item(index);
+}
+
+void ElementImpl::NNM_removeAll()
+{
+ if (getAttributes() != null)
+ getAttributes()->removeAll();
+}
+
+NodeImpl *ElementImpl::NNM_removeNamedItem(const DOMString &name)
+{
+ if (getAttributes() == null)
+ throw DOM_DOMException(DOM_DOMException::NOT_FOUND_ERR, null);
+ else
+ return getAttributes()->removeNamedItem(name);
+ return null;
+}
+
+NodeImpl *ElementImpl::NNM_setNamedItem(NodeImpl *arg)
+{
+ if (getAttributes() == null)
+ attributes = new NamedNodeMapImpl(this);
+ return attributes->setNamedItem(arg);
+}
+
+void ElementImpl::NNM_setReadOnly(bool readOnly, bool deep)
+{
+ if (getAttributes() != null)
+ getAttributes()->setReadOnly(readOnly, deep);
+}
+
+int ElementImpl::NNM_findNamePoint(const DOMString &namespaceURI, const DOMString &localName)
+{
+ return (getAttributes() == null) ? -1 : getAttributes()->findNamePoint(namespaceURI, localName);
+}
+
+NodeImpl *ElementImpl::NNM_getNamedItemNS(const DOMString &namespaceURI, const DOMString &localName)
+{
+ return (getAttributes() == null) ? null : getAttributes()->getNamedItemNS(namespaceURI, localName);
+}
+
+NodeImpl *ElementImpl::NNM_setNamedItemNS(NodeImpl *arg)
+{
+ if (getAttributes() == null)
+ attributes = new NamedNodeMapImpl(this);
+ return getAttributes()->setNamedItemNS(arg);
+}
+
+NodeImpl *ElementImpl::NNM_removeNamedItemNS(const DOMString &namespaceURI, const DOMString &localName)
+{
+ if (getAttributes() == null)
+ throw DOM_DOMException(DOM_DOMException::NOT_FOUND_ERR, null);
+ else
+ return getAttributes()->removeNamedItemNS(namespaceURI, localName);
+ return null;
+}
+
+void ElementImpl::NNM_setOwnerDocument(DocumentImpl *doc)
+{
+ if (getAttributes() != null)
+ getAttributes()->setOwnerDocument(doc);
+}
1.13 +23 -1 xml-xerces/c/src/dom/ElementImpl.hpp
Index: ElementImpl.hpp
===================================================================
RCS file: /home/cvs/xml-xerces/c/src/dom/ElementImpl.hpp,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- ElementImpl.hpp 2000/04/27 02:52:43 1.12
+++ ElementImpl.hpp 2000/06/07 22:49:41 1.13
@@ -58,7 +58,7 @@
*/
/*
- * $Id: ElementImpl.hpp,v 1.12 2000/04/27 02:52:43 lehors Exp $
+ * $Id: ElementImpl.hpp,v 1.13 2000/06/07 22:49:41 andyh Exp $
*/
//
@@ -117,6 +117,28 @@
const DOMString &localName);
virtual void setOwnerDocument(DocumentImpl *doc);
+
+ //Utils for the DOM_NamedNodeMap wrapper class
+ //All NamedNodeMap utils begin with NNM
+ virtual NamedNodeMapImpl *NNM_cloneMap(NodeImpl *ownerNode);
+// static void NNM_addRef(NamedNodeMapImpl *);
+ virtual int NNM_findNamePoint(const DOMString &name);
+ virtual unsigned int NNM_getLength();
+ virtual NodeImpl *NNM_getNamedItem(const DOMString &name);
+ virtual NodeImpl *NNM_item(unsigned int index);
+ virtual void NNM_removeAll();
+ virtual NodeImpl *NNM_removeNamedItem(const DOMString &name);
+// static void NNM_removeRef(NamedNodeMapImpl *);
+ virtual NodeImpl *NNM_setNamedItem(NodeImpl *arg);
+ virtual void NNM_setReadOnly(bool readOnly, bool deep);
+ //Introduced in DOM Level 2
+ virtual int NNM_findNamePoint(const DOMString &namespaceURI, const DOMString &localName);
+ virtual NodeImpl *NNM_getNamedItemNS(const DOMString &namespaceURI, const DOMString &localName);
+ virtual NodeImpl *NNM_setNamedItemNS(NodeImpl *arg);
+ virtual NodeImpl *NNM_removeNamedItemNS(const DOMString &namespaceURI, const DOMString &localName);
+ virtual void NNM_setOwnerDocument(DocumentImpl *doc);
+
+
};
#endif