[antlr-interest] ANTLR3_MALLOC

Cristian Târşoagă cristian.tarsoaga at gmail.com
Thu May 20 06:02:52 PDT 2010


Hello (again),

I'm back with a suggestion related to the ANTLR3_MALLOC macro.

I am using C++, so I use the C target with antlr.

After finding a workaround for my previous problem (regarding memory
initialization issues), I'm back with a small suggestion.

This is somehow related to my previous posts, and possibly offers a clean
way to override memory allocators/deallocators in antlr.



There are currently 2 macros to do just that, ANTLR3_MALLOC and ANTLR3_FREE

Since I use C++, I thought about replacing malloc with new and replace free
with delete!

But that is not possible because ANTLR3_MALLOC is defined (by default) as
  #define ANTLR3_MALLOC(request)   malloc((size_t)request)

suggestion:

Instead of passing as argument the 'size' of the memory to be allocated, I
think a better macro could take the 'type' as parameter, like

  #define ANTLR3_ALLOC(type) malloc(sizeof(type)) //use C's malloc

This would make it easier to override memory allocation in antlr, e.g by
overriding it with

  #define ANTLR3_ALLOC(type) new type() //use C++'s new

I think this should be easy to change, backward compatible and offer more
flexibility (for guys like me, who need c++, at least for the types with
noarg constructors)

What do you think?


  thanks
     Chris


More information about the antlr-interest mailing list