Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <algorithm>
21 : #include <new>
22 : #include <string.h>
23 : #include <osl/diagnose.h>
24 : #include <rtl/alloc.h>
25 :
26 : // AllocatorTraits
27 :
28 : namespace
29 : {
30 :
31 : struct AllocatorTraits
32 : {
33 : typedef char const signature_type[8];
34 : const signature_type & m_signature;
35 :
36 0 : explicit AllocatorTraits (signature_type const & s)
37 0 : : m_signature (s)
38 0 : {}
39 :
40 0 : std::size_t size (std::size_t n) const
41 : {
42 0 : n = std::max(n, std::size_t(1));
43 : #if OSL_DEBUG_LEVEL > 0
44 : n += sizeof(signature_type);
45 : #endif /* OSL_DEBUG_LEVEL */
46 0 : return n;
47 : }
48 :
49 0 : void* init (void * p) const
50 : {
51 : #if OSL_DEBUG_LEVEL > 0
52 : memcpy (p, m_signature, sizeof(signature_type));
53 : p = static_cast<char*>(p) + sizeof(signature_type);
54 : #endif /* OSL_DEBUG_LEVEL */
55 0 : return p;
56 : }
57 :
58 0 : void* fini (void * p) const
59 : {
60 : #if OSL_DEBUG_LEVEL > 0
61 : p = static_cast<char*>(p) - sizeof(signature_type);
62 : if (memcmp (p, m_signature, sizeof(signature_type)) != 0)
63 : {
64 : OSL_FAIL("operator delete mismatch");
65 : }
66 : #endif /* OSL_DEBUG_LEVEL */
67 0 : return p;
68 : }
69 : };
70 :
71 : struct VectorTraits : public AllocatorTraits
72 : {
73 : static const signature_type g_signature;
74 :
75 0 : VectorTraits()
76 0 : : AllocatorTraits (g_signature)
77 0 : {}
78 : };
79 :
80 : struct ScalarTraits : public AllocatorTraits
81 : {
82 : static const signature_type g_signature;
83 :
84 0 : ScalarTraits()
85 0 : : AllocatorTraits (g_signature)
86 0 : {}
87 : };
88 :
89 : const AllocatorTraits::signature_type VectorTraits::g_signature = "new[]()";
90 : const AllocatorTraits::signature_type ScalarTraits::g_signature = "new() ";
91 :
92 : } // anonymous namespace
93 :
94 : // Allocator
95 :
96 0 : static void default_handler()
97 : {
98 : // Multithreading race in 'std::set_new_handler()' call sequence below.
99 0 : throw std::bad_alloc();
100 : }
101 :
102 0 : static void* allocate (
103 : std::size_t n, AllocatorTraits const & rTraits)
104 : {
105 0 : n = rTraits.size (n);
106 : for (;;)
107 : {
108 0 : void * p = rtl_allocateMemory (sal_Size(n));
109 0 : if (p != 0)
110 0 : return rTraits.init (p);
111 :
112 0 : std::new_handler d = default_handler, f = std::set_new_handler (d);
113 0 : if (f != d)
114 0 : std::set_new_handler (f);
115 :
116 0 : if (f == 0)
117 0 : throw std::bad_alloc();
118 0 : (*f)();
119 0 : }
120 : }
121 :
122 0 : static void* allocate_nothrow (
123 : std::size_t n, AllocatorTraits const & rTraits)
124 : {
125 : try
126 : {
127 0 : return allocate (n, rTraits);
128 : }
129 0 : catch (std::bad_alloc const &)
130 : {
131 0 : return 0;
132 : }
133 : }
134 :
135 0 : static void deallocate (void * p, AllocatorTraits const & rTraits)
136 : {
137 0 : if (p)
138 : {
139 0 : rtl_freeMemory (rTraits.fini(p));
140 : }
141 0 : }
142 :
143 : // T * p = new T; delete p;
144 :
145 0 : void* SAL_CALL operator new (std::size_t n) throw (std::bad_alloc)
146 : {
147 0 : return allocate (n, ScalarTraits());
148 : }
149 :
150 0 : void SAL_CALL operator delete (void * p) throw ()
151 : {
152 0 : deallocate (p, ScalarTraits());
153 0 : }
154 :
155 : // T * p = new(nothrow) T; delete(nothrow) p;
156 :
157 0 : void* SAL_CALL operator new (std::size_t n, std::nothrow_t const &) throw ()
158 : {
159 0 : return allocate_nothrow (n, ScalarTraits());
160 : }
161 :
162 0 : void SAL_CALL operator delete (void * p, std::nothrow_t const &) throw ()
163 : {
164 0 : deallocate (p, ScalarTraits());
165 0 : }
166 :
167 : // T * p = new T[n]; delete[] p;
168 :
169 0 : void* SAL_CALL operator new[] (std::size_t n) throw (std::bad_alloc)
170 : {
171 0 : return allocate (n, VectorTraits());
172 : }
173 :
174 0 : void SAL_CALL operator delete[] (void * p) throw ()
175 : {
176 0 : deallocate (p, VectorTraits());
177 0 : }
178 :
179 : // T * p = new(nothrow) T[n]; delete(nothrow)[] p;
180 :
181 0 : void* SAL_CALL operator new[] (std::size_t n, std::nothrow_t const &) throw ()
182 : {
183 0 : return allocate_nothrow (n, VectorTraits());
184 : }
185 :
186 0 : void SAL_CALL operator delete[] (void * p, std::nothrow_t const &) throw ()
187 : {
188 0 : deallocate (p, VectorTraits());
189 0 : }
190 :
191 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|