GNU Radio 3.6.5 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 <gruel/api.h>
00025 #include <cstddef>
00026 #include <vector>
00027 #include <boost/thread.hpp>
00028 
00029 namespace pmt {
00030 
00031 /*!
00032  * \brief very simple thread-safe fixed-size allocation pool
00033  *
00034  * FIXME may want to go to global allocation with per-thread free list.
00035  * This would eliminate virtually all lock contention.
00036  */
00037 class GRUEL_API pmt_pool {
00038 
00039   struct GRUEL_API item {
00040     struct item *d_next;
00041   };
00042 
00043   typedef boost::unique_lock<boost::mutex>  scoped_lock;
00044   mutable boost::mutex          d_mutex;
00045   boost::condition_variable     d_cond;
00046 
00047   size_t              d_itemsize;
00048   size_t              d_alignment;
00049   size_t              d_allocation_size;
00050   size_t              d_max_items;
00051   size_t              d_n_items;
00052   item               *d_freelist;
00053   std::vector<char *> d_allocations;
00054 
00055 public:
00056   /*!
00057    * \param itemsize size in bytes of the items to be allocated.
00058    * \param alignment alignment in bytes of all objects to be allocated (must be power-of-2).
00059    * \param allocation_size number of bytes to allocate at a time from the underlying allocator.
00060    * \param max_items is the maximum number of items to allocate.  If this number is exceeded,
00061    *          the allocate blocks.  0 implies no limit.
00062    */
00063   pmt_pool(size_t itemsize, size_t alignment = 16,
00064            size_t allocation_size = 4096, size_t max_items = 0);
00065   ~pmt_pool();
00066 
00067   void *malloc();
00068   void free(void *p);
00069 };
00070 
00071 } /* namespace pmt */
00072 
00073 #endif /* INCLUDED_PMT_POOL_H */