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