-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.c
95 lines (82 loc) · 1.59 KB
/
utils.c
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
#include "align3.h"
double UniformRand (void)
{
double r = (rand () / (double) 0x7FFF);
assert (0 <= r && r <= 1);
return (r);
}
int BitRand (void)
{
return (rand () % 2);
}
void **Allocations = (void **)NULL;
int NAllocations = 0;
int MaxAllocations = 0;
void *Allocate_D (unsigned u)
{
void *p;
if (NAllocations >= MaxAllocations) {
MaxAllocations = Max (2048, 2 * MaxAllocations);
Allocations = realloc (Allocations, MaxAllocations * sizeof (void *));
assert (Allocations);
assert (NAllocations < MaxAllocations);
}
p = (void *)malloc (u);
assert (p != (void *)NULL);
Allocations[NAllocations++] = p;
return (p);
}
void *Reallocate_D (void *p, unsigned n)
{
int i;
for (i = NAllocations - 1; i >= 0; i--)
if (p is Allocations[i])
break;
assert (i >= 0);
p = realloc (p, n);
assert (p);
Allocations[i] = p;
return (p);
}
void DeallocateEverything () {
int i;
LoopBelow (i, NAllocations)
free (Allocations[i]);
free (Allocations);
Allocations = (void **) NULL;
MaxAllocations = NAllocations = 0;
}
int streq (char *s1, char *s2)
{
return (0 == strcmp (s1, s2));
}
int round (double x)
{
return ((int) (0.5+x));
}
char *CopyString (char *s)
{
char *t = Allocate_D (1 + strlen (s));
strcpy (t, s);
return (t);
}
void fail (char *s)
{
fprintf (stderr, "Error: %s\n", s);
strcpy (NULL, "Kaboom!");
exit (-1);
}
int CountBits (int x) {
int bits = 0, i;
LoopBelow (i, WORDSIZE)
if (x & (1 << i))
bits++;
return (bits);
}
int MissingDataCode (c)
char c;
{
return (c is '-' ||
c is '.' ||
c is '?');
}