GME  13
XMemory.cpp
Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one or more
00003  * contributor license agreements.  See the NOTICE file distributed with
00004  * this work for additional information regarding copyright ownership.
00005  * The ASF licenses this file to You under the Apache License, Version 2.0
00006  * (the "License"); you may not use this file except in compliance with
00007  * the License.  You may obtain a copy of the License at
00008  * 
00009  *      http://www.apache.org/licenses/LICENSE-2.0
00010  * 
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 /*
00019  * $Id: XMemory.cpp 635226 2008-03-09 12:04:39Z borisk $
00020  */
00021 
00022 
00023 // ---------------------------------------------------------------------------
00024 //  Includes
00025 // ---------------------------------------------------------------------------
00026 #include <xercesc/util/XMemory.hpp>
00027 #include <xercesc/util/PlatformUtils.hpp>
00028 #include <xercesc/framework/MemoryManager.hpp>
00029 #include <assert.h>
00030 
00031 XERCES_CPP_NAMESPACE_BEGIN
00032 
00033 void* XMemory::operator new(size_t size)
00034 {
00035         size_t headerSize = XMLPlatformUtils::alignPointerForNewBlockAllocation(
00036                                                                                 sizeof(MemoryManager*));
00037         
00038     void* const block = XMLPlatformUtils::fgMemoryManager->allocate
00039         (
00040                 headerSize + size
00041         );
00042     *(MemoryManager**)block = XMLPlatformUtils::fgMemoryManager;
00043 
00044     return (char*)block + headerSize;
00045 }
00046 
00047 #if defined(XERCES_MFC_SUPPORT)
00048 
00049 void* XMemory::operator new(size_t size, const char* /*file*/, int /*line*/)
00050 { 
00051                  return operator new(size); 
00052 }
00053  
00054 void XMemory::operator delete(void* p, const char* /*file*/, int /*line*/)
00055 { 
00056                  operator delete(p); 
00057 }
00058  
00059 #endif
00060 
00061 void* XMemory::operator new(size_t size, MemoryManager* manager)
00062 {
00063     assert(manager != 0);
00064         
00065     size_t headerSize = XMLPlatformUtils::alignPointerForNewBlockAllocation(
00066        sizeof(MemoryManager*));
00067        
00068     void* const block = manager->allocate(headerSize + size);
00069     *(MemoryManager**)block = manager;
00070 
00071     return (char*)block + headerSize;
00072 }
00073 
00074 void* XMemory::operator new(size_t /*size*/, void* ptr)
00075 {
00076     return ptr;
00077 }
00078 
00079 void XMemory::operator delete(void* p)
00080 {
00081     if (p != 0)
00082     {
00083         size_t headerSize = XMLPlatformUtils::alignPointerForNewBlockAllocation(
00084           sizeof(MemoryManager*));
00085         void* const block = (char*)p - headerSize;
00086 
00087         MemoryManager* const manager = *(MemoryManager**)block;
00088         assert(manager != 0);
00089         manager->deallocate(block);
00090     }
00091 }
00092 
00093 //The Borland compiler is complaining about duplicate overloading of delete
00094 #if !defined(XERCES_NO_MATCHING_DELETE_OPERATOR)
00095 
00096 void XMemory::operator delete(void* p, MemoryManager* manager)
00097 {
00098     assert(manager != 0);
00099         
00100     if (p != 0)
00101     {
00102         size_t headerSize = XMLPlatformUtils::alignPointerForNewBlockAllocation(sizeof(MemoryManager*));
00103         void* const block = (char*)p - headerSize;
00104 
00105         /***
00106          * assert(*(MemoryManager**)block == manager);                 
00107          *
00108          * NOTE: for compiler which can't properly trace the memory manager used in the 
00109          *       placement new, we use the memory manager embedded in the memory rather 
00110          *       than the one passed in
00111          */ 
00112         MemoryManager* pM = *(MemoryManager**)block;
00113         pM->deallocate(block);
00114     }
00115 }
00116 
00117 void XMemory::operator delete(void* /*p*/, void* /*ptr*/)
00118 {
00119 }
00120 
00121 #endif
00122 
00123 XERCES_CPP_NAMESPACE_END
00124