[176] | 1 | // -*- C++ -*- |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | |
---|
| 5 | Heap Layers: An Extensible Memory Allocation Infrastructure |
---|
| 6 | |
---|
| 7 | Copyright (C) 2000-2003 by Emery Berger |
---|
| 8 | http://www.cs.umass.edu/~emery |
---|
| 9 | emery@cs.umass.edu |
---|
| 10 | |
---|
| 11 | This program is free software; you can redistribute it and/or modify |
---|
| 12 | it under the terms of the GNU General Public License as published by |
---|
| 13 | the Free Software Foundation; either version 2 of the License, or |
---|
| 14 | (at your option) any later version. |
---|
| 15 | |
---|
| 16 | This program is distributed in the hope that it will be useful, |
---|
| 17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 19 | GNU General Public License for more details. |
---|
| 20 | |
---|
| 21 | You should have received a copy of the GNU General Public License |
---|
| 22 | along with this program; if not, write to the Free Software |
---|
| 23 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 24 | |
---|
| 25 | */ |
---|
| 26 | |
---|
| 27 | |
---|
| 28 | #ifndef _ADAPT_H_ |
---|
| 29 | #define _ADAPT_H_ |
---|
| 30 | |
---|
| 31 | /** |
---|
| 32 | * @class AdaptHeap |
---|
| 33 | * @brief Maintains dictionary entries through freed objects. |
---|
| 34 | * Sample dictionaries include DLList and SLList. |
---|
| 35 | */ |
---|
| 36 | |
---|
| 37 | namespace HL { |
---|
| 38 | |
---|
| 39 | template <class Dictionary, class SuperHeap> |
---|
| 40 | class AdaptHeap : public SuperHeap { |
---|
| 41 | public: |
---|
| 42 | |
---|
| 43 | /// Allocate an object (remove from the dictionary). |
---|
| 44 | inline void * malloc (const size_t) { |
---|
| 45 | void * ptr = (Entry *) dict.get(); |
---|
| 46 | return ptr; |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | /// Deallocate the object (return to the dictionary). |
---|
| 50 | inline void free (void * ptr) { |
---|
| 51 | Entry * entry = (Entry *) ptr; |
---|
| 52 | dict.insert (entry); |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | /// Remove an object from the dictionary. |
---|
| 56 | inline int remove (void * ptr) { |
---|
| 57 | dict.remove ((Entry *) ptr); |
---|
| 58 | return 1; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | /// Clear the dictionary. |
---|
| 62 | inline void clear (void) { |
---|
| 63 | Entry * ptr; |
---|
| 64 | while ((ptr = (Entry *) dict.get()) != NULL) { |
---|
| 65 | SuperHeap::free (ptr); |
---|
| 66 | } |
---|
| 67 | dict.clear(); |
---|
| 68 | SuperHeap::clear(); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | private: |
---|
| 73 | |
---|
| 74 | /// The dictionary object. |
---|
| 75 | Dictionary dict; |
---|
| 76 | |
---|
| 77 | class Entry : public Dictionary::Entry {}; |
---|
| 78 | }; |
---|
| 79 | |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | #endif // _ADAPT_H_ |
---|