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  scfx_mant.cpp -
23
24  Original Author: Robert Graulich, Synopsys, Inc.
25                   Martin Janssen,  Synopsys, Inc.
26
27 *****************************************************************************/
28
29/*****************************************************************************
30
31  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
32  changes you are making here.
33
34      Name, Affiliation, Date:
35  Description of Modification:
36
37 *****************************************************************************/
38
39
40// $Log: scfx_mant.cpp,v $
41// Revision 1.1.1.1  2006/12/15 20:20:04  acg
42// SystemC 2.3
43//
44// Revision 1.3  2006/01/13 18:53:58  acg
45// Andy Goodrich: added $Log command so that CVS comments are reproduced in
46// the source.
47//
48
49#include "systemc/ext/dt/fx/scfx_mant.hh"
50
51namespace sc_dt
52{
53
54// ----------------------------------------------------------------------------
55//  word memory management
56// ----------------------------------------------------------------------------
57
58class word_list // Entry in free_words bucket.
59{
60  public:
61    word_list *m_next_p;
62};
63
64static inline unsigned
65next_pow2_index(std::size_t size)
66{
67    unsigned index = scfx_find_msb(size);
68    // If this was a power of 2 we are one bucket too low.
69    if (~(UINT64_ONE << index) & size)
70        index++;
71    // If this is a 64-bit machine and we are using 32-bit words go down
72    // one slot size, as all the slots are 2x in size.
73    if (index != 0 && (sizeof(word_list) != sizeof(word))) {
74        index -= 1;
75    }
76    return index;
77}
78
79static word_list *free_words[32] = { 0 };
80
81word *
82scfx_mant::alloc_word(std::size_t size)
83{
84    const int ALLOC_SIZE = 128;
85
86    unsigned slot_index = next_pow2_index(size);
87    unsigned alloc_size = (UINT64_ONE << slot_index);
88
89    word_list*& slot = free_words[slot_index];
90
91    if (!slot) {
92        slot = new word_list[ALLOC_SIZE * alloc_size];
93        unsigned i;
94        for (i = 0; i < alloc_size * (ALLOC_SIZE - 1) ; i += alloc_size) {
95            slot[i].m_next_p = &slot[i + alloc_size];
96        }
97        slot[i].m_next_p = 0;
98    }
99
100    word *result = (word *)slot;
101    free_words[slot_index] = slot[0].m_next_p;
102    return result;
103}
104
105void
106scfx_mant::free_word(word *array, std::size_t size)
107{
108    if (array && size) {
109        int slot_index = next_pow2_index(size);
110        word_list *wl_p = (word_list *)array;
111
112        wl_p->m_next_p = free_words[slot_index];
113        free_words[slot_index] = wl_p;
114    }
115}
116
117} // namespace sc_dt
118