00001 /** @file rutz/freelist.h memory allocation via a free-list pool */ 00002 00003 /////////////////////////////////////////////////////////////////////// 00004 // 00005 // Copyright (c) 2001-2004 California Institute of Technology 00006 // Copyright (c) 2004-2007 University of Southern California 00007 // Rob Peters <rjpeters at usc dot edu> 00008 // 00009 // created: Fri Jul 20 07:54:29 2001 00010 // commit: $Id: freelist.h 8291 2007-04-24 00:16:26Z rjpeters $ 00011 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/rutz/freelist.h $ 00012 // 00013 // -------------------------------------------------------------------- 00014 // 00015 // This file is part of GroovX. 00016 // [http://ilab.usc.edu/rjpeters/groovx/] 00017 // 00018 // GroovX is free software; you can redistribute it and/or modify it 00019 // under the terms of the GNU General Public License as published by 00020 // the Free Software Foundation; either version 2 of the License, or 00021 // (at your option) any later version. 00022 // 00023 // GroovX is distributed in the hope that it will be useful, but 00024 // WITHOUT ANY WARRANTY; without even the implied warranty of 00025 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00026 // General Public License for more details. 00027 // 00028 // You should have received a copy of the GNU General Public License 00029 // along with GroovX; if not, write to the Free Software Foundation, 00030 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 00031 // 00032 /////////////////////////////////////////////////////////////////////// 00033 00034 #ifndef GROOVX_RUTZ_FREELIST_H_UTC20050626084020_DEFINED 00035 #define GROOVX_RUTZ_FREELIST_H_UTC20050626084020_DEFINED 00036 00037 #include <cstddef> 00038 00039 namespace rutz 00040 { 00041 class free_list_base; 00042 template <class T> class free_list; 00043 } 00044 00045 /// Un-typesafe base class for maintaining a free-list memory pool. 00046 class rutz::free_list_base 00047 { 00048 private: 00049 /// Free-node class for free-list memory pools. 00050 struct node 00051 { 00052 node* next; 00053 }; 00054 00055 node* m_node_list; 00056 std::size_t m_num_allocations; 00057 const std::size_t m_size_check; 00058 00059 free_list_base(const free_list_base&); 00060 free_list_base& operator=(const free_list_base&); 00061 00062 public: 00063 /// Construct an (empty) free list. 00064 /** All objects from this list must be of size \a size_check. */ 00065 free_list_base(std::size_t size_check); 00066 00067 /// Allocate space for a new object. 00068 /** If there are chunks available in the free list, one of those is 00069 returned; otherwise new memory is allocated with malloc() or 00070 equivalent. */ 00071 void* allocate(std::size_t bytes); 00072 00073 /// Return an object to the free list. 00074 void deallocate(void* space); 00075 00076 /// Release all nodes currently on the free list (e.g. to conserve memory). 00077 void release_free_nodes(); 00078 00079 /// Query the chunk size that this freelist is for. 00080 std::size_t alloc_size() const { return m_size_check; } 00081 00082 /// Query how many allocations have been made. 00083 std::size_t num_allocations() const { return m_num_allocations; } 00084 }; 00085 00086 /// Typesafe wrapper of free_list_base for maintaining free-list memory pools. 00087 template <class T> 00088 class rutz::free_list 00089 { 00090 private: 00091 rutz::free_list_base m_base; 00092 00093 public: 00094 /// Construct an (empty) free list. 00095 /** All objects allocated from this list must be of size sizeof(T). */ 00096 free_list() : m_base(sizeof(T)) {} 00097 00098 void* allocate(std::size_t bytes) 00099 { 00100 return m_base.allocate(bytes); 00101 } 00102 00103 void deallocate(void* space) 00104 { 00105 m_base.deallocate(space); 00106 } 00107 00108 /// Release all nodes currently on the free list (e.g. to conserve memory). 00109 void release_free_nodes() { m_base.release_free_nodes(); } 00110 }; 00111 00112 static const char __attribute__((used)) vcid_groovx_rutz_freelist_h_utc20050626084020[] = "$Id: freelist.h 8291 2007-04-24 00:16:26Z rjpeters $ $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/rutz/freelist.h $"; 00113 #endif // !GROOVX_RUTZ_FREELIST_H_UTC20050626084020_DEFINED