summaryrefslogtreecommitdiff
path: root/misc/uClibc++/include/uClibc++/memory
blob: f01f9771708941ba0fabeb175d180ea623ac2fea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*  Copyright (C) 2004 Garrett A. Kajmowicz
 *
 * This file is part of the uClibc++ Library.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <new>
#include <cstddef>
#include <cstdlib>
#include <iterator_base>
#include <utility>
#include <cstdio>

#ifndef HEADER_STD_MEMORY
#define HEADER_STD_MEMORY 1

#pragma GCC visibility push(default)

extern "C++"
{

namespace std
{

template <class T> class allocator;
  // Specialize for void:

template <> class _UCXXEXPORT allocator<void>
{
public:
  typedef void*       pointer;
  typedef const void* const_pointer;
  typedef void  value_type;
  template <class U> struct rebind { typedef allocator<U> other; };
};

template <class T> class _UCXXEXPORT allocator
{
public:
  typedef T value_type;
  typedef size_t size_type;
  typedef ptrdiff_t difference_type;

  typedef T* pointer;
  typedef const T* const_pointer;

  typedef T& reference;
  typedef const T& const_reference;

  pointer address(reference r) const { return &r; }
  const_pointer address(const_reference r) const { return &r; }

  allocator() throw(){}
  template <class U> allocator(const allocator<U>& ) throw();
  ~allocator() throw(){}

  // Space for n Ts

  pointer allocate(size_type n, typename allocator<void>::const_pointer = 0)
  {
    return (T*)(::operator new( n * sizeof(T) ));
  }

  void deallocate(pointer p, size_type)
  {
    ::operator delete(p);
  }

  // Use placement new to engage the constructor

  void construct(pointer p, const T& val) { new((void*)p) T(val); }
  void destroy(pointer p){ ((T*)p)->~T(); }  //Call destructor

  size_type max_size() const throw();
  template<class U> struct rebind { typedef allocator<U> other; };
};

template <class Out, class T> class _UCXXEXPORT raw_storage_iterator
  : public iterator<output_iterator_tag, void, void, void, void>
{
    Out p;

public:
  explicit raw_storage_iterator(Out pp) : p (pp) {  }
  raw_storage_iterator & operator*() { return *this; }

  raw_storage_iterator & operator=(const T& val)
  {
    T* pp = &*p;
    new(pp) T(val);
    return *this;
  }

  raw_storage_iterator & operator++() { ++p; return *this; }

  raw_storage_iterator operator++(int)
  {
    raw_storage_iterator t = *this;
    ++p;
    return t;
  }
};

template <class T> _UCXXEXPORT pair<T*, ptrdiff_t> get_temporary_buffer(ptrdiff_t n)
{
  pair<T*, ptrdiff_t> retval;
  retval.first = static_cast<T*>(malloc(n * sizeof(T)));
  if (retval.first == 0)
  {
    retval.second = 0;
  }
  else
  {
    retval.second = n;
  }

  return retval;
}

template <class T> _UCXXEXPORT void return_temporary_buffer(T* p)
{
  free(p);
}


template <class T> class _UCXXEXPORT auto_ptr
{
private:
  T * object;
  template <class Y> struct auto_ptr_ref
  {
    Y * p;
  };

public:

  typedef T element_type;

  explicit auto_ptr(T* p =0) throw() : object(p){  }
  auto_ptr(auto_ptr& p) throw() : object(p.release()){ }

  auto_ptr(auto_ptr_ref<T> r) throw() : object(r.p)
  {
    r.p = 0;
  }

  template<class Y> auto_ptr(auto_ptr<Y>& p) throw() : object(p.release()){ }

  auto_ptr& operator=(auto_ptr& p) throw()
  {
    if (&p == this)
    {
      return *this;
    }

    delete object;
    object = p.release();
    return *this;
  }

  template<class Y> auto_ptr& operator=(auto_ptr<Y>& p) throw()
  {
    if (&p == this)
    {
      return *this;
    }

    delete object;
    object = p.release();
    return *this;
  }

  ~auto_ptr()
  {
    delete object;
  }

  T& operator*() const throw()
  {
    return *object;
  }

  T* operator->() const throw()
  {
    return object;
  }

  T* get() const throw()
  {
    return object;
  }

  T* release() throw()
  {
    T * temp(object);
    object = 0;
    return temp;
  }

  void reset(T * p=0) throw()
  {
    if(p != object)
    {
      delete object;
      object = p;
    }
  }

  template<class Y> operator auto_ptr_ref<Y>() throw()
  {
    auto_ptr_ref<Y> retval;
    retval.p = object;
    object = 0;
    return retval;
  }

  template<class Y> operator auto_ptr<Y>() throw()
  {
    auto_ptr<Y> retval(object);
    object = 0;
    return retval;
  }
};

} // namespace std
} // extern "C++"

#pragma GCC visibility pop

#endif