00001 /***************************************************************************** 00002 00003 The following code is derived, directly or indirectly, from the SystemC 00004 source code Copyright (c) 1996-2014 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 (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.accellera.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 CHANGE LOG AT END OF FILE 00025 *****************************************************************************/ 00026 00027 #ifndef SC_TEMPORARY_H 00028 #define SC_TEMPORARY_H 00029 00030 #include <cstddef> // std::size_t 00031 00032 namespace sc_core { 00033 00034 //------------------------------------------------------------------------------ 00035 // sc_byte_heap - CLASS MANAGING A TEMPORARY HEAP OF BYTES 00036 // 00037 // This facility implements a heap of temporary byte allocations. Once an 00038 // request has been allocated it is not freed. However the entire heap 00039 // wraps and the storage is reused. This means that no allocations should 00040 // be assumed as permanent. Allocations are double-word aligned. This is 00041 // raw storage, so objects which contain virtual methods cannot be allocated 00042 // with this object. See the sc_vpool object for that type of storage 00043 // allocation. 00044 // 00045 // char* allocate( int size ) 00046 // This method returns a pointer to block of size bytes. The block 00047 // returned is the next available one in the heap. If the current heap 00048 // cannot fullfil the request it will be rewound and storage allocated from 00049 // its start. All allocations start on an 8-byte boundary. 00050 // size = number of bytes to be allocated. 00051 // 00052 // void initialize( int heap_size=0x100000 ) 00053 // This method allocates the storage to be managed. If there is already 00054 // a block of storage under management it is freed. If no argument is 00055 // provided for the heap size, a megabyte will be allocated. 00056 // heap_size = number of bytes to allocate for the heap. 00057 // 00058 // unsigned int length() 00059 // This method returns the size of this object's heap in bytes. 00060 // 00061 // sc_byte_heap() 00062 // This is the non-initialized object instance constructor. It does not 00063 // allocate the heap storage, that is done by the initialize() method. 00064 // 00065 // sc_byte_heap(int) 00066 // This is the initializing object instance constructor. It does allocates 00067 // a heap of the specified number of bytes. 00068 // heap_size = number of bytes to allocate for the heap. 00069 //------------------------------------------------------------------------------ 00070 class sc_byte_heap { 00071 public: 00072 char* m_bgn_p; // Beginning of heap storage. 00073 char* m_end_p; // End of heap storage. 00074 char* m_next_p; // Next heap location to be allocated. 00075 00076 inline char* allocate( std::size_t bytes_n ) 00077 { 00078 char* result_p; 00079 bytes_n = (bytes_n + 7) & ((std::size_t)(-8)); 00080 result_p = m_next_p; 00081 m_next_p += bytes_n; 00082 if ( m_next_p >= m_end_p ) 00083 { 00084 result_p = m_bgn_p; 00085 m_next_p = m_bgn_p + bytes_n; 00086 } 00087 return result_p; 00088 } 00089 00090 inline void initialize( std::size_t heap_size=0x100000 ) 00091 { 00092 delete [] m_bgn_p; 00093 m_bgn_p = new char[heap_size]; 00094 m_end_p = &m_bgn_p[heap_size]; 00095 m_next_p = m_bgn_p; 00096 } 00097 00098 inline std::size_t length() 00099 { 00100 return (std::size_t)(m_end_p - m_bgn_p); 00101 } 00102 00103 inline sc_byte_heap() : 00104 m_bgn_p(0), m_end_p(0), m_next_p(0) 00105 { 00106 } 00107 00108 inline sc_byte_heap( std::size_t heap_size ) : 00109 m_bgn_p(0), m_end_p(0), m_next_p(0) 00110 { 00111 initialize( heap_size ); 00112 } 00113 00114 inline ~sc_byte_heap() 00115 { 00116 delete [] m_bgn_p; 00117 } 00118 00119 }; 00120 00121 00122 //------------------------------------------------------------------------------ 00123 // sc_vpool<T> - CLASS MANAGING A TEMPORARY VECTOR OF CLASS T INSTANCES 00124 // 00125 // This class implements a fixed pool of objects contained in a vector. These 00126 // objects are allocated via the allocate() method. An index, m_pool_i, 00127 // indicates the next object to be allocated. The vector is a power of 2 in 00128 // size, and this fact is used to wrap the list when m_pool_i reaches the 00129 // end of the vector. 00130 // 00131 // sc_vpool( int log2, T* pool_p=0 ) 00132 // This is the object instance constructor for this class. It configures 00133 // the object to manage a vector of 2**log2 entries. If a vector is 00134 // not supplied one will be allocated. 00135 // log2 = the log base two of the size of the vector. 00136 // pool_p -> vector of 2**log2 entries to be managed or 0. 00137 // 00138 // ~sc_vpool() 00139 // This is the object instance destructor for this class. It frees the 00140 // block of storage which was being managed. 00141 // 00142 // T* allocate() 00143 // This method returns the address of the next entry in the vector, m_pool_p, 00144 // pointed to by the index, m_pool_i, and updates that index. The index 00145 // update consists of adding 1 to m_pool_i and masking it by m_wrap. 00146 // 00147 // void reset() 00148 // This method resets the allocation index, m_pool_i, to point to the start 00149 // of the vector of objects under management. This call is not usually made 00150 // since there are a fixed number of entries and the index wraps. However, 00151 // for diagnostics tests it is convenient to be able to reset to the start 00152 // of the vector. 00153 // 00154 // int size() 00155 // This method returns the number of object instances contained in the 00156 // vector being managed by this object instance. 00157 //------------------------------------------------------------------------------ 00158 template<class T> 00159 class sc_vpool { 00160 protected: 00161 std::size_t m_pool_i; // Index of next entry to m_pool_m to provide. 00162 T* m_pool_p; // Vector of temporaries. 00163 std::size_t m_wrap; // Mask to wrap vector index. 00164 00165 public: 00166 inline sc_vpool( int log2, T* pool_p=0 ); 00167 inline ~sc_vpool(); 00168 inline T* allocate(); 00169 inline void reset(); 00170 inline std::size_t size(); 00171 }; 00172 00173 template<class T> sc_vpool<T>::sc_vpool( int log2, T* pool_p ) 00174 : m_pool_i( 0 ) 00175 , m_pool_p( pool_p ? pool_p : new T[static_cast<std::size_t>(1) << log2] ) 00176 , m_wrap( ~(static_cast<std::size_t>(-1) << log2) ) 00177 { 00178 // if ( log2 > 32 ) SC_REPORT_ERROR(SC_ID_POOL_SIZE_, ""); 00179 } 00180 00181 template<class T> sc_vpool<T>::~sc_vpool() 00182 { 00183 // delete [] m_pool_p; 00184 } 00185 00186 template<class T> T* sc_vpool<T>::allocate() 00187 { 00188 T* result_p; // Entry to return. 00189 00190 result_p = &m_pool_p[m_pool_i]; 00191 m_pool_i = (m_pool_i + 1) & m_wrap; 00192 return result_p; 00193 } 00194 00195 template<class T> void sc_vpool<T>::reset() 00196 { 00197 m_pool_i = 0; 00198 } 00199 00200 template<class T> std::size_t sc_vpool<T>::size() 00201 { 00202 return m_wrap + 1; 00203 } 00204 00205 } // namespace sc_core 00206 00207 // $Log: sc_temporary.h,v $ 00208 // Revision 1.4 2011/08/26 20:46:19 acg 00209 // Andy Goodrich: moved the modification log to the end of the file to 00210 // eliminate source line number skew when check-ins are done. 00211 // 00212 // Revision 1.3 2011/08/24 22:05:56 acg 00213 // Torsten Maehne: initialization changes to remove warnings. 00214 // 00215 // Revision 1.2 2011/02/18 20:38:44 acg 00216 // Andy Goodrich: Updated Copyright notice. 00217 // 00218 // Revision 1.1.1.1 2006/12/15 20:20:06 acg 00219 // SystemC 2.3 00220 // 00221 // Revision 1.3 2006/01/13 18:53:11 acg 00222 // Andy Goodrich: Added $Log command so that CVS comments are reproduced in 00223 // the source. 00224 // 00225 00226 #endif // SC_TEMPORARY_H