3 * Copyright 2007 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
7 * GNU Radio is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3, or (at your option)
12 * GNU Radio is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 ROUNDUP(size_t x, size_t stride)
32 return ((((x) + (stride) - 1)/(stride)) * (stride));
35 pmt_pool::pmt_pool(size_t itemsize, size_t alignment,
36 size_t allocation_size, size_t max_items)
38 d_itemsize(ROUNDUP(itemsize, alignment)),
39 d_alignment(alignment),
40 d_allocation_size(std::max(allocation_size, 16 * itemsize)),
41 d_max_items(max_items), d_n_items(0),
48 for (unsigned int i = 0; i < d_allocations.size(); i++){
49 delete [] d_allocations[i];
56 omni_mutex_lock l(d_mutex);
59 if (d_max_items != 0){
60 while (d_n_items >= d_max_items)
64 if (d_freelist){ // got something?
66 d_freelist = p->d_next;
71 // allocate a new chunk
72 char *alloc = new char[d_allocation_size + d_alignment - 1];
73 d_allocations.push_back(alloc);
75 // get the alignment we require
76 char *start = (char *)(((uintptr_t)alloc + d_alignment-1) & -d_alignment);
77 char *end = alloc + d_allocation_size + d_alignment - 1;
78 size_t n = (end - start) / d_itemsize;
80 // link the new items onto the free list.
82 for (size_t i = 0; i < n; i++){
83 p->d_next = d_freelist;
85 p = (item *)((char *) p + d_itemsize);
88 // now return the first one
90 d_freelist = p->d_next;
96 pmt_pool::free(void *foo)
101 omni_mutex_lock l(d_mutex);
103 item *p = (item *) foo;
104 p->d_next = d_freelist;
107 if (d_max_items != 0)