Sync usage with man page.
[netbsd-mini2440.git] / gnu / lib / libg++ / g++-include / Obstack.h
blob0cc2e9bb378870eafe65aade18fcd83ba30d13f1
1 // This may look like C code, but it is really -*- C++ -*-
2 /*
3 Copyright (C) 1988 Free Software Foundation
4 written by Doug Lea (dl@rocky.oswego.edu)
6 This file is part of GNU CC.
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.
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.
25 #ifndef _Obstack_h
26 #ifdef __GNUG__
27 #pragma once
28 #pragma interface
29 #endif
30 #define _Obstack_h 1
32 #include <std.h>
34 class Obstack
36 struct _obstack_chunk
38 char* limit;
39 _obstack_chunk* prev;
40 char contents[4];
43 protected:
44 long chunksize;
45 _obstack_chunk* chunk;
46 char* objectbase;
47 char* nextfree;
48 char* chunklimit;
49 int alignmentmask;
51 void _free(void* obj);
52 void newchunk(int size);
54 public:
55 Obstack(int size = 4080, int alignment = 4); // 4080=4096-mallocslop
57 ~Obstack();
59 void* base();
60 void* next_free();
61 int alignment_mask();
62 int chunk_size();
63 int size();
64 int room();
65 int contains(void* p); // does Obstack hold pointer p?
67 void grow(const void* data, int size);
68 void grow(const void* data, int size, char terminator);
69 void grow(const char* s);
70 void grow(char c);
71 void grow_fast(char c);
72 void blank(int size);
73 void blank_fast(int size);
75 void* finish();
76 void* finish(char terminator);
78 void* copy(const void* data, int size);
79 void* copy(const void* data, int size, char terminator);
80 void* copy(const char* s);
81 void* copy(char c);
82 void* alloc(int size);
84 void free(void* obj);
85 void shrink(int size = 1); // suggested by ken@cs.rochester.edu
87 int OK(); // rep invariant
90 #if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
93 inline Obstack::~Obstack()
95 _free(0);
98 inline void* Obstack::base()
100 return objectbase;
103 inline void* Obstack::next_free()
105 return nextfree;
108 inline int Obstack::alignment_mask()
110 return alignmentmask;
113 inline int Obstack::chunk_size()
115 return chunksize;
118 inline int Obstack::size()
120 return nextfree - objectbase;
123 inline int Obstack::room()
125 return chunklimit - nextfree;
128 inline void Obstack:: grow(const void* data, int size)
130 if (nextfree+size > chunklimit)
131 newchunk(size);
132 bcopy(data, nextfree, size);
133 nextfree += size;
136 inline void Obstack:: grow(const void* data, int size, char terminator)
138 if (nextfree+size+1 > chunklimit)
139 newchunk(size+1);
140 bcopy(data, nextfree, size);
141 nextfree += size;
142 *(nextfree)++ = terminator;
145 inline void Obstack:: grow(const char* s)
147 grow((void*)s, strlen(s), 0);
150 inline void Obstack:: grow(char c)
152 if (nextfree+1 > chunklimit)
153 newchunk(1);
154 *(nextfree)++ = c;
157 inline void Obstack:: blank(int size)
159 if (nextfree+size > chunklimit)
160 newchunk(size);
161 nextfree += size;
164 inline void* Obstack::finish(char terminator)
166 grow(terminator);
167 return finish();
170 inline void* Obstack::copy(const void* data, int size)
172 grow (data, size);
173 return finish();
176 inline void* Obstack::copy(const void* data, int size, char terminator)
178 grow(data, size, terminator);
179 return finish();
182 inline void* Obstack::copy(const char* s)
184 grow((void*)s, strlen(s), 0);
185 return finish();
188 inline void* Obstack::copy(char c)
190 grow(c);
191 return finish();
194 inline void* Obstack::alloc(int size)
196 blank(size);
197 return finish();
200 inline void Obstack:: free(void* obj)
202 if (obj >= (void*)chunk && obj<(void*)chunklimit)
203 nextfree = objectbase = (char *) obj;
204 else
205 _free(obj);
208 inline void Obstack:: grow_fast(char c)
210 *(nextfree)++ = c;
213 inline void Obstack:: blank_fast(int size)
215 nextfree += size;
218 inline void Obstack:: shrink(int size) // from ken@cs.rochester.edu
220 if (nextfree >= objectbase + size)
221 nextfree -= size;
224 #endif
226 #endif