/elec/propeller-clock

To get this branch, use:
bzr branch http://bzr.ed.am/elec/propeller-clock

« back to all changes in this revision

Viewing changes to src/utility/memory

  • Committer: edam
  • Date: 2012-02-25 01:31:43 UTC
  • Revision ID: tim@ed.am-20120225013143-9fet2y2d3fjlrwez
added ulibc

Show diffs side-by-side

added added

removed removed

 
1
/*      Copyright (C) 2004 Garrett A. Kajmowicz
 
2
 
 
3
        This file is part of the uClibc++ Library.
 
4
 
 
5
        This library is free software; you can redistribute it and/or
 
6
        modify it under the terms of the GNU Lesser General Public
 
7
        License as published by the Free Software Foundation; either
 
8
        version 2.1 of the License, or (at your option) any later version.
 
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, MA  02111-1307  USA
 
18
*/
 
19
 
 
20
#include <new>
 
21
#include <cstddef>
 
22
#include <cstdlib>
 
23
#include <iterator_base>
 
24
#include <utility.h>
 
25
#include <cstdio>
 
26
 
 
27
#ifndef HEADER_STD_MEMORY
 
28
#define HEADER_STD_MEMORY 1
 
29
 
 
30
#pragma GCC visibility push(default)
 
31
 
 
32
namespace std{
 
33
 
 
34
template <class T> class allocator;
 
35
        // Specialize for void:
 
36
 
 
37
template <> class _UCXXEXPORT allocator<void> {
 
38
public:
 
39
        typedef void*       pointer;
 
40
        typedef const void* const_pointer;
 
41
        typedef void  value_type;
 
42
        template <class U> struct rebind { typedef allocator<U> other; };
 
43
};
 
44
 
 
45
template <class T> class _UCXXEXPORT allocator{
 
46
public:
 
47
        typedef T value_type;
 
48
        typedef size_t size_type;
 
49
        typedef ptrdiff_t difference_type;
 
50
        
 
51
        typedef T* pointer;
 
52
        typedef const T* const_pointer;
 
53
 
 
54
        typedef T& reference;
 
55
        typedef const T& const_reference;
 
56
 
 
57
        pointer address(reference r) const { return &r; }
 
58
        const_pointer address(const_reference r) const { return &r; }
 
59
        
 
60
        allocator() throw(){}
 
61
        template <class U> allocator(const allocator<U>& ) throw();
 
62
        ~allocator() throw(){}
 
63
 
 
64
        //Space for n Ts
 
65
        pointer allocate(size_type n, typename allocator<void>::const_pointer = 0){
 
66
                return (T*)(::operator new( n * sizeof(T) ));
 
67
        }
 
68
        void deallocate(pointer p, size_type){
 
69
                ::operator delete(p);
 
70
        }
 
71
 
 
72
        //Use placement new to engage the constructor
 
73
        void construct(pointer p, const T& val) { new((void*)p) T(val); }
 
74
        void destroy(pointer p){ ((T*)p)->~T(); }       //Call destructor
 
75
 
 
76
        size_type max_size() const throw();
 
77
        template<class U> struct rebind { typedef allocator<U> other; };
 
78
 
 
79
};
 
80
 
 
81
template <class Out, class T> class _UCXXEXPORT raw_storage_iterator
 
82
        : public iterator<output_iterator_tag, void, void, void, void>
 
83
{
 
84
                Out p;
 
85
 
 
86
public:
 
87
        explicit raw_storage_iterator(Out pp) : p (pp) {  }
 
88
        raw_storage_iterator & operator*() { return *this; }
 
89
        raw_storage_iterator & operator=(const T& val) {
 
90
                T* pp = &*p;
 
91
                new(pp) T(val);
 
92
                return *this;
 
93
        }
 
94
 
 
95
        raw_storage_iterator & operator++() { ++p; return *this; }
 
96
        raw_storage_iterator operator++(int) {
 
97
                raw_storage_iterator t = *this;
 
98
                ++p;
 
99
                return t;
 
100
        }
 
101
};
 
102
 
 
103
template <class T> _UCXXEXPORT pair<T*, ptrdiff_t> get_temporary_buffer(ptrdiff_t n){
 
104
        pair<T*, ptrdiff_t> retval;
 
105
        retval.first = static_cast<T*>(malloc(n * sizeof(T)));
 
106
        if(retval.first == 0){
 
107
                retval.second = 0;
 
108
        }else{
 
109
                retval.second = n;
 
110
        }
 
111
        return retval;
 
112
}
 
113
 
 
114
template <class T> _UCXXEXPORT void return_temporary_buffer(T* p){
 
115
        free(p);
 
116
}
 
117
 
 
118
 
 
119
template <class T> class _UCXXEXPORT auto_ptr{
 
120
 
 
121
private:
 
122
        T * object;
 
123
        template <class Y> struct auto_ptr_ref{
 
124
                Y * p;
 
125
        };
 
126
 
 
127
public:
 
128
 
 
129
        typedef T element_type;
 
130
 
 
131
        explicit auto_ptr(T* p =0) throw() : object(p){  }
 
132
        auto_ptr(auto_ptr& p) throw() : object(p.release()){ }
 
133
        auto_ptr(auto_ptr_ref<T> r) throw() : object(r.p){
 
134
                r.p = 0;
 
135
        }
 
136
        template<class Y> auto_ptr(auto_ptr<Y>& p) throw() : object(p.release()){ }
 
137
        auto_ptr& operator=(auto_ptr& p) throw(){
 
138
                if(&p == this){
 
139
                        return *this;
 
140
                }
 
141
                delete object;
 
142
                object = p.release();
 
143
                return *this;
 
144
        }
 
145
        template<class Y> auto_ptr& operator=(auto_ptr<Y>& p) throw(){
 
146
                if(&p == this){
 
147
                        return *this;
 
148
                }
 
149
                delete object;
 
150
                object = p.release();
 
151
                return *this;
 
152
        }
 
153
        ~auto_ptr(){
 
154
                delete object;
 
155
        }
 
156
 
 
157
        T& operator*() const throw(){
 
158
                return *object;
 
159
        }
 
160
        T* operator->() const throw(){
 
161
                return object;
 
162
        }
 
163
        T* get() const throw(){
 
164
                return object;
 
165
        }
 
166
        T* release() throw(){
 
167
                T * temp(object);
 
168
                object = 0;
 
169
                return temp;
 
170
        }
 
171
        void reset(T * p=0) throw(){
 
172
                if(p != object){
 
173
                        delete object;
 
174
                        object = p;
 
175
                }
 
176
        }
 
177
        template<class Y> operator auto_ptr_ref<Y>() throw(){
 
178
                auto_ptr_ref<Y> retval;
 
179
                retval.p = object;
 
180
                object = 0;
 
181
                return retval;
 
182
        }
 
183
        template<class Y> operator auto_ptr<Y>() throw(){
 
184
                auto_ptr<Y> retval(object);
 
185
                object = 0;
 
186
                return retval;
 
187
        }
 
188
        
 
189
};
 
190
 
 
191
}       //namespace std
 
192
 
 
193
#pragma GCC visibility pop
 
194
 
 
195
#endif
 
196