home *** CD-ROM | disk | FTP | other *** search
/ pc.louisiana.edu/pub/unix/ / Louisiana_UNIX.tar / Louisiana_UNIX / xspread3.0.zoo / xmalloc.c < prev    next >
C/C++ Source or Header  |  1994-04-18  |  1KB  |  88 lines

  1. /*
  2.  * A safer saner malloc, for careless programmers
  3.  * $Revision: 6.21 A $
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include "config.h"
  8. #ifndef DOINGX
  9. #include <curses.h>
  10. #endif
  11. #include "sc.h"
  12.  
  13. #ifdef __STDC__
  14. static    void    fatal(char *);
  15. #else    /* __STDC__ */
  16. static void    fatal();
  17. extern void    free();
  18. extern char *    malloc();
  19. extern char *    realloc();
  20. #endif    /* __STDC__ */
  21.  
  22. #define    MAGIC    (double)1234567890.12344
  23.  
  24. char *
  25. scxmalloc(n)
  26. unsigned int    n;
  27. {
  28.     register char *ptr;
  29.  
  30.     if ((ptr = malloc(n + sizeof(double))) == NULL)
  31.         fatal("scxmalloc: no memory");
  32.     *((double *) ptr) = MAGIC;        /* magic number */
  33.     return(ptr + sizeof(double));
  34. }
  35.  
  36. /* we make sure realloc will do a malloc if needed */
  37. char *
  38. scxrealloc(ptr, n)
  39. char    *ptr;
  40. unsigned int n;
  41. {
  42.     if (ptr == NULL)
  43.         return(scxmalloc(n));
  44.  
  45.     ptr -= sizeof(double);
  46.     if (*((double *) ptr) != MAGIC)
  47.         fatal("scxrealloc: storage not scxmalloc'ed");
  48.  
  49.     if ((ptr = realloc(ptr, n + sizeof(double))) == NULL)
  50.         fatal("scxmalloc: no memory");
  51.     *((double *) ptr) = MAGIC;        /* magic number */
  52.     return(ptr + sizeof(double));
  53. }
  54.  
  55. void
  56. scxfree(p)
  57. char *p;
  58. {
  59.     if (p == NULL)
  60.         fatal("scxfree: NULL");
  61.     p -= sizeof(double);
  62.     if (*((double *) p) != MAGIC)
  63.         fatal("scxfree: storage not malloc'ed");
  64.     free(p);
  65. }
  66.  
  67. #ifdef PSC
  68. void
  69. static fatal(str)
  70. char *str;
  71. {
  72.     (void) fprintf(stderr,"%s\n", str);
  73.     exit(1);
  74. }
  75. #else
  76. static void
  77. fatal(str)
  78. char *str;
  79. {
  80. #ifndef DOINGX
  81.     deraw();
  82. #endif
  83.     (void) fprintf(stderr,"%s\n", str);
  84.     diesave();
  85.     exit(1);
  86. }
  87. #endif /* PSC */
  88.