187d2adbcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
387d2adbcSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
487d2adbcSAndrew Rist * or more contributor license agreements. See the NOTICE file
587d2adbcSAndrew Rist * distributed with this work for additional information
687d2adbcSAndrew Rist * regarding copyright ownership. The ASF licenses this file
787d2adbcSAndrew Rist * to you under the Apache License, Version 2.0 (the
887d2adbcSAndrew Rist * "License"); you may not use this file except in compliance
987d2adbcSAndrew Rist * with the License. You may obtain a copy of the License at
10cdf0e10cSrcweir *
1187d2adbcSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir *
1387d2adbcSAndrew Rist * Unless required by applicable law or agreed to in writing,
1487d2adbcSAndrew Rist * software distributed under the License is distributed on an
1587d2adbcSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1687d2adbcSAndrew Rist * KIND, either express or implied. See the License for the
1787d2adbcSAndrew Rist * specific language governing permissions and limitations
1887d2adbcSAndrew Rist * under the License.
19cdf0e10cSrcweir *
2087d2adbcSAndrew Rist *************************************************************/
2187d2adbcSAndrew Rist
2287d2adbcSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sal.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #ifdef WNT /* avoid 'std::bad_alloc' unresolved externals */
28cdf0e10cSrcweir #define _CRTIMP
29cdf0e10cSrcweir #define _NTSDK
30cdf0e10cSrcweir #endif /* WNT */
31cdf0e10cSrcweir
32cdf0e10cSrcweir #ifndef INCLUDED_ALGORITHM
33cdf0e10cSrcweir #include <algorithm>
34cdf0e10cSrcweir #define INCLUDED_ALGORITHM
35cdf0e10cSrcweir #endif
36cdf0e10cSrcweir
37cdf0e10cSrcweir #ifndef INCLUDED_NEW
38cdf0e10cSrcweir #include <new>
39cdf0e10cSrcweir #define INCLUDED_NEW
40cdf0e10cSrcweir #endif
41cdf0e10cSrcweir
42cdf0e10cSrcweir #ifndef INCLUDED_STRING_H
43cdf0e10cSrcweir #include <string.h>
44cdf0e10cSrcweir #define INCLUDED_STRING_H
45cdf0e10cSrcweir #endif
46cdf0e10cSrcweir #include <osl/diagnose.h>
47cdf0e10cSrcweir #include <rtl/alloc.h>
48cdf0e10cSrcweir
49cdf0e10cSrcweir using std::nothrow_t;
50cdf0e10cSrcweir
51cdf0e10cSrcweir // =======================================================================
52cdf0e10cSrcweir // AllocatorTraits
53cdf0e10cSrcweir // =======================================================================
54cdf0e10cSrcweir
55cdf0e10cSrcweir namespace
56cdf0e10cSrcweir {
57cdf0e10cSrcweir
58cdf0e10cSrcweir struct AllocatorTraits
59cdf0e10cSrcweir {
60cdf0e10cSrcweir typedef char const signature_type[8];
61cdf0e10cSrcweir const signature_type & m_signature;
62cdf0e10cSrcweir
63cdf0e10cSrcweir explicit AllocatorTraits (signature_type const & s) SAL_THROW(())
64cdf0e10cSrcweir : m_signature (s)
65cdf0e10cSrcweir {}
66cdf0e10cSrcweir
size__anon79e63f810111::AllocatorTraits67cdf0e10cSrcweir std::size_t size (std::size_t n) const SAL_THROW(())
68cdf0e10cSrcweir {
69cdf0e10cSrcweir n = std::max(n, std::size_t(1));
70cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
71*787e1130SDon Lewis # ifdef NEED_ALIGN16
72*787e1130SDon Lewis n += 2*sizeof(signature_type);
73*787e1130SDon Lewis # else
74cdf0e10cSrcweir n += sizeof(signature_type);
75*787e1130SDon Lewis # endif
76cdf0e10cSrcweir #endif /* OSL_DEBUG_LEVEL */
77cdf0e10cSrcweir return n;
78cdf0e10cSrcweir }
79cdf0e10cSrcweir
init__anon79e63f810111::AllocatorTraits80cdf0e10cSrcweir void* init (void * p) const SAL_THROW(())
81cdf0e10cSrcweir {
82cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
83cdf0e10cSrcweir memcpy (p, m_signature, sizeof(signature_type));
84*787e1130SDon Lewis # ifdef NEED_ALIGN16
85*787e1130SDon Lewis p = static_cast<char*>(p) + 2*sizeof(signature_type);
86*787e1130SDon Lewis # else
87cdf0e10cSrcweir p = static_cast<char*>(p) + sizeof(signature_type);
88*787e1130SDon Lewis # endif
89cdf0e10cSrcweir #endif /* OSL_DEBUG_LEVEL */
90cdf0e10cSrcweir return p;
91cdf0e10cSrcweir }
92cdf0e10cSrcweir
fini__anon79e63f810111::AllocatorTraits93cdf0e10cSrcweir void* fini (void * p) const SAL_THROW(())
94cdf0e10cSrcweir {
95cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
96*787e1130SDon Lewis # ifdef NEED_ALIGN16
97*787e1130SDon Lewis p = static_cast<char*>(p) - 2*sizeof(signature_type);
98*787e1130SDon Lewis # else
99cdf0e10cSrcweir p = static_cast<char*>(p) - sizeof(signature_type);
100*787e1130SDon Lewis # endif
101cdf0e10cSrcweir if (memcmp (p, m_signature, sizeof(signature_type)) != 0)
102cdf0e10cSrcweir {
103cdf0e10cSrcweir OSL_ENSURE(0, "operator delete mismatch");
104cdf0e10cSrcweir }
105cdf0e10cSrcweir #endif /* OSL_DEBUG_LEVEL */
106cdf0e10cSrcweir return p;
107cdf0e10cSrcweir }
108cdf0e10cSrcweir };
109cdf0e10cSrcweir
110cdf0e10cSrcweir // =======================================================================
111cdf0e10cSrcweir
112cdf0e10cSrcweir struct VectorTraits : public AllocatorTraits
113cdf0e10cSrcweir {
114cdf0e10cSrcweir static const signature_type g_signature;
115cdf0e10cSrcweir
116cdf0e10cSrcweir VectorTraits() SAL_THROW(())
117cdf0e10cSrcweir : AllocatorTraits (g_signature)
118cdf0e10cSrcweir {}
119cdf0e10cSrcweir };
120cdf0e10cSrcweir
121cdf0e10cSrcweir struct ScalarTraits : public AllocatorTraits
122cdf0e10cSrcweir {
123cdf0e10cSrcweir static const signature_type g_signature;
124cdf0e10cSrcweir
125cdf0e10cSrcweir ScalarTraits() SAL_THROW(())
126cdf0e10cSrcweir : AllocatorTraits (g_signature)
127cdf0e10cSrcweir {}
128cdf0e10cSrcweir };
129cdf0e10cSrcweir
130cdf0e10cSrcweir const AllocatorTraits::signature_type VectorTraits::g_signature = "new[]()";
131cdf0e10cSrcweir const AllocatorTraits::signature_type ScalarTraits::g_signature = "new() ";
132cdf0e10cSrcweir
133cdf0e10cSrcweir } // anonymous namespace
134cdf0e10cSrcweir
135cdf0e10cSrcweir // =======================================================================
136cdf0e10cSrcweir // Allocator
137cdf0e10cSrcweir // =======================================================================
138cdf0e10cSrcweir
default_handler(void)139cdf0e10cSrcweir static void default_handler (void)
140cdf0e10cSrcweir {
141cdf0e10cSrcweir // Multithreading race in 'std::set_new_handler()' call sequence below.
142cdf0e10cSrcweir throw std::bad_alloc();
143cdf0e10cSrcweir }
144cdf0e10cSrcweir
145cdf0e10cSrcweir // =======================================================================
146cdf0e10cSrcweir
allocate(std::size_t n,AllocatorTraits const & rTraits)147cdf0e10cSrcweir static void* allocate (
148cdf0e10cSrcweir std::size_t n, AllocatorTraits const & rTraits)
149cdf0e10cSrcweir SAL_THROW((std::bad_alloc))
150cdf0e10cSrcweir {
151cdf0e10cSrcweir n = rTraits.size (n);
152cdf0e10cSrcweir for (;;)
153cdf0e10cSrcweir {
154cdf0e10cSrcweir void * p = rtl_allocateMemory (sal_Size(n));
155cdf0e10cSrcweir if (p != 0)
156cdf0e10cSrcweir return rTraits.init (p);
157cdf0e10cSrcweir
158cdf0e10cSrcweir std::new_handler d = default_handler, f = std::set_new_handler (d);
159cdf0e10cSrcweir if (f != d)
160cdf0e10cSrcweir std::set_new_handler (f);
161cdf0e10cSrcweir
162cdf0e10cSrcweir if (f == 0)
163cdf0e10cSrcweir throw std::bad_alloc();
164cdf0e10cSrcweir (*f)();
165cdf0e10cSrcweir }
166cdf0e10cSrcweir }
167cdf0e10cSrcweir
168cdf0e10cSrcweir // =======================================================================
169cdf0e10cSrcweir
allocate(std::size_t n,AllocatorTraits const & rTraits,std::nothrow_t const &)170cdf0e10cSrcweir static void* allocate (
171cdf0e10cSrcweir std::size_t n, AllocatorTraits const & rTraits, std::nothrow_t const &)
172cdf0e10cSrcweir SAL_THROW(())
173cdf0e10cSrcweir {
174cdf0e10cSrcweir try
175cdf0e10cSrcweir {
176cdf0e10cSrcweir return allocate (n, rTraits);
177cdf0e10cSrcweir }
178cdf0e10cSrcweir catch (std::bad_alloc const &)
179cdf0e10cSrcweir {
180cdf0e10cSrcweir return (0);
181cdf0e10cSrcweir }
182cdf0e10cSrcweir }
183cdf0e10cSrcweir
184cdf0e10cSrcweir // =======================================================================
185cdf0e10cSrcweir
deallocate(void * p,AllocatorTraits const & rTraits)186cdf0e10cSrcweir static void deallocate (void * p, AllocatorTraits const & rTraits)
187cdf0e10cSrcweir SAL_THROW(())
188cdf0e10cSrcweir {
189cdf0e10cSrcweir if (p)
190cdf0e10cSrcweir {
191cdf0e10cSrcweir rtl_freeMemory (rTraits.fini(p));
192cdf0e10cSrcweir }
193cdf0e10cSrcweir }
194cdf0e10cSrcweir
195cdf0e10cSrcweir // =======================================================================
196cdf0e10cSrcweir // T * p = new T; delete p;
197cdf0e10cSrcweir // =======================================================================
198cdf0e10cSrcweir
operator new(std::size_t n)199cdf0e10cSrcweir void* SAL_CALL operator new (std::size_t n) throw (std::bad_alloc)
200cdf0e10cSrcweir {
201cdf0e10cSrcweir return allocate (n, ScalarTraits());
202cdf0e10cSrcweir }
203cdf0e10cSrcweir
204cdf0e10cSrcweir // =======================================================================
205cdf0e10cSrcweir
operator delete(void * p)206cdf0e10cSrcweir void SAL_CALL operator delete (void * p) throw ()
207cdf0e10cSrcweir {
208cdf0e10cSrcweir deallocate (p, ScalarTraits());
209cdf0e10cSrcweir }
210cdf0e10cSrcweir
211cdf0e10cSrcweir // =======================================================================
212cdf0e10cSrcweir // T * p = new(nothrow) T; delete(nothrow) p;
213cdf0e10cSrcweir // =======================================================================
214cdf0e10cSrcweir
operator new(std::size_t n,std::nothrow_t const &)215cdf0e10cSrcweir void* SAL_CALL operator new (std::size_t n, std::nothrow_t const &) throw ()
216cdf0e10cSrcweir {
217cdf0e10cSrcweir return allocate (n, ScalarTraits(), nothrow_t());
218cdf0e10cSrcweir }
219cdf0e10cSrcweir
220cdf0e10cSrcweir // =======================================================================
221cdf0e10cSrcweir
operator delete(void * p,std::nothrow_t const &)222cdf0e10cSrcweir void SAL_CALL operator delete (void * p, std::nothrow_t const &) throw ()
223cdf0e10cSrcweir {
224cdf0e10cSrcweir deallocate (p, ScalarTraits());
225cdf0e10cSrcweir }
226cdf0e10cSrcweir
227cdf0e10cSrcweir // =======================================================================
228cdf0e10cSrcweir // T * p = new T[n]; delete[] p;
229cdf0e10cSrcweir // =======================================================================
230cdf0e10cSrcweir
operator new[](std::size_t n)231cdf0e10cSrcweir void* SAL_CALL operator new[] (std::size_t n) throw (std::bad_alloc)
232cdf0e10cSrcweir {
233cdf0e10cSrcweir return allocate (n, VectorTraits());
234cdf0e10cSrcweir }
235cdf0e10cSrcweir
236cdf0e10cSrcweir // =======================================================================
237cdf0e10cSrcweir
operator delete[](void * p)238cdf0e10cSrcweir void SAL_CALL operator delete[] (void * p) throw ()
239cdf0e10cSrcweir {
240cdf0e10cSrcweir deallocate (p, VectorTraits());
241cdf0e10cSrcweir }
242cdf0e10cSrcweir
243cdf0e10cSrcweir // =======================================================================
244cdf0e10cSrcweir // T * p = new(nothrow) T[n]; delete(nothrow)[] p;
245cdf0e10cSrcweir // =======================================================================
246cdf0e10cSrcweir
operator new[](std::size_t n,std::nothrow_t const &)247cdf0e10cSrcweir void* SAL_CALL operator new[] (std::size_t n, std::nothrow_t const &) throw ()
248cdf0e10cSrcweir {
249cdf0e10cSrcweir return allocate (n, VectorTraits(), nothrow_t());
250cdf0e10cSrcweir }
251cdf0e10cSrcweir
252cdf0e10cSrcweir // =======================================================================
253cdf0e10cSrcweir
operator delete[](void * p,std::nothrow_t const &)254cdf0e10cSrcweir void SAL_CALL operator delete[] (void * p, std::nothrow_t const &) throw ()
255cdf0e10cSrcweir {
256cdf0e10cSrcweir deallocate (p, VectorTraits());
257cdf0e10cSrcweir }
258cdf0e10cSrcweir
259cdf0e10cSrcweir // =======================================================================
260