save.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #include "defs.h"
  20. extern bool incore;
  21. void savemagic()
  22. {
  23. 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. void savehdr(struct ar_hdr * hdr)
  32. {
  33. char *p;
  34. if (!incore)
  35. return;
  36. if ((p=core_alloc(ALLOMODL,(long)sizeof(struct ar_hdr)))!=(char *)0) {
  37. *(struct ar_hdr *)p = *hdr;
  38. core_position += int_align(sizeof(struct ar_hdr));
  39. }
  40. }
  41. long NLChars = 0; /* Size of string area for local names. */
  42. long NGChars = 0; /* Idem for global names. */
  43. /*
  44. * Put the string in cp into the block allocated for the string area.
  45. * Return its offset in this area. We don't use the first char of the string
  46. * area, so that empty strings can be distinguished from the first string.
  47. */
  48. ind_t savechar(int piece, ind_t off)
  49. {
  50. long len;
  51. ind_t newoff;
  52. if (off == (ind_t)0)
  53. return 0;
  54. len = strlen(modulptr(off)) + 1;
  55. if (piece == ALLOLCHR) {
  56. NLChars += len;
  57. if (!incore || (newoff = alloc(piece, len)) == BADOFF)
  58. return BADOFF;
  59. } else {
  60. assert(piece == ALLOGCHR);
  61. NGChars += len;
  62. if ((newoff = hard_alloc(piece, len)) == BADOFF)
  63. return BADOFF;
  64. }
  65. strcpy(address(piece, newoff), modulptr(off));
  66. return newoff;
  67. }
  68. /*
  69. * Put the local in `name' in the piece allocated for local names that must
  70. * be saved. `Name' points to a private copy, so will not become invalid after
  71. * allocation, but the string of which name->on_foff is the offset may be
  72. * destroyed, so we save that first.
  73. */
  74. void savelocal(struct outname *name)
  75. {
  76. ind_t savindex;
  77. struct outname *new;
  78. if ((savindex = savechar(ALLOLCHR, (ind_t)name->on_foff)) == BADOFF)
  79. return;
  80. new = (struct outname *)
  81. core_alloc(ALLOLOCL, (long)sizeof(struct outname));
  82. if (new != (struct outname *)0) {
  83. *new = *name;
  84. new->on_foff = savindex;
  85. }
  86. }