home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / anwor032.zip / antiword.0.32 / xmalloc.c < prev    next >
C/C++ Source or Header  |  2001-09-26  |  2KB  |  89 lines

  1. /*
  2.  * xmalloc.c
  3.  * Copyright (C) 1998-2001 A.J. van Os
  4.  *
  5.  * Description:
  6.  * Extended malloc and friends
  7.  */
  8.  
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "antiword.h"
  12.  
  13. #if !defined(DMALLOC)
  14. static char *szWarning = "Memory allocation failed, unable to continue";
  15.  
  16.  
  17. /*
  18.  * xmalloc - Allocates dynamic memory
  19.  *
  20.  * See malloc(3), but unlike malloc(3) xmalloc does not return in case
  21.  * of error.
  22.  */
  23. void *
  24. xmalloc(size_t tSize)
  25. {
  26.     void    *pvTmp;
  27.  
  28.     pvTmp = malloc(tSize);
  29.     if (pvTmp == NULL) {
  30.         werr(1, szWarning);
  31.     }
  32.     return pvTmp;
  33. } /* end of xmalloc */
  34.  
  35. /*
  36.  * xrealloc - Changes the size of a memory object
  37.  *
  38.  * See realloc(3), but unlike realloc(3) xrealloc does not return in case
  39.  * of error.
  40.  */
  41. void *
  42. xrealloc(void *pvArg, size_t tSize)
  43. {
  44.     void    *pvTmp;
  45.  
  46.     pvTmp = realloc(pvArg, tSize);
  47.     if (pvTmp == NULL) {
  48.         werr(1, szWarning);
  49.     }
  50.     return pvTmp;
  51. } /* end of xrealloc */
  52.  
  53. /*
  54.  * xstrdup - Duplicate a string
  55.  *
  56.  * See strdup(3), but unlike strdup(3) xstrdup does not return in case
  57.  * of error.
  58.  *
  59.  * NOTE:
  60.  * Does not use strdup(3), because some systems don't have it.
  61.  */
  62. char *
  63. xstrdup(const char *szArg)
  64. {
  65.     char    *szTmp;
  66.  
  67.     szTmp = xmalloc(strlen(szArg) + 1);
  68.     strcpy(szTmp, szArg);
  69.     return szTmp;
  70. } /* end of xstrdup */
  71. #endif /* !DMALLOC */
  72.  
  73. /*
  74.  * xfree - Deallocates dynamic memory
  75.  *
  76.  * See free(3).
  77.  *
  78.  * returns NULL;
  79.  * This makes p=xfree(p) possible, free memory and overwrite the pointer to it.
  80.  */
  81. void *
  82. xfree(void *pvArg)
  83. {
  84.     if (pvArg != NULL) {
  85.         free(pvArg);
  86.     }
  87.     return NULL;
  88. } /* end of xfree */
  89.