You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
2.1 KiB

  1. /* LINTLIBRARY */
  2. #include <stdio.h>
  3. #include "util.h"
  4. /*
  5. * These are interface routines to be placed between a program and the
  6. * system memory allocator.
  7. *
  8. * It forces well-defined semantics for several 'borderline' cases:
  9. *
  10. * malloc() of a 0 size object is guaranteed to return something
  11. * which is not 0, and can safely be freed (but not dereferenced)
  12. * free() accepts (silently) an 0 pointer
  13. * realloc of a 0 pointer is allowed, and is equiv. to malloc()
  14. * For the IBM/PC it forces no object > 64K; note that the size argument
  15. * to malloc/realloc is a 'long' to catch this condition
  16. *
  17. * The function pointer MMoutOfMemory() contains a vector to handle a
  18. * 'out-of-memory' error (which, by default, points at a simple wrap-up
  19. * and exit routine).
  20. */
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. extern char *MMalloc(long);
  25. extern void MMout_of_memory(long);
  26. extern char *MMrealloc(char *, long);
  27. void (*MMoutOfMemory)(long) = MMout_of_memory;
  28. #ifdef __cplusplus
  29. }
  30. #endif
  31. /* MMout_of_memory -- out of memory for lazy people, flush and exit */
  32. void
  33. MMout_of_memory(long size)
  34. {
  35. (void) fflush(stdout);
  36. (void) fprintf(stderr, "\nout of memory allocating %lu bytes\n",
  37. (unsigned long) size);
  38. exit(1);
  39. }
  40. char *
  41. MMalloc(long size)
  42. {
  43. char *p;
  44. #ifdef IBMPC
  45. if (size > 65000L) {
  46. if (MMoutOfMemory != (void (*)(long)) 0 ) (*MMoutOfMemory)(size);
  47. return NIL(char);
  48. }
  49. #endif
  50. if (size == 0) size = sizeof(long);
  51. if ((p = (char *) malloc((unsigned long) size)) == NIL(char)) {
  52. if (MMoutOfMemory != 0 ) (*MMoutOfMemory)(size);
  53. return NIL(char);
  54. }
  55. return p;
  56. }
  57. char *
  58. MMrealloc(char *obj, long size)
  59. {
  60. char *p;
  61. #ifdef IBMPC
  62. if (size > 65000L) {
  63. if (MMoutOfMemory != 0 ) (*MMoutOfMemory)(size);
  64. return NIL(char);
  65. }
  66. #endif
  67. if (obj == NIL(char)) return MMalloc(size);
  68. if (size <= 0) size = sizeof(long);
  69. if ((p = (char *) realloc(obj, (unsigned long) size)) == NIL(char)) {
  70. if (MMoutOfMemory != 0 ) (*MMoutOfMemory)(size);
  71. return NIL(char);
  72. }
  73. return p;
  74. }
  75. void
  76. MMfree(char *obj)
  77. {
  78. if (obj != 0) {
  79. free(obj);
  80. }
  81. }