1 // This may look like C code, but it is really -*- C++ -*- 2 /* 3 Copyright (C) 1989 Free Software Foundation 4 written by Doug Lea (dl@rocky.oswego.edu) 5 6 This file is part of GNU CC. 7 8 GNU CC is distributed in the hope that it will be useful, 9 but WITHOUT ANY WARRANTY. No author or distributor 10 accepts responsibility to anyone for the consequences of using it 11 or for whether it serves any particular purpose or works at all, 12 unless he says so in writing. Refer to the GNU CC General Public 13 License for full details. 14 15 Everyone is granted permission to copy, modify and redistribute 16 GNU CC, but only under the conditions described in the 17 GNU CC General Public License. A copy of this license is 18 supposed to have been given to you along with GNU CC so you 19 can know your rights and responsibilities. It should be in a 20 file named COPYING. Among other things, the copyright notice 21 and this notice must be preserved on all copies. 22 */ 23 24 25 #ifndef _AllocRing_h 26 #ifdef __GNUG__ 27 #pragma once 28 #pragma interface 29 #endif 30 #define _AllocRing_h 1 31 32 33 /* 34 An AllocRing holds the last n malloc'ed strings, reallocating/reusing 35 one only when the queue wraps around. It thus guarantees that the 36 last n allocations are intact. It is useful for things like I/O 37 formatting where reasonable restrictions may be made about the 38 number of allowable live allocations before auto-deletion. 39 */ 40 41 class AllocRing 42 { 43 44 struct AllocQNode 45 { 46 void* ptr; 47 int sz; 48 }; 49 50 AllocQNode* nodes; 51 int n; 52 int current; 53 54 int find(void* p); 55 56 public: 57 58 AllocRing(int max); 59 ~AllocRing(); 60 61 void* alloc(int size); 62 int contains(void* ptr); 63 void clear(); 64 void free(void* p); 65 }; 66 67 68 #endif 69