File: loaderlib.c

package info (click to toggle)
vmelilo 1.1.1-1
  • links: PTS
  • area: main
  • in suites: slink
  • size: 360 kB
  • ctags: 761
  • sloc: ansic: 7,361; makefile: 98
file content (625 lines) | stat: -rw-r--r-- 10,503 bytes parent folder | download
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
/*
 *	VME Linux/m68k Loader
 *
 *	(c) Copyright 1997 by Nick Holgate
 *
 *	This file is subject to the terms and conditions of the GNU General Public
 *	License.  See the file COPYING for more details.
 */

/*--------------------------------------------------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

#include "loader.h"
#include "loaderlib.h"

/*--------------------------------------------------------------------------*/

extern const FILEMAP * find_file_map(const char *path);

/*--------------------------------------------------------------------------*/

typedef struct memfrag_str
{
	unsigned long		size; 		/* size of free block including header  */
	struct memfrag_str	*next;		/* pointer to next memory fragment		*/

} memfrag_t;

/*--------------------------------------------------------------------------*/

static memfrag_t		*free_list = NULL;	/* ptr to first free mem frag	*/

/*--------------------------------------------------------------------------*/
/* initialise memory allocator
 */

void
mem_alloc_init
(	void			*heap_base,
	unsigned long	heap_size
)
{
	if (free_list == NULL)
	{
		free_list       = heap_base;
		free_list->size = heap_size;
		free_list->next = NULL;
	}
}

/*--------------------------------------------------------------------------*/

void *
mem_alloc
(	unsigned long	size
)
{	memfrag_t		*frag;
	memfrag_t		*last;

	/* long word align and add space to store allocation size */
	size = (size + sizeof(frag->size) + 3) & ~3;

	frag = free_list;
	last = NULL;

	while (frag)
	{
		if (frag->size >= size)
		{
			goto frag_found;
		}

		/* onwards ... */
		last = frag;
		frag = frag->next;
	}

	/* nothing found! */
	return NULL;

	/* A suitable fragment has been found
	 * now we must unlink it from the free list.
	 * If the fragment is large enough just take
	 * the allocation from the end.
	 */
frag_found:
	/* is it big enough to split into two fragments */
	if (frag->size >= (size + sizeof(memfrag_t)))
	{
		/* adjust for amount removed */
		frag->size -= size;

		/* point at allocated chunk at end */
		frag = (memfrag_t *) ((char *)frag + frag->size);

		/* store allocated size */
		frag->size = size;
	}
	else
	{
		/* unlink found fragment */
		if (last)
		{
			last->next = frag->next;
		}
		else
		{
			free_list  = frag->next;
		}
	}

	/* zero intialise allocated memory */
	mem_clear(&(frag->next), frag->size - sizeof(frag->size));

	/* return pointer to allocated memory */
	return &(frag->next);
}

/*--------------------------------------------------------------------------*/
/* return size of largest free memory block
 */

#ifdef NOT_USED

unsigned long
mem_maxalloc
(	void
)
{	memfrag_t		*frag;
	unsigned long	size = 0;

	/* find size of largest fragment */
	for (frag = free_list; frag; frag = frag->next)
	{
		if (frag->size > size)
		{
			size = frag->size;
		}
	}

	/* anything found? */
	if (size)
	{
		/* adjust for allocator overhead */
		size -= sizeof(frag->size);
	}

	/* return size available */
	return size;
}

#endif

/*--------------------------------------------------------------------------*/
/* resize block of allocated memory
 * (can only handle size reduction)
 */

#ifdef NOT_USED

int
mem_resize
(	void			*memaddr,
	unsigned long	newsize
)
{	memfrag_t		*frag;
	memfrag_t		*newfrag;

	/* recover pointer to base of allocation */
	frag = (memfrag_t *) ((char *)memaddr - sizeof(frag->size));

	/* long word align and add space to store allocation size */
	newsize = (newsize + sizeof(frag->size) + 3) & ~3;

	/* do they want more than we have */
	if (newsize > frag->size)
	{
		return -1;
	}

	/* is there enough to make a new fragment from remainder */
	if ((frag->size - newsize) >= sizeof(memfrag_t))
	{
		/* make new fragment from end of old */
		newfrag       = (memfrag_t *)((char *)frag + newsize);
		newfrag->size = frag->size - newsize; 

		/* give fragment back to heap */
		mem_free(&(newfrag->next));

		/* remember new fragment size */
		frag->size = newsize;
	}

	return 0;
}

#endif

/*--------------------------------------------------------------------------*/

int
mem_free
(	void		*memaddr
)
{	memfrag_t	*last;
	memfrag_t	*frag;
	memfrag_t	*dead;

	if (memaddr == NULL)
	{
		return -1;
	}

	/* recover pointer to base of allocation */
	dead = (memfrag_t *) ((char *)memaddr - sizeof(frag->size));

	frag = free_list;
	last = NULL;

	/* find fragment that will follow us */
	while (frag && (frag < dead))
	{
		last = frag;
		frag = frag->next;
	}

	/* make sure it doesn't overlap previous fragment */
	if (last)
	{
		if ((char *)dead < ((char *)last + last->size))
		{
			return -1;
		}
	}

	/* make sure it doesn't overlap next fragment */
	if (frag)
	{
		if (((char *)dead + dead->size) > (char *)frag)
		{
			return -1;
		}
	}

	/* can we merge with next fragment */
	if (((char *)dead + dead->size) == (char *)frag)
	{
		/* merge with next fragment */
		dead->next  = frag->next;
		dead->size += frag->size;
	}
	else
	{
		/* link to next */
		dead->next = frag;
	}

	/* if there is a previous fragment */
	if (last)
	{
		/* can we merge with previous fragment */
		if (((char *)last + last->size) == (char *)dead)
		{
			/* merge with previous fragment */
			last->next  = dead->next;
			last->size += dead->size;
		}
		else
		{
			/* just link to previous fragment */
			last->next  = dead;
		}
	}

	/* no previous fragment */
	else
	{
		/* make new head of list */
		free_list = dead;
	}

	return 0;
}

/*--------------------------------------------------------------------------*/

unsigned long
disable_icache
(	void
)
{	unsigned long	old_cacr;

	/* disable and invalidate instruction cache */
	__asm__ __volatile__ (
	"	movec	%%cacr,%%d1
		move.l	%%d1,%0
		bclr	#15,%%d1
		movec	%%d1,%%cacr
		.word	0xf498			| cinva	ic
	"
	: "=d" (old_cacr)
	: /* no inputs */
	: "d1"
	);

	return old_cacr;
}

/*--------------------------------------------------------------------------*/

unsigned long
enable_icache
(	void
)
{	unsigned long	old_cacr;

	/* enable instruction cache */
	__asm__ __volatile__ (
	"	movec	%%cacr,%%d1
		move.l	%%d1,%0
		bset	#15,%%d1
		movec	%%d1,%%cacr
	"
	: "=d" (old_cacr)
	: /* no inputs */
	: "d1"
	);

	return old_cacr;
}

/*--------------------------------------------------------------------------*/

void
invalidate_icache
(	void
)
{
	__asm__ __volatile__ (
	"	.word	0xf498		| cinva ic - invalidate instruction cache
	"
	);
}

/*--------------------------------------------------------------------------*/

int
str_len
(	const char	*s
)
{	const char	*e = s;

	while (*e++)
		;

	return (e - s) - 1;
}

/*--------------------------------------------------------------------------*/

void
str_cpy
(	char		*d,
	const char	*s
)
{
	while ((*d++ = *s++) != '\0')
		;
}

/*--------------------------------------------------------------------------*/

void
str_cat
(	char		*d,
	const char	*s
)
{
	while (*d++ != '\0')
		;

	d--;

	while ((*d++ = *s++) != '\0')
		;
}

/*--------------------------------------------------------------------------*/

void
str_ncat
(	char			*d,
	const char		*s,
	unsigned long	n
)
{
	while (n && (*d++ != '\0'))
	{
		n--;
	}

	d--;

	while (n-- && *s)
	{
		*d++ = *s++;
	}

	*d = '\0';
}

/*--------------------------------------------------------------------------*/

void
str_ncpy
(	char			*d,
	const char		*s,
	unsigned long	n
)
{	const char		*save;
	unsigned long	max;

	save = s;
	max  = 1;

	while (*s++) max++;

	s = save;

	if (n < max) max = n;

	n -= max;

	while (max--)
	{
		*d++ = *s++;
	}

	while (n--)
	{
		*d++ = '\0';
	}
}

/*--------------------------------------------------------------------------*/
/* Check whether two strings exist and second is a prefix of the first
 * while ignoring case.
 */

int
prefix_string
(	const char	*s,						/* string							*/
	const char	*p						/* prefix							*/
)
{	char		sc;
	char		pc;

	if ((s == NULL) || (p == NULL))
	{
		return FALSE;
	}

	/* until we reach end of prefix */
	while (*p)
	{
		sc = *s++;
		pc = *p++;

		/* convert to lower case */
		if ((sc >= 'A') && (sc <= 'Z')) sc += 'a' - 'A';
		if ((pc >= 'A') && (pc <= 'Z')) pc += 'a' - 'A';

		if (sc != pc)
		{
			return FALSE;
		}
	}

	/* make sure we are either at end of source or in white space */
	if (*s >= '!')
	{
		return FALSE;
	}

	return TRUE;
}

/*--------------------------------------------------------------------------*/
/* Check whether two strings exist and are equal ignoring case.
 */

int
equal_strings
(	const char	*s1,
	const char	*s2
)
{	char		c1;
	char		c2;

	if ((s1 == NULL) || (s2 == NULL))
	{
		return FALSE;
	}

	while (*s1 || *s2)
	{
		c1 = *s1++;
		c2 = *s2++;

		/* convert to lower case */
		if ((c1 >= 'A') && (c1 <= 'Z')) c1 += 'a' - 'A';
		if ((c2 >= 'A') && (c2 <= 'Z')) c2 += 'a' - 'A';

		if (c1 != c2)
		{
			return FALSE;
		}
	}

	return TRUE;
}

/*--------------------------------------------------------------------------*/
/* Check whether two strings exist and are equal including case
 */

int
case_equal_strings
(	const char	*s1,
	const char	*s2
)
{
	if ((s1 == NULL) || (s2 == NULL))
	{
		return FALSE;
	}

	while (*s1 || *s2)
	{
		if (*s1++ != *s2++)
		{
			return FALSE;
		}
	}

	return TRUE;
}

/*--------------------------------------------------------------------------*/

int
file_size
(	const char		*path
)
{	const FILEMAP	*map;

	if ((map = find_file_map(path)) == NULL)
	{
		return -1;
	}

	return MAP_FILESIZE(map);
}

/*--------------------------------------------------------------------------*/

static unsigned	pcnt_current;
static unsigned	pcnt_total;
static int		pcnt_backspaces;

void
percent_init
(	unsigned long	total
)
{
	pcnt_total      = total;
	pcnt_current    = 999;
	pcnt_backspaces = 0;
}

/*--------------------------------------------------------------------------*/

void
percent_term
(	const char	*msg
)
{
	/* remove old percentage */
	while (pcnt_backspaces--)
		put_str("\b \b");

	put_str(msg);
}

/*--------------------------------------------------------------------------*/

void
percent_show
(	unsigned long	current
)
{	unsigned 		percentage;

	/* calculate new percentage */
	percentage = current * 100 / pcnt_total;

	/* if percentage changed */
	if (percentage != pcnt_current)
	{
		/* remove old percentage */
		while (pcnt_backspaces--)
			put_char('\b');

		/* output new percentage */
		Printf("%ld%% %n", percentage, &pcnt_backspaces);

		/* remember it for next time */
		pcnt_current = percentage;
	}
}

/*-----------------------------< end of file >------------------------------*/