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