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