alloc.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* $Id$ */
  2. /*
  3. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  4. * See the copyright notice in the ACK home directory, in the file "Copyright".
  5. */
  6. /* S H A R E D F I L E
  7. *
  8. * A L L O C . C
  9. */
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include "types.h"
  13. #include "debug.h"
  14. #include "alloc.h"
  15. char * myalloc();
  16. #ifdef DEBUG
  17. STATIC unsigned maxuse, curruse;
  18. char *newcore(size)
  19. int size;
  20. {
  21. if ((curruse += (unsigned) (size+2)) > maxuse) maxuse = curruse;
  22. return myalloc(size);
  23. }
  24. oldcore(p,size)
  25. char *p;
  26. int size;
  27. {
  28. curruse -= (size+2);
  29. free(p);
  30. }
  31. coreusage()
  32. {
  33. fprintf(stderr,"Maximal core usage (excl. buffers):%u\n",maxuse);
  34. }
  35. #endif
  36. /*
  37. * The following two sizetables contain the sizes of the various kinds
  38. * of line and argument structures.
  39. * The assumption when making the tables was that every non-byte object
  40. * had to be aligned on an even boundary. On machines where alignment
  41. * is worse ( for example a long has to be aligned on a longword bound )
  42. * these tables should be revised.
  43. * A wasteful but safe approach is to replace every line of them by
  44. * sizeof(line_t)
  45. * and
  46. * sizeof(arg_t)
  47. * respectively.
  48. */
  49. #ifndef NOTCOMPACT
  50. int lsizetab[] = {
  51. 2*sizeof(line_p)+2*sizeof(byte),
  52. 2*sizeof(line_p)+2*sizeof(byte)+sizeof(short),
  53. 2*sizeof(line_p)+2*sizeof(byte)+sizeof(offset),
  54. 2*sizeof(line_p)+2*sizeof(byte)+sizeof(lab_id),
  55. 2*sizeof(line_p)+2*sizeof(byte)+sizeof(obj_p),
  56. 2*sizeof(line_p)+2*sizeof(byte)+sizeof(proc_p),
  57. 2*sizeof(line_p)+2*sizeof(byte)+sizeof(arg_p),
  58. };
  59. int asizetab[] = {
  60. sizeof(arg_p)+sizeof(short)+sizeof(offset),
  61. sizeof(arg_p)+sizeof(short)+sizeof(lab_id),
  62. sizeof(arg_p)+sizeof(short)+sizeof(obj_p),
  63. sizeof(arg_p)+sizeof(short)+sizeof(proc_p),
  64. sizeof(arg_p)+sizeof(short)+sizeof(argb_t),
  65. sizeof(arg_p)+sizeof(short)+sizeof(short)+sizeof(argb_t),
  66. sizeof(arg_p)+sizeof(short)+sizeof(short)+sizeof(argb_t),
  67. sizeof(arg_p)+sizeof(short)+sizeof(short)+sizeof(argb_t)
  68. };
  69. #else
  70. int lsizetab[] = {
  71. sizeof(struct line),
  72. sizeof(struct line),
  73. sizeof(struct line),
  74. sizeof(struct line),
  75. sizeof(struct line),
  76. sizeof(struct line),
  77. sizeof(struct line)
  78. };
  79. int asizetab[] = {
  80. sizeof (struct arg),
  81. sizeof (struct arg),
  82. sizeof (struct arg),
  83. sizeof (struct arg),
  84. sizeof (struct arg),
  85. sizeof (struct arg),
  86. sizeof (struct arg),
  87. sizeof (struct arg)
  88. };
  89. #endif
  90. /*
  91. * alloc routines:
  92. * Two parts:
  93. * 1) typed alloc and free routines
  94. * 2) untyped raw core allocation
  95. */
  96. /*
  97. * PART 1
  98. */
  99. line_p newline(optyp) int optyp; {
  100. register line_p lnp;
  101. register kind=optyp;
  102. lnp = (line_p) newcore(lsizetab[kind]);
  103. TYPE(lnp) = optyp;
  104. return(lnp);
  105. }
  106. oldline(lnp) register line_p lnp; {
  107. register kind=TYPE(lnp)&BMASK;
  108. if (kind == OPLIST)
  109. oldargs(ARG(lnp));
  110. oldcore((char *) lnp,lsizetab[kind]);
  111. }
  112. arg_p newarg(kind) int kind; {
  113. register arg_p ap;
  114. ap = (arg_p) newcore(asizetab[kind]);
  115. ap->a_type = kind;
  116. return(ap);
  117. }
  118. oldargs(ap) register arg_p ap; {
  119. register arg_p next;
  120. while (ap != (arg_p) 0) {
  121. next = ap->a_next;
  122. switch(ap->a_type) {
  123. case ARGSTRING:
  124. oldargb(ap->a_a.a_string.ab_next);
  125. break;
  126. case ARGICN:
  127. case ARGUCN:
  128. case ARGFCN:
  129. oldargb(ap->a_a.a_con.ac_con.ab_next);
  130. break;
  131. }
  132. oldcore((char *) ap,asizetab[ap->a_type]);
  133. ap = next;
  134. }
  135. }
  136. oldargb(abp) register argb_p abp; {
  137. register argb_p next;
  138. while (abp != (argb_p) 0) {
  139. next = abp->ab_next;
  140. oldcore((char *) abp,sizeof (argb_t));
  141. abp = next;
  142. }
  143. }
  144. oldobjects(op) register obj_p op; {
  145. register obj_p next;
  146. while (op != (obj_p) 0) {
  147. next = op->o_next;
  148. oldcore((char *) op, sizeof(struct obj));
  149. op = next;
  150. }
  151. }
  152. olddblock(dbl) dblock_p dbl; {
  153. oldobjects(dbl->d_objlist);
  154. oldargs(dbl->d_values);
  155. oldcore((char *) dbl, sizeof(struct dblock));
  156. }
  157. short **newmap(length) short length; {
  158. return((short **) newcore((length+1) * sizeof(short *)));
  159. }
  160. /*ARGSUSED1*/
  161. oldmap(mp,length) short **mp, length; {
  162. oldcore((char *) mp, (length+1) * sizeof(short *));
  163. }
  164. cset newbitvect(n) short n; {
  165. return((cset) newcore((n-1)*sizeof(int) + sizeof(struct bitvector)));
  166. /* sizeof(struct bitvector) equals to the size of a struct with
  167. * one short, followed by one ALLIGNED int. So the above statement
  168. * also works e.g. on a VAX.
  169. */
  170. }
  171. /*ARGSUSED1*/
  172. oldbitvect(s,n) cset s; short n; {
  173. oldcore((char *) s, (n-1)*sizeof(int) + sizeof(struct bitvector));
  174. }
  175. short *newtable(length) short length; {
  176. return((short *) newcore((length+1) * sizeof(short)));
  177. }
  178. /*ARGSUSED1*/
  179. oldtable(mp,length) short **mp, length; {
  180. oldcore((char *) mp, (length+1) * sizeof(short));
  181. }
  182. cond_p newcondtab(l) int l;
  183. {
  184. return (cond_p) newcore(l * (sizeof (struct cond_tab)));
  185. }
  186. oldcondtab(tab) cond_p tab;
  187. {
  188. int i;
  189. for (i = 0; tab[i].mc_cond != DEFAULT; i++);
  190. oldcore((char *) tab,((i+1) * sizeof (struct cond_tab)));
  191. }
  192. char *myalloc(size) register size; {
  193. register char *p;
  194. p = calloc((unsigned) size, 1);
  195. if (p == 0)
  196. error("out of memory");
  197. return(p);
  198. }