/****Copyright (c) 1997-2004  Alexandros V. Gerbessiotis.
 **** All rights reserved.
 ****Permission to use, copy, modify, and distribute this software,
 ****and to incorporate it, in whole or in part, into
 ****other software,is hereby granted without fee, provided that
 ****  (1) the above copyright notice and this permission
 ****      notice appear in all copies of the source code, and
 ****      the above copyright notice appear in clearly visible
 ****      form on all supporting documentation and distribution
 ****      media, and
 ****     (2) any redistribution of the software, in original
 ****         or modified form, be without fee and subject to
 ****         these same conditions.
 ****   No guarantee, whatsoever, is offered that the code works as
 ****   advertised or is absent of any, even damaging,
 ****   side-effects. Use this code at your own personal risk.
 ****/

#include <stdlib.h>
#include <stdio.h>
#include "sbsp.h"
void check_if_null(void *x,char *s,char *t)
{
  if (NULL == x) {
     printf("%s::Malloc error (%s)\n",s,t);
     bsp_abort("check_if_null");
  }
}

int GlobalCount=0, GlobalMax=-1;
void *Malloc(size_t size, char *s) {
  void *zp;
  GlobalCount+=size; 
  if (GlobalCount > GlobalMax) GlobalMax=GlobalCount;
#ifdef TRACE_MALLOC
  fprintf(stdout,"**********  %10s : malloc %8d bytes:(%8d)\n",s,(int)size,GlobalCount);
  fflush(stdout);
#endif
  zp=malloc(size);
  if (zp != NULL) return(zp);
   else  {
    fprintf(stdout," *****SEVERE ***** MALLOC to RETURN NULL STORAGE \n");
    fflush(stdout);
    return(NULL); 
   }
}

void Free(void *ptr,size_t size,char *s)
{
  free((void *) ptr);
  GlobalCount-= (int)size;
  if (GlobalCount > GlobalMax) GlobalMax=GlobalCount;
#ifdef TRACE_MALLOC
  fprintf(stdout,"**********  %10s : free   %8d bytes:(%8d) MAXMALLOC THIS PROG %d\n",s,(int)size,Gl
obalCount,GlobalMax);
  fflush(stdout);
#endif
}

