/* libsizzle/alloc.c - memory allocation module implementation
 
  Copyright (C) 1999, 2000 Martin Grabmueller <mgrabmue@cs.tu-berlin.de>
 
  This is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2, or (at your option)
  any later version.
  
  This software is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with this package; see the file COPYING.  If not, write to the
  Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  MA 02111-1307, USA.  */

#if HAVE_CONFIG_H
# include <config.h>
#endif 

#include <stdlib.h>
#include <stdio.h>

#if ENABLE_NLS
# include <libintl.h>
# define _(String) gettext(String)
#else
# define _(String) (String)
#endif

#include "alloc.h"

unsigned zzz_allocated_bytes = 0;

void * 
zzz_malloc (unsigned size)
{
  void * ptr;

  if ((ptr = (void *) malloc(size)))
    zzz_allocated_bytes += size;
  else
    {
      fprintf(stderr, "%s\n", _("fatal: virtual memory exhausted"));
      exit(1);
    }
  return ptr;
}

void *
zzz_realloc (void * ptr, unsigned size, unsigned old_size)
{
  if (ptr)
    {
      if ((ptr = (void *) realloc(ptr, size)))
	zzz_allocated_bytes += size - old_size;
      else
	{
	  fprintf(stderr, "%s\n", _("fatal: virtual memory exhausted"));
	  exit(1);
	}
    }
  else
    ptr = zzz_malloc(size);
  return ptr;
}

void
zzz_free (void * ptr, unsigned size)
{
  free(ptr);
  zzz_allocated_bytes -= size;
}
