Skip to content

Possible less verbose allocation/deallocation functions ? #2

@mingodad

Description

@mingodad

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;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions