memory.icc
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
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include <cstring>
00039 #include <cstdlib>
00040
00041 namespace Gecode {
00042
00044 namespace Memory {
00045
00055
00056 void* malloc(size_t s);
00058 void* realloc(void* p, size_t s);
00060 void free(void* p);
00061
00063 template <class T>
00064 T* bmalloc(size_t n);
00066 template <class T>
00067 T* brealloc(T* b, size_t n, size_t m);
00069 template <class T>
00070 T* bcopy(T* d, const T* s, size_t n);
00071
00073
00074
00075
00076
00077
00078
00079
00080 forceinline void*
00081 malloc(size_t s) {
00082 void* p = ::malloc(s);
00083 if (p != NULL)
00084 return p;
00085 throw MemoryExhausted();
00086 }
00087
00088 forceinline void*
00089 realloc(void *p, size_t s) {
00090 p = ::realloc(p,s);
00091 if (p != NULL || s == 0)
00092 return p;
00093 throw MemoryExhausted();
00094 }
00095
00096 forceinline void
00097 free(void* p) {
00098 ::free(p);
00099 }
00100
00101
00102 template <class T>
00103 forceinline T*
00104 bmalloc(size_t n) {
00105 return static_cast<T*>(Memory::malloc(sizeof(T)*n));
00106 }
00107
00108 template <class T>
00109 forceinline T*
00110 brealloc(T* b, size_t, size_t m) {
00111 return static_cast<T*>(Memory::realloc(b,m*sizeof(T)));
00112 }
00113
00114 template <class T>
00115 forceinline T*
00116 bcopy(T* d, const T* s, size_t n) {
00117 return static_cast<T*>(memcpy(d,s,n*sizeof(T)));
00118 }
00119
00120 }
00121
00122 }
00123
00130
00146
00147
00148
00149
00150
00151
00152
00153
00154 #if defined(__GNUC__) && (! defined(_WIN32) ) && \
00155 (! (defined(__APPLE__) && defined(__i386) && \
00156 __GNUC__ == 4 && __GNUC_MINOR__ < 2))
00157
00158 #define GECODE_AUTOARRAY(T,X,N) \
00159 char GECODE_FRESH(X) [(N)*sizeof(T)]; \
00160 T* X = ::Gecode::Support::ptr_cast<T*>(GECODE_FRESH(X));
00161
00162 #elif defined(_MSC_VER)
00163
00164 #include <cstdlib>
00165 #include <malloc.h>
00166
00167 namespace Gecode { namespace Memory {
00168
00172 class ManageMemory {
00173 private:
00174 void* _x;
00175 public:
00176 ManageMemory(void* p) : _x(p) {}
00177 ~ManageMemory(void) { _freea(_x); }
00178 };
00179
00180 }}
00181
00182
00183 #define GECODE_AUTOARRAY(T,X,N) \
00184 T* X; \
00185 X = static_cast<T*>(_malloca(sizeof(T)*(N))); \
00186 ::Gecode::Memory::ManageMemory __GECODE__ ## X ## __LINE__(X);
00187
00188
00189 #else
00190
00191 #include <cstdlib>
00192
00193 namespace Gecode { namespace Memory {
00194
00198 template <class T>
00199 class AutoArray {
00200 private:
00201 T* _x;
00202 public:
00203 AutoArray(const int n) {
00204 _x = static_cast<T*>(::malloc(sizeof(T)*n));
00205 }
00206 ~AutoArray(void) {
00207 ::free(_x);
00208 }
00209 T &operator [](const int i){ return _x[i]; }
00210 T &operator [](const unsigned int i){ return _x[i]; }
00211 operator T*(void) { return _x; }
00212 };
00213
00214 }}
00215
00216 #define GECODE_AUTOARRAY(T,X,N) ::Gecode::Memory::AutoArray< T > X(N)
00217
00218 #endif
00219
00220