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-2005 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 #ifndef SC_TEMPORARY_H
00038 #define SC_TEMPORARY_H
00039 
00040 namespace sc_core {
00041 
00042 //------------------------------------------------------------------------------
00043 // sc_byte_heap - CLASS MANAGING A TEMPORARY HEAP OF BYTES
00044 //
00045 // This facility implements a heap of temporary byte allocations. Once an 
00046 // request has been allocated it is not freed. However the entire heap 
00047 // wraps and the storage is reused. This means that no allocations should
00048 // be assumed as permanent. Allocations are double-word aligned. This is
00049 // raw storage, so objects which contain virtual methods cannot be allocated
00050 // with this object. See the sc_vpool object for that type of storage 
00051 // allocation.
00052 //
00053 // char* allocate( int size )
00054 //   This method returns a pointer to block of size bytes. The block
00055 //   returned is the next available one in the heap. If the current heap
00056 //   cannot fullfil the request it will be rewound and storage allocated from
00057 //   its start. All allocations start on an 8-byte boundary.
00058 //       size = number of bytes to be allocated.
00059 //
00060 // void initialize( int heap_size=0x100000 )
00061 //   This method allocates the storage to be managed. If there is already
00062 //   a block of storage under management it is freed. If no argument is
00063 //   provided for the heap size, a megabyte will be allocated.
00064 //       heap_size = number of bytes to allocate for the heap.
00065 //
00066 // unsigned int length()
00067 //   This method returns the size of this object's heap in bytes.
00068 //
00069 // sc_byte_heap()
00070 //   This is the non-initialized object instance constructor. It does not 
00071 //   allocate the heap storage, that is done by the initialize() method.
00072 //
00073 // sc_byte_heap()
00074 //   This is the initializing object instance constructor. It does allocates
00075 //   a heap of the specified number of bytes.
00076 //       heap_size = number of bytes to allocate for the heap.
00077 //------------------------------------------------------------------------------
00078 class sc_byte_heap {
00079   public:
00080     char*  m_bgn_p;  // Beginning of heap storage.
00081     char*  m_end_p;  // End of heap storage.
00082     char*  m_next_p; // Next heap location to be allocated.
00083 
00084     inline char* allocate( int bytes_n )
00085     {
00086         char*   result_p;
00087         bytes_n = (bytes_n + 7) & 0xfffffff8;
00088         result_p = m_next_p;
00089         m_next_p += bytes_n;
00090         if ( m_next_p >= m_end_p )
00091         {
00092             result_p = m_bgn_p;
00093             m_next_p = m_bgn_p + bytes_n;
00094         }
00095         return result_p; 
00096     }
00097 
00098     inline void initialize( int heap_size=0x100000 )
00099     {
00100                 if ( m_bgn_p ) delete [] m_bgn_p;
00101         m_bgn_p = new char[heap_size];
00102         m_end_p = &m_bgn_p[heap_size];
00103         m_next_p = m_bgn_p;
00104     }
00105     
00106         inline unsigned int length()
00107         {
00108                 return (unsigned int)(m_end_p - m_bgn_p);
00109         }
00110 
00111         inline sc_byte_heap()
00112         {
00113                 m_bgn_p = 0;
00114         }
00115 
00116         inline sc_byte_heap( int heap_size )
00117         {
00118                 m_bgn_p = 0;
00119                 initialize( heap_size );
00120         }
00121 
00122         inline ~sc_byte_heap()
00123         {
00124                 if ( m_bgn_p ) delete [] m_bgn_p;
00125         }
00126 
00127 };
00128 
00129 
00130 //------------------------------------------------------------------------------
00131 // sc_vpool<T> - CLASS MANAGING A TEMPORARY VECTOR OF CLASS T INSTANCES
00132 //
00133 // This class implements a fixed pool of objects contained in a vector. These
00134 // objects are allocated via the allocate() method. An index, m_pool_i, 
00135 // indicates the next object to be allocated. The vector is a power of 2 in
00136 // size, and this fact is used to wrap the list when m_pool_i reaches the
00137 // end of the vector.  
00138 // 
00139 // sc_vpool( int log2, T* pool_p=0 )
00140 //   This is the object instance constructor for this class. It configures
00141 //   the object to manage a vector of 2**log2 entries. If a vector is 
00142 //   not supplied one will be allocated.
00143 //     log2   =  the log base two of the size of the vector.
00144 //     pool_p -> vector of 2**log2 entries to be managed or 0.
00145 //
00146 // ~sc_vpool()
00147 //   This is the object instance destructor for this class. It frees the
00148 //   block of storage which was being managed.
00149 //
00150 // T* allocate()
00151 //   This method returns the address of the next entry in the vector, m_pool_p, 
00152 //   pointed to by the index, m_pool_i, and updates that index. The index
00153 //   update consists of adding 1 to m_pool_i and masking it by m_wrap.
00154 // 
00155 // void reset()
00156 //   This method resets the allocation index, m_pool_i, to point to the start
00157 //   of the vector of objects under management. This call is not usually made
00158 //   since there are a fixed number of entries and the index wraps. However,
00159 //   for diagnostics tests it is convenient to be able to reset to the start
00160 //   of the vector.
00161 //
00162 // int size()
00163 //   This method returns the number of object instances contained in the
00164 //   vector being managed by this object instance.
00165 //------------------------------------------------------------------------------
00166 template<class T>
00167 class sc_vpool {
00168   protected:
00169         int m_pool_i;   // Index of next entry to m_pool_m to provide.
00170         T*  m_pool_p;   // Vector of temporaries.
00171         int m_wrap;             // Mask to wrap vector index.
00172 
00173   public:
00174         inline sc_vpool( int log2, T* pool_p=0 );
00175         inline ~sc_vpool();
00176         inline T* allocate();
00177         inline void reset();
00178         inline int size();
00179 };
00180 
00181 template<class T> sc_vpool<T>::sc_vpool( int log2, T* pool_p )
00182 {
00183         // if ( log2 > 32 ) SC_REPORT_ERROR(SC_ID_POOL_SIZE_, "");
00184         m_pool_i = 0;
00185         m_pool_p = pool_p ? pool_p : new T[1 << log2];
00186         m_wrap = ~(-1 << log2);
00187 }
00188 
00189 template<class T> sc_vpool<T>::~sc_vpool()
00190 {
00191         // delete [] m_pool_p;
00192 }
00193 
00194 template<class T> T* sc_vpool<T>::allocate()
00195 {
00196         T* result_p;    // Entry to return.
00197 
00198         result_p = &m_pool_p[m_pool_i];
00199         m_pool_i = (m_pool_i + 1) & m_wrap;
00200         return result_p;
00201 }
00202 
00203 template<class T> void sc_vpool<T>::reset()
00204 {
00205         m_pool_i = 0;
00206 }
00207 
00208 template<class T> int sc_vpool<T>::size()
00209 {
00210         return m_wrap + 1;
00211 }
00212 
00213 } // namespace sc_core
00214 
00215 #endif // SC_TEMPORARY_H
00216 
00217 
00218 
Generated by
Matthieu Moy <Matthieu.Moy@st.com>
Back to Pinapa Home Page