SystemC  2.3.2
Accellera SystemC proof-of-concept library
sc_temporary.h
Go to the documentation of this file.
1 /*****************************************************************************
2 
3  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
4  more contributor license agreements. See the NOTICE file distributed
5  with this work for additional information regarding copyright ownership.
6  Accellera licenses this file to you under the Apache License, Version 2.0
7  (the "License"); you may not use this file except in compliance with the
8  License. You may obtain a copy of the License at
9 
10  http://www.apache.org/licenses/LICENSE-2.0
11 
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15  implied. See the License for the specific language governing
16  permissions and limitations under the License.
17 
18  *****************************************************************************/
19 
20 /*****************************************************************************
21 
22  sc_temporary.h -- Temporary value pool classes.
23 */
33 #ifndef SC_TEMPORARY_H
34 #define SC_TEMPORARY_H
35 #include "sysc/kernel/sc_cmnhdr.h"
36 
37 #include <cstddef> // std::size_t
38 
39 namespace sc_core {
40 
41 //------------------------------------------------------------------------------
42 // sc_byte_heap - CLASS MANAGING A TEMPORARY HEAP OF BYTES
43 //
44 // This facility implements a heap of temporary byte allocations. Once an
45 // request has been allocated it is not freed. However the entire heap
46 // wraps and the storage is reused. This means that no allocations should
47 // be assumed as permanent. Allocations are double-word aligned. This is
48 // raw storage, so objects which contain virtual methods cannot be allocated
49 // with this object. See the sc_vpool object for that type of storage
50 // allocation.
51 //
52 // char* allocate( int size )
53 // This method returns a pointer to block of size bytes. The block
54 // returned is the next available one in the heap. If the current heap
55 // cannot fullfil the request it will be rewound and storage allocated from
56 // its start. All allocations start on an 8-byte boundary.
57 // size = number of bytes to be allocated.
58 //
59 // void initialize( int heap_size=0x100000 )
60 // This method allocates the storage to be managed. If there is already
61 // a block of storage under management it is freed. If no argument is
62 // provided for the heap size, a megabyte will be allocated.
63 // heap_size = number of bytes to allocate for the heap.
64 //
65 // unsigned int length()
66 // This method returns the size of this object's heap in bytes.
67 //
68 // sc_byte_heap()
69 // This is the non-initialized object instance constructor. It does not
70 // allocate the heap storage, that is done by the initialize() method.
71 //
72 // sc_byte_heap(int)
73 // This is the initializing object instance constructor. It does allocates
74 // a heap of the specified number of bytes.
75 // heap_size = number of bytes to allocate for the heap.
76 //------------------------------------------------------------------------------
78  public:
79  char* m_bgn_p; // Beginning of heap storage.
80  char* m_end_p; // End of heap storage.
81  char* m_next_p; // Next heap location to be allocated.
82 
83  inline char* allocate( std::size_t bytes_n )
84  {
85  char* result_p;
86  bytes_n = (bytes_n + 7) & ((std::size_t)(-8));
87  result_p = m_next_p;
88  m_next_p += bytes_n;
89  if ( m_next_p >= m_end_p )
90  {
91  result_p = m_bgn_p;
92  m_next_p = m_bgn_p + bytes_n;
93  }
94  return result_p;
95  }
96 
97  inline void initialize( std::size_t heap_size=0x100000 )
98  {
99  delete [] m_bgn_p;
100  m_bgn_p = new char[heap_size];
101  m_end_p = &m_bgn_p[heap_size];
102  m_next_p = m_bgn_p;
103  }
104 
105  inline std::size_t length()
106  {
107  return (std::size_t)(m_end_p - m_bgn_p);
108  }
109 
110  inline sc_byte_heap() :
111  m_bgn_p(0), m_end_p(0), m_next_p(0)
112  {
113  }
114 
115  inline sc_byte_heap( std::size_t heap_size ) :
116  m_bgn_p(0), m_end_p(0), m_next_p(0)
117  {
118  initialize( heap_size );
119  }
120 
121  inline ~sc_byte_heap()
122  {
123  delete [] m_bgn_p;
124  }
125 
126 };
127 
128 
129 //------------------------------------------------------------------------------
130 // sc_vpool<T> - CLASS MANAGING A TEMPORARY VECTOR OF CLASS T INSTANCES
131 //
132 // This class implements a fixed pool of objects contained in a vector. These
133 // objects are allocated via the allocate() method. An index, m_pool_i,
134 // indicates the next object to be allocated. The vector is a power of 2 in
135 // size, and this fact is used to wrap the list when m_pool_i reaches the
136 // end of the vector.
137 //
138 // sc_vpool( int log2, T* pool_p=0 )
139 // This is the object instance constructor for this class. It configures
140 // the object to manage a vector of 2**log2 entries. If a vector is
141 // not supplied one will be allocated.
142 // log2 = the log base two of the size of the vector.
143 // pool_p -> vector of 2**log2 entries to be managed or 0.
144 //
145 // ~sc_vpool()
146 // This is the object instance destructor for this class. It frees the
147 // block of storage which was being managed.
148 //
149 // T* allocate()
150 // This method returns the address of the next entry in the vector, m_pool_p,
151 // pointed to by the index, m_pool_i, and updates that index. The index
152 // update consists of adding 1 to m_pool_i and masking it by m_wrap.
153 //
154 // void reset()
155 // This method resets the allocation index, m_pool_i, to point to the start
156 // of the vector of objects under management. This call is not usually made
157 // since there are a fixed number of entries and the index wraps. However,
158 // for diagnostics tests it is convenient to be able to reset to the start
159 // of the vector.
160 //
161 // int size()
162 // This method returns the number of object instances contained in the
163 // vector being managed by this object instance.
164 //------------------------------------------------------------------------------
165 template<class T>
166 class sc_vpool {
167  protected:
168  std::size_t m_pool_i; // Index of next entry to m_pool_m to provide.
169  T* m_pool_p; // Vector of temporaries.
170  std::size_t m_wrap; // Mask to wrap vector index.
171 
172  public:
173  inline sc_vpool( int log2, T* pool_p=0 );
174  inline ~sc_vpool();
175  inline T* allocate();
176  inline void reset();
177  inline std::size_t size();
178 };
179 
180 template<class T> sc_vpool<T>::sc_vpool( int log2, T* pool_p )
181  : m_pool_i( 0 )
182  , m_pool_p( pool_p ? pool_p : new T[static_cast<std::size_t>(1) << log2] )
183  , m_wrap( ~(static_cast<std::size_t>(-1) << log2) )
184 {
185  // if ( log2 > 32 ) SC_REPORT_ERROR(SC_ID_POOL_SIZE_, "");
186 }
187 
188 template<class T> sc_vpool<T>::~sc_vpool()
189 {
190  // delete [] m_pool_p;
191 }
192 
193 template<class T> T* sc_vpool<T>::allocate()
194 {
195  T* result_p; // Entry to return.
196 
197  result_p = &m_pool_p[m_pool_i];
198  m_pool_i = (m_pool_i + 1) & m_wrap;
199  return result_p;
200 }
201 
202 template<class T> void sc_vpool<T>::reset()
203 {
204  m_pool_i = 0;
205 }
206 
207 template<class T> std::size_t sc_vpool<T>::size()
208 {
209  return m_wrap + 1;
210 }
211 
212 } // namespace sc_core
213 
214 // $Log: sc_temporary.h,v $
215 // Revision 1.4 2011/08/26 20:46:19 acg
216 // Andy Goodrich: moved the modification log to the end of the file to
217 // eliminate source line number skew when check-ins are done.
218 //
219 // Revision 1.3 2011/08/24 22:05:56 acg
220 // Torsten Maehne: initialization changes to remove warnings.
221 //
222 // Revision 1.2 2011/02/18 20:38:44 acg
223 // Andy Goodrich: Updated Copyright notice.
224 //
225 // Revision 1.1.1.1 2006/12/15 20:20:06 acg
226 // SystemC 2.3
227 //
228 // Revision 1.3 2006/01/13 18:53:11 acg
229 // Andy Goodrich: Added $Log command so that CVS comments are reproduced in
230 // the source.
231 //
232 
233 #endif // SC_TEMPORARY_H
std::size_t length()
Definition: sc_temporary.h:105
void initialize(std::size_t heap_size=0x100000)
Definition: sc_temporary.h:97
sc_byte_heap(std::size_t heap_size)
Definition: sc_temporary.h:115
char * allocate(std::size_t bytes_n)
Definition: sc_temporary.h:83
std::size_t m_pool_i
Definition: sc_temporary.h:168
std::size_t m_wrap
Definition: sc_temporary.h:170
sc_vpool(int log2, T *pool_p=0)
Definition: sc_temporary.h:180
STL namespace.
#define SC_API
Definition: sc_cmnhdr.h:168