alloc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* $Header$ */
  2. /* M E M O R Y A L L O C A T I O N R O U T I N E S */
  3. /* The allocation of memory in this program, which plays an important
  4. role in reading files, replacing macros and building expression
  5. trees, is not performed by malloc etc. The reason for having own
  6. memory allocation routines (malloc(), realloc() and free()) is
  7. plain: the garbage collection performed by the library functions
  8. malloc(), realloc() and free() costs a lot of time, while in most
  9. cases (on a VAX) the freeing and reallocation of memory is not
  10. necessary. The only reallocation done in this program is at
  11. building strings in memory. This means that the last
  12. (re-)allocated piece of memory can be extended.
  13. The (basic) memory allocating routines offered by this memory
  14. handling package are:
  15. char *malloc(n) : allocate n bytes
  16. char *realloc(ptr, n) : reallocate buffer to n bytes
  17. (works only if ptr was last allocated)
  18. free(ptr) : if ptr points to last allocated
  19. memory, this memory is re-allocatable
  20. Salloc(str, sz) : save string in malloc storage
  21. */
  22. #include <system.h>
  23. #include "myalloc.h" /* UF */
  24. #include "debug.h" /* UF */
  25. #include "alloc.h"
  26. #include "assert.h"
  27. #ifdef OWNALLOC
  28. char *sys_break();
  29. /* the following variables are used for book-keeping */
  30. static int nfreebytes = 0; /* # free bytes in sys_break space */
  31. static char *freeb; /* pointer to first free byte */
  32. static char *lastalloc; /* pointer to last malloced sp */
  33. static int lastnbytes; /* nr of bytes in last allocated */
  34. /* space */
  35. static char *firstfreeb = 0;
  36. #endif OWNALLOC
  37. char *
  38. Salloc(str, sz)
  39. register char str[];
  40. register int sz;
  41. {
  42. /* Salloc() is not a primitive function: it just allocates a
  43. piece of storage and copies a given string into it.
  44. */
  45. char *res = Malloc(sz);
  46. register char *m = res;
  47. while (sz--)
  48. *m++ = *str++;
  49. return res;
  50. }
  51. #ifdef OWNALLOC
  52. #define ALIGN(m) (ALIGNSIZE * (((m) - 1) / ALIGNSIZE + 1))
  53. char *
  54. malloc(n)
  55. unsigned n;
  56. {
  57. /* malloc() is a very simple malloc().
  58. */
  59. n = ALIGN(n);
  60. if (nfreebytes < n) {
  61. register nbts = (n <= ALLOCSIZ) ? ALLOCSIZ : n;
  62. if (!nfreebytes) {
  63. if ((freeb = sys_break(nbts)) == ILL_BREAK)
  64. fatal("out of memory");
  65. }
  66. else {
  67. if (sys_break(nbts) == ILL_BREAK)
  68. fatal("out of memory");
  69. }
  70. nfreebytes += nbts;
  71. }
  72. lastalloc = freeb;
  73. freeb = lastalloc + n;
  74. lastnbytes = n;
  75. nfreebytes -= n;
  76. return lastalloc;
  77. }
  78. /*ARGSUSED*/
  79. char *
  80. realloc(ptr, n)
  81. char *ptr;
  82. unsigned n;
  83. {
  84. /* realloc() is designed to append more bytes to the latest
  85. allocated piece of memory. However reallocation should be
  86. performed, even if the mentioned memory is not the latest
  87. allocated one, this situation will not occur. To do so,
  88. realloc should know how many bytes are allocated the last
  89. time for that piece of memory. ????
  90. */
  91. register int nbytes = n;
  92. ASSERT(ptr == lastalloc); /* security */
  93. nbytes -= lastnbytes; /* # bytes required */
  94. if (nbytes == 0) /* no extra bytes */
  95. return lastalloc;
  96. /* if nbytes < 0: free last allocated bytes;
  97. if nbytes > 0: allocate more bytes
  98. */
  99. if (nbytes > 0)
  100. nbytes = ALIGN(nbytes);
  101. if (nfreebytes < nbytes) {
  102. register int nbts = (nbytes < ALLOCSIZ) ? ALLOCSIZ : nbytes;
  103. if (sys_break(nbts) == ILL_BREAK)
  104. fatal("out of memory");
  105. nfreebytes += nbts;
  106. }
  107. freeb += nbytes; /* less bytes */
  108. lastnbytes += nbytes; /* change nr of last all. bytes */
  109. nfreebytes -= nbytes; /* less or more free bytes */
  110. return lastalloc;
  111. }
  112. /* to ensure that the alloc library package will not be loaded: */
  113. /*ARGSUSED*/
  114. free(p)
  115. char *p;
  116. {}
  117. init_mem()
  118. {
  119. firstfreeb = sys_break(0);
  120. /* align the first memory unit to ALIGNSIZE ??? */
  121. if ((long) firstfreeb % ALIGNSIZE != 0) {
  122. register char *fb = firstfreeb;
  123. fb = (char *)ALIGN((long)fb);
  124. firstfreeb = sys_break(fb - firstfreeb);
  125. firstfreeb = fb;
  126. ASSERT((long)firstfreeb % ALIGNSIZE == 0);
  127. }
  128. }
  129. #ifdef DEBUG
  130. mem_stat()
  131. {
  132. extern char options[];
  133. if (options['m'])
  134. print("Total nr of bytes allocated: %d\n",
  135. sys_break(0) - firstfreeb);
  136. }
  137. #endif DEBUG
  138. #endif OWNALLOC