Generated on Mon Aug 25 11:35:44 2008 for Gecode by doxygen 1.5.6

memory.icc

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Christian Schulte <schulte@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Christian Schulte, 2004
00008  *
00009  *  Last modified:
00010  *     $Date: 2007-11-13 21:48:32 +0100 (Tue, 13 Nov 2007) $ by $Author: schulte $
00011  *     $Revision: 5291 $
00012  *
00013  *  This file is part of Gecode, the generic constraint
00014  *  development environment:
00015  *     http://www.gecode.org
00016  *
00017  *  Permission is hereby granted, free of charge, to any person obtaining
00018  *  a copy of this software and associated documentation files (the
00019  *  "Software"), to deal in the Software without restriction, including
00020  *  without limitation the rights to use, copy, modify, merge, publish,
00021  *  distribute, sublicense, and/or sell copies of the Software, and to
00022  *  permit persons to whom the Software is furnished to do so, subject to
00023  *  the following conditions:
00024  *
00025  *  The above copyright notice and this permission notice shall be
00026  *  included in all copies or substantial portions of the Software.
00027  *
00028  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00029  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00030  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00031  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00032  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00033  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00034  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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      * Implementations
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  * Automatic stack memory management
00149  *
00150  */
00151 
00152 // The variable-sized arrays gcc extension is broken on
00153 // i386/Apple gcc prior to 4.2
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 // STATISTICS: support-any