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

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

Penultimate check-in for icXML 0.9

File size: 3.5 KB
Line 
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/*
19 * $Id: XMemory.cpp 635226 2008-03-09 12:04:39Z borisk $
20 */
21
22
23// ---------------------------------------------------------------------------
24//  Includes
25// ---------------------------------------------------------------------------
26#include <xercesc/util/XMemory.hpp>
27#include <icxercesc/util/PlatformUtils.hpp>
28#include <xercesc/framework/MemoryManager.hpp>
29#include <assert.h>
30
31XERCES_CPP_NAMESPACE_BEGIN
32
33void* XMemory::operator new(size_t size)
34{
35        size_t headerSize = XMLPlatformUtils::alignPointerForNewBlockAllocation(
36                                                                                sizeof(MemoryManager*));
37       
38    void* const block = XMLPlatformUtils::fgMemoryManager->allocate
39        (
40                headerSize + size
41        );
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    assert(manager != 0);
64       
65    size_t headerSize = XMLPlatformUtils::alignPointerForNewBlockAllocation(
66       sizeof(MemoryManager*));
67       
68    void* const block = manager->allocate(headerSize + size);
69    *(MemoryManager**)block = manager;
70
71    return (char*)block + headerSize;
72}
73
74void* XMemory::operator new(size_t /*size*/, void* ptr)
75{
76    return ptr;
77}
78
79void XMemory::operator delete(void* p)
80{
81    if (p != 0)
82    {
83        size_t headerSize = XMLPlatformUtils::alignPointerForNewBlockAllocation(
84          sizeof(MemoryManager*));
85        void* const block = (char*)p - headerSize;
86
87        MemoryManager* const manager = *(MemoryManager**)block;
88        assert(manager != 0);
89        manager->deallocate(block);
90    }
91}
92
93//The Borland compiler is complaining about duplicate overloading of delete
94#if !defined(XERCES_NO_MATCHING_DELETE_OPERATOR)
95
96void XMemory::operator delete(void* p, MemoryManager* manager)
97{
98    assert(manager != 0);
99       
100    if (p != 0)
101    {
102        size_t headerSize = XMLPlatformUtils::alignPointerForNewBlockAllocation(sizeof(MemoryManager*));
103        void* const block = (char*)p - headerSize;
104
105        /***
106         * assert(*(MemoryManager**)block == manager);                 
107         *
108         * NOTE: for compiler which can't properly trace the memory manager used in the
109         *       placement new, we use the memory manager embedded in the memory rather
110         *       than the one passed in
111         */ 
112        MemoryManager* pM = *(MemoryManager**)block;
113        pM->deallocate(block);
114    }
115}
116
117void XMemory::operator delete(void* /*p*/, void* /*ptr*/)
118{
119}
120
121#endif
122
123XERCES_CPP_NAMESPACE_END
124
Note: See TracBrowser for help on using the repository browser.