"Fossies" - the Fresh Open Source Software Archive

Member "imapfilter-2.8.2/src/memory.c" (26 Dec 2023, 1322 Bytes) of package /linux/privat/imapfilter-2.8.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "memory.c" see the Fossies "Dox" file reference documentation.

    1 #include <stdio.h>
    2 #include <stdlib.h>
    3 #include <string.h>
    4 #include <errno.h>
    5 
    6 #include "imapfilter.h"
    7 
    8 
    9 /*
   10  * A malloc() that checks the results and dies in case of error.
   11  */
   12 void *
   13 xmalloc(size_t size)
   14 {
   15     void *ptr;
   16 
   17     ptr = (void *)malloc(size);
   18 
   19     if (ptr == NULL)
   20         fatal(ERROR_MEMALLOC,
   21             "allocating memory; %s\n", strerror(errno));
   22 
   23     return ptr;
   24 }
   25 
   26 
   27 /*
   28  * A realloc() that checks the results and dies in case of error.
   29  */
   30 void *
   31 xrealloc(void *ptr, size_t size)
   32 {
   33 
   34     ptr = (void *)realloc(ptr, size);
   35 
   36     if (ptr == NULL)
   37         fatal(ERROR_MEMALLOC,
   38             "allocating memory; %s\n", strerror(errno));
   39 
   40     return ptr;
   41 }
   42 
   43 
   44 /*
   45  * A free() that dies if fed with NULL pointer.
   46  */
   47 void
   48 xfree(void *ptr)
   49 {
   50 
   51     if (ptr == NULL)
   52         fatal(ERROR_MEMALLOC,
   53             "NULL pointer given as argument\n");
   54     free(ptr);
   55 }
   56 
   57 
   58 /*
   59  * A strdup() that checks the results and dies in case of error.
   60  */
   61 char *
   62 xstrdup(const char *str)
   63 {
   64     char *dup;
   65 
   66     dup = strdup(str);
   67 
   68     if (dup == NULL)
   69         fatal(ERROR_MEMALLOC, "allocating memory; %s\n",
   70             strerror(errno));
   71 
   72     return dup;
   73 }
   74 
   75 
   76 /*
   77  * A strndup() implementation that also checks the results and dies in case of
   78  * error.
   79  */
   80 char *
   81 xstrndup(const char *str, size_t len)
   82 {
   83     char *dup;
   84 
   85     dup = (char *)xmalloc((len + 1) * sizeof(char));
   86 
   87     memcpy(dup, str, len);
   88 
   89     dup[len] = '\0';
   90 
   91     return dup;
   92 }