Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible less verbose allocation/deallocation functions ? #2

Open
mingodad opened this issue Dec 8, 2023 · 4 comments
Open

Possible less verbose allocation/deallocation functions ? #2

mingodad opened this issue Dec 8, 2023 · 4 comments

Comments

@mingodad
Copy link

mingodad commented Dec 8, 2023

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
Copy link
Owner

thutt commented Dec 9, 2023

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
Copy link
Author

mingodad commented Dec 13, 2023

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

@thutt
Copy link
Owner

thutt commented Dec 17, 2023

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
Copy link
Author

Get anything that is useful here and forget the rest !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants