Quick Image Viewer (qiv) http://qiv.spiegl.de/ http://qiv.spiegl.de/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

78 lines
1.7 KiB

/* xmalloc.c - Do-or-die Memory management functions.
*
* Created by Kevin Locke (from numerous canonical examples)
*
* I hereby place this file in the public domain. It may be freely reproduced,
* distributed, used, modified, built upon, or otherwise employed by anyone
* for any purpose without restriction.
*/
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#ifndef EXIT_SUCCESS
#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1
#endif
void *xmalloc(size_t size)
{
void *allocated = malloc(size);
if (allocated == NULL) {
fprintf(stderr, "Error: Insufficient memory "
# if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
"(attempt to malloc %zu bytes)\n",
#else
"(attempt to malloc %u bytes)\n",
#endif
(unsigned int) size);
exit(EXIT_FAILURE);
}
return allocated;
}
void *xcalloc(size_t num, size_t size)
{
void *allocated = calloc(num, size);
if (allocated == NULL) {
fprintf(stderr, "Error: Insufficient memory "
# if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
"(attempt to calloc %zu bytes)\n",
#else
"(attempt to calloc %u bytes)\n",
#endif
(unsigned int) size);
exit(EXIT_FAILURE);
}
return allocated;
}
void *xrealloc(void *ptr, size_t size)
{
void *allocated;
/* Protect against non-standard behavior */
if (ptr == NULL) {
allocated = malloc(size);
} else {
allocated = realloc(ptr, size);
}
if (allocated == NULL) {
fprintf(stderr, "Error: Insufficient memory "
# if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
"(attempt to realloc %zu bytes)\n",
#else
"(attempt to realloc %u bytes)\n",
#endif
(unsigned int) size);
exit(EXIT_FAILURE);
}
return allocated;
}