- Main Page
- Related Pages
- Modules
- Data Structures
- Files

# rpmio/rpmmalloc.c

Go to the documentation of this file.

    00001
    00005 #include "system.h"
    00006 #include "debug.h"
    00007
    00008 #if defined(WITH_DMALLOC)
    00009 #undef xmalloc
    00010 #undef xcalloc
    00011 #undef xrealloc
    00012 #undef xstrdup
    00013 #endif
    00014
    00015 #if !defined(EXIT_FAILURE)
    00016 #define EXIT_FAILURE    1
    00017 #endif
    00018
    00019 /*@-modfilesys@*/
    00020 /*@only@*/ void *vmefail(size_t size)
    00021 {
    00022     fprintf(stderr, _("memory alloc (%u bytes) returned NULL.\n"), (unsigned)size);
    00023     exit(EXIT_FAILURE);
    00024     /*@notreached@*/
    00025     return NULL;
    00026 }
    00027 /*@=modfilesys@*/
    00028
    00029 #if !(HAVE_MCHECK_H && defined(__GNUC__)) && !defined(__LCLINT__)
    00030
    00031 /*@out@*/ /*@only@*/ void * xmalloc (size_t size)
    00032 {
    00033     register void *value;
    00034     if (size == 0) size++;
    00035     value = malloc (size);
    00036     if (value == 0)
    00037         value = vmefail(size);
    00038     return value;
    00039 }
    00040
    00041 /*@only@*/ void * xcalloc (size_t nmemb, size_t size)
    00042 {
    00043     register void *value;
    00044     if (size == 0) size++;
    00045     if (nmemb == 0) nmemb++;
    00046     value = calloc (nmemb, size);
    00047     if (value == 0)
    00048         value = vmefail(size);
    00049     return value;
    00050 }
    00051
    00052 /*@only@*/ void * xrealloc (/*@only@*/ void *ptr, size_t size)
    00053 {
    00054     register void *value;
    00055     if (size == 0) size++;
    00056     value = realloc (ptr, size);
    00057     if (value == 0)
    00058         value = vmefail(size);
    00059     return value;
    00060 }
    00061
    00062 /*@only@*/ char * xstrdup (const char *str)
    00063 {
    00064     size_t size = strlen(str) + 1;
    00065     char *newstr = (char *) malloc (size);
    00066     if (newstr == 0)
    00067         newstr = (char *) vmefail(size);
    00068     strcpy (newstr, str);
    00069     return newstr;
    00070 }
    00071
    00072 #endif  /* !(HAVE_MCHECK_H && defined(__GNUC__)) */