alloc.c 4.7 KB

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