source: icXML/icXML-devel/src/icxercesc/util/XMemory.cpp @ 2720

Last change on this file since 2720 was 2720, checked in by cameron, 6 years ago

Initial check-in of icXML 0.8 source files

File size: 2.8 KB
Line 
1/*
2 * Unless required by applicable law or agreed to in writing, software
3 * distributed under the License is distributed on an "AS IS" BASIS,
4 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
5 * See the License for the specific language governing permissions and
6 * limitations under the License.
7 */
8
9/*
10 * $Id: XMemory.cpp 635226 2008-03-09 12:04:39Z borisk $
11 */
12
13
14// ---------------------------------------------------------------------------
15//  Includes
16// ---------------------------------------------------------------------------
17#include <xercesc/util/XMemory.hpp>
18
19#include <xercesc/util/PlatformUtils.hpp>
20#include <xercesc/framework/MemoryManager.hpp>
21#include <assert.h>
22
23XERCES_CPP_NAMESPACE_BEGIN
24
25enum
26{
27        kDefaultAlignment =
28        #ifdef XML_PLATFORM_NEW_BLOCK_ALIGNMENT
29                XML_PLATFORM_NEW_BLOCK_ALIGNMENT
30        #else
31                ((sizeof(void*) >= sizeof(double)) ? sizeof(void*) : sizeof(double))
32        #endif
33        , headerSize =
34                (sizeof(MemoryManager*) % kDefaultAlignment) == 0 ?
35                        sizeof(MemoryManager*) :
36                        sizeof(MemoryManager*) + kDefaultAlignment - (sizeof(MemoryManager*) % kDefaultAlignment)
37};
38
39void* XMemory::operator new(size_t size)
40{
41        void* const block = XMLPlatformUtils::fgMemoryManager->allocate(headerSize + size);
42        *(MemoryManager**)block = XMLPlatformUtils::fgMemoryManager;
43
44        return (char*)block + headerSize;
45}
46
47#if defined(XERCES_MFC_SUPPORT)
48
49void* XMemory::operator new(size_t size, const char* /*file*/, int /*line*/)
50{
51        return operator new(size);
52}
53
54void XMemory::operator delete(void* p, const char* /*file*/, int /*line*/)
55{
56        operator delete(p);
57}
58
59#endif
60
61void* XMemory::operator new(size_t size, MemoryManager* manager)
62{
63        void * const block = manager->allocate(headerSize + size);
64        *(MemoryManager**)block = manager;
65        return (char*)block + headerSize;
66}
67
68void* XMemory::operator new(size_t /*size*/, void* ptr)
69{
70        return ptr;
71}
72
73void XMemory::operator delete(void * p)
74{
75        if (p != 0)
76        {
77                void* const block = (char*)p - headerSize;
78                MemoryManager* const manager = *(MemoryManager**)block;
79                manager->deallocate(block);
80        }
81}
82
83//The Borland compiler is complaining about duplicate overloading of delete
84#if !defined(XERCES_NO_MATCHING_DELETE_OPERATOR)
85
86
87void XMemory::operator delete(void* p, MemoryManager* manager)
88{
89        if (p != 0)
90        {
91                void* const block = (char*)p - headerSize;
92                /***
93                 * assert(*(MemoryManager**)block == manager);
94                 *
95                 * NOTE: for compiler which can't properly trace the memory manager used in the
96                 *       placement new, we use the memory manager embedded in the memory rather
97                 *       than the one passed in
98                 */
99                MemoryManager* pM = *(MemoryManager**)block;
100                pM->deallocate(block);
101        }
102}
103
104
105void XMemory::operator delete(void* /*p*/, void* /*ptr*/)
106{
107}
108
109#endif
110
111#undef XML_PLATFORM_DEFAULT_NEW_BLOCK_ALIGNMENT
112
113XERCES_CPP_NAMESPACE_END
Note: See TracBrowser for help on using the repository browser.