save.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. */
  5. #ifndef lint
  6. static char rcsid[] = "$Id$";
  7. #endif
  8. /*
  9. * If everything is kept in core, we must save some things for the second pass.
  10. */
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include "arch.h"
  15. #include "out.h"
  16. #include "const.h"
  17. #include "assert.h"
  18. #include "memory.h"
  19. extern bool incore;
  20. extern char *core_alloc();
  21. savemagic()
  22. {
  23. register char *p;
  24. if (!incore)
  25. return;
  26. if ((p = core_alloc(ALLOMODL, (long)sizeof(int))) != (char *)0) {
  27. *(unsigned short *)p = AALMAG;
  28. core_position += sizeof(int);
  29. }
  30. }
  31. savehdr(hdr)
  32. struct ar_hdr *hdr;
  33. {
  34. register char *p;
  35. if (!incore)
  36. return;
  37. if ((p=core_alloc(ALLOMODL,(long)sizeof(struct ar_hdr)))!=(char *)0) {
  38. *(struct ar_hdr *)p = *hdr;
  39. core_position += int_align(sizeof(struct ar_hdr));
  40. }
  41. }
  42. long NLChars = 0; /* Size of string area for local names. */
  43. long NGChars = 0; /* Idem for global names. */
  44. /*
  45. * Put the string in cp into the block allocated for the string area.
  46. * Return its offset in this area. We don't use the first char of the string
  47. * area, so that empty strings can be distinguished from the first string.
  48. */
  49. ind_t
  50. savechar(piece, off)
  51. register int piece;
  52. register ind_t off;
  53. {
  54. register long len;
  55. register ind_t newoff;
  56. extern ind_t alloc();
  57. extern ind_t hard_alloc();
  58. if (off == (ind_t)0)
  59. return 0;
  60. len = strlen(modulptr(off)) + 1;
  61. if (piece == ALLOLCHR) {
  62. NLChars += len;
  63. if (!incore || (newoff = alloc(piece, len)) == BADOFF)
  64. return BADOFF;
  65. } else {
  66. assert(piece == ALLOGCHR);
  67. NGChars += len;
  68. if ((newoff = hard_alloc(piece, len)) == BADOFF)
  69. return BADOFF;
  70. }
  71. strcpy(address(piece, newoff), modulptr(off));
  72. return newoff;
  73. }
  74. /*
  75. * Put the local in `name' in the piece allocated for local names that must
  76. * be saved. `Name' points to a private copy, so will not become invalid after
  77. * allocation, but the string of which name->on_foff is the offset may be
  78. * destroyed, so we save that first.
  79. */
  80. savelocal(name)
  81. struct outname *name;
  82. {
  83. ind_t savindex;
  84. struct outname *new;
  85. if ((savindex = savechar(ALLOLCHR, (ind_t)name->on_foff)) == BADOFF)
  86. return;
  87. new = (struct outname *)
  88. core_alloc(ALLOLOCL, (long)sizeof(struct outname));
  89. if (new != (struct outname *)0) {
  90. *new = *name;
  91. new->on_foff = savindex;
  92. }
  93. }