You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by jb...@apache.org on 2003/05/22 23:28:29 UTC
cvs commit: xml-xerces/c/src/xercesc/util PlatformUtils.hpp XMemory.cpp
jberry 2003/05/22 14:28:29
Modified: c/src/xercesc/dom/impl DOMDocumentImpl.cpp
c/src/xercesc/util PlatformUtils.hpp XMemory.cpp
Log:
Move pointer alignment functionality into XMLPlatform header; revise XMemory and DOMDocumentImpl to return blocks aligned by this function
Revision Changes Path
1.41 +28 -21 xml-xerces/c/src/xercesc/dom/impl/DOMDocumentImpl.cpp
Index: DOMDocumentImpl.cpp
===================================================================
RCS file: /home/cvs/xml-xerces/c/src/xercesc/dom/impl/DOMDocumentImpl.cpp,v
retrieving revision 1.40
retrieving revision 1.41
diff -u -r1.40 -r1.41
--- DOMDocumentImpl.cpp 22 May 2003 19:35:04 -0000 1.40
+++ DOMDocumentImpl.cpp 22 May 2003 21:28:29 -0000 1.41
@@ -805,25 +805,23 @@
// allocating directly with system.
void * DOMDocumentImpl::allocate(size_t amount)
-{
-
- size_t sizeOfPointer = sizeof(void *);
-// some MIPS or IA64 machines may misallign if the class has a long data type member
-// see Bug 10648 for details
-// size_t sizeOfPointer = sizeof(long);
-// REVISIT: sizeof(long) crashes on IA64 machine, we will look into it later.
-
- if (amount%sizeOfPointer!=0)
- amount = amount + (sizeOfPointer - (amount % sizeOfPointer));
+{
+ // Align the request size so that suballocated blocks
+ // beyond this one will be maintained at the same alignment.
+ amount = XMLPlatformUtils::alignPointerForNewBlockAllocation(amount);
// If the request is for a largish block, hand it off to the system
// allocator. The block still must be linked into the list of
// allocated blocks so that it will be deleted when the time comes.
if (amount > kMaxSubAllocationSize)
{
+ // The size of the header we add to our raw blocks
+ size_t sizeOfHeader = XMLPlatformUtils::alignPointerForNewBlockAllocation(sizeof(void *));
+
+ // Try to allocate the block
void* newBlock = 0;
try {
- newBlock = fMemoryManager->allocate(amount + sizeOfPointer); //new char[amount + sizeOfPointer];
+ newBlock = fMemoryManager->allocate(sizeOfHeader + amount); //new char[amount + sizeOfHeader];
}
catch (...) {
ThrowXML(RuntimeException, XMLExcepts::Out_Of_Memory);
@@ -831,6 +829,9 @@
if (!newBlock)
ThrowXML(RuntimeException, XMLExcepts::Out_Of_Memory);
+ // Link it into the list beyond current block, as current block
+ // is still being subdivided. If there is no current block
+ // then track that we have no bytes to further divide.
if (fCurrentBlock)
{
*(void **)newBlock = *(void **)fCurrentBlock;
@@ -842,16 +843,21 @@
fFreePtr = 0;
fFreeBytesRemaining = 0;
}
- void *retPtr = (char *)newBlock + sizeOfPointer;
+
+ void *retPtr = (char *)newBlock + sizeOfHeader;
return retPtr;
}
-
- // It's a normal (sub-allocatable) request.
- if (amount > fFreeBytesRemaining)
+ // It's a normal (sub-allocatable) request.
+ // Are we out of room in our current block?
+ if (amount > fFreeBytesRemaining)
{
// Request doesn't fit in the current block.
- // Get a new one from the system allocator.
+
+ // The size of the header we add to our raw blocks
+ size_t sizeOfHeader = XMLPlatformUtils::alignPointerForNewBlockAllocation(sizeof(void *));
+
+ // Get a new block from the system allocator.
void* newBlock = 0;
try {
newBlock = fMemoryManager->allocate(kHeapAllocSize); //new char[kHeapAllocSize];
@@ -864,28 +870,29 @@
*(void **)newBlock = fCurrentBlock;
fCurrentBlock = newBlock;
- fFreePtr = (char *)newBlock + sizeOfPointer;
- fFreeBytesRemaining = kHeapAllocSize - sizeOfPointer;
+ fFreePtr = (char *)newBlock + sizeOfHeader;
+ fFreeBytesRemaining = kHeapAllocSize - sizeOfHeader;
}
+ // Subdivide the request off current block
void *retPtr = fFreePtr;
fFreePtr += amount;
fFreeBytesRemaining -= amount;
+
return retPtr;
}
void DOMDocumentImpl::deleteHeap()
{
- void *block = fCurrentBlock;
while (fCurrentBlock != 0)
{
void *nextBlock = *(void **)fCurrentBlock;
fMemoryManager->deallocate(fCurrentBlock); //delete [] (char*) fCurrentBlock;
fCurrentBlock = nextBlock;
}
-
}
+
DOMNodeList *DOMDocumentImpl::getDeepNodeList(const DOMNode *rootNode, const XMLCh *tagName)
{
1.15 +39 -2 xml-xerces/c/src/xercesc/util/PlatformUtils.hpp
Index: PlatformUtils.hpp
===================================================================
RCS file: /home/cvs/xml-xerces/c/src/xercesc/util/PlatformUtils.hpp,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- PlatformUtils.hpp 15 May 2003 19:04:35 -0000 1.14
+++ PlatformUtils.hpp 22 May 2003 21:28:29 -0000 1.15
@@ -699,7 +699,15 @@
*/
static bool isStrictIANAEncoding();
//@}
-
+
+ /**
+ * Aligns the specified pointer per platform block allocation
+ * requirements.
+ *
+ * The results of this function may be altered by defining
+ * XML_PLATFORM_NEW_BLOCK_ALIGNMENT.
+ */
+ static inline void* alignPointerForNewBlockAllocation(void* ptr);
private :
/** @name Private static methods */
@@ -778,6 +786,35 @@
MakeXMLException(XMLPlatformUtilsException, XMLUTIL_EXPORT)
+
+
+// ---------------------------------------------------------------------------
+// XMLPlatformUtils: alignPointerForNewBlockAllocation
+// ---------------------------------------------------------------------------
+// Calculate alignment required by platform for a new
+// block allocation. We use this in our custom allocators
+// to ensure that returned blocks are properly aligned.
+inline void*
+XMLPlatformUtils::alignPointerForNewBlockAllocation(void* ptr)
+{
+ // Macro XML_PLATFORM_NEW_BLOCK_ALIGNMENT may be defined
+ // as needed to dictate alignment requirements on a
+ // per-architecture basis. In the absense of that we
+ // take an educated guess.
+ #ifdef XML_PLATFORM_NEW_BLOCK_ALIGNMENT
+ size_t alignment = XML_PLATFORM_NEW_BLOCK_ALIGNMENT;
+ #else
+ size_t alignment = (sizeof(void*) >= sizeof(double)) ? sizeof(void*) : sizeof(double);
+ #endif
+
+ // Calculate current alignment of pointer
+ size_t current = (long)ptr % alignment;
+
+ // Adjust pointer alignment as needed
+ return (current == 0)
+ ? ptr
+ : ((char*)ptr + alignment - current);
+}
1.5 +10 -27 xml-xerces/c/src/xercesc/util/XMemory.cpp
Index: XMemory.cpp
===================================================================
RCS file: /home/cvs/xml-xerces/c/src/xercesc/util/XMemory.cpp,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- XMemory.cpp 22 May 2003 19:33:16 -0000 1.4
+++ XMemory.cpp 22 May 2003 21:28:29 -0000 1.5
@@ -69,31 +69,11 @@
XERCES_CPP_NAMESPACE_BEGIN
-
-// Calculate alignment required by platform.
-// Total size of our header must match platform
-// architecture-specific alignment, in order
-// that the returned block ptr (which follows our
-// header), maintains block/structure alignment.
-inline size_t
-CalculateBlockHeaderSize()
-{
- // Macro XML_NEW_BLOCK_ALIGNMENT may be defined
- // as needed to calculate alignment on a per-architecture
- // basis.
- #ifdef XML_NEW_BLOCK_ALIGNMENT
- size_t alignment = XML_NEW_BLOCK_ALIGNMENT;
- #else
- size_t alignment = sizeof(void*) > sizeof(double) ? sizeof(void*) : sizeof(double);
- #endif
-
- size_t headerUsage = sizeof(MemoryManager*);
- return (headerUsage + (alignment - headerUsage % alignment));
-}
-
void* XMemory::operator new(size_t size)
{
- size_t headerSize = CalculateBlockHeaderSize();
+ size_t headerSize = XMLPlatformUtils::alignPointerForNewBlockAllocation(
+ sizeof(MemoryManager*));
+
void* const block = XMLPlatformUtils::fgMemoryManager->allocate
(
headerSize + size
@@ -107,7 +87,8 @@
{
assert(manager != 0);
- size_t headerSize = CalculateBlockHeaderSize();
+ size_t headerSize = XMLPlatformUtils::alignPointerForNewBlockAllocation(
+ sizeof(MemoryManager*));
void* const block = manager->allocate(headerSize + size);
*(MemoryManager**)block = manager;
@@ -118,7 +99,8 @@
{
if (p != 0)
{
- size_t headerSize = CalculateBlockHeaderSize();
+ size_t headerSize = XMLPlatformUtils::alignPointerForNewBlockAllocation(
+ sizeof(MemoryManager*));
void* const block = (char*)p - headerSize;
MemoryManager* const manager = *(MemoryManager**)block;
@@ -136,7 +118,8 @@
if (p != 0)
{
- size_t headerSize = CalculateBlockHeaderSize();
+ size_t headerSize = XMLPlatformUtils::alignPointerForNewBlockAllocation(
+ sizeof(MemoryManager*));
void* const block = (char*)p - headerSize;
assert(*(MemoryManager**)block == manager);
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-cvs-help@xml.apache.org