source: trunk/src/gcc/libiberty/obstacks.texi@ 2228

Last change on this file since 2228 was 2, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 29.5 KB
Line 
1@node Obstacks,Licenses,Functions,Top
2@chapter Obstacks
3@cindex obstacks
4
5An @dfn{obstack} is a pool of memory containing a stack of objects. You
6can create any number of separate obstacks, and then allocate objects in
7specified obstacks. Within each obstack, the last object allocated must
8always be the first one freed, but distinct obstacks are independent of
9each other.
10
11Aside from this one constraint of order of freeing, obstacks are totally
12general: an obstack can contain any number of objects of any size. They
13are implemented with macros, so allocation is usually very fast as long as
14the objects are usually small. And the only space overhead per object is
15the padding needed to start each object on a suitable boundary.
16
17@menu
18* Creating Obstacks:: How to declare an obstack in your program.
19* Preparing for Obstacks:: Preparations needed before you can
20 use obstacks.
21* Allocation in an Obstack:: Allocating objects in an obstack.
22* Freeing Obstack Objects:: Freeing objects in an obstack.
23* Obstack Functions:: The obstack functions are both
24 functions and macros.
25* Growing Objects:: Making an object bigger by stages.
26* Extra Fast Growing:: Extra-high-efficiency (though more
27 complicated) growing objects.
28* Status of an Obstack:: Inquiries about the status of an obstack.
29* Obstacks Data Alignment:: Controlling alignment of objects in obstacks.
30* Obstack Chunks:: How obstacks obtain and release chunks;
31 efficiency considerations.
32* Summary of Obstacks::
33@end menu
34
35@node Creating Obstacks
36@section Creating Obstacks
37
38The utilities for manipulating obstacks are declared in the header
39file @file{obstack.h}.
40@pindex obstack.h
41
42@comment obstack.h
43@comment GNU
44@deftp {Data Type} {struct obstack}
45An obstack is represented by a data structure of type @code{struct
46obstack}. This structure has a small fixed size; it records the status
47of the obstack and how to find the space in which objects are allocated.
48It does not contain any of the objects themselves. You should not try
49to access the contents of the structure directly; use only the functions
50described in this chapter.
51@end deftp
52
53You can declare variables of type @code{struct obstack} and use them as
54obstacks, or you can allocate obstacks dynamically like any other kind
55of object. Dynamic allocation of obstacks allows your program to have a
56variable number of different stacks. (You can even allocate an
57obstack structure in another obstack, but this is rarely useful.)
58
59All the functions that work with obstacks require you to specify which
60obstack to use. You do this with a pointer of type @code{struct obstack
61*}. In the following, we often say ``an obstack'' when strictly
62speaking the object at hand is such a pointer.
63
64The objects in the obstack are packed into large blocks called
65@dfn{chunks}. The @code{struct obstack} structure points to a chain of
66the chunks currently in use.
67
68The obstack library obtains a new chunk whenever you allocate an object
69that won't fit in the previous chunk. Since the obstack library manages
70chunks automatically, you don't need to pay much attention to them, but
71you do need to supply a function which the obstack library should use to
72get a chunk. Usually you supply a function which uses @code{malloc}
73directly or indirectly. You must also supply a function to free a chunk.
74These matters are described in the following section.
75
76@node Preparing for Obstacks
77@section Preparing for Using Obstacks
78
79Each source file in which you plan to use the obstack functions
80must include the header file @file{obstack.h}, like this:
81
82@smallexample
83#include <obstack.h>
84@end smallexample
85
86@findex obstack_chunk_alloc
87@findex obstack_chunk_free
88Also, if the source file uses the macro @code{obstack_init}, it must
89declare or define two functions or macros that will be called by the
90obstack library. One, @code{obstack_chunk_alloc}, is used to allocate
91the chunks of memory into which objects are packed. The other,