00001 /* 00002 * free_list.h 00003 * 00004 * Copyright (C) 2010 Thomas A. Vaughan 00005 * All rights reserved. 00006 * 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions are met: 00010 * * Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * * Redistributions in binary form must reproduce the above copyright 00013 * notice, this list of conditions and the following disclaimer in the 00014 * documentation and/or other materials provided with the distribution. 00015 * * Neither the name of the <organization> nor the 00016 * names of its contributors may be used to endorse or promote products 00017 * derived from this software without specific prior written permission. 00018 * 00019 * THIS SOFTWARE IS PROVIDED BY THOMAS A. VAUGHAN ''AS IS'' AND ANY 00020 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00022 * DISCLAIMED. IN NO EVENT SHALL THOMAS A. VAUGHAN BE LIABLE FOR ANY 00023 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00026 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 * 00030 * 00031 * Template for a free list of objects 00032 */ 00033 00034 #ifndef WAVEPACKET_UTIL_FREE_LIST_H__ 00035 #define WAVEPACKET_UTIL_FREE_LIST_H__ 00036 00037 // includes -------------------------------------------------------------------- 00038 #include "common/common.h" 00039 #include "threadsafe/smart_ptr.h" 00040 00041 00042 /// \ingroup util 00043 /// 00044 /// Simple free list manager. Your class T must have a public 'next' member. 00045 /// The 'next' member MUST be a smart_ptr<T>. 00046 template <class T> 00047 class free_list_t { 00048 public: 00049 // constructor --------------------------------------------------------- 00050 free_list_t(IN int maxSize) throw() { 00051 ASSERT(maxSize > 0, "Bad maximum size: %d", maxSize); 00052 m_max = maxSize; 00053 m_size = 0; 00054 } 00055 00056 ~free_list_t(void) throw() { this->clear(); } 00057 00058 // free_list_t - public class methods ---------------------------------- 00059 smart_ptr<T> getElement(void) { 00060 smart_ptr<T> retval; 00061 if (m_head) { 00062 retval = m_head; 00063 m_head = retval->next; 00064 retval->next = NULL; 00065 --m_size; 00066 } else { 00067 retval = new T; 00068 ASSERT(retval, 00069 "Failed to allocate from free_list_t"); 00070 retval->next = NULL; 00071 } 00072 return retval; 00073 } 00074 00075 void returnElement(IN smart_ptr<T>& p) throw() { 00076 ASSERT(p, "returning null object to free_list_t"); 00077 ASSERT(!p->next, "canot return list to free_list_t"); 00078 if (m_size >= m_max) { 00079 p = NULL; // just delete outright 00080 } else { 00081 // add p to head of list 00082 p->next = m_head; 00083 m_head = p; 00084 ++m_size; 00085 } 00086 } 00087 00088 void clear(void) throw() { 00089 m_head = NULL; 00090 m_size = 0; 00091 } 00092 00093 int size(void) const throw() { return m_size; } 00094 00095 private: 00096 // private member data ------------------------------------------------- 00097 smart_ptr<T> m_head; 00098 int m_size; 00099 int m_max; 00100 }; 00101 00102 00103 00104 #endif // WAVEPACKET_UTIL_FREE_LIST_H__ 00105