/* c.routines --- auxilary routines.  */

#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include "d2rd.h"
#include "kernel.h"

int
fatal (const char *fmt, ...)
{
  va_list ap;
  static struct
   {
     int dummy;
     char msg[2048];
   } msg;

  va_start (ap, fmt);
  vsprintf (msg.msg, fmt, ap);
  va_end (ap);

  wimp_reporterror ((os_error *) &msg, wimp_EHICANCEL, PROGRAM_NAME);

  return (-1);
}


void
tfatal (const char *fmt, ...)
{
  va_list ap;
  static struct
   {
     int dummy;
     char msg[2048];
   } msg;

  va_start (ap, fmt);
  vsprintf (msg.msg, fmt, ap);
  va_end (ap);

  wimp_reporterror ((os_error *) &msg, wimp_EHICANCEL, PROGRAM_NAME);
  exit (1);
}


void *
xmalloc (int i)
{
  void *r = malloc (i ? i : 1);

  if (r == NULL)
    {
      tfatal ("Out of VM\n");
      abort ();
    }
  return (r);
}
  

void *
xcalloc (int i, int j)
{
  void *r = calloc (i, j);

  if (r == NULL)
    {
      tfatal ("Out of VM\n");
      abort ();
    }
  return (r);
}
  

void *
xrealloc (void *i, int j)
{
  void *r = realloc (i, j);

  if (r == NULL && j != 0)
    {
      tfatal ("Out of VM\n");
      abort ();
    }
  return (r);
}

/* EOF */
