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>
31 struct AllocatorTraits
33 typedef char const signature_type
[8];
34 const signature_type
& m_signature
;
36 explicit AllocatorTraits (signature_type
const & s
)
40 std::size_t size (std::size_t n
) const
42 n
= std::max(n
, std::size_t(1));
43 #if OSL_DEBUG_LEVEL > 0
44 n
+= sizeof(signature_type
);
45 #endif /* OSL_DEBUG_LEVEL */
49 void* init (void * p
) const
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 */
58 void* fini (void * p
) const
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)
64 OSL_FAIL("operator delete mismatch");
66 #endif /* OSL_DEBUG_LEVEL */
71 struct VectorTraits
: public AllocatorTraits
73 static const signature_type g_signature
;
76 : AllocatorTraits (g_signature
)
80 struct ScalarTraits
: public AllocatorTraits
82 static const signature_type g_signature
;
85 : AllocatorTraits (g_signature
)
89 const AllocatorTraits::signature_type
VectorTraits::g_signature
= "new[]()";
90 const AllocatorTraits::signature_type
ScalarTraits::g_signature
= "new() ";
92 } // anonymous namespace
96 static void default_handler()
98 // Multithreading race in 'std::set_new_handler()' call sequence below.
99 throw std::bad_alloc();
102 static void* allocate (
103 std::size_t n
, AllocatorTraits
const & rTraits
)
105 n
= rTraits
.size (n
);
108 void * p
= rtl_allocateMemory (sal_Size(n
));
110 return rTraits
.init (p
);
112 std::new_handler d
= default_handler
, f
= std::set_new_handler (d
);
114 std::set_new_handler (f
);
117 throw std::bad_alloc();
122 static void* allocate_nothrow (
123 std::size_t n
, AllocatorTraits
const & rTraits
)
127 return allocate (n
, rTraits
);
129 catch (std::bad_alloc
const &)
135 static void deallocate (void * p
, AllocatorTraits
const & rTraits
)
139 rtl_freeMemory (rTraits
.fini(p
));
143 // T * p = new T; delete p;
145 void* SAL_CALL
operator new (std::size_t n
) throw (std::bad_alloc
)
147 return allocate (n
, ScalarTraits());
150 void SAL_CALL
operator delete (void * p
) throw ()
152 deallocate (p
, ScalarTraits());
155 // T * p = new(nothrow) T; delete(nothrow) p;
157 void* SAL_CALL
operator new (std::size_t n
, std::nothrow_t
const &) throw ()
159 return allocate_nothrow (n
, ScalarTraits());
162 void SAL_CALL
operator delete (void * p
, std::nothrow_t
const &) throw ()
164 deallocate (p
, ScalarTraits());
167 // T * p = new T[n]; delete[] p;
169 void* SAL_CALL
operator new[] (std::size_t n
) throw (std::bad_alloc
)
171 return allocate (n
, VectorTraits());
174 void SAL_CALL
operator delete[] (void * p
) throw ()
176 deallocate (p
, VectorTraits());
179 // T * p = new(nothrow) T[n]; delete(nothrow)[] p;
181 void* SAL_CALL
operator new[] (std::size_t n
, std::nothrow_t
const &) throw ()
183 return allocate_nothrow (n
, VectorTraits());
186 void SAL_CALL
operator delete[] (void * p
, std::nothrow_t
const &) throw ()
188 deallocate (p
, VectorTraits());
191 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */