GNU Radio 3.2.2 C++ API
pmt_pool.h
Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2007 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 <gnuradio/omnithread.h>
00026 #include <vector>
00027 
00028 /*!
00029  * \brief very simple thread-safe fixed-size allocation pool
00030  *
00031  * FIXME may want to go to global allocation with per-thread free list.
00032  * This would eliminate virtually all lock contention.
00033  */
00034 class pmt_pool {
00035 
00036   struct item {
00037     struct item *d_next;
00038   };
00039   
00040   omni_mutex          d_mutex;
00041   omni_condition      d_cond;
00042   
00043   size_t              d_itemsize;
00044   size_t              d_alignment;
00045   size_t              d_allocation_size;
00046   size_t              d_max_items;
00047   size_t              d_n_items;
00048   item               *d_freelist;
00049   std::vector<char *> d_allocations;
00050 
00051 public:
00052   /*!
00053    * \param itemsize size in bytes of the items to be allocated.
00054    * \param alignment alignment in bytes of all objects to be allocated (must be power-of-2).
00055    * \param allocation_size number of bytes to allocate at a time from the underlying allocator.
00056    * \param max_items is the maximum number of items to allocate.  If this number is exceeded,
00057    *          the allocate blocks.  0 implies no limit.
00058    */
00059   pmt_pool(size_t itemsize, size_t alignment = 16,
00060            size_t allocation_size = 4096, size_t max_items = 0);
00061   ~pmt_pool();
00062 
00063   void *malloc();
00064   void free(void *p);
00065 };
00066 
00067 #endif /* INCLUDED_PMT_POOL_H */