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 : #ifndef INCLUDED_RTL_ALLOCATOR_HXX
20 : #define INCLUDED_RTL_ALLOCATOR_HXX
21 :
22 : #include <sal/config.h>
23 :
24 : #include <sal/types.h>
25 : #include <rtl/alloc.h>
26 : #include <cstddef>
27 :
28 : /// @cond INTERNAL
29 :
30 :
31 : // This is no general purpose STL allocator but one
32 : // necessary to use STL for some implementation but
33 : // avoid linking sal against the STLPort library!!!
34 : // For more information on when and how to define a
35 : // custom stl allocator have a look at Scott Meyers:
36 : // "Effective STL", Nicolai M. Josuttis:
37 : // "The C++ Standard Library - A Tutorial and Reference"
38 : // and at http://www.josuttis.com/cppcode/allocator.html
39 :
40 : namespace rtl {
41 :
42 : template<class T>
43 : class Allocator
44 : {
45 : public:
46 : typedef T value_type;
47 : typedef T* pointer;
48 : typedef const T* const_pointer;
49 : typedef T& reference;
50 : typedef const T& const_reference;
51 : typedef ::std::size_t size_type;
52 : typedef ::std::ptrdiff_t difference_type;
53 :
54 :
55 : template<class U>
56 : struct rebind
57 : {
58 : typedef Allocator<U> other;
59 : };
60 :
61 :
62 : pointer address (reference value) const
63 : {
64 : return &value;
65 : }
66 :
67 :
68 : const_pointer address (const_reference value) const
69 : {
70 : return &value;
71 : }
72 :
73 :
74 637586 : Allocator() SAL_THROW(())
75 637586 : {}
76 :
77 :
78 : template<class U>
79 425054 : Allocator (SAL_UNUSED_PARAMETER const Allocator<U>&) SAL_THROW(())
80 425054 : {}
81 :
82 :
83 425054 : Allocator(const Allocator&) SAL_THROW(())
84 425054 : {}
85 :
86 :
87 1062635 : ~Allocator() SAL_THROW(())
88 1062635 : {}
89 :
90 :
91 0 : size_type max_size() const SAL_THROW(())
92 : {
93 0 : return size_type(-1)/sizeof(T);
94 : }
95 :
96 :
97 : /* Normally the code for allocate should
98 : throw a std::bad_alloc exception if the
99 : requested memory could not be allocated:
100 : (C++ standard 20.4.1.1):
101 :
102 : pointer allocate (size_type n, const void* hint = 0)
103 : {
104 : pointer p = reinterpret_cast<pointer>(
105 : rtl_allocateMemory(sal_uInt32(n * sizeof(T))));
106 :
107 : if (NULL == p)
108 : throw ::std::bad_alloc();
109 :
110 : return p;
111 : }
112 :
113 : but some compilers do not compile it if exceptions
114 : are not enabled, e.g. GCC under Linux and it is
115 : in general not desired to compile sal with exceptions
116 : enabled. */
117 2125348 : pointer allocate (size_type n, SAL_UNUSED_PARAMETER const void* = 0)
118 : {
119 : return reinterpret_cast<pointer>(
120 2125348 : rtl_allocateMemory(sal_uInt32(n * sizeof(T))));
121 : }
122 :
123 :
124 42519 : void deallocate (pointer p, SAL_UNUSED_PARAMETER size_type /* n */)
125 : {
126 42519 : rtl_freeMemory(p);
127 42519 : }
128 :
129 :
130 : #if HAVE_CXX11_PERFECT_FORWARDING && !defined(_LIBCPP_VERSION)
131 : template< typename... Args >
132 3570535 : void construct (pointer p, Args &&... value)
133 : {
134 3570535 : new ((void*)p)T(std::forward< Args >(value)...);
135 3570535 : }
136 : #else
137 : void construct (pointer p, const T& value)
138 : {
139 : new ((void*)p)T(value);
140 : }
141 : #endif
142 :
143 :
144 42519 : void destroy (pointer p)
145 : {
146 42519 : p->~T();
147 : (void)p; //MSVC2005 annoyingly warns this is unused
148 42519 : }
149 : };
150 :
151 :
152 : // Custom STL allocators must be stateless (see
153 : // references above) that's why the operators below
154 : // return always true or false
155 :
156 : template<class T, class U> inline bool operator ==(
157 : SAL_UNUSED_PARAMETER const Allocator<T>&,
158 : SAL_UNUSED_PARAMETER const Allocator<U>&) SAL_THROW(())
159 : {
160 : return true;
161 : }
162 :
163 : template<class T, class U>
164 : inline bool operator!= (const Allocator<T>&, const Allocator<U>&) SAL_THROW(())
165 : {
166 : return false;
167 : }
168 :
169 : } /* namespace rtl */
170 :
171 : /// @endcond
172 :
173 : #endif /* INCLUDED_RTL_ALLOCATOR_HXX */
174 :
175 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|