7

Is there any way to specify my own allocator/deallocator functions for heap management instead of malloc()/free() for a pure push parser in bison?

Flavius
  • 13,566
  • 13
  • 80
  • 126

1 Answers1

3

Most of Bison's memory allocations can be redirected with macros - in the prologue (between %{ and %}) you can write

#define YYMALLOC mymalloc
#define YYFREE myfree

and Bison will then call mymalloc and myfree instead of malloc and free. However, it expects whatever functions you provide to have exactly the same type signature as the standard malloc and free; there is no way to get it to pass extra/different arguments. And I wouldn't use function-like macros if I were you. Worse, in my copy (Bison 2.4.1) yypstate_new calls malloc directly, with no override possible -- this is arguably a bug.

zwol
  • 135,547
  • 38
  • 252
  • 361
  • 1
    Yeah I've found it out already, by looking at the generated source. +1 and accepted, a good answer. It doesn't have this bug in 2.4.3. – Flavius Mar 22 '11 at 09:41