Mercurial > hg > octave-nkf
annotate liboctave/oct-mem.h @ 14626:f947d2922feb stable rc-3-6-2-0
3.6.2-rc0 release candidate
* configure.ac (AC_INIT): Version is now 3.6.2-rc0.
(OCTAVE_RELEASE_DATE): Now 2012-05-11.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 11 May 2012 13:46:18 -0400 |
parents | 72c96de7a403 |
children | 460a3c6d8bf1 |
rev | line source |
---|---|
9776
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
1 /* |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
2 |
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
3 Copyright (C) 2009-2012 VZLU Prague |
9776
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
4 |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
5 This file is part of Octave. |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
6 |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
7 Octave is free software; you can redistribute it and/or modify it |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
8 under the terms of the GNU General Public License as published by the |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
9 Free Software Foundation; either version 3 of the License, or (at your |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
10 option) any later version. |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
11 |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
15 for more details. |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
16 |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
17 You should have received a copy of the GNU General Public License |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
18 along with Octave; see the file COPYING. If not, see |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
19 <http://www.gnu.org/licenses/>. |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
20 |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
21 */ |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
22 |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
23 #if !defined (octave_memory_h) |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
24 #define octave_memory_h 1 |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
25 |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
26 #include <cstddef> |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
27 #include <cstring> |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
28 #include <algorithm> |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
29 |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
30 #include "oct-cmplx.h" |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
31 #include "oct-inttypes.h" |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
32 |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
33 // NOTE: These functions are used to optimize stuff where performance is a |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
34 // priority. They assume that the std::complex and octave_int can be |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
35 // manipulated as plain memory, an assumption that is always true in practice |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
36 // but not theoretically guaranteed by the C++ standard. In the future, C++ may |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
37 // provide a better way to accomplish these tasks. |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
38 |
10359
ec05728ce7f0
guard against >2GB allocations with 32-bit ptrs on weird platforms
Jaroslav Hajek <highegg@gmail.com>
parents:
10358
diff
changeset
|
39 inline size_t safe_size_comp (size_t n, size_t size) |
ec05728ce7f0
guard against >2GB allocations with 32-bit ptrs on weird platforms
Jaroslav Hajek <highegg@gmail.com>
parents:
10358
diff
changeset
|
40 { |
ec05728ce7f0
guard against >2GB allocations with 32-bit ptrs on weird platforms
Jaroslav Hajek <highegg@gmail.com>
parents:
10358
diff
changeset
|
41 if (n > static_cast<size_t> (-1) / size) |
ec05728ce7f0
guard against >2GB allocations with 32-bit ptrs on weird platforms
Jaroslav Hajek <highegg@gmail.com>
parents:
10358
diff
changeset
|
42 throw std::bad_alloc (); |
ec05728ce7f0
guard against >2GB allocations with 32-bit ptrs on weird platforms
Jaroslav Hajek <highegg@gmail.com>
parents:
10358
diff
changeset
|
43 return n * size; |
ec05728ce7f0
guard against >2GB allocations with 32-bit ptrs on weird platforms
Jaroslav Hajek <highegg@gmail.com>
parents:
10358
diff
changeset
|
44 } |
ec05728ce7f0
guard against >2GB allocations with 32-bit ptrs on weird platforms
Jaroslav Hajek <highegg@gmail.com>
parents:
10358
diff
changeset
|
45 |
9776
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
46 // Unaliased copy. This boils down to memcpy, even for octave_int and complex types. |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
47 |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
48 template <class T> |
10358
72fab01e5d68
improve some size_t queries
Jaroslav Hajek <highegg@gmail.com>
parents:
9780
diff
changeset
|
49 inline void copy_or_memcpy (size_t n, const T *src, T *dest) |
9776
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
50 { std::copy (src, src + n, dest); } |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
51 |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
52 #define DEFINE_POD_UCOPY(T) \ |
10358
72fab01e5d68
improve some size_t queries
Jaroslav Hajek <highegg@gmail.com>
parents:
9780
diff
changeset
|
53 inline void copy_or_memcpy (size_t n, const T *src, T *dest) \ |
9776
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
54 { std::memcpy (dest, src, n * sizeof (T)); } |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
55 |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
56 DEFINE_POD_UCOPY (double) |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
57 DEFINE_POD_UCOPY (float) |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
58 DEFINE_POD_UCOPY (char) |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
59 DEFINE_POD_UCOPY (short) |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
60 DEFINE_POD_UCOPY (int) |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
61 DEFINE_POD_UCOPY (long) |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
62 DEFINE_POD_UCOPY (unsigned char) |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
63 DEFINE_POD_UCOPY (unsigned short) |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
64 DEFINE_POD_UCOPY (unsigned int) |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
65 DEFINE_POD_UCOPY (unsigned long) |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
66 |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
67 DEFINE_POD_UCOPY (Complex) |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
68 DEFINE_POD_UCOPY (FloatComplex) |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
69 |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
70 template <class T> |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
71 DEFINE_POD_UCOPY (octave_int<T>) |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
72 |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
73 // Fill by value, with a check for zero. This boils down to memset if value is |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
74 // a POD zero. |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
75 template <class T> |
10358
72fab01e5d68
improve some size_t queries
Jaroslav Hajek <highegg@gmail.com>
parents:
9780
diff
changeset
|
76 inline void fill_or_memset (size_t n, const T& value, T *dest) |
9776
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
77 { std::fill_n (dest, n, value); } |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
78 |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
79 template <class T> |
9780
6dafc60dde31
rename oct-mem functions
Jaroslav Hajek <highegg@gmail.com>
parents:
9776
diff
changeset
|
80 inline bool helper_is_zero_mem (const T& value) |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
81 { |
9780
6dafc60dde31
rename oct-mem functions
Jaroslav Hajek <highegg@gmail.com>
parents:
9776
diff
changeset
|
82 typedef typename query_integer_type<sizeof (T), false>::type IT; // get integer type of the same size. |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
83 return *(reinterpret_cast<const IT *>(&value)) == 0; |
9780
6dafc60dde31
rename oct-mem functions
Jaroslav Hajek <highegg@gmail.com>
parents:
9776
diff
changeset
|
84 } |
9776
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
85 |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
86 template <class T> |
9780
6dafc60dde31
rename oct-mem functions
Jaroslav Hajek <highegg@gmail.com>
parents:
9776
diff
changeset
|
87 inline bool helper_is_zero_mem (const std::complex<T>& value) |
6dafc60dde31
rename oct-mem functions
Jaroslav Hajek <highegg@gmail.com>
parents:
9776
diff
changeset
|
88 { |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
89 return (helper_is_zero_mem (value.real ()) |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
90 && helper_is_zero_mem (value.imag ())); |
9780
6dafc60dde31
rename oct-mem functions
Jaroslav Hajek <highegg@gmail.com>
parents:
9776
diff
changeset
|
91 } |
9776
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
92 |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
93 template <class T> |
9780
6dafc60dde31
rename oct-mem functions
Jaroslav Hajek <highegg@gmail.com>
parents:
9776
diff
changeset
|
94 inline bool helper_is_zero_mem (const octave_int<T>& value) |
9776
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
95 { return value.value () == T(); } |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
96 |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
97 #define DEFINE_POD_FILL(T) \ |
10358
72fab01e5d68
improve some size_t queries
Jaroslav Hajek <highegg@gmail.com>
parents:
9780
diff
changeset
|
98 inline void fill_or_memset (size_t n, const T& value, T *dest) \ |
9776
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
99 { \ |
9780
6dafc60dde31
rename oct-mem functions
Jaroslav Hajek <highegg@gmail.com>
parents:
9776
diff
changeset
|
100 if (helper_is_zero_mem (value)) \ |
9776
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
101 std::memset (dest, 0, n * sizeof (T)); \ |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
102 else \ |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
103 std::fill_n (dest, n, value); \ |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
104 } |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
105 |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
106 DEFINE_POD_FILL (double) |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
107 DEFINE_POD_FILL (float) |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
108 DEFINE_POD_FILL (char) |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
109 DEFINE_POD_FILL (short) |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
110 DEFINE_POD_FILL (int) |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
111 DEFINE_POD_FILL (long) |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
112 DEFINE_POD_FILL (unsigned char) |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
113 DEFINE_POD_FILL (unsigned short) |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
114 DEFINE_POD_FILL (unsigned int) |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
115 DEFINE_POD_FILL (unsigned long) |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
116 |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
117 DEFINE_POD_FILL (Complex) |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
118 DEFINE_POD_FILL (FloatComplex) |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
119 |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
120 template <class T> |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
121 DEFINE_POD_FILL (octave_int<T>) |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
122 |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
123 // Uninitialized allocation. Will not initialize memory for complex and octave_int. |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
124 // Memory allocated by octave_new should be freed by octave_delete. |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
125 template <class T> |
10358
72fab01e5d68
improve some size_t queries
Jaroslav Hajek <highegg@gmail.com>
parents:
9780
diff
changeset
|
126 inline T *no_ctor_new (size_t n) |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
127 { |
10359
ec05728ce7f0
guard against >2GB allocations with 32-bit ptrs on weird platforms
Jaroslav Hajek <highegg@gmail.com>
parents:
10358
diff
changeset
|
128 // Some systems let us allocate > 2GB memory even though size_t, which is either |
ec05728ce7f0
guard against >2GB allocations with 32-bit ptrs on weird platforms
Jaroslav Hajek <highegg@gmail.com>
parents:
10358
diff
changeset
|
129 // buggy or completely cuckoo, so let's check here to stay safe. |
ec05728ce7f0
guard against >2GB allocations with 32-bit ptrs on weird platforms
Jaroslav Hajek <highegg@gmail.com>
parents:
10358
diff
changeset
|
130 safe_size_comp (n, sizeof (T)); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
131 return new T[n]; |
10359
ec05728ce7f0
guard against >2GB allocations with 32-bit ptrs on weird platforms
Jaroslav Hajek <highegg@gmail.com>
parents:
10358
diff
changeset
|
132 } |
9776
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
133 template <class T> |
9780
6dafc60dde31
rename oct-mem functions
Jaroslav Hajek <highegg@gmail.com>
parents:
9776
diff
changeset
|
134 inline void no_ctor_delete (T *ptr) |
9776
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
135 { delete [] ptr; } |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
136 |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
137 #define DEFINE_POD_NEW_DELETE(T) \ |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
138 template <> \ |
10358
72fab01e5d68
improve some size_t queries
Jaroslav Hajek <highegg@gmail.com>
parents:
9780
diff
changeset
|
139 inline T *no_ctor_new<T > (size_t n) \ |
10359
ec05728ce7f0
guard against >2GB allocations with 32-bit ptrs on weird platforms
Jaroslav Hajek <highegg@gmail.com>
parents:
10358
diff
changeset
|
140 { return reinterpret_cast<T *> (new char[safe_size_comp (n, sizeof (T))]); } \ |
9776
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
141 template <> \ |
9780
6dafc60dde31
rename oct-mem functions
Jaroslav Hajek <highegg@gmail.com>
parents:
9776
diff
changeset
|
142 inline void no_ctor_delete<T > (T *ptr) \ |
9776
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
143 { delete [] reinterpret_cast<char *> (ptr); } |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
144 |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
145 DEFINE_POD_NEW_DELETE (Complex) |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
146 DEFINE_POD_NEW_DELETE (FloatComplex) |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
147 |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
148 DEFINE_POD_NEW_DELETE (octave_int8) |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
149 DEFINE_POD_NEW_DELETE (octave_int16) |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
150 DEFINE_POD_NEW_DELETE (octave_int32) |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
151 DEFINE_POD_NEW_DELETE (octave_int64) |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
152 DEFINE_POD_NEW_DELETE (octave_uint8) |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
153 DEFINE_POD_NEW_DELETE (octave_uint16) |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
154 DEFINE_POD_NEW_DELETE (octave_uint32) |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
155 DEFINE_POD_NEW_DELETE (octave_uint64) |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
156 |
266eae13dcf3
ommitted file from 9773:01f897d8a130
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
157 #endif /* octave_memory_h */ |