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_memory_h)
00024 #define octave_memory_h 1
00025
00026 #include <cstddef>
00027 #include <cstring>
00028 #include <algorithm>
00029
00030 #include "oct-cmplx.h"
00031 #include "oct-inttypes.h"
00032
00033
00034
00035
00036
00037
00038
00039 inline size_t safe_size_comp (size_t n, size_t size)
00040 {
00041 if (n > static_cast<size_t> (-1) / size)
00042 throw std::bad_alloc ();
00043 return n * size;
00044 }
00045
00046
00047
00048 template <class T>
00049 inline void copy_or_memcpy (size_t n, const T *src, T *dest)
00050 { std::copy (src, src + n, dest); }
00051
00052 #define DEFINE_POD_UCOPY(T) \
00053 inline void copy_or_memcpy (size_t n, const T *src, T *dest) \
00054 { std::memcpy (dest, src, n * sizeof (T)); }
00055
00056 DEFINE_POD_UCOPY (double)
00057 DEFINE_POD_UCOPY (float)
00058 DEFINE_POD_UCOPY (char)
00059 DEFINE_POD_UCOPY (short)
00060 DEFINE_POD_UCOPY (int)
00061 DEFINE_POD_UCOPY (long)
00062 DEFINE_POD_UCOPY (unsigned char)
00063 DEFINE_POD_UCOPY (unsigned short)
00064 DEFINE_POD_UCOPY (unsigned int)
00065 DEFINE_POD_UCOPY (unsigned long)
00066
00067 DEFINE_POD_UCOPY (Complex)
00068 DEFINE_POD_UCOPY (FloatComplex)
00069
00070 template <class T>
00071 DEFINE_POD_UCOPY (octave_int<T>)
00072
00073
00074
00075 template <class T>
00076 inline void fill_or_memset (size_t n, const T& value, T *dest)
00077 { std::fill_n (dest, n, value); }
00078
00079 template <class T>
00080 inline bool helper_is_zero_mem (const T& value)
00081 {
00082 typedef typename query_integer_type<sizeof (T), false>::type IT;
00083 return *(reinterpret_cast<const IT *>(&value)) == 0;
00084 }
00085
00086 template <class T>
00087 inline bool helper_is_zero_mem (const std::complex<T>& value)
00088 {
00089 return (helper_is_zero_mem (value.real ())
00090 && helper_is_zero_mem (value.imag ()));
00091 }
00092
00093 template <class T>
00094 inline bool helper_is_zero_mem (const octave_int<T>& value)
00095 { return value.value () == T(); }
00096
00097 #define DEFINE_POD_FILL(T) \
00098 inline void fill_or_memset (size_t n, const T& value, T *dest) \
00099 { \
00100 if (helper_is_zero_mem (value)) \
00101 std::memset (dest, 0, n * sizeof (T)); \
00102 else \
00103 std::fill_n (dest, n, value); \
00104 }
00105
00106 DEFINE_POD_FILL (double)
00107 DEFINE_POD_FILL (float)
00108 DEFINE_POD_FILL (char)
00109 DEFINE_POD_FILL (short)
00110 DEFINE_POD_FILL (int)
00111 DEFINE_POD_FILL (long)
00112 DEFINE_POD_FILL (unsigned char)
00113 DEFINE_POD_FILL (unsigned short)
00114 DEFINE_POD_FILL (unsigned int)
00115 DEFINE_POD_FILL (unsigned long)
00116
00117 DEFINE_POD_FILL (Complex)
00118 DEFINE_POD_FILL (FloatComplex)
00119
00120 template <class T>
00121 DEFINE_POD_FILL (octave_int<T>)
00122
00123
00124
00125 template <class T>
00126 inline T *no_ctor_new (size_t n)
00127 {
00128
00129
00130 safe_size_comp (n, sizeof (T));
00131 return new T[n];
00132 }
00133 template <class T>
00134 inline void no_ctor_delete (T *ptr)
00135 { delete [] ptr; }
00136
00137 #define DEFINE_POD_NEW_DELETE(T) \
00138 template <> \
00139 inline T *no_ctor_new<T > (size_t n) \
00140 { return reinterpret_cast<T *> (new char[safe_size_comp (n, sizeof (T))]); } \
00141 template <> \
00142 inline void no_ctor_delete<T > (T *ptr) \
00143 { delete [] reinterpret_cast<char *> (ptr); }
00144
00145 DEFINE_POD_NEW_DELETE (Complex)
00146 DEFINE_POD_NEW_DELETE (FloatComplex)
00147
00148 DEFINE_POD_NEW_DELETE (octave_int8)
00149 DEFINE_POD_NEW_DELETE (octave_int16)
00150 DEFINE_POD_NEW_DELETE (octave_int32)
00151 DEFINE_POD_NEW_DELETE (octave_int64)
00152 DEFINE_POD_NEW_DELETE (octave_uint8)
00153 DEFINE_POD_NEW_DELETE (octave_uint16)
00154 DEFINE_POD_NEW_DELETE (octave_uint32)
00155 DEFINE_POD_NEW_DELETE (octave_uint64)
00156
00157 #endif