imapfilter/src/memory.c
Lefteris Chatzimparmpas 4fe1a3d649 Change file and directory structure
Move the source code to the src dir, the man pages to the doc dir, and
the example configuration files to the samples dir, and update all
relevant files to reflect the changes.
2011-03-06 13:34:44 +01:00

93 lines
1.3 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "imapfilter.h"
/*
* A malloc() that checks the results and dies in case of error.
*/
void *
xmalloc(size_t size)
{
void *ptr;
ptr = (void *)malloc(size);
if (ptr == NULL)
fatal(ERROR_MEMALLOC,
"allocating memory; %s\n", strerror(errno));
return ptr;
}
/*
* A realloc() that checks the results and dies in case of error.
*/
void *
xrealloc(void *ptr, size_t size)
{
ptr = (void *)realloc(ptr, size);
if (ptr == NULL)
fatal(ERROR_MEMALLOC,
"allocating memory; %s\n", strerror(errno));
return ptr;
}
/*
* A free() that dies if fed with NULL pointer.
*/
void
xfree(void *ptr)
{
if (ptr == NULL)
fatal(ERROR_MEMALLOC,
"NULL pointer given as argument\n");
free(ptr);
}
/*
* A strdup() that checks the results and dies in case of error.
*/
char *
xstrdup(const char *str)
{
char *dup;
dup = strdup(str);
if (dup == NULL)
fatal(ERROR_MEMALLOC, "allocating memory; %s\n",
strerror(errno));
return dup;
}
/*
* A strndup() implementation that also checks the results and dies in case of
* error.
*/
char *
xstrndup(const char *str, size_t len)
{
char *dup;
dup = (char *)xmalloc((len + 1) * sizeof(char));
memcpy(dup, str, len);
dup[len] = '\0';
return dup;
}