8377
|
1 /* |
|
2 |
|
3 Copyright (C) 2008 Jaroslav Hajek <highegg@gmail.com> |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
|
20 |
|
21 */ |
|
22 |
|
23 #if !defined (octave_local_buffer_h) |
|
24 #define octave_local_buffer_h 1 |
|
25 |
|
26 #include <cstddef> |
|
27 |
|
28 // The default local buffer simply encapsulates an *array* pointer that gets |
|
29 // delete[]d automatically. For common POD types, we provide specializations. |
|
30 |
|
31 template <class T> |
|
32 class octave_local_buffer |
|
33 { |
|
34 public: |
|
35 octave_local_buffer (size_t size) |
|
36 : data (0) |
|
37 { |
|
38 if (size) |
|
39 data = new T[size]; |
|
40 } |
|
41 ~octave_local_buffer (void) { delete [] data; } |
|
42 operator T *() const { return data; } |
|
43 private: |
|
44 T *data; |
|
45 }; |
|
46 |
|
47 |
|
48 // If the compiler supports dynamic stack arrays, we can use the attached hack to |
|
49 // place small buffer arrays on the stack. |
|
50 |
|
51 #ifdef HAVE_DYNAMIC_AUTO_ARRAYS |
|
52 |
|
53 // Maximum buffer size (in bytes) to be placed on the stack. |
|
54 |
|
55 #define OCTAVE_LOCAL_BUFFER_MAX_STACK_SIZE 8192 |
|
56 |
|
57 // If we have automatic arrays, we use an automatic array if the size is small |
|
58 // enough. To avoid possibly evaluating `size' multiple times, we first cache |
|
59 // it. Note that we always construct both the stack array and the |
|
60 // octave_local_buffer object, but only one of them will be nonempty. |
|
61 |
|
62 #define OCTAVE_LOCAL_BUFFER(T, buf, size) \ |
|
63 const size_t _bufsize_ ## buf = size; \ |
|
64 const bool _lbufaut_ ## buf = _bufsize_ ## buf * sizeof (T) \ |
|
65 <= OCTAVE_LOCAL_BUFFER_MAX_STACK_SIZE; \ |
|
66 T _bufaut_ ## buf [_lbufaut_ ## buf ? _bufsize_ ## buf : 0]; \ |
|
67 octave_local_buffer<T> _bufheap_ ## buf (!_lbufaut_ ## buf ? _bufsize_ ## buf : 0); \ |
|
68 T *buf = _lbufaut_ ## buf ? _bufaut_ ## buf : static_cast<T *> (_bufheap_ ## buf); |
|
69 |
|
70 #else |
|
71 |
|
72 // If we don't have automatic arrays, we simply always use octave_local_buffer. |
|
73 |
|
74 #define OCTAVE_LOCAL_BUFFER(T, buf, size) \ |
|
75 octave_local_buffer<T> _buffer_ ## buf (size); \ |
|
76 T *buf = _buffer_ ## buf; |
|
77 |
|
78 #endif |
|
79 |
|
80 #endif |
|
81 |