| 1 | @node Obstacks,Licenses,Functions,Top
|
|---|
| 2 | @chapter Obstacks
|
|---|
| 3 | @cindex obstacks
|
|---|
| 4 |
|
|---|
| 5 | An @dfn{obstack} is a pool of memory containing a stack of objects. You
|
|---|
| 6 | can create any number of separate obstacks, and then allocate objects in
|
|---|
| 7 | specified obstacks. Within each obstack, the last object allocated must
|
|---|
| 8 | always be the first one freed, but distinct obstacks are independent of
|
|---|
| 9 | each other.
|
|---|
| 10 |
|
|---|
| 11 | Aside from this one constraint of order of freeing, obstacks are totally
|
|---|
| 12 | general: an obstack can contain any number of objects of any size. They
|
|---|
| 13 | are implemented with macros, so allocation is usually very fast as long as
|
|---|
| 14 | the objects are usually small. And the only space overhead per object is
|
|---|
| 15 | the 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 |
|
|---|
| 38 | The utilities for manipulating obstacks are declared in the header
|
|---|
| 39 | file @file{obstack.h}.
|
|---|
| 40 | @pindex obstack.h
|
|---|
| 41 |
|
|---|
| 42 | @comment obstack.h
|
|---|
| 43 | @comment GNU
|
|---|
| 44 | @deftp {Data Type} {struct obstack}
|
|---|
| 45 | An obstack is represented by a data structure of type @code{struct
|
|---|
| 46 | obstack}. This structure has a small fixed size; it records the status
|
|---|
| 47 | of the obstack and how to find the space in which objects are allocated.
|
|---|
| 48 | It does not contain any of the objects themselves. You should not try
|
|---|
| 49 | to access the contents of the structure directly; use only the functions
|
|---|
| 50 | described in this chapter.
|
|---|
| 51 | @end deftp
|
|---|
| 52 |
|
|---|
| 53 | You can declare variables of type @code{struct obstack} and use them as
|
|---|
| 54 | obstacks, or you can allocate obstacks dynamically like any other kind
|
|---|
| 55 | of object. Dynamic allocation of obstacks allows your program to have a
|
|---|
| 56 | variable number of different stacks. (You can even allocate an
|
|---|
| 57 | obstack structure in another obstack, but this is rarely useful.)
|
|---|
| 58 |
|
|---|
| 59 | All the functions that work with obstacks require you to specify which
|
|---|
| 60 | obstack to use. You do this with a pointer of type @code{struct obstack
|
|---|
| 61 | *}. In the following, we often say ``an obstack'' when strictly
|
|---|
| 62 | speaking the object at hand is such a pointer.
|
|---|
| 63 |
|
|---|
| 64 | The objects in the obstack are packed into large blocks called
|
|---|
| 65 | @dfn{chunks}. The @code{struct obstack} structure points to a chain of
|
|---|
| 66 | the chunks currently in use.
|
|---|
| 67 |
|
|---|
| 68 | The obstack library obtains a new chunk whenever you allocate an object
|
|---|
| 69 | that won't fit in the previous chunk. Since the obstack library manages
|
|---|
| 70 | chunks automatically, you don't need to pay much attention to them, but
|
|---|
| 71 | you do need to supply a function which the obstack library should use to
|
|---|
| 72 | get a chunk. Usually you supply a function which uses @code{malloc}
|
|---|
| 73 | directly or indirectly. You must also supply a function to free a chunk.
|
|---|
| 74 | These matters are described in the following section.
|
|---|
| 75 |
|
|---|
| 76 | @node Preparing for Obstacks
|
|---|
| 77 | @section Preparing for Using Obstacks
|
|---|
| 78 |
|
|---|
| 79 | Each source file in which you plan to use the obstack functions
|
|---|
| 80 | must 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
|
|---|
| 88 | Also, if the source file uses the macro @code{obstack_init}, it must
|
|---|
| 89 | declare or define two functions or macros that will be called by the
|
|---|
| 90 | obstack library. One, @code{obstack_chunk_alloc}, is used to allocate
|
|---|
| 91 | the chunks of memory into which objects are packed. The other,
|
|---|
|
|---|