home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume20 / memlintok / memlintok.h < prev    next >
Encoding:
C/C++ Source or Header  |  1989-10-26  |  2.9 KB  |  110 lines

  1. #ifndef INCLUDED_LINTOK
  2.  
  3. /* $Header: memlintok.h,v 1.3 89/05/02 15:01:43 bobl Locked $ */
  4.  
  5. /*
  6.  *    These definitions stop lint from complaining about malloc, calloc,
  7.  *    realloc, and free while at the same time performing checks of
  8.  *    their arguments.  See the memlintok(3) man page for more details.
  9.  *
  10.  *    Bob Lewis (bobl@tessi.uucp) produced these macros using his own
  11.  *    resources and hereby releases them to the public domain.
  12.  *    The author will not be held responsible for their use, misuse,
  13.  *    abuse, or any damages arising from same.
  14.  */
  15.  
  16. #ifndef NULL
  17. #include <stdio.h>
  18. #endif
  19.  
  20. extern char *calloc();
  21. #ifdef lint
  22. #define CALLOC_LINTOK(ptr, nelem, type) \
  23.     calloc((ptr = (type *) NULL, (unsigned) (nelem)), (unsigned) sizeof(type))
  24. #else
  25. #define CALLOC_LINTOK(ptr, nelem, type) \
  26.     (ptr = (type *) calloc((unsigned) (nelem), (unsigned) sizeof(type)))
  27. #endif
  28.  
  29. extern char *malloc();
  30. #ifdef lint
  31. #define MALLOC_LINTOK(ptr, nelem, type) \
  32.     malloc((ptr = (type *) NULL, (unsigned) ((nelem) * sizeof(type))))
  33. #else
  34. #define MALLOC_LINTOK(ptr, nelem, type) \
  35.     (ptr = (type *) malloc((unsigned) ((nelem) * sizeof(type))))
  36. #endif
  37.  
  38. extern char *realloc();
  39. #ifdef lint
  40. #define REALLOC_LINTOK(ptr, nelem, type) \
  41.     realloc( \
  42.         (ptr = (type *) NULL, (char *) NULL), \
  43.         (unsigned) ((nelem) * sizeof(type)))
  44. #else
  45. #define REALLOC_LINTOK(ptr, nelem, type) \
  46.     (ptr = (type *) realloc( \
  47.         (char *) ptr, \
  48.         (unsigned) ((nelem) * sizeof(type))))
  49. #endif
  50.  
  51. /* common use of malloc/realloc -- use it or don't use it */
  52. #define MR_ALLOC_LINTOK(ptr, nelem, type) \
  53.     ( (ptr) == NULL \
  54.         ? MALLOC_LINTOK(ptr, (nelem), type) \
  55.         : REALLOC_LINTOK(ptr, (nelem), type) )
  56.  
  57. /*
  58.  *    These next macros invoke CALLOC_LINTOK, MALLOC_LINTOK, REALLOC_LINTOK,
  59.  *    and MR_ALLOC_LINTOK with an error exit if they fail.
  60.  *
  61.  *    If you want to handle your own memory allocation errors, just
  62.  *    "#undef ERROR_EXIT_LINTOK" and define your own.
  63.  */
  64. #define ERROR_EXIT_LINTOK(nelem, size) \
  65.     { \
  66.         (void) fprintf(stderr, \
  67.             "Memory allocation of %d * %d bytes on line %d of \"%s\" failed.\n", \
  68.             (nelem), (size), __LINE__, __FILE__); \
  69.         exit(1); \
  70.     }
  71.  
  72. #define CALLOC_OR_ELSE_LINTOK(ptr, nelem, type) \
  73.     { \
  74.         if (CALLOC_LINTOK(ptr, (nelem), type) == NULL) \
  75.             ERROR_EXIT_LINTOK((nelem), sizeof(type)); \
  76.     }
  77.  
  78. #define MALLOC_OR_ELSE_LINTOK(ptr, nelem, type) \
  79.     { \
  80.         if (MALLOC_LINTOK(ptr, (nelem), type) == NULL) \
  81.             ERROR_EXIT_LINTOK((nelem), sizeof(type)); \
  82.     }
  83.  
  84. #define REALLOC_OR_ELSE_LINTOK(ptr, nelem, type) \
  85.     { \
  86.         if (REALLOC_LINTOK(ptr, (nelem), type) == NULL) \
  87.             ERROR_EXIT_LINTOK((nelem), sizeof(type)); \
  88.     }
  89.  
  90. #define MR_ALLOC_OR_ELSE_LINTOK(ptr, nelem, type) \
  91.     { \
  92.         if (MR_ALLOC_LINTOK(ptr, (nelem), type) == NULL) \
  93.             ERROR_EXIT_LINTOK((nelem), sizeof(type)); \
  94.     }
  95.  
  96. extern free();
  97. #ifdef lint
  98. #define FREE_LINTOK(ptr) \
  99.     free((ptr = NULL, (char *) NULL))
  100. #else
  101. #define FREE_LINTOK(ptr) \
  102.     free((char *) ptr)
  103. #endif
  104.  
  105. /* We could have a "CFREE_LINTOK", but what's the point? */
  106.  
  107. #define INCLUDED_LINTOK
  108. #endif
  109.  
  110.