home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 328_02 / wmalloc.c < prev    next >
C/C++ Source or Header  |  1991-03-17  |  2KB  |  77 lines

  1. /* wmalloc.c()
  2.  *
  3.  *    general purpose malloc() and farmalloc()
  4.  *    with validation and ptr normalization
  5.  *    also note that in large data models, wmalloc is not generated.
  6.  */
  7.  
  8. #include "wsys.h"
  9.  
  10. #undef WANT_NEAR
  11.  
  12. #ifdef __TINY__
  13.     #define WANT_NEAR
  14. #endif
  15. #ifdef __SMALL__
  16.     #define WANT_NEAR
  17. #endif
  18. #ifdef __MEDIUM__
  19.     #define WANT_NEAR
  20. #endif
  21.  
  22.  
  23.  
  24. #ifdef WANT_NEAR
  25. #define MSG_SIZE  17
  26. static char OUTOF_NEAR[MSG_SIZE+1] = "OUT OF MEMORY in ";
  27.  
  28. void *wmalloc ( size_t size,  char *errmsg )
  29.     {
  30.     void *ptr;
  31.     #define  MAX_BOTH_MSG  200
  32.     char both_msg[MAX_BOTH_MSG];
  33.  
  34.     ptr = malloc ( size );
  35.  
  36.     _NORMALIZE (ptr);    /* model-dependent */
  37.  
  38.     if (  (ptr==NULL)  &&  (errmsg!=NULL) )
  39.         {
  40.         memcpy (both_msg, OUTOF_NEAR, MSG_SIZE);
  41.         memcpy (both_msg+MSG_SIZE, errmsg, MAX_BOTH_MSG - MSG_SIZE -1);
  42.         werror ( 'W', both_msg );
  43.         }
  44.  
  45.     return (ptr);
  46.  
  47.     }
  48. #endif        /* WANT_NEAR */
  49.  
  50.  
  51.  
  52. #define OUTOF_FAR_SIZE 21 
  53. static char OUTOF_FAR[OUTOF_FAR_SIZE+1] = "OUT OF FAR MEMORY in ";
  54.  
  55. void far *wfarmalloc ( unsigned long nbytes, char *errmsg )
  56.     {
  57.     #define MAX_BOTHFAR_MSG    200
  58.     char bothfar_msg[MAX_BOTHFAR_MSG];
  59.     void far *ptr;
  60.  
  61.     ptr = farmalloc ( nbytes );
  62.  
  63.     if (  (ptr==NULL)  &&  (errmsg!=NULL) )
  64.         {
  65.         memcpy (bothfar_msg, OUTOF_FAR, OUTOF_FAR_SIZE);
  66.         memcpy (bothfar_msg+OUTOF_FAR_SIZE, errmsg, 
  67.                         MAX_BOTHFAR_MSG - OUTOF_FAR_SIZE -1);
  68.         werror ('W', bothfar_msg );
  69.         }
  70.     NORMALIZE (ptr);
  71.     return (ptr);        /* wfarmalloc */
  72.  
  73.     }
  74.  
  75.  
  76.  
  77. /*----------------------- end of wmalloc ----------------------*/