Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*************************************************************************
3 : *
4 : * Effective License of whole file:
5 : *
6 : * This library is free software; you can redistribute it and/or
7 : * modify it under the terms of the GNU Lesser General Public
8 : * License version 2.1, as published by the Free Software Foundation.
9 : *
10 : * This library is distributed in the hope that it will be useful,
11 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 : * Lesser General Public License for more details.
14 : *
15 : * You should have received a copy of the GNU Lesser General Public
16 : * License along with this library; if not, write to the Free Software
17 : * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
18 : * MA 02111-1307 USA
19 : *
20 : * Parts "Copyright by Sun Microsystems, Inc" prior to August 2011:
21 : *
22 : * The Contents of this file are made available subject to the terms of
23 : * the GNU Lesser General Public License Version 2.1
24 : *
25 : * Copyright: 2002 by Sun Microsystems, Inc.
26 : *
27 : * All parts contributed on or after August 2011:
28 : *
29 : * Version: MPL 1.1 / GPLv3+ / LGPLv2.1+
30 : *
31 : * The contents of this file are subject to the Mozilla Public License Version
32 : * 1.1 (the "License"); you may not use this file except in compliance with
33 : * the License or as specified alternatively below. You may obtain a copy of
34 : * the License at http://www.mozilla.org/MPL/
35 : *
36 : * Software distributed under the License is distributed on an "AS IS" basis,
37 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
38 : * for the specific language governing rights and limitations under the
39 : * License.
40 : *
41 : * Major Contributor(s):
42 : * [ Copyright (C) 2011 Lionel Elie Mamane <lionel@mamane.lu> ]
43 : *
44 : * All Rights Reserved.
45 : *
46 : * For minor contributions see the git repository.
47 : *
48 : * Alternatively, the contents of this file may be used under the terms of
49 : * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
50 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPLv2.1+"),
51 : * in which case the provisions of the GPLv3+ or the LGPLv2.1+ are applicable
52 : * instead of those above.
53 : *
54 : ************************************************************************/
55 :
56 : #ifndef _PQ_ALLOCATOR_
57 : #define _PQ_ALLOCATOR_
58 :
59 : #include <cstddef>
60 : #include "sal/types.h"
61 :
62 : /** jbu: This source has been copied from sal/inc/internal/allocator.hxx,
63 : because it is not a public interface. Thx a lot for figuring this
64 : out.
65 : */
66 :
67 : //######################################################
68 : // This is no general purpose STL allocator but one
69 : // necessary to use STL for some implementation but
70 : // avoid linking sal against the STLPort library!!!
71 : // For more information on when and how to define a
72 : // custom stl allocator have a look at Scott Meyers:
73 : // "Effective STL", Nicolai M. Josuttis:
74 : // "The C++ Standard Library - A Tutorial and Reference"
75 : // and at http://www.josuttis.com/cppcode/allocator.html
76 :
77 : namespace pq_sdbc_driver {
78 :
79 : template<class T>
80 : class Allocator
81 : {
82 : public:
83 : typedef T value_type;
84 : typedef T* pointer;
85 : typedef const T* const_pointer;
86 : typedef T& reference;
87 : typedef const T& const_reference;
88 : typedef ::std::size_t size_type;
89 : typedef ::std::ptrdiff_t difference_type;
90 :
91 : //-----------------------------------------
92 : template<class U>
93 : struct rebind
94 : {
95 : typedef Allocator<U> other;
96 : };
97 :
98 : //-----------------------------------------
99 : pointer address (reference value) const
100 : {
101 : return &value;
102 : }
103 :
104 : //-----------------------------------------
105 : const_pointer address (const_reference value) const
106 : {
107 : return &value;
108 : }
109 :
110 : //-----------------------------------------
111 0 : Allocator() SAL_THROW(())
112 0 : {}
113 :
114 : //-----------------------------------------
115 : template<class U>
116 0 : Allocator (const Allocator<U>&) SAL_THROW(())
117 0 : {}
118 :
119 : //-----------------------------------------
120 0 : Allocator(const Allocator&) SAL_THROW(())
121 0 : {}
122 :
123 : //-----------------------------------------
124 0 : ~Allocator() SAL_THROW(())
125 0 : {}
126 :
127 : //-----------------------------------------
128 0 : size_type max_size() const SAL_THROW(())
129 : {
130 0 : return size_type(-1)/sizeof(T);
131 : }
132 :
133 : //-----------------------------------------
134 : /* Normally the code for allocate should
135 : throw a std::bad_alloc exception if the
136 : requested memory could not be allocated:
137 : (C++ standard 20.4.1.1):
138 :
139 : pointer allocate (size_type n, const void* hint = 0)
140 : {
141 : pointer p = reinterpret_cast<pointer>(
142 : rtl_allocateMemory(sal_uInt32(n * sizeof(T))));
143 :
144 : if (NULL == p)
145 : throw ::std::bad_alloc();
146 :
147 : return p;
148 : }
149 :
150 : but some compilers do not compile it if exceptions
151 : are not enabled, e.g. GCC under Linux and it is
152 : in general not desired to compile sal with exceptions
153 : enabled. */
154 0 : pointer allocate (size_type n, SAL_UNUSED_PARAMETER const void* = 0)
155 : {
156 : return reinterpret_cast<pointer>(
157 0 : rtl_allocateMemory(sal_uInt32(n * sizeof(T))));
158 : }
159 :
160 : //-----------------------------------------
161 0 : void deallocate (pointer p, SAL_UNUSED_PARAMETER size_type)
162 : {
163 0 : rtl_freeMemory(p);
164 0 : }
165 :
166 : //-----------------------------------------
167 0 : void construct (pointer p, const T& value)
168 : {
169 0 : new ((void*)p)T(value);
170 0 : }
171 :
172 : // LEM: GNU libstdc++ vectors expect this one to exist,
173 : // at least if one intends to create vectors by giving
174 : // only a size and no initialising value.
175 : //-----------------------------------------
176 0 : void construct (pointer p)
177 : {
178 0 : new ((void*)p)T;
179 0 : }
180 :
181 : //-----------------------------------------
182 0 : void destroy (pointer p)
183 : {
184 0 : p->~T();
185 : #ifdef _MSC_VER
186 : (void) p; // spurious warning C4100: 'p': unreferenced formal parameter
187 : #endif
188 0 : }
189 : };
190 :
191 : //######################################################
192 : // Custom STL allocators must be stateless (see
193 : // references above) that's why the operators below
194 : // return always true or false
195 : template<class T, class U>
196 0 : inline bool operator== (const Allocator<T>&, const Allocator<U>&) SAL_THROW(())
197 : {
198 0 : return true;
199 : }
200 :
201 : template<class T, class U>
202 : inline bool operator!= (const Allocator<T>&, const Allocator<U>&) SAL_THROW(())
203 : {
204 : return false;
205 : }
206 :
207 : } /* namespace sal */
208 :
209 : //######################################################
210 : /* REQUIRED BY STLPort (see stlport '_alloc.h'):
211 : Hack for compilers that do not support member
212 : template classes (e.g. MSVC 6) */
213 : template<class T, class U>
214 : inline pq_sdbc_driver::Allocator<U> & __stl_alloc_rebind (
215 : pq_sdbc_driver::Allocator<T> & a, U const *)
216 : {
217 : return (pq_sdbc_driver::Allocator<U>&)(a);
218 : }
219 :
220 : #endif /* _PQ_ALLOCATOR_ */
|