Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : : #ifndef INCLUDED_RTL_ALLOCATOR_HXX
29 : : #define INCLUDED_RTL_ALLOCATOR_HXX
30 : :
31 : : #include "sal/types.h"
32 : : #include "rtl/alloc.h"
33 : : #include <cstddef>
34 : :
35 : : /// @cond INTERNAL
36 : :
37 : : //######################################################
38 : : // This is no general purpose STL allocator but one
39 : : // necessary to use STL for some implementation but
40 : : // avoid linking sal against the STLPort library!!!
41 : : // For more information on when and how to define a
42 : : // custom stl allocator have a look at Scott Meyers:
43 : : // "Effective STL", Nicolai M. Josuttis:
44 : : // "The C++ Standard Library - A Tutorial and Reference"
45 : : // and at http://www.josuttis.com/cppcode/allocator.html
46 : :
47 : : namespace rtl {
48 : :
49 : : template<class T>
50 : : class Allocator
51 : : {
52 : : public:
53 : : typedef T value_type;
54 : : typedef T* pointer;
55 : : typedef const T* const_pointer;
56 : : typedef T& reference;
57 : : typedef const T& const_reference;
58 : : typedef ::std::size_t size_type;
59 : : typedef ::std::ptrdiff_t difference_type;
60 : :
61 : : //-----------------------------------------
62 : : template<class U>
63 : : struct rebind
64 : : {
65 : : typedef Allocator<U> other;
66 : : };
67 : :
68 : : //-----------------------------------------
69 : : pointer address (reference value) const
70 : : {
71 : : return &value;
72 : : }
73 : :
74 : : //-----------------------------------------
75 : : const_pointer address (const_reference value) const
76 : : {
77 : : return &value;
78 : : }
79 : :
80 : : //-----------------------------------------
81 : 2266 : Allocator() SAL_THROW(())
82 : 2266 : {}
83 : :
84 : : //-----------------------------------------
85 : : template<class U>
86 : 4454 : Allocator (SAL_UNUSED_PARAMETER const Allocator<U>&) SAL_THROW(())
87 : 4454 : {}
88 : :
89 : : //-----------------------------------------
90 : 3027 : Allocator(const Allocator&) SAL_THROW(())
91 : 3027 : {}
92 : :
93 : : //-----------------------------------------
94 : 9747 : ~Allocator() SAL_THROW(())
95 : 9747 : {}
96 : :
97 : : //-----------------------------------------
98 : 530 : size_type max_size() const SAL_THROW(())
99 : : {
100 : 530 : return size_type(-1)/sizeof(T);
101 : : }
102 : :
103 : : //-----------------------------------------
104 : : /* Normally the code for allocate should
105 : : throw a std::bad_alloc exception if the
106 : : requested memory could not be allocated:
107 : : (C++ standard 20.4.1.1):
108 : :
109 : : pointer allocate (size_type n, const void* hint = 0)
110 : : {
111 : : pointer p = reinterpret_cast<pointer>(
112 : : rtl_allocateMemory(sal_uInt32(n * sizeof(T))));
113 : :
114 : : if (NULL == p)
115 : : throw ::std::bad_alloc();
116 : :
117 : : return p;
118 : : }
119 : :
120 : : but some compilers do not compile it if exceptions
121 : : are not enabled, e.g. GCC under Linux and it is
122 : : in general not desired to compile sal with exceptions
123 : : enabled. */
124 : 6454 : pointer allocate (size_type n, SAL_UNUSED_PARAMETER const void* = 0)
125 : : {
126 : : return reinterpret_cast<pointer>(
127 : 6454 : rtl_allocateMemory(sal_uInt32(n * sizeof(T))));
128 : : }
129 : :
130 : : //-----------------------------------------
131 : 6454 : void deallocate (pointer p, SAL_UNUSED_PARAMETER size_type /* n */)
132 : : {
133 : 6454 : rtl_freeMemory(p);
134 : 6454 : }
135 : :
136 : : //-----------------------------------------
137 : 55014 : void construct (pointer p, const T& value)
138 : : {
139 [ + - ][ + - ]: 55014 : new ((void*)p)T(value);
[ + - ][ + - ]
[ # # ][ + - ]
140 : 55014 : }
141 : :
142 : : //-----------------------------------------
143 : 55014 : void destroy (pointer p)
144 : : {
145 : 33619 : p->~T();
146 : : (void)p; //MSVC2005 annoyingly warns this is unused
147 : 55014 : }
148 : : };
149 : :
150 : : //######################################################
151 : : // Custom STL allocators must be stateless (see
152 : : // references above) that's why the operators below
153 : : // return always true or false
154 : :
155 : : template<class T, class U> inline bool operator ==(
156 : : SAL_UNUSED_PARAMETER const Allocator<T>&,
157 : : SAL_UNUSED_PARAMETER const Allocator<U>&) SAL_THROW(())
158 : : {
159 : : return true;
160 : : }
161 : :
162 : : template<class T, class U>
163 : : inline bool operator!= (const Allocator<T>&, const Allocator<U>&) SAL_THROW(())
164 : : {
165 : : return false;
166 : : }
167 : :
168 : : } /* namespace rtl */
169 : :
170 : : /** REQUIRED BY STLPort (see stlport '_alloc.h'):
171 : : Hack for compilers that do not support member
172 : : template classes (e.g. MSVC 6)
173 : : */
174 : : namespace _STL
175 : : {
176 : : template<class T, class U>
177 : : inline ::rtl::Allocator<U> & __stl_alloc_rebind (::rtl::Allocator<T> & a, U const *)
178 : : {
179 : : return (::rtl::Allocator<U>&)(a);
180 : : }
181 : : }
182 : :
183 : : /// @endcond
184 : :
185 : : #endif /* INCLUDED_RTL_ALLOCATOR_HXX */
186 : :
187 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|