/* *************************************************************
   RWhois Software

   Copyright (c) 1994 Scott Williamson and Mark Kosters
   Copyright (c) 1996-2000 Network Solutions, Inc.

   See the file LICENSE for conditions of use and distribution.
**************************************************************** */

#include "rwhois.h"

void *
xmalloc(size_t size)
{
  void *m;

  if (size == 0) {
    size = 1;
  }

  m = malloc(size);
  if (m == NULL) {
#ifdef DEBUG
    fprintf(stderr, "cannot allocate %lld bytes\n", (long long) size);
#endif
    abort();
  }

  return m;
}

void *
xcalloc(size_t nelem, size_t size)
{
  void *m;

  if (nelem == 0) {
    nelem = 1;
  }
  if (size == 0) {
    size = 1;
  }

  m = calloc(nelem, size);
  if (m == NULL) {
#ifdef DEBUG
    fprintf(stderr, "cannot allocate %lld bytes\n", (long long) size);
#endif
    abort();
  }

  return m;
}

char *
xstrdup(const char *str)
{
  char *s;

  if (str == NULL) {
    return (char *) NULL;
  }

  s = xmalloc(strlen(str) + 1);
  (void) strcpy(s, str);

  return s;
}

void *
xrealloc(void *ptr, size_t size)
{
  char *cp;

  if (!ptr) {
    cp = malloc(size);
  }
  else {
    cp = realloc(ptr, size);
  }

  if (cp == NULL) {
#ifdef DEBUG
    fprintf(stderr, "cannot (re)allocate %lld bytes\n", (long long) size);
#endif
    abort();
  }
  return cp;
}
