GNU Radio 3.4.2 C++ API
pmt_pool.h
Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2007,2009 Free Software Foundation, Inc.
00004  * 
00005  * This file is part of GNU Radio
00006  * 
00007  * GNU Radio is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 3, or (at your option)
00010  * any later version.
00011  * 
00012  * GNU Radio is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License along
00018  * with this program; if not, write to the Free Software Foundation, Inc.,
00019  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00020  */
00021 #ifndef INCLUDED_PMT_POOL_H
00022 #define INCLUDED_PMT_POOL_H
00023 
00024 #include <cstddef>
00025 #include <vector>
00026 #include <boost/thread.hpp>
00027 
00028 namespace pmt {
00029 
00030 /*!
00031  * \brief very simple thread-safe fixed-size allocation pool
00032  *
00033  * FIXME may want to go to global allocation with per-thread free list.
00034  * This would eliminate virtually all lock contention.
00035  */
00036 class pmt_pool {
00037 
00038   struct item {
00039     struct item *d_next;
00040   };
00041   
00042   typedef boost::unique_lock<boost::mutex>  scoped_lock;
00043   mutable boost::mutex          d_mutex;
00044   boost::condition_variable     d_cond;
00045   
00046   size_t              d_itemsize;
00047   size_t              d_alignment;
00048   size_t              d_allocation_size;
00049   size_t              d_max_items;
00050   size_t              d_n_items;
00051   item               *d_freelist;
00052   std::vector<char *> d_allocations;
00053 
00054 public:
00055   /*!
00056    * \param itemsize size in bytes of the items to be allocated.
00057    * \param alignment alignment in bytes of all objects to be allocated (must be power-of-2).
00058    * \param allocation_size number of bytes to allocate at a time from the underlying allocator.
00059    * \param max_items is the maximum number of items to allocate.  If this number is exceeded,
00060    *          the allocate blocks.  0 implies no limit.
00061    */
00062   pmt_pool(size_t itemsize, size_t alignment = 16,
00063            size_t allocation_size = 4096, size_t max_items = 0);
00064   ~pmt_pool();
00065 
00066   void *malloc();
00067   void free(void *p);
00068 };
00069 
00070 } /* namespace pmt */
00071 
00072 #endif /* INCLUDED_PMT_POOL_H */