sc_temporary.h

Go to the documentation of this file.
00001 /*****************************************************************************
00002 
00003   The following code is derived, directly or indirectly, from the SystemC
00004   source code Copyright (c) 1996-2006 by all Contributors.
00005   All Rights reserved.
00006 
00007   The contents of this file are subject to the restrictions and limitations
00008   set forth in the SystemC Open Source License Version 2.4 (the "License");
00009   You may not use this file except in compliance with such restrictions and
00010   limitations. You may obtain instructions on how to receive a copy of the
00011   License at http://www.systemc.org/. Software distributed by Contributors
00012   under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
00013   ANY KIND, either express or implied. See the License for the specific
00014   language governing rights and limitations under the License.
00015 
00016  *****************************************************************************/
00017 
00018 /*****************************************************************************
00019 
00020   sc_temporary.h -- Temporary value pool classes.
00021 
00022   Original Author: Andy Goodrich, Forte Design Systems, Inc.
00023 
00024  *****************************************************************************/
00025 
00026 /*****************************************************************************
00027 
00028   MODIFICATION LOG - modifiers, enter your name, affiliation, date and
00029   changes you are making here.
00030 
00031       Name, Affiliation, Date:
00032   Description of Modification:
00033 
00034  *****************************************************************************/
00035 
00036 
00037 // $Log: sc_temporary.h,v $
00038 // Revision 1.1.1.1  2006/12/15 20:31:39  acg
00039 // SystemC 2.2
00040 //
00041 // Revision 1.3  2006/01/13 18:53:11  acg
00042 // Andy Goodrich: Added $Log command so that CVS comments are reproduced in
00043 // the source.
00044 //
00045 
00046 #ifndef SC_TEMPORARY_H
00047 #define SC_TEMPORARY_H
00048 
00049 namespace sc_core {
00050 
00051 //------------------------------------------------------------------------------
00052 // sc_byte_heap - CLASS MANAGING A TEMPORARY HEAP OF BYTES
00053 //
00054 // This facility implements a heap of temporary byte allocations. Once an 
00055 // request has been allocated it is not freed. However the entire heap 
00056 // wraps and the storage is reused. This means that no allocations should
00057 // be assumed as permanent. Allocations are double-word aligned. This is
00058 // raw storage, so objects which contain virtual methods cannot be allocated
00059 // with this object. See the sc_vpool object for that type of storage 
00060 // allocation.
00061 //
00062 // char* allocate( int size )
00063 //   This method returns a pointer to block of size bytes. The block
00064 //   returned is the next available one in the heap. If the current heap
00065 //   cannot fullfil the request it will be rewound and storage allocated from
00066 //   its start. All allocations start on an 8-byte boundary.
00067 //       size = number of bytes to be allocated.
00068 //
00069 // void initialize( int heap_size=0x100000 )
00070 //   This method allocates the storage to be managed. If there is already
00071 //   a block of storage under management it is freed. If no argument is
00072 //   provided for the heap size, a megabyte will be allocated.
00073 //       heap_size = number of bytes to allocate for the heap.
00074 //
00075 // unsigned int length()
00076 //   This method returns the size of this object's heap in bytes.
00077 //
00078 // sc_byte_heap()
00079 //   This is the non-initialized object instance constructor. It does not 
00080 //   allocate the heap storage, that is done by the initialize() method.
00081 //
00082 // sc_byte_heap()
00083 //   This is the initializing object instance constructor. It does allocates
00084 //   a heap of the specified number of bytes.
00085 //       heap_size = number of bytes to allocate for the heap.
00086 //------------------------------------------------------------------------------
00087 class sc_byte_heap {
00088   public:
00089     char*  m_bgn_p;  // Beginning of heap storage.
00090     char*  m_end_p;  // End of heap storage.
00091     char*  m_next_p; // Next heap location to be allocated.
00092 
00093     inline char* allocate( int bytes_n )
00094     {
00095         char*   result_p;
00096         bytes_n = (bytes_n + 7) & 0xfffffff8;
00097         result_p = m_next_p;
00098         m_next_p += bytes_n;
00099         if ( m_next_p >= m_end_p )
00100         {
00101             result_p = m_bgn_p;
00102             m_next_p = m_bgn_p + bytes_n;
00103         }
00104         return result_p; 
00105     }
00106 
00107     inline void initialize( int heap_size=0x100000 )
00108     {
00109         if ( m_bgn_p ) delete [] m_bgn_p;
00110         m_bgn_p = new char[heap_size];
00111         m_end_p = &m_bgn_p[heap_size];
00112         m_next_p = m_bgn_p;
00113     }
00114     
00115     inline unsigned int length()
00116     {
00117         return (unsigned int)(m_end_p - m_bgn_p);
00118     }
00119 
00120     inline sc_byte_heap()
00121     {
00122         m_bgn_p = 0;
00123     }
00124 
00125     inline sc_byte_heap( int heap_size )
00126     {
00127         m_bgn_p = 0;
00128         initialize( heap_size );
00129     }
00130 
00131     inline ~sc_byte_heap()
00132     {
00133         if ( m_bgn_p ) delete [] m_bgn_p;
00134     }
00135 
00136 };
00137 
00138 
00139 //------------------------------------------------------------------------------
00140 // sc_vpool<T> - CLASS MANAGING A TEMPORARY VECTOR OF CLASS T INSTANCES
00141 //
00142 // This class implements a fixed pool of objects contained in a vector. These
00143 // objects are allocated via the allocate() method. An index, m_pool_i, 
00144 // indicates the next object to be allocated. The vector is a power of 2 in
00145 // size, and this fact is used to wrap the list when m_pool_i reaches the
00146 // end of the vector.  
00147 // 
00148 // sc_vpool( int log2, T* pool_p=0 )
00149 //   This is the object instance constructor for this class. It configures
00150 //   the object to manage a vector of 2**log2 entries. If a vector is 
00151 //   not supplied one will be allocated.
00152 //     log2   =  the log base two of the size of the vector.
00153 //     pool_p -> vector of 2**log2 entries to be managed or 0.
00154 //
00155 // ~sc_vpool()
00156 //   This is the object instance destructor for this class. It frees the
00157 //   block of storage which was being managed.
00158 //
00159 // T* allocate()
00160 //   This method returns the address of the next entry in the vector, m_pool_p, 
00161 //   pointed to by the index, m_pool_i, and updates that index. The index
00162 //   update consists of adding 1 to m_pool_i and masking it by m_wrap.
00163 // 
00164 // void reset()
00165 //   This method resets the allocation index, m_pool_i, to point to the start
00166 //   of the vector of objects under management. This call is not usually made
00167 //   since there are a fixed number of entries and the index wraps. However,
00168 //   for diagnostics tests it is convenient to be able to reset to the start
00169 //   of the vector.
00170 //
00171 // int size()
00172 //   This method returns the number of object instances contained in the
00173 //   vector being managed by this object instance.
00174 //------------------------------------------------------------------------------
00175 template<class T>
00176 class sc_vpool {
00177   protected:
00178     int m_pool_i;   // Index of next entry to m_pool_m to provide.
00179     T*  m_pool_p;   // Vector of temporaries.
00180     int m_wrap;     // Mask to wrap vector index.
00181 
00182   public:
00183     inline sc_vpool( int log2, T* pool_p=0 );
00184     inline ~sc_vpool();
00185     inline T* allocate();
00186     inline void reset();
00187     inline int size();
00188 };
00189 
00190 template<class T> sc_vpool<T>::sc_vpool( int log2, T* pool_p )
00191 {
00192     // if ( log2 > 32 ) SC_REPORT_ERROR(SC_ID_POOL_SIZE_, "");
00193     m_pool_i = 0;
00194     m_pool_p = pool_p ? pool_p : new T[1 << log2];
00195     m_wrap = ~(-1 << log2);
00196 }
00197 
00198 template<class T> sc_vpool<T>::~sc_vpool()
00199 {
00200     // delete [] m_pool_p;
00201 }
00202 
00203 template<class T> T* sc_vpool<T>::allocate()
00204 {
00205     T* result_p;    // Entry to return.
00206 
00207     result_p = &m_pool_p[m_pool_i];
00208     m_pool_i = (m_pool_i + 1) & m_wrap;
00209     return result_p;
00210 }
00211 
00212 template<class T> void sc_vpool<T>::reset()
00213 {
00214     m_pool_i = 0;
00215 }
00216 
00217 template<class T> int sc_vpool<T>::size()
00218 {
00219     return m_wrap + 1;
00220 }
00221 
00222 } // namespace sc_core
00223 
00224 #endif // SC_TEMPORARY_H
00225 
00226 
00227 

Generated on Wed Jan 21 15:32:10 2009 for SystemC by  doxygen 1.5.5