| 1 | /* obstack.h - object stack macros
|
|---|
| 2 | Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1996, 1997, 1998,
|
|---|
| 3 | 1999, 2000
|
|---|
| 4 | Free Software Foundation, Inc.
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 | NOTE: The canonical source of this file is maintained with the GNU C Library.
|
|---|
| 8 | Bugs can be reported to [email protected].
|
|---|
| 9 |
|
|---|
| 10 | This program is free software; you can redistribute it and/or modify it
|
|---|
| 11 | under the terms of the GNU General Public License as published by the
|
|---|
| 12 | Free Software Foundation; either version 2, or (at your option) any
|
|---|
| 13 | later version.
|
|---|
| 14 |
|
|---|
| 15 | This program is distributed in the hope that it will be useful,
|
|---|
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 18 | GNU General Public License for more details.
|
|---|
| 19 |
|
|---|
| 20 | You should have received a copy of the GNU General Public License
|
|---|
| 21 | along with this program; if not, write to the Free Software
|
|---|
| 22 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
|---|
| 23 | USA. */
|
|---|
| 24 |
|
|---|
| 25 | /* Summary:
|
|---|
| 26 |
|
|---|
| 27 | All the apparent functions defined here are macros. The idea
|
|---|
| 28 | is that you would use these pre-tested macros to solve a
|
|---|
| 29 | very specific set of problems, and they would run fast.
|
|---|
| 30 | Caution: no side-effects in arguments please!! They may be
|
|---|
| 31 | evaluated MANY times!!
|
|---|
| 32 |
|
|---|
| 33 | These macros operate a stack of objects. Each object starts life
|
|---|
| 34 | small, and may grow to maturity. (Consider building a word syllable
|
|---|
| 35 | by syllable.) An object can move while it is growing. Once it has
|
|---|
| 36 | been "finished" it never changes address again. So the "top of the
|
|---|
| 37 | stack" is typically an immature growing object, while the rest of the
|
|---|
| 38 | stack is of mature, fixed size and fixed address objects.
|
|---|
| 39 |
|
|---|
| 40 | These routines grab large chunks of memory, using a function you
|
|---|
| 41 | supply, called `obstack_chunk_alloc'. On occasion, they free chunks,
|
|---|
| 42 | by calling `obstack_chunk_free'. You must define them and declare
|
|---|
| 43 | them before using any obstack macros.
|
|---|
| 44 |
|
|---|
| 45 | Each independent stack is represented by a `struct obstack'.
|
|---|
| 46 | Each of the obstack macros expects a pointer to such a structure
|
|---|
| 47 | as the first argument.
|
|---|
| 48 |
|
|---|
| 49 | One motivation for this package is the problem of growing char strings
|
|---|
| 50 | in symbol tables. Unless you are "fascist pig with a read-only mind"
|
|---|
| 51 | --Gosper's immortal quote from HAKMEM item 154, out of context--you
|
|---|
| 52 | would not like to put any arbitrary upper limit on the length of your
|
|---|
| 53 | symbols.
|
|---|
| 54 |
|
|---|
| 55 | In practice this often means you will build many short symbols and a
|
|---|
| 56 | few long symbols. At the time you are reading a symbol you don't know
|
|---|
| 57 | how long it is. One traditional method is to read a symbol into a
|
|---|
| 58 | buffer, realloc()ating the buffer every time you try to read a symbol
|
|---|
| 59 | that is longer than the buffer. This is beaut, but you still will
|
|---|
| 60 | want to copy the symbol from the buffer to a more permanent
|
|---|
| 61 | symbol-table entry say about half the time.
|
|---|
| 62 |
|
|---|
| 63 | With obstacks, you can work differently. Use one obstack for all symbol
|
|---|
| 64 | names. As you read a symbol, grow the name in the obstack gradually.
|
|---|
| 65 | When the name is complete, finalize it. Then, if the symbol exists already,
|
|---|
| 66 | free the newly read name.
|
|---|
|
|---|