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
|
/* -*- c++ -*- */
/*
* Copyright 2007 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
* GNU Radio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* GNU Radio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <pmt_pool.h>
#include <algorithm>
#include <stdint.h>
static inline size_t
ROUNDUP(size_t x, size_t stride)
{
return ((((x) + (stride) - 1)/(stride)) * (stride));
}
pmt_pool::pmt_pool(size_t itemsize, size_t alignment, size_t allocation_size)
: d_itemsize(ROUNDUP(itemsize, alignment)),
d_alignment(alignment),
d_allocation_size(std::max(allocation_size, 16 * itemsize)),
d_freelist(0)
{
}
pmt_pool::~pmt_pool()
{
for (unsigned int i = 0; i < d_allocations.size(); i++){
delete [] d_allocations[i];
}
}
void *
pmt_pool::malloc()
{
omni_mutex_lock l(d_mutex);
item *p;
if (d_freelist){ // got something?
p = d_freelist;
d_freelist = p->d_next;
return p;
}
// allocate a new chunk
char *alloc = new char[d_allocation_size + d_alignment - 1];
d_allocations.push_back(alloc);
// get the alignment we require
char *start = (char *)(((uintptr_t)alloc + d_alignment-1) & -d_alignment);
char *end = alloc + d_allocation_size + d_alignment - 1;
size_t n = (end - start) / d_itemsize;
// link the new items onto the free list.
p = (item *) start;
for (size_t i = 0; i < n; i++){
p->d_next = d_freelist;
d_freelist = p;
p = (item *)((char *) p + d_itemsize);
}
// now return the first one
p = d_freelist;
d_freelist = p->d_next;
return p;
}
void
pmt_pool::free(void *foo)
{
if (!foo)
return;
omni_mutex_lock l(d_mutex);
item *p = (item *) foo;
p->d_next = d_freelist;
d_freelist = p;
}
|