/**********************************************************************/
/*  file array_alloc_macs.h                                           */
/*  macros for allocating memory, adapted from Fiscus's "memory.h".   */
/*  NOTE: at the point in the code where these are executed, "proc"   */
/*    must be defined as a character string name of the procedure.    */
/*  defined:                                                          */
/*   alloc_1dimarr()                                                  */
/*   alloc_2dimarr()                                                  */
/*   free_1dimarr()                                                   */
/*   free_2dimarr()                                                   */
/**********************************************************************/
#ifndef MEM_ALLOC_MACS
#define MEM_ALLOC_MACS

#ifndef WARN_ZERO_MALLOC
#define alloc_1dimarr(_arr,_len,_type) \
  {int _mem_to_allocate_ = (_len) * sizeof(_type); \
   if (_mem_to_allocate_ == 0) _mem_to_allocate_ = 1; \
   _arr = (_type *)malloc_safe(_mem_to_allocate_,proc); \
  }
#else
#define alloc_1dimarr(_arr,_len,_type) \
  {register int _mem_to_allocate_ = (_len) * sizeof(_type); \
   if (mem_to_allocate == 0) \
     {fprintf(stderr,"*WARNING:%s: malloc of size 0 changed to 1\n",proc); \
      _mem_to_allocate_ = 1; \
     } \
   _arr = (_type *)malloc_safe(mem_to_allocate,proc); \
  }
#endif


#define alloc_2dimarr(_arr,_num,_len,_type) \
  {register int _i; \
   alloc_1dimarr(_arr,_num,_type *); \
   for(_i=0;_i<_num;_i++) \
     alloc_1dimarr(_arr[_i],_len,_type); \
  }


#define free_1dimarr(_arr,_type) \
  {free((char *) _arr); \
   _arr=(_type *)0; \
  }

#define free_2dimarr(_arr,_num,_type)\
  {register int _i; \
   for (_i=0;_i<_num;_i++) \
      free_1dimarr(_arr[_i], _type); \
   free_1dimarr(_arr,_type *); \
  }

#endif
