OSDN Git Service

5c971e6208a80df79455c87735f19e0fc68e4a2b
[pf3gnuchains/gcc-fork.git] / libstdc++ / stl / stl_tempbuf.h
1 /*
2  *
3  * Copyright (c) 1994
4  * Hewlett-Packard Company
5  *
6  * Permission to use, copy, modify, distribute and sell this software
7  * and its documentation for any purpose is hereby granted without fee,
8  * provided that the above copyright notice appear in all copies and
9  * that both that copyright notice and this permission notice appear
10  * in supporting documentation.  Hewlett-Packard Company makes no
11  * representations about the suitability of this software for any
12  * purpose.  It is provided "as is" without express or implied warranty.
13  *
14  *
15  * Copyright (c) 1996,1997
16  * Silicon Graphics Computer Systems, Inc.
17  *
18  * Permission to use, copy, modify, distribute and sell this software
19  * and its documentation for any purpose is hereby granted without fee,
20  * provided that the above copyright notice appear in all copies and
21  * that both that copyright notice and this permission notice appear
22  * in supporting documentation.  Silicon Graphics makes no
23  * representations about the suitability of this software for any
24  * purpose.  It is provided "as is" without express or implied warranty.
25  */
26
27 /* NOTE: This is an internal header file, included by other STL headers.
28  *   You should not attempt to use it directly.
29  */
30
31 #ifndef __SGI_STL_INTERNAL_TEMPBUF_H
32 #define __SGI_STL_INTERNAL_TEMPBUF_H
33
34
35 __STL_BEGIN_NAMESPACE
36
37 template <class T>
38 pair<T*, ptrdiff_t> get_temporary_buffer(ptrdiff_t len, T*) {
39   if (len > ptrdiff_t(INT_MAX / sizeof(T)))
40     len = INT_MAX / sizeof(T);
41
42   while (len > 0) {
43     T* tmp = (T*) malloc((size_t)len * sizeof(T));
44     if (tmp != 0)
45       return pair<T*, ptrdiff_t>(tmp, len);
46     len /= 2;
47   }
48
49   return pair<T*, ptrdiff_t>((T*)0, 0);
50 }
51
52 template <class T>
53 void return_temporary_buffer(T* p) {
54   free(p);
55 }
56
57 template <class ForwardIterator,
58           class T 
59 #ifdef __STL_CLASS_PARTIAL_SPECIALIZATION
60                   = iterator_traits<ForwardIterator>::value_type 
61 #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
62          >
63 class temporary_buffer {
64 private:
65   ptrdiff_t original_len;
66   ptrdiff_t len;
67   T* buffer;
68
69   void allocate_buffer() {
70     original_len = len;
71     buffer = 0;
72
73     if (len > (ptrdiff_t)(INT_MAX / sizeof(T)))
74       len = INT_MAX / sizeof(T);
75
76     while (len > 0) {
77       buffer = (T*) malloc(len * sizeof(T));
78       if (buffer)
79         break;
80       len /= 2;
81     }
82   }
83
84   void initialize_buffer(const T&, __true_type) {}
85   void initialize_buffer(const T& val, __false_type) {
86     uninitialized_fill_n(buffer, len, val);
87   }
88
89 public:
90   ptrdiff_t size() const { return len; }
91   ptrdiff_t requested_size() const { return original_len; }
92   T* begin() { return buffer; }
93   T* end() { return buffer + len; }
94
95   temporary_buffer(ForwardIterator first, ForwardIterator last) {
96     __STL_TRY {
97       len = 0;
98       distance(first, last, len);
99       allocate_buffer();
100       if (len > 0)
101         initialize_buffer(*first,
102                           __type_traits<T>::has_trivial_default_constructor());
103     }
104     __STL_UNWIND(free(buffer); buffer = 0; len = 0);
105   }
106  
107   ~temporary_buffer() {  
108     destroy(buffer, buffer + len);
109     free(buffer);
110   }
111
112 private:
113   temporary_buffer(const temporary_buffer&) {}
114   void operator=(const temporary_buffer&) {}
115 };
116
117 __STL_END_NAMESPACE
118
119 #endif /* __SGI_STL_INTERNAL_TEMPBUF_H */
120
121 // Local Variables:
122 // mode:C++
123 // End: