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 "sysc/datatypes/fx/scfx_mant.h"
50
51
52namespace sc_dt
53{
54
55// ----------------------------------------------------------------------------
56//  word memory management
57// ----------------------------------------------------------------------------
58
59class word_list { // Entry in free_words bucket.
60  public:
61    word_list* m_next_p;
62};
63
64static inline
65int
66next_pow2_index( std::size_t size )
67{
68    int index = scfx_find_msb( size );
69    // If this was a power of 2 we are one bucket too low.
70    if( ~ (1 << index) & size ) 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	{
75		index -= 1;
76	}
77    return index;
78}
79
80static word_list* free_words[32] = { 0 };
81
82word*
83scfx_mant::alloc_word( std::size_t size )
84{
85    const int ALLOC_SIZE = 128;
86
87    int slot_index = next_pow2_index( size );
88
89    int alloc_size = ( 1 << slot_index );
90
91    word_list*& slot = free_words[slot_index];
92
93    if( ! slot )
94    {
95        slot = new word_list[ALLOC_SIZE * alloc_size];
96	int i;
97	for( i = 0; i < alloc_size*(ALLOC_SIZE-1) ; i+=alloc_size )
98	{
99	    slot[i].m_next_p = &slot[i+alloc_size];
100	}
101	slot[i].m_next_p = 0;
102    }
103
104    word* result = (word*)slot;
105    free_words[slot_index] = slot[0].m_next_p;
106    return result;
107}
108
109void
110scfx_mant::free_word( word* array, std::size_t size )
111{
112    if( array && size )
113    {
114        int slot_index = next_pow2_index( size );
115	word_list* wl_p = (word_list*)array;
116
117	wl_p->m_next_p = free_words[slot_index];
118	free_words[slot_index] = wl_p;
119    }
120}
121
122} // namespace sc_dt
123
124
125// Taf!
126