examples/PIPS/antiword/src/xmalloc.c

00001 /*
00002  * xmalloc.c
00003  * Copyright (C) 1998-2005 A.J. van Os
00004  *
00005  * Description:
00006  * Extended malloc and friends
00007  */
00008 
00009 #include <stdlib.h>
00010 #include <string.h>
00011 #include "antiword.h"
00012 
00013 static char *szMessage =
00014         "Memory allocation failed, unable to continue";
00015 #if defined(__dos) && !defined(__DJGPP__)
00016 static char *szDosMessage =
00017         "DOS can't allocate this kind of memory, unable to continue";
00018 #endif /* __dos && !__DJGPP__ */
00019 
00020 
00021 /*
00022  * xmalloc - Allocates dynamic memory
00023  *
00024  * See malloc(3), but unlike malloc(3) xmalloc does not return in case
00025  * of error.
00026  */
00027 void *
00028 xmalloc(size_t tSize)
00029 {
00030         void    *pvTmp;
00031 
00032         TRACE_MSG("xmalloc");
00033 
00034         if (tSize == 0) {
00035                 tSize = 1;
00036         }
00037         pvTmp = malloc(tSize);
00038         if (pvTmp == NULL) {
00039                 DBG_MSG("xmalloc returned NULL");
00040                 DBG_DEC(tSize);
00041                 werr(1, szMessage);
00042         }
00043         return pvTmp;
00044 } /* end of xmalloc */
00045 
00046 /*
00047  * xcalloc - Allocates and zeros dynamic memory
00048  *
00049  * See calloc(3), but unlike calloc(3) xcalloc does not return in case of error
00050  */
00051 void *
00052 xcalloc(size_t tNmemb, size_t tSize)
00053 {
00054         void    *pvTmp;
00055 
00056         TRACE_MSG("xcalloc");
00057 
00058 #if defined(__dos) && !defined(__DJGPP__)
00059         if ((ULONG)tNmemb * (ULONG)tSize > 0xffffUL) {
00060                 DBG_DEC((ULONG)tNmemb * (ULONG)tSize);
00061                 werr(1, szDosMessage);
00062         }
00063 #endif /* __dos && !__DJGPP__ */
00064 
00065         if (tNmemb == 0 || tSize == 0) {
00066                 tNmemb = 1;
00067                 tSize = 1;
00068         }
00069         pvTmp = calloc(tNmemb, tSize);
00070         if (pvTmp == NULL) {
00071                 DBG_MSG("xcalloc returned NULL");
00072                 werr(1, szMessage);
00073         }
00074         return pvTmp;
00075 } /* end of xcalloc */
00076 
00077 /*
00078  * xrealloc - Changes the size of a memory object
00079  *
00080  * See realloc(3), but unlike realloc(3) xrealloc does not return in case
00081  * of error.
00082  */
00083 void *
00084 xrealloc(void *pvArg, size_t tSize)
00085 {
00086         void    *pvTmp;
00087 
00088         TRACE_MSG("xrealloc");
00089 
00090         pvTmp = realloc(pvArg, tSize);
00091         if (pvTmp == NULL) {
00092                 DBG_MSG("realloc returned NULL");
00093                 werr(1, szMessage);
00094         }
00095         return pvTmp;
00096 } /* end of xrealloc */
00097 
00098 /*
00099  * xstrdup - Duplicate a string
00100  *
00101  * See strdup(3), but unlike strdup(3) xstrdup does not return in case
00102  * of error.
00103  *
00104  * NOTE:
00105  * Does not use strdup(3), because some systems don't have it.
00106  */
00107 char *
00108 xstrdup(const char *szArg)
00109 {
00110         char    *szTmp;
00111 
00112         TRACE_MSG("xstrdup");
00113 
00114         szTmp = xmalloc(strlen(szArg) + 1);
00115         strcpy(szTmp, szArg);
00116         return szTmp;
00117 } /* end of xstrdup */
00118 
00119 /*
00120  * xfree - Deallocates dynamic memory
00121  *
00122  * See free(3).
00123  *
00124  * returns NULL;
00125  * This makes p=xfree(p) possible, free memory and overwrite the pointer to it.
00126  */
00127 void *
00128 xfree(void *pvArg)
00129 {
00130         TRACE_MSG("xfree");
00131 
00132         if (pvArg != NULL) {
00133                 free(pvArg);
00134         }
00135         return NULL;
00136 } /* end of xfree */

Generated by  doxygen 1.6.2