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 .
23 #include <osl/diagnose.h>
24 #include <rtl/alloc.h>
26 // =======================================================================
28 // =======================================================================
33 struct AllocatorTraits
35 typedef char const signature_type
[8];
36 const signature_type
& m_signature
;
38 explicit AllocatorTraits (signature_type
const & s
) SAL_THROW(())
42 std::size_t size (std::size_t n
) const SAL_THROW(())
44 n
= std::max(n
, std::size_t(1));
45 #if OSL_DEBUG_LEVEL > 0
46 n
+= sizeof(signature_type
);
47 #endif /* OSL_DEBUG_LEVEL */
51 void* init (void * p
) const SAL_THROW(())
53 #if OSL_DEBUG_LEVEL > 0
54 memcpy (p
, m_signature
, sizeof(signature_type
));
55 p
= static_cast<char*>(p
) + sizeof(signature_type
);
56 #endif /* OSL_DEBUG_LEVEL */
60 void* fini (void * p
) const SAL_THROW(())
62 #if OSL_DEBUG_LEVEL > 0
63 p
= static_cast<char*>(p
) - sizeof(signature_type
);
64 if (memcmp (p
, m_signature
, sizeof(signature_type
)) != 0)
66 OSL_FAIL("operator delete mismatch");
68 #endif /* OSL_DEBUG_LEVEL */
73 // =======================================================================
75 struct VectorTraits
: public AllocatorTraits
77 static const signature_type g_signature
;
79 VectorTraits() SAL_THROW(())
80 : AllocatorTraits (g_signature
)
84 struct ScalarTraits
: public AllocatorTraits
86 static const signature_type g_signature
;
88 ScalarTraits() SAL_THROW(())
89 : AllocatorTraits (g_signature
)
93 const AllocatorTraits::signature_type
VectorTraits::g_signature
= "new[]()";
94 const AllocatorTraits::signature_type
ScalarTraits::g_signature
= "new() ";
96 } // anonymous namespace
98 // =======================================================================
100 // =======================================================================
102 static void default_handler (void)
104 // Multithreading race in 'std::set_new_handler()' call sequence below.
105 throw std::bad_alloc();
108 // =======================================================================
110 static void* allocate (
111 std::size_t n
, AllocatorTraits
const & rTraits
)
112 SAL_THROW((std::bad_alloc
))
114 n
= rTraits
.size (n
);
117 void * p
= rtl_allocateMemory (sal_Size(n
));
119 return rTraits
.init (p
);
121 std::new_handler d
= default_handler
, f
= std::set_new_handler (d
);
123 std::set_new_handler (f
);
126 throw std::bad_alloc();
131 // =======================================================================
133 static void* allocate_nothrow (
134 std::size_t n
, AllocatorTraits
const & rTraits
)
139 return allocate (n
, rTraits
);
141 catch (std::bad_alloc
const &)
147 // =======================================================================
149 static void deallocate (void * p
, AllocatorTraits
const & rTraits
)
154 rtl_freeMemory (rTraits
.fini(p
));
158 // =======================================================================
159 // T * p = new T; delete p;
160 // =======================================================================
162 void* SAL_CALL
operator new (std::size_t n
) throw (std::bad_alloc
)
164 return allocate (n
, ScalarTraits());
167 // =======================================================================
169 void SAL_CALL
operator delete (void * p
) throw ()
171 deallocate (p
, ScalarTraits());
174 // =======================================================================
175 // T * p = new(nothrow) T; delete(nothrow) p;
176 // =======================================================================
178 void* SAL_CALL
operator new (std::size_t n
, std::nothrow_t
const &) throw ()
180 return allocate_nothrow (n
, ScalarTraits());
183 // =======================================================================
185 void SAL_CALL
operator delete (void * p
, std::nothrow_t
const &) throw ()
187 deallocate (p
, ScalarTraits());
190 // =======================================================================
191 // T * p = new T[n]; delete[] p;
192 // =======================================================================
194 void* SAL_CALL
operator new[] (std::size_t n
) throw (std::bad_alloc
)
196 return allocate (n
, VectorTraits());
199 // =======================================================================
201 void SAL_CALL
operator delete[] (void * p
) throw ()
203 deallocate (p
, VectorTraits());
206 // =======================================================================
207 // T * p = new(nothrow) T[n]; delete(nothrow)[] p;
208 // =======================================================================
210 void* SAL_CALL
operator new[] (std::size_t n
, std::nothrow_t
const &) throw ()
212 return allocate_nothrow (n
, VectorTraits());
215 // =======================================================================
217 void SAL_CALL
operator delete[] (void * p
, std::nothrow_t
const &) throw ()
219 deallocate (p
, VectorTraits());
222 // =======================================================================
224 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */