Generated on Wed Nov 1 15:04:43 2006 for Gecode by doxygen 1.4.5

memory.icc

Go to the documentation of this file.
00001 /*
00002  *  Main authors:
00003  *     Christian Schulte <schulte@gecode.org>
00004  *
00005  *  Copyright:
00006  *     Christian Schulte, 2004
00007  *
00008  *  Last modified:
00009  *     $Date: 2006-08-04 16:03:05 +0200 (Fri, 04 Aug 2006) $ by $Author: schulte $
00010  *     $Revision: 3510 $
00011  *
00012  *  This file is part of Gecode, the generic constraint
00013  *  development environment:
00014  *     http://www.gecode.org
00015  *
00016  *  See the file "LICENSE" for information on usage and
00017  *  redistribution of this file, and for a
00018  *     DISCLAIMER OF ALL WARRANTIES.
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      * Implementations
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  * Automatic stack memory management
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 // STATISTICS: kernel-core