1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
/* malloc.c -- safe versions of malloc and realloc */
/* Return a pointer to free()able block of memory large enough
to hold BYTES number of bytes. If the memory can't be allocated,
print an error message and abort. */
#include <stdlib.h>
#include "gkscore.h"
char *gks_malloc(int size)
{
char *temp;
temp = (char *)calloc(1, size);
if (temp == 0)
{
gks_fatal_error("can't allocate memory");
}
return (temp);
}
char *gks_realloc(void *ptr, int size)
{
char *temp;
temp = ptr ? (char *)realloc(ptr, size) : (char *)malloc(size);
if (temp == 0)
{
gks_fatal_error("can't re-allocate memory");
}
return (temp);
}
/* Use this as the function to call when adding unwind protects so we
don't need to know what free() returns. */
void gks_free(void *ptr)
{
if (ptr) free(ptr);
}
|