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