1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #ifdef _MSC_VER /* avoid 'std::bad_alloc' unresolved externals */
29 #include <osl/diagnose.h>
30 #include <rtl/alloc.h>
32 // =======================================================================
34 // =======================================================================
39 struct AllocatorTraits
41 typedef char const signature_type
[8];
42 const signature_type
& m_signature
;
44 explicit AllocatorTraits (signature_type
const & s
) SAL_THROW(())
48 std::size_t size (std::size_t n
) const SAL_THROW(())
50 n
= std::max(n
, std::size_t(1));
51 #if OSL_DEBUG_LEVEL > 0
52 n
+= sizeof(signature_type
);
53 #endif /* OSL_DEBUG_LEVEL */
57 void* init (void * p
) const SAL_THROW(())
59 #if OSL_DEBUG_LEVEL > 0
60 memcpy (p
, m_signature
, sizeof(signature_type
));
61 p
= static_cast<char*>(p
) + sizeof(signature_type
);
62 #endif /* OSL_DEBUG_LEVEL */
66 void* fini (void * p
) const SAL_THROW(())
68 #if OSL_DEBUG_LEVEL > 0
69 p
= static_cast<char*>(p
) - sizeof(signature_type
);
70 if (memcmp (p
, m_signature
, sizeof(signature_type
)) != 0)
72 OSL_FAIL("operator delete mismatch");
74 #endif /* OSL_DEBUG_LEVEL */
79 // =======================================================================
81 struct VectorTraits
: public AllocatorTraits
83 static const signature_type g_signature
;
85 VectorTraits() SAL_THROW(())
86 : AllocatorTraits (g_signature
)
90 struct ScalarTraits
: public AllocatorTraits
92 static const signature_type g_signature
;
94 ScalarTraits() SAL_THROW(())
95 : AllocatorTraits (g_signature
)
99 const AllocatorTraits::signature_type
VectorTraits::g_signature
= "new[]()";
100 const AllocatorTraits::signature_type
ScalarTraits::g_signature
= "new() ";
102 } // anonymous namespace
104 // =======================================================================
106 // =======================================================================
108 static void default_handler (void)
110 // Multithreading race in 'std::set_new_handler()' call sequence below.
111 throw std::bad_alloc();
114 // =======================================================================
116 static void* allocate (
117 std::size_t n
, AllocatorTraits
const & rTraits
)
118 SAL_THROW((std::bad_alloc
))
120 n
= rTraits
.size (n
);
123 void * p
= rtl_allocateMemory (sal_Size(n
));
125 return rTraits
.init (p
);
127 std::new_handler d
= default_handler
, f
= std::set_new_handler (d
);
129 std::set_new_handler (f
);
132 throw std::bad_alloc();
137 // =======================================================================
139 static void* allocate_nothrow (
140 std::size_t n
, AllocatorTraits
const & rTraits
)
145 return allocate (n
, rTraits
);
147 catch (std::bad_alloc
const &)
153 // =======================================================================
155 static void deallocate (void * p
, AllocatorTraits
const & rTraits
)
160 rtl_freeMemory (rTraits
.fini(p
));
164 // =======================================================================
165 // T * p = new T; delete p;
166 // =======================================================================
168 void* SAL_CALL
operator new (std::size_t n
) throw (std::bad_alloc
)
170 return allocate (n
, ScalarTraits());
173 // =======================================================================
175 void SAL_CALL
operator delete (void * p
) throw ()
177 deallocate (p
, ScalarTraits());
180 // =======================================================================
181 // T * p = new(nothrow) T; delete(nothrow) p;
182 // =======================================================================
184 void* SAL_CALL
operator new (std::size_t n
, std::nothrow_t
const &) throw ()
186 return allocate_nothrow (n
, ScalarTraits());
189 // =======================================================================
191 void SAL_CALL
operator delete (void * p
, std::nothrow_t
const &) throw ()
193 deallocate (p
, ScalarTraits());
196 // =======================================================================
197 // T * p = new T[n]; delete[] p;
198 // =======================================================================
200 void* SAL_CALL
operator new[] (std::size_t n
) throw (std::bad_alloc
)
202 return allocate (n
, VectorTraits());
205 // =======================================================================
207 void SAL_CALL
operator delete[] (void * p
) throw ()
209 deallocate (p
, VectorTraits());
212 // =======================================================================
213 // T * p = new(nothrow) T[n]; delete(nothrow)[] p;
214 // =======================================================================
216 void* SAL_CALL
operator new[] (std::size_t n
, std::nothrow_t
const &) throw ()
218 return allocate_nothrow (n
, VectorTraits());
221 // =======================================================================
223 void SAL_CALL
operator delete[] (void * p
, std::nothrow_t
const &) throw ()
225 deallocate (p
, VectorTraits());
228 // =======================================================================
230 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */