00001 // -*- mode: C++; c-file-style: "stroustrup"; c-basic-offset: 4; indent-tabs-mode: nil; -*- 00002 //////////////////////////////////////////////////////////////////// 00003 // 00004 // Filename : DBMAllocator.h 00005 // 00006 // This file is a part of the UPPAAL toolkit. 00007 // Copyright (c) 1995 - 2003, Uppsala University and Aalborg University. 00008 // All right reserved. 00009 // 00010 // $Id: DBMAllocator.h,v 1.4 2005/07/22 12:55:54 adavid Exp $$ 00011 // 00012 /////////////////////////////////////////////////////////////////// 00013 00014 #ifndef DBM_DBMALLOCATOR_H 00015 #define DBM_DBMALLOCATOR_H 00016 00017 #include "dbm/fed.h" 00018 #include "base/array_t.h" 00019 00020 /** @file 00021 * Allocation of classes related to fed_t, dbm_t, etc. 00022 * Declaration of the DBM allocator and DBM table. 00023 */ 00024 #ifndef ENABLE_DBM_NEW 00025 namespace dbm 00026 { 00027 ///< Allocator for DBMs, only if we don't use new for them. 00028 class DBMAllocator 00029 { 00030 public: 00031 ///< Default constructor 00032 DBMAllocator() 00033 : freeList(16), dbm1x1(1) { 00034 raw_t lezero = dbm_LE_ZERO; 00035 dbm1x1.newCopy(&lezero, 1); 00036 dbm1x1.intern(); 00037 } 00038 00039 ///< free memory of the deallocated idbm_t 00040 void cleanUp(); 00041 00042 /** Allocate memory to instantiate an idbm_t 00043 * @param dim: dimension of the DBM 00044 */ 00045 void* alloc(cindex_t dim) { 00046 idbm_t *dbm = freeList.get(dim); 00047 if (dbm) 00048 { 00049 freeList[dim] = dbm->getNext(); 00050 return dbm; 00051 } 00052 return new int32_t[intsizeof(idbm_t)+dim*dim]; 00053 } 00054 00055 /** Deallocate an idbm_t 00056 * @param dbm: dbm to deallocate 00057 */ 00058 void dealloc(idbm_t *dbm) 00059 { 00060 assert(dbm); 00061 cindex_t dim = dbm->getDimension(); 00062 dbm->next = freeList.get(dim); 00063 freeList.set(dim, dbm); 00064 } 00065 00066 #ifdef ENABLE_MONITOR 00067 // only one global instance hidden here: 00068 // deallocation is not needed because it is done 00069 // only at program exit. 00070 ~DBMAllocator() { 00071 dbm1x1.nil(); 00072 cleanUp(); 00073 } 00074 #endif 00075 00076 ///< @return a constant DBM 1x1 (copies will make it non-mutable). 00077 dbm_t& dbm1() { assert(!dbm1x1.isEmpty()); return dbm1x1; } 00078 00079 private: 00080 base::array_t<idbm_t*> freeList; 00081 dbm_t dbm1x1; 00082 }; 00083 00084 ///< Allocator instance 00085 extern DBMAllocator dbm_allocator; 00086 } 00087 #endif // ENABLE_DBM_NEW 00088 00089 #endif // DBM_FED_ALLOC_H