Re: Question About YACC's Memory Management

pardo@cs.washington.edu (David Keppel)
23 Oct 90 17:32:46 GMT

          From comp.compilers

Related articles
Question About YACC's Memory Management andrea@mprgate.mpr.ca (1990-10-11)
Re: Question About YACC's Memory Management bliss@sp64.csrd.uiuc.edu (1990-10-18)
Re: Question About YACC's Memory Management djones@megatest.uucp (1990-10-21)
Re: Question About YACC's Memory Management pardo@cs.washington.edu (1990-10-23)
| List of all articles for this month |

Newsgroups: comp.compilers
From: pardo@cs.washington.edu (David Keppel)
Keywords: yacc
Organization: University of Washington, Computer Science, Seattle
References: <1990Oct18.213224.21195@csrd.uiuc.edu> <14252@goofy.megatest.UUCP>
Date: 23 Oct 90 17:32:46 GMT

>andrea@mprgate.mpr.ca (Jennitta Andrea) writes:
>> [Call the parser repeatedly from a driver.]
>> [|malloc| each token in lex; yacc doesn't |free|.]


djones@megatest.uucp (Dave Jones) writes:
>[Borrow an string table package; free at one go.
> But why copy at all? Read input into a buffer & pools of tokens.]


Ah, yes -- The way GNU CC does it (at least recently) is to use an
``obstack'' package. The ``obstack'' package is sort of a cross
between |malloc| and |alloca|. Things are allocated in stack order on
an ``obstack'' and are freed ``when you know it is safe.'' In
addition, it is possible to tentatively allocate a thing on the stack,
and then grow it or shrink it until its size is known, then ``freeze''
it and go on to the next allocation.


Furthermore, if you have things that need to live until the end of the
parse, but aren't needed as long as the parse tree, you can allocate
stuff on a shorter-lived store.


        obstack_init (&short_store);
        obstack_init (&long_store);
        short_watermark = oballoc (&short_store, 0);
        long_watermark = oballoc (&long_store, 0);


        while (1) {
parse();
/* Free temps used by the parser but not used in parse tree. */
obfree (&short_store, short_watermark);


use (parser_output);
/* Free everything allocaetd by the parser. */
obfree (&long_store, long_watermark);


... other stuff not needing parser output ...
        }


Dave Jones' sollution is probably fastest, while using a generic
allocator allows for more efficient use of memory when there are
variable-sized objects.


;-D oN ( HACC memory management ) Pardo
--
pardo@cs.washington.edu
        {rutgers,cornell,ucsd,ubc-cs,tektronix}!uw-beaver!june!pardo
--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.