Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #if !defined (octave_local_buffer_h)
00024 #define octave_local_buffer_h 1
00025
00026 #include <cstddef>
00027 #include "oct-cmplx.h"
00028
00029
00030
00031
00032
00033 template <class T>
00034 class octave_local_buffer
00035 {
00036 public:
00037 octave_local_buffer (size_t size)
00038 : data (0)
00039 {
00040 if (size)
00041 data = new T[size];
00042 }
00043 ~octave_local_buffer (void) { delete [] data; }
00044 operator T *() const { return data; }
00045
00046 private:
00047 T *data;
00048
00049
00050 octave_local_buffer (const octave_local_buffer&);
00051 octave_local_buffer& operator = (const octave_local_buffer&);
00052 };
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 class octave_chunk_buffer
00068 {
00069 public:
00070
00071 OCTAVE_API octave_chunk_buffer (size_t size);
00072
00073 OCTAVE_API virtual ~octave_chunk_buffer (void);
00074
00075 char *data (void) const { return dat; }
00076
00077 static OCTAVE_API void clear (void);
00078
00079 private:
00080
00081
00082
00083 static const size_t chunk_size;
00084
00085
00086 static char *top;
00087
00088
00089 static char *chunk;
00090
00091
00092 static size_t left;
00093
00094
00095 static size_t active;
00096
00097
00098 char *cnk;
00099
00100
00101 char *dat;
00102
00103
00104 octave_chunk_buffer (const octave_chunk_buffer&);
00105 octave_chunk_buffer& operator = (const octave_chunk_buffer&);
00106 };
00107
00108
00109
00110 #define SPECIALIZE_POD_BUFFER(TYPE) \
00111 template <> \
00112 class octave_local_buffer<TYPE> : private octave_chunk_buffer \
00113 { \
00114 public: \
00115 octave_local_buffer (size_t size) \
00116 : octave_chunk_buffer (size * sizeof (TYPE)) { } \
00117 \
00118 operator TYPE *() const \
00119 { \
00120 return reinterpret_cast<TYPE *> (this->data ()); \
00121 } \
00122 }
00123
00124 SPECIALIZE_POD_BUFFER (bool);
00125 SPECIALIZE_POD_BUFFER (char);
00126 SPECIALIZE_POD_BUFFER (unsigned short);
00127 SPECIALIZE_POD_BUFFER (short);
00128 SPECIALIZE_POD_BUFFER (int);
00129 SPECIALIZE_POD_BUFFER (unsigned int);
00130 SPECIALIZE_POD_BUFFER (long);
00131 SPECIALIZE_POD_BUFFER (unsigned long);
00132 SPECIALIZE_POD_BUFFER (float);
00133 SPECIALIZE_POD_BUFFER (double);
00134
00135 SPECIALIZE_POD_BUFFER (Complex);
00136 SPECIALIZE_POD_BUFFER (FloatComplex);
00137
00138
00139
00140 template <class T>
00141 class octave_local_buffer<T *> : private octave_chunk_buffer
00142 {
00143 public:
00144 octave_local_buffer (size_t size)
00145 : octave_chunk_buffer (size * sizeof (T *))
00146 { }
00147
00148 operator T **() const { return reinterpret_cast<T **> (this->data ()); }
00149 };
00150
00151 template <class T>
00152 class octave_local_buffer<const T *> : private octave_chunk_buffer
00153 {
00154 public:
00155 octave_local_buffer (size_t size)
00156 : octave_chunk_buffer (size * sizeof (const T *))
00157 { }
00158
00159 operator const T **() const
00160 {
00161 return reinterpret_cast<const T **> (this->data ());
00162 }
00163 };
00164
00165
00166
00167
00168
00169
00170 #if 0 // defined (HAVE_DYNAMIC_AUTO_ARRAYS)
00171
00172
00173
00174 #define OCTAVE_LOCAL_BUFFER_MAX_STACK_SIZE 8192
00175
00176
00177
00178
00179
00180
00181
00182 #define OCTAVE_LOCAL_BUFFER(T, buf, size) \
00183 const size_t _bufsize_ ## buf = size; \
00184 const bool _lbufaut_ ## buf = _bufsize_ ## buf * sizeof (T) \
00185 <= OCTAVE_LOCAL_BUFFER_MAX_STACK_SIZE; \
00186 T _bufaut_ ## buf [_lbufaut_ ## buf ? _bufsize_ ## buf : 0]; \
00187 octave_local_buffer<T> _bufheap_ ## buf \
00188 (!_lbufaut_ ## buf ? _bufsize_ ## buf : 0); \
00189 T *buf = _lbufaut_ ## buf \
00190 ? _bufaut_ ## buf : static_cast<T *> (_bufheap_ ## buf)
00191
00192 #else
00193
00194
00195
00196
00197 #define OCTAVE_LOCAL_BUFFER(T, buf, size) \
00198 octave_local_buffer<T> _buffer_ ## buf (size); \
00199 T *buf = _buffer_ ## buf
00200
00201 #endif
00202
00203
00204
00205
00206 #define OCTAVE_LOCAL_BUFFER_INIT(T, buf, size, value) \
00207 OCTAVE_LOCAL_BUFFER(T, buf, size); \
00208 for (size_t _buf_iter = 0, _buf_size = size; \
00209 _buf_iter < _buf_size; _buf_iter++) \
00210 buf[_buf_iter] = value
00211
00212 #endif
00213