#include <stdio.h>
#include "q.h"

void * malloc ();
void free ();
void * realloc ();

char    whoami[] = "Q";

int     q_initialized = 0;

/* Initializes com_err table */

void
Q_init () {
    if (!q_initialized) {
	initialize_q_error_table ();
	q_initialized++;
    }
}

/* Causes program to die when malloc fails, printing in error. */

void *
Q_malloc (size)
int     size;
{
    void * buf;

    buf = malloc (size);
    if (buf == NULL) {
	fprintf (stderr, "%s: malloc of size %d failed\n", whoami, size);
	exit (1);
    }
    return buf;
}

/* Causes program to die when realloc fails, and prints on error. */

void *
Q_realloc (ptr, size)
void * ptr;
int     size;
{
    ptr = realloc (ptr, size);
    if (ptr == NULL) {
	fprintf (stderr, "%s: realloc of pointer to size %d failed\n",
		whoami, size);
	exit (1);
    }
    return ptr;
}

/* Frees a pointer.  If NULL, don't do anything. */

void
Q_free (ptr)
void * ptr;
{
    if (ptr != NULL)
	free (ptr);
}
