summaryrefslogtreecommitdiff
path: root/ext/systemc/src/sysc/utils/sc_mempool.cpp
blob: 7369c12a0855536de53b82fbeddad93cce37fb56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*****************************************************************************

  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
  more contributor license agreements.  See the NOTICE file distributed
  with this work for additional information regarding copyright ownership.
  Accellera licenses this file to you under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with the
  License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  implied.  See the License for the specific language governing
  permissions and limitations under the License.

 *****************************************************************************/

/*****************************************************************************

  sc_mempool.cpp - Memory pools for small objects.

  Original Author: Stan Y. Liao, Synopsys, Inc.

  CHANGE LOG AT END OF FILE
 *****************************************************************************/




//  <sc_mempool> is a class that manages the memory for small objects,
//  of sizes <increment>, 2 * <increment>, ..., <num_pools> *
//  <increment>.  When a memory request of <k> bytes is made through
//  the memory pool, the smallest pool <j> such that <j> * <increment>
//  >= <k> is used.  The default values of <increment> and <num_pools>
//  are 8 and 8, respectively.  Each pool has an allocator, that
//  simply keeps a free list of cells, and allocate new blocks
//  whenever necessary.  We are relying on malloc() to return a
//  properly aligned memory blocks.  Note that the memory blocks
//  allocated by the mempool are never freed.  Thus, if purify is
//  used, we may get MIU (memory-in-use) warnings.  To disable this,
//  set the environment variable SYSTEMC_MEMPOOL_DONT_USE to 1.


static const char* dont_use_envstring = "SYSTEMC_MEMPOOL_DONT_USE";
static bool use_default_new = false;


#include <stdio.h>
#include <stdlib.h> // duplicate (c)stdlib.h headers for Solaris
#include <cstdlib>
#include "sysc/utils/sc_mempool.h"

namespace sc_core {

//  An allocator is one that handles a particular size.  It keeps a
//  <free_list> from which a cell may be allocated quickly if there
//  is one available.  If no cell is available from <free_list>, then
//  the allocator tries to find whether space is available from the
//  most-recently-allocated block, as pointed to by <next_avail>.  If
//  so, then the cell pointed to by <next_avail> is returned, while
//  <next_avail> is advanced.  If <next_avail> now points beyond
//  the current block, then it's reset to 0.  On the other hand,
//  if <next_avail> was 0 when a request to the block is made, then
//  a new block is allocated by calling system malloc(), and the new
//  block becomes the head of <block_list>.


class sc_allocator {
    friend class sc_mempool;

public:
    sc_allocator( int blksz, int cellsz );
    ~sc_allocator();
    void* allocate();
    void release(void* p);
    
    void display_statistics();

private:
    union link {
        link* next;
        double align;          // alignment required.
    };

    int block_size;            // size of each block in bytes,
                               // including the link
    int cell_size;             // size of each cell in bytes

    char* block_list;
    link* free_list;
    char* next_avail;

    int total_alloc;
    int total_freed;
    int free_list_alloc;
};

sc_allocator::sc_allocator( int blksz, int cellsz )
  : block_size(sizeof(link) + (((blksz - 1) / cellsz) + 1) * cellsz),
    cell_size(cellsz), block_list(0), free_list(0), next_avail(0),
    total_alloc(0), total_freed(0), free_list_alloc(0)
{}

sc_allocator::~sc_allocator()
{
    // Shouldn't free the block_list, since global objects that use
    // the memory pool may not have been destroyed yet ...
    // Let it leak, let it leak, let it leak ...
}

void*
sc_allocator::allocate()
{
    void* result = 0;
    total_alloc++;
    if (free_list != 0) {
        free_list_alloc++;
        result = free_list;
        free_list = free_list->next;
        return result;
    }
    else if (next_avail != 0) {
        result = next_avail;
        next_avail += cell_size;
        // next_avail goes beyond the block
        if (next_avail >= block_list + block_size)
            next_avail = 0;
        return result;
    }
    else {  // (next_avail == 0)
        link* new_block = (link*) malloc(block_size);  // need alignment?
        new_block->next = (link*) block_list;
        block_list = (char*) new_block;
        result = (block_list + sizeof(link));
        // Assume that the block will hold more than one cell ... why
        // wouldn't it?
        next_avail = ((char*) result) + cell_size;
        return result;
    }
}

void
sc_allocator::release(void* p)
{
    total_freed++;
    ((link*) p)->next = free_list;
    free_list = (link*) p;
}

void
sc_allocator::display_statistics()
{
    int nblocks = 0;
    for (link* b = (link*) block_list; b != 0; b = b->next)
        nblocks++;
    printf("size %3d: %2d block(s), %3d requests (%3d from free list), %3d freed.\n",
           cell_size, nblocks, total_alloc, free_list_alloc, total_freed);
}


static const int cell_sizes[] = {
/* 0 */   0,
/* 1 */   8,
/* 2 */  16,
/* 3 */  24,
/* 4 */  32,
/* 5 */  48,
/* 6 */  64,
/* 7 */  80,
/* 8 */  96,
/* 9 */ 128
};

static const int cell_size_to_allocator[] = {
/*  0 */    0,
/*  1 */    1,
/*  2 */    2,
/*  3 */    3,
/*  4 */    4,
/*  5 */    5,
/*  6 */    5,
/*  7 */    6,
/*  8 */    6,
/*  9 */    7,
/* 10 */    7,
/* 11 */    8,
/* 12 */    8,
/* 13 */    9,
/* 14 */    9,
/* 15 */    9,
/* 16 */    9
};


class sc_mempool_int {
    friend class sc_mempool;

public:
    sc_mempool_int(int blksz, int npools, int incr);
    ~sc_mempool_int();
    void* do_allocate(std::size_t);
    void  do_release(void*, std::size_t);

    void display_statistics();
    
private:
    sc_allocator** allocators;
    int num_pools;
    int increment;
    int max_size;
};


static bool
compute_use_default_new()
{
    const char* e = getenv(dont_use_envstring);
    return (e != 0) && (atoi(e) != 0);
}

sc_mempool_int::sc_mempool_int(int blksz, int npools, int incr) :
    allocators(0), num_pools(0), increment(0), max_size(0)
{
    use_default_new = compute_use_default_new();
    if (! use_default_new) {
        num_pools = npools;
        increment = incr;
        max_size = cell_sizes[sizeof(cell_sizes)/sizeof(cell_sizes[0]) - 1];
        allocators = new sc_allocator*[npools + 1];
        for (int i = 1; i <= npools; ++i)
            allocators[i] = new sc_allocator(blksz, cell_sizes[i]);
        allocators[0] = allocators[1];
    }
}

sc_mempool_int::~sc_mempool_int()
{
    for (int i = 1; i <= num_pools; ++i)
        delete allocators[i];
    delete[] allocators;
}

static sc_mempool_int* the_mempool = 0;

void*
sc_mempool_int::do_allocate(std::size_t sz)
{
    int which_allocator = cell_size_to_allocator[(sz - 1) / increment + 1];
    void* p = allocators[which_allocator]->allocate();
    return p;
}

void
sc_mempool_int::do_release(void* p, std::size_t sz)
{
    int which_allocator = cell_size_to_allocator[(sz - 1) / increment + 1];
    allocators[which_allocator]->release(p);
}

void
sc_mempool_int::display_statistics()
{
    printf("*** Memory Pool Statistics ***\n");
    for (int i = 1; i <= num_pools; ++i)
        allocators[i]->display_statistics();
}

/****************************************************************************/

void*
sc_mempool::allocate(std::size_t sz)
{
    if (use_default_new)
        return ::operator new(sz);

    if (the_mempool == 0) {
        use_default_new = compute_use_default_new();
        if (use_default_new)
            return ::operator new(sz);

        // Note that the_mempool is never freed.  This is going to cause
        // memory leaks when the program exits.
        the_mempool = new sc_mempool_int( 1984, sizeof(cell_sizes)/sizeof(cell_sizes[0]) - 1, 8 );
    }

    if (sz > (unsigned) the_mempool->max_size)
        return ::operator new(sz);

    return the_mempool->do_allocate(sz);
}

void
sc_mempool::release(void* p, std::size_t sz)
{
    if (p) {
        
        if (use_default_new || sz > (unsigned) the_mempool->max_size) {
            ::operator delete(p);
            return;
        }

        the_mempool->do_release(p, sz);
    }
}

void
sc_mempool::display_statistics()
{
    if (the_mempool && !use_default_new) {
        the_mempool->display_statistics();
    } else {
        printf("SystemC info: no memory allocation was done through the memory pool.\n");
    }
}

} // namespace sc_core

// $Log: sc_mempool.cpp,v $
// Revision 1.4  2011/08/26 20:46:18  acg
//  Andy Goodrich: moved the modification log to the end of the file to
//  eliminate source line number skew when check-ins are done.
//
// Revision 1.3  2011/08/24 22:05:56  acg
//  Torsten Maehne: initialization changes to remove warnings.
//
// Revision 1.2  2011/02/18 20:38:44  acg
//  Andy Goodrich: Updated Copyright notice.
//
// Revision 1.1.1.1  2006/12/15 20:20:06  acg
// SystemC 2.3
//
// Revision 1.3  2006/01/13 18:53:10  acg
// Andy Goodrich: Added $Log command so that CVS comments are reproduced in
// the source.

// taf