xmalloc.c 1.13 KB
Newer Older
1 2 3
/*
 *	BIRD Library -- malloc() With Checking
 *
Martin Mareš's avatar
Martin Mareš committed
4
 *	(c) 1998--2000 Martin Mares <mj@ucw.cz>
5 6 7 8 9 10 11 12 13
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

#include <stdlib.h>

#include "nest/bird.h"
#include "lib/resource.h"

14 15
#ifndef HAVE_LIBDMALLOC

Martin Mareš's avatar
Martin Mareš committed
16 17 18 19 20 21 22 23
/**
 * xmalloc - malloc with checking
 * @size: block size
 *
 * This function is equivalent to malloc() except that in case of
 * failure it calls die() to quit the program instead of returning
 * a %NULL pointer.
 *
24
 * Wherever possible, please use the memory resources instead.
Martin Mareš's avatar
Martin Mareš committed
25
 */
26
void *
Pavel Tvrdík's avatar
Pavel Tvrdík committed
27
xmalloc(uint size)
28 29 30 31 32 33
{
  void *p = malloc(size);
  if (p)
    return p;
  die("Unable to allocate %d bytes of memory", size);
}
34

35 36 37 38 39 40 41 42 43 44 45 46
/**
 * xrealloc - realloc with checking
 * @ptr: original memory block
 * @size: block size
 *
 * This function is equivalent to realloc() except that in case of
 * failure it calls die() to quit the program instead of returning
 * a %NULL pointer.
 *
 * Wherever possible, please use the memory resources instead.
 */
void *
Pavel Tvrdík's avatar
Pavel Tvrdík committed
47
xrealloc(void *ptr, uint size)
48 49 50 51 52 53 54
{
  void *p = realloc(ptr, size);
  if (p)
    return p;
  die("Unable to allocate %d bytes of memory", size);
}

55
#endif