| 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.
|
|---|
| 67 |
|
|---|
| 68 | The way we do this is to take a large chunk, allocating memory from
|
|---|
| 69 | low addresses. When you want to build a symbol in the chunk you just
|
|---|
| 70 | add chars above the current "high water mark" in the chunk. When you
|
|---|
| 71 | have finished adding chars, because you got to the end of the symbol,
|
|---|
| 72 | you know how long the chars are, and you can create a new object.
|
|---|
| 73 | Mostly the chars will not burst over the highest address of the chunk,
|
|---|
| 74 | because you would typically expect a chunk to be (say) 100 times as
|
|---|
| 75 | long as an average object.
|
|---|
| 76 |
|
|---|
| 77 | In case that isn't clear, when we have enough chars to make up
|
|---|
| 78 | the object, THEY ARE ALREADY CONTIGUOUS IN THE CHUNK (guaranteed)
|
|---|
| 79 | so we just point to it where it lies. No moving of chars is
|
|---|
| 80 | needed and this is the second win: potentially long strings need
|
|---|
| 81 | never be explicitly shuffled. Once an object is formed, it does not
|
|---|
| 82 | change its address during its lifetime.
|
|---|
| 83 |
|
|---|
| 84 | When the chars burst over a chunk boundary, we allocate a larger
|
|---|
| 85 | chunk, and then copy the partly formed object from the end of the old
|
|---|
| 86 | chunk to the beginning of the new larger chunk. We then carry on
|
|---|
| 87 | accreting characters to the end of the object as we normally would.
|
|---|
| 88 |
|
|---|
| 89 | A special macro is provided to add a single char at a time to a
|
|---|
| 90 | growing object. This allows the use of register variables, which
|
|---|
| 91 | break the ordinary 'growth' macro.
|
|---|
| 92 |
|
|---|
| 93 | Summary:
|
|---|
| 94 | We allocate large chunks.
|
|---|
| 95 | We carve out one object at a time from the current chunk.
|
|---|
| 96 | Once carved, an object never moves.
|
|---|
| 97 | We are free to append data of any size to the currently
|
|---|
| 98 | growing object.
|
|---|
| 99 | Exactly one object is growing in an obstack at any one time.
|
|---|
| 100 | You can run one obstack per control block.
|
|---|
| 101 | You may have as many control blocks as you dare.
|
|---|
| 102 | Because of the way we do it, you can `unwind' an obstack
|
|---|
| 103 | back to a previous state. (You may remove objects much
|
|---|
| 104 | as you would with a stack.)
|
|---|
| 105 | */
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 | /* Don't do the contents of this file more than once. */
|
|---|
| 109 |
|
|---|
| 110 | #ifndef _OBSTACK_H
|
|---|
| 111 | #define _OBSTACK_H 1
|
|---|
| 112 |
|
|---|
| 113 | #ifdef __cplusplus
|
|---|
| 114 | extern "C" {
|
|---|
| 115 | #endif
|
|---|
| 116 | |
|---|
| 117 |
|
|---|
| 118 | /* We use subtraction of (char *) 0 instead of casting to int
|
|---|
|
|---|