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 namespace Gecode {
00023
00025 namespace Memory {
00026
00036
00037 void* malloc(size_t s);
00039 void* realloc(void* p, size_t s);
00041 void free(void* p);
00042
00044 template <class T>
00045 T* bmalloc(size_t n);
00047 template <class T>
00048 T* brealloc(T* b, size_t n);
00050 template <class T>
00051 T* bcopy(T* d, const T* s, size_t n);
00052
00054
00055
00056
00057
00058
00059
00060
00061 forceinline void*
00062 malloc(size_t s) {
00063 void* p = ::malloc(s);
00064 if (p != NULL)
00065 return p;
00066 throw MemoryExhausted();
00067 }
00068
00069 forceinline void*
00070 realloc(void *p, size_t s) {
00071 p = ::realloc(p,s);
00072 if (p != NULL)
00073 return p;
00074 throw MemoryExhausted();
00075 }
00076
00077 forceinline void
00078 free(void* p) {
00079 ::free(p);
00080 }
00081
00082
00083 template <class T>
00084 forceinline T*
00085 bmalloc(size_t n) {
00086 return reinterpret_cast<T*>(Memory::malloc(sizeof(T)*n));
00087 }
00088
00089 template <class T>
00090 forceinline T*
00091 brealloc(T* b, size_t, size_t m) {
00092 return reinterpret_cast<T*>(Memory::realloc(b,m*sizeof(T)));
00093 }
00094
00095 template <class T>
00096 forceinline T*
00097 bcopy(T* d, const T* s, size_t n) {
00098 return reinterpret_cast<T*>(memcpy(d,s,n*sizeof(T)));
00099 }
00100
00101 }
00102
00103 }
00104
00111
00127
00128
00129
00130
00131
00132
00133 #if defined(__GNUC__) && (! defined(_WIN32) )
00134
00135 #define GECODE_AUTOARRAY(T,X,N) \
00136 char __GECODE__ ## X ## __LINE__ [(N)*sizeof(T)]; \
00137 T* X = reinterpret_cast<T*>(__GECODE__ ## X ## __LINE__);
00138
00139 #elif defined(_MSC_VER) || (defined(__GNUC__) && defined(_WIN32))
00140
00141 #include <cstdlib>
00142 #include <malloc.h>
00143
00144 namespace Gecode { namespace Memory {
00145
00149 class ManageMemory {
00150 private:
00151 void* _x;
00152 public:
00153 ManageMemory(void) {}
00154 void init(void* p) { _x = p; }
00155 ~ManageMemory(void) {
00156 if (_x != NULL)
00157 ::free(_x);
00158 }
00159 };
00160
00161 }}
00162
00163
00164 #define GECODE_AUTOARRAY(T,X,N) \
00165 T* X; \
00166 ::Gecode::Memory::ManageMemory __GECODE__ ## X ## __LINE__; \
00167 if ((sizeof(T)*(N)) <= 2048) { \
00168 X = reinterpret_cast<T*>(_alloca(sizeof(T)*(N))); \
00169 __GECODE__ ## X ## __LINE__ .init(NULL); \
00170 } else { \
00171 X = reinterpret_cast<T*>(::malloc(sizeof(T)*(N))); \
00172 __GECODE__ ## X ## __LINE__ .init(X); \
00173 }
00174
00175 #else
00176
00177 #include <cstdlib>
00178
00179 namespace Gecode { namespace Memory {
00180
00184 template <class T>
00185 class AutoArray {
00186 private:
00187 T* _x;
00188 public:
00189 AutoArray(const int n) {
00190 _x = reinterpret_cast<T*>(::malloc(sizeof(T)*n));
00191 }
00192 ~AutoArray(void) {
00193 ::free(_x);
00194 }
00195 T &operator [](const int i){ return _x[i]; }
00196 operator T*(void) { return _x; }
00197 };
00198
00199 }}
00200
00201 #define GECODE_AUTOARRAY(T,X,N) ::Gecode::Memory::AutoArray< T > X(N)
00202
00203 #endif
00204
00205