aboutsummaryrefslogtreecommitdiff
path: root/libf2c/src/mem.c
blob: 4e3d777dbb7410bafc0d51d83c3c3bd11d7ba89b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/****************************************************************
Copyright 1990, 1991, 1994 by AT&T, Lucent Technologies and Bellcore.

Permission to use, copy, modify, and distribute this software
and its documentation for any purpose and without fee is hereby
granted, provided that the above copyright notice appear in all
copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the names of AT&T, Bell Laboratories,
Lucent or Bellcore or any of their entities not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.

AT&T, Lucent and Bellcore disclaim all warranties with regard to
this software, including all implied warranties of
merchantability and fitness.  In no event shall AT&T, Lucent or
Bellcore be liable for any special, indirect or consequential
damages or any damages whatsoever resulting from loss of use,
data or profits, whether in an action of contract, negligence or
other tortious action, arising out of or in connection with the
use or performance of this software.
****************************************************************/

#include "defs.h"
#include "iob.h"

#define MEMBSIZE	32000
#define GMEMBSIZE	16000

 char *
#ifdef KR_headers
gmem(n, round)
	int n;
	int round;
#else
gmem(int n, int round)
#endif
{
	static char *last, *next;
	char *rv;
	if (round)
#ifdef CRAY
		if ((long)next & 0xe000000000000000)
			next = (char *)(((long)next & 0x1fffffffffffffff) + 1);
#else
#ifdef MSDOS
		if ((int)next & 1)
			next++;
#else
		next = (char *)(((long)next + sizeof(char *)-1)
				& ~((long)sizeof(char *)-1));
#endif
#endif
	rv = next;
	if ((next += n) > last) {
		rv = Alloc(n + GMEMBSIZE);

		next = rv + n;
		last = next + GMEMBSIZE;
		}
	return rv;
	}

 struct memblock {
	struct memblock *next;
	char buf[MEMBSIZE];
	};
 typedef struct memblock memblock;

 static memblock *mem0;
 memblock *curmemblock, *firstmemblock;

 char *mem_first, *mem_next, *mem_last, *mem0_last;

 void
mem_init(Void)
{
	curmemblock = firstmemblock = mem0
		= (memblock *)Alloc(sizeof(memblock));
	mem_first = mem0->buf;
	mem_next  = mem0->buf;
	mem_last  = mem0->buf + MEMBSIZE;
	mem0_last = mem0->buf + MEMBSIZE;
	mem0->next = 0;
	}

 char *
#ifdef KR_headers
mem(n, round)
	int n;
	int round;
#else
mem(int n, int round)
#endif
{
	memblock *b;
	register char *rv, *s;

	if (round)
#ifdef CRAY
		if ((long)mem_next & 0xe000000000000000)
			mem_next = (char *)(((long)mem_next & 0x1fffffffffffffff) + 1);
#else
#ifdef MSDOS
		if ((int)mem_next & 1)
			mem_next++;
#else
		mem_next = (char *)(((long)mem_next + sizeof(char *)-1)
				& ~((long)sizeof(char *)-1));
#endif
#endif
	rv = mem_next;
	s = rv + n;
	if (s >= mem_last) {
		if (n > MEMBSIZE)  {
			fprintf(stderr, "mem(%d) failure!\n", n);
			exit(1);
			}
		if (!(b = curmemblock->next)) {
			b = (memblock *)Alloc(sizeof(memblock));
			curmemblock->next = b;
			b->next = 0;
			}
		curmemblock = b;
		rv = b->buf;
		mem_last = rv + sizeof(b->buf);
		s = rv + n;
		}
	mem_next = s;
	return rv;
	}

 char *
#ifdef KR_headers
tostring(s, n)
	register char *s;
	int n;
#else
tostring(register char *s, int n)
#endif
{
	register char *s1, *se, **sf;
	char *rv, *s0;
	register int k = n + 2, t;

	sf = str_fmt;
	sf['%'] = "%";
	s0 = s;
	se = s + n;
	for(; s < se; s++) {
		t = *(unsigned char *)s;
		s1 = sf[t];
		while(*++s1)
			k++;
		}
	sf['%'] = "%%";
	rv = s1 = mem(k,0);
	*s1++ = '"';
	for(s = s0; s < se; s++) {
		t = *(unsigned char *)s;
		sprintf(s1, sf[t], t);
		s1 += strlen(s1);
		}
	*s1 = 0;
	return rv;
	}

 char *
#ifdef KR_headers
cpstring(s)
	register char *s;
#else
cpstring(register char *s)
#endif
{
	return strcpy(mem(strlen(s)+1,0), s);
	}

 void
#ifdef KR_headers
new_iob_data(ios, name)
	register io_setup *ios;
	char *name;
#else
new_iob_data(register io_setup *ios, char *name)
#endif
{
	register iob_data *iod;
	register char **s, **se;

	iod = (iob_data *)
		mem(sizeof(iob_data) + ios->nelt*sizeof(char *), 1);
	iod->next = iob_list;
	iob_list = iod;
	iod->type = ios->fields[0];
	iod->name = cpstring(name);
	s = iod->fields;
	se = s + ios->nelt;
	while(s < se)
		*s++ = "0";
	*s = 0;
	}

 char *
#ifdef KR_headers
string_num(pfx, n)
	char *pfx;
	long n;
#else
string_num(char *pfx, long n)
#endif
{
	char buf[32];
	sprintf(buf, "%s%ld", pfx, n);
	/* can't trust return type of sprintf -- BSD gets it wrong */
	return strcpy(mem(strlen(buf)+1,0), buf);
	}

static defines *define_list;

 void
#ifdef KR_headers
def_start(outfile, s1, s2, post)
	FILE *outfile;
	char *s1;
	char *s2;
	char *post;
#else
def_start(FILE *outfile, char *s1, char *s2, char *post)
#endif
{
	defines *d;
	int n, n1;
	extern int in_define;

	n = n1 = strlen(s1);
	if (s2)
		n += strlen(s2);
	d = (defines *)mem(sizeof(defines)+n, 1);
	d->next = define_list;
	define_list = d;
	strcpy(d->defname, s1);
	if (s2)
		strcpy(d->defname + n1, s2);
	in_define = 1;
	nice_printf(outfile, "#define %s", d->defname);
	if (post)
		nice_printf(outfile, " %s", post);
	}

 void
#ifdef KR_headers
other_undefs(outfile)
	FILE *outfile;
#else
other_undefs(FILE *outfile)
#endif
{
	defines *d;
	if (d = define_list) {
		define_list = 0;
		nice_printf(outfile, "\n");
		do
			nice_printf(outfile, "#undef %s\n", d->defname);
			while(d = d->next);
		nice_printf(outfile, "\n");
		}
	}