thutt / lrstar

Port of lrstar parser generator to Linux
BSD 3-Clause "New" or "Revised" License
2 stars 2 forks source link

Possible less verbose allocation/deallocation functions ? #2

Open mingodad opened 10 months ago

mingodad commented 10 months ago

Looking at how to eliminate compiler warnings I found that changing the function signatures of allocation/deallocation wrapers can eliminate the warnings like this:

...
   // Function-call defines ...
      #ifdef _DEBUG
        #define ALLOC(x,norg)               alloc (#x, (char**)&x, sizeof(*x), norg)
      #else
        #define ALLOC(x,norg)               alloc ((char**)&x, sizeof(*x), norg)
      #endif

      #define REALLOC(x,norg,nnew)      ralloc((char**)&x, sizeof(*x), norg, nnew)
      #define FREE(x,n)                     frea  ((char**)&x, sizeof(*x), n)
...
        #ifdef _DEBUG
        extern char*  alloc (char *s, char** x, int size, int n);
        #else
        extern char*  alloc (char** x, int size, int n);
        #endif        

        extern void   ralloc (char** x, int size, int n1, int n2);
        extern void   frea  (char** x, int size, int n);
...

Implementation:

#ifdef _DEBUG
char* alloc (char *s, char** x, int size, int n)
{
      *x = (char*)malloc (n*size);
      if (*x == NULL)
      {
         n_errors++;
            prt_log ("Allocation error for '%s', %u bytes not available.\n\n", s, size*n);
         Quit ();
      }
        memory_usage += n*size;
        if (memory_usage > memory_max) memory_max = memory_usage;
      return (*x);
}
#else
char* alloc (char** x, int size, int n)
{
      *x = (char*)malloc (n*size);
      if (*x == NULL)
      {
         n_errors++;
            prt_log ("Allocation error, %u bytes not available.\n\n", size*n);
            Quit ();
      }
        memory_usage += n*size;
        if (memory_usage > memory_max) memory_max = memory_usage;
      return (*x);
}
#endif

///////////////////////////////////////////////////////////////////////////////

void  ralloc (char** x, int size, int n1, int n2)
{
      *x = (char*)realloc (*x, size*n2);
        memory_usage -= (n1-n2)*size;
}

///////////////////////////////////////////////////////////////////////////////

void  frea  (char** x, int size, int n)
{
      free (*x);
        memory_usage -= n*size;
      *x = NULL;
}
thutt commented 10 months ago

What branch are you using?

The linux-port branch has no warnings produced when building lrstar, dfa or any of the samples in grammars & examples.

The warning level for lrstar & dfa is not as high as for grammars & examples, but I will eventually turn my attention to that and fix any issues that are revealed.

mingodad commented 10 months ago

Sorry by the delay but here is what I found building with g++-9.4 https://github.com/thutt/lrstar/pull/4 . It's mostly removing (commenting out) unused variables and replacing ALLOC/REALLOC/FREE by less verbose ones.

thutt commented 10 months ago

Commenting out variables is not a practice that will be accepted into this branch. While there is commented-out code in the source tree already, it's not a clean development style when using a SCM. If the code / variables are unused, they need to be removed.

mingodad commented 10 months ago

Get anything that is useful here and forget the rest !