save.c 2.4 KB

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