obstack.h File Reference
object stack macros. Summary:
More...
This graph shows which files directly or indirectly include this file:
Go to the source code of this file.
|
Classes |
struct | _obstack_chunk |
| Obstack chunk. This is the header for each chunk of memory in the obstack. More...
|
struct | obstack |
| object stack. Control block for each object in obstack. More...
|
Defines |
#define | __PTR_TO_INT(P) ((P) - (char *)0) |
| Pointer to int.
|
#define | __INT_TO_PTR(P) ((P) + (char *)0) |
| integer to pointer.
|
#define | PTR_INT_TYPE long |
#define | obstack_base(h) ((h)->alloc_failed ? 0 : (h)->object_base) |
| Obstack base pointer.
|
#define | obstack_chunk_size(h) ((h)->chunk_size) |
| Size for allocating ordinary chunks.
|
#define | obstack_next_free(h) ((h)->alloc_failed ? 0 : (h)->next_free) |
| Next free byte.
|
#define | obstack_alignment_mask(h) ((h)->alignment_mask) |
| Alighment mask.
|
#define | obstack_init(h) |
#define | obstack_begin(h, size) |
#define | obstack_specify_allocation(h, size, alignment, chunkfun, freefun) |
#define | obstack_specify_allocation_with_arg(h, size, alignment, chunkfun, freefun, arg) |
#define | obstack_chunkfun(h, newchunkfun) ((h) -> chunkfun = (struct _obstack_chunk *(*)()) (newchunkfun)) |
#define | obstack_freefun(h, newfreefun) ((h) -> freefun = (void (*)()) (newfreefun)) |
#define | obstack_1grow_fast(h, achar) (*((h)->next_free)++ = achar) |
#define | obstack_blank_fast(h, n) ((h)->next_free += (n)) |
#define | obstack_memory_used(h) _obstack_memory_used (h) |
#define | obstack_object_size(h) (unsigned) ((h)->alloc_failed ? 0 : (h)->next_free - (h)->object_base) |
#define | obstack_room(h) (unsigned) ((h)->chunk_limit - (h)->next_free) |
#define | obstack_grow(h, where, length) |
#define | obstack_grow0(h, where, length) |
#define | obstack_1grow(h, datum) |
#define | obstack_ptr_grow(h, datum) |
#define | obstack_int_grow(h, datum) |
#define | obstack_ptr_grow_fast(h, aptr) (*((char **)(h)->next_free)++ = (char *)aptr) |
#define | obstack_int_grow_fast(h, aint) (*((int *)(h)->next_free)++ = (int)aint) |
#define | obstack_blank(h, length) |
#define | obstack_alloc(h, length) (obstack_blank ((h), (length)), obstack_finish ((h))) |
#define | obstack_copy(h, where, length) (obstack_grow ((h), (where), (length)), obstack_finish ((h))) |
#define | obstack_copy0(h, where, length) (obstack_grow0 ((h), (where), (length)), obstack_finish ((h))) |
#define | obstack_finish(h) |
#define | obstack_free(h, obj) |
Functions |
void | _obstack_newchunk () |
void | _obstack_free () |
int | _obstack_begin () |
int | _obstack_begin_1 () |
int | _obstack_memory_used () |
Detailed Description
object stack macros. Summary:
All the apparent functions defined here are macros. The idea is that you would use these pre-tested macros to solve a very specific set of problems, and they would run fast. Caution: no side-effects in arguments please!! They may be evaluated MANY times!!
These macros operate a stack of objects. Each object starts life small, and may grow to maturity. (Consider building a word syllable by syllable.) An object can move while it is growing. Once it has been "finished" it never changes address again. So the "top of the stack" is typically an immature growing object, while the rest of the stack is of mature, fixed size and fixed address objects.
These routines grab large chunks of memory, using a function you supply, called `obstack_chunk_alloc'. On occasion, they free chunks, by calling `obstack_chunk_free'. You must define them and declare them before using any obstack macros.
Each independent stack is represented by a `struct obstack'. Each of the obstack macros expects a pointer to such a structure as the first argument.
One motivation for this package is the problem of growing char strings in symbol tables. Unless you are "fascist pig with a read-only mind" --Gosper's immortal quote from HAKMEM item 154, out of context--you would not like to put any arbitrary upper limit on the length of your symbols.
In practice this often means you will build many short symbols and a few long symbols. At the time you are reading a symbol you don't know how long it is. One traditional method is to read a symbol into a buffer, realloc()ating the buffer every time you try to read a symbol that is longer than the buffer. This is beaut, but you still will want to copy the symbol from the buffer to a more permanent symbol-table entry say about half the time.
With obstacks, you can work differently. Use one obstack for all symbol names. As you read a symbol, grow the name in the obstack gradually. When the name is complete, finalize it. Then, if the symbol exists already, free the newly read name.
The way we do this is to take a large chunk, allocating memory from low addresses. When you want to build a symbol in the chunk you just add chars above the current "high water mark" in the chunk. When you have finished adding chars, because you got to the end of the symbol, you know how long the chars are, and you can create a new object. Mostly the chars will not burst over the highest address of the chunk, because you would typically expect a chunk to be (say) 100 times as long as an average object.
In case that isn't clear, when we have enough chars to make up the object, THEY ARE ALREADY CONTIGUOUS IN THE CHUNK (guaranteed) so we just point to it where it lies. No moving of chars is needed and this is the second win: potentially long strings need never be explicitly shuffled. Once an object is formed, it does not change its address during its lifetime.
When the chars burst over a chunk boundary, we allocate a larger chunk, and then copy the partly formed object from the end of the old chunk to the beginning of the new larger chunk. We then carry on accreting characters to the end of the object as we normally would.
A special macro is provided to add a single char at a time to a growing object. This allows the use of register variables, which break the ordinary 'growth' macro.
Summary:
- We allocate large chunks.
- We carve out one object at a time from the current chunk. -Once carved, an object never moves. -We are free to append data of any size to the currently growing object.
- Exactly one object is growing in an obstack at any one time.
- You can run one obstack per control block.
- You may have as many control blocks as you dare.
- Because of the way we do it, you can `unwind' an obstack back to a previous state. (You may remove objects much as you would with a stack.)
Define Documentation
#define __INT_TO_PTR |
( |
P |
|
) |
((P) + (char *)0)
|
|
|
integer to pointer.
We use the addition of (char *)0 instead of casting to int because on word-addressable machines a simple cas to int may ignore the byte-within-world field of the pointer. - Parameters:
-
- Returns:
- pointer to character.
|
#define __PTR_TO_INT |
( |
P |
|
) |
((P) - (char *)0)
|
|
|
Pointer to int.
We use subtraction of (char *)0 instead of casting to int because on word-addressable machines a simple cast to int may ignore the byte-within-word field of the pointer. - Parameters:
-
- Returns:
- integer value of the pointer.
|
#define obstack_1grow |
( |
h, |
|
|
datum |
|
) |
|
|
|
Value: ( (((h)->next_free + 1 > (h)->chunk_limit) \
? (_obstack_newchunk ((h), 1), 0) : 0), \
((h)->alloc_failed ? 0 : \
(*((h)->next_free)++ = (datum))))
|
#define obstack_1grow_fast |
( |
h, |
|
|
achar |
|
) |
(*((h)->next_free)++ = achar)
|
|
#define obstack_alignment_mask |
( |
h |
|
) |
((h)->alignment_mask)
|
|
|
Alighment mask.
Mask specifying low bits that should be clear in address of an object. - Parameters:
-
h | pointer to obstack control block. |
|
#define obstack_alloc |
( |
h, |
|
|
length |
|
) |
(obstack_blank ((h), (length)), obstack_finish ((h)))
|
|
#define obstack_base |
( |
h |
|
) |
((h)->alloc_failed ? 0 : (h)->object_base)
|
|
|
Obstack base pointer.
Pointer to beginning of object being allocated or to be allocated next. - Note:
- This might not be the final address of the object because a new chunk might be needed to hold the final size.
- Parameters:
-
h | pointer to obstack control block. |
- Returns:
- pointer to beginning of object.
|
#define obstack_begin |
( |
h, |
|
|
size |
|
) |
|
|
|
Value: _obstack_begin ((h), (size), 0, \
(void *(*) (int)) obstack_chunk_alloc, (void (*) (void *)) obstack_chunk_free)
|
#define obstack_blank |
( |
h, |
|
|
length |
|
) |
|
|
|
Value: ( (h)->temp = (length), \
(((h)->chunk_limit - (h)->next_free < (h)->temp) \
? (_obstack_newchunk ((h), (h)->temp), 0) : 0), \
((h)->alloc_failed ? 0 : \
((h)->next_free += (h)->temp)))
|
#define obstack_blank_fast |
( |
h, |
|
|
n |
|
) |
((h)->next_free += (n))
|
|
#define obstack_chunk_size |
( |
h |
|
) |
((h)->chunk_size)
|
|
|
Size for allocating ordinary chunks.
- Parameters:
-
h | pointer to obstack control block. |
|
#define obstack_chunkfun |
( |
h, |
|
|
newchunkfun |
|
) |
((h) -> chunkfun = (struct _obstack_chunk *(*)()) (newchunkfun))
|
|
#define obstack_copy |
( |
h, |
|
|
where, |
|
|
length |
|
) |
(obstack_grow ((h), (where), (length)), obstack_finish ((h)))
|
|
#define obstack_copy0 |
( |
h, |
|
|
where, |
|
|
length |
|
) |
(obstack_grow0 ((h), (where), (length)), obstack_finish ((h)))
|
|
#define obstack_finish |
( |
h |
|
) |
|
|
|
Value: ( (h)->alloc_failed ? 0 : \
(((h)->next_free == (h)->object_base \
? (((h)->maybe_empty_object = 1), 0) \
: 0), \
(h)->temp = __PTR_TO_INT ((h)->object_base), \
(h)->next_free \
= __INT_TO_PTR ((__PTR_TO_INT ((h)->next_free)+(h)->alignment_mask) \
& ~ ((h)->alignment_mask)), \
(((h)->next_free - (char *)(h)->chunk \
> (h)->chunk_limit - (char *)(h)->chunk) \
? ((h)->next_free = (h)->chunk_limit) : 0), \
(h)->object_base = (h)->next_free, \
__INT_TO_PTR ((h)->temp)))
|
#define obstack_free |
( |
h, |
|
|
obj |
|
) |
|
|
|
Value: ( (h)->temp = (char *)(obj) - (char *) (h)->chunk, \
(((h)->temp > 0 && (h)->temp < (h)->chunk_limit - (char *) (h)->chunk)\
? (int) ((h)->next_free = (h)->object_base \
= (h)->temp + (char *) (h)->chunk) \
: (_obstack_free ((h), (h)->temp + (char *) (h)->chunk), 0)))
|
#define obstack_freefun |
( |
h, |
|
|
newfreefun |
|
) |
((h) -> freefun = (void (*)()) (newfreefun))
|
|
#define obstack_grow |
( |
h, |
|
|
where, |
|
|
length |
|
) |
|
|
|
Value: ( (h)->temp = (length), \
(((h)->next_free + (h)->temp > (h)->chunk_limit) \
? (_obstack_newchunk ((h), (h)->temp), 0) : 0), \
((h)->alloc_failed ? 0 : \
(bcopy (where, (h)->next_free, (h)->temp), \
(h)->next_free += (h)->temp)))
|
#define obstack_grow0 |
( |
h, |
|
|
where, |
|
|
length |
|
) |
|
|
|
Value: ( (h)->temp = (length), \
(((h)->next_free + (h)->temp + 1 > (h)->chunk_limit) \
? (_obstack_newchunk ((h), (h)->temp + 1), 0) : 0), \
((h)->alloc_failed ? 0 : \
(bcopy (where, (h)->next_free, (h)->temp), \
(h)->next_free += (h)->temp, \
*((h)->next_free)++ = 0)))
|
#define obstack_init |
( |
h |
|
) |
|
|
|
Value: _obstack_begin ((h), 0, 0, \
(void *(*) ()) obstack_chunk_alloc, (void (*) ()) obstack_chunk_free)
|
#define obstack_int_grow |
( |
h, |
|
|
datum |
|
) |
|
|
|
Value: ( (((h)->next_free + sizeof (int) > (h)->chunk_limit) \
? (_obstack_newchunk ((h), sizeof (int)), 0) : 0), \
((h)->alloc_failed ? 0 : \
(*((int *)(((h)->next_free+=sizeof(int))-sizeof(int))) = ((int)datum))))
|
#define obstack_int_grow_fast |
( |
h, |
|
|
aint |
|
) |
(*((int *)(h)->next_free)++ = (int)aint)
|
|
#define obstack_memory_used |
( |
h |
|
) |
_obstack_memory_used (h)
|
|
#define obstack_next_free |
( |
h |
|
) |
((h)->alloc_failed ? 0 : (h)->next_free)
|
|
|
Next free byte.
Pointer to next byte not yet allocated in current chunk. - Parameters:
-
h | pointer to obstack control block. |
|
#define obstack_object_size |
( |
h |
|
) |
(unsigned) ((h)->alloc_failed ? 0 : (h)->next_free - (h)->object_base)
|
|
#define obstack_ptr_grow |
( |
h, |
|
|
datum |
|
) |
|
|
|
Value: ( (((h)->next_free + sizeof (char *) > (h)->chunk_limit) \
? (_obstack_newchunk ((h), sizeof (char *)), 0) : 0), \
((h)->alloc_failed ? 0 : \
(*((char **)(((h)->next_free+=sizeof(char *))-sizeof(char *))) = ((char *)datum))))
|
#define obstack_ptr_grow_fast |
( |
h, |
|
|
aptr |
|
) |
(*((char **)(h)->next_free)++ = (char *)aptr)
|
|
#define obstack_room |
( |
h |
|
) |
(unsigned) ((h)->chunk_limit - (h)->next_free)
|
|
#define obstack_specify_allocation |
( |
h, |
|
|
size, |
|
|
alignment, |
|
|
chunkfun, |
|
|
freefun |
|
) |
|
|
|
Value: _obstack_begin ((h), (size), (alignment), \
(void *(*) ()) (chunkfun), (void (*) ()) (freefun))
|
#define obstack_specify_allocation_with_arg |
( |
h, |
|
|
size, |
|
|
alignment, |
|
|
chunkfun, |
|
|
freefun, |
|
|
arg |
|
) |
|
|
|
Value: _obstack_begin_1 ((h), (size), (alignment), \
(void *(*) ()) (chunkfun), (void (*) ()) (freefun), (arg))
|
#define PTR_INT_TYPE long
|
|
Function Documentation
int _obstack_memory_used |
( |
|
) |
|
|
void _obstack_newchunk |
( |
|
) |
|
|
Generated on Sat Sep 17 10:29:25 2005 for XconqKernel by
1.3.6