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