-
Notifications
You must be signed in to change notification settings - Fork 0
/
al_helper.c
172 lines (129 loc) · 3.79 KB
/
al_helper.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
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
/***********************************************************************************
Copyright 2014 Arizona Board of Regents; all rights reserved.
This software is being provided by the copyright holders under the
following license. By obtaining, using and/or copying this software, you
agree that you have read, understood, and will comply with the following
terms and conditions:
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee or royalty is hereby granted,
provided that the full text of this notice appears on all copies of the
software and documentation or portions thereof, including modifications,
that you make.
This software is provided "as is," and copyright holders make no
representations or warranties, expressed or implied. By way of example, but
not limitation, copyright holders make no representations or warranties of
merchantability or fitness for any particular purpose or that the use of the
software or documentation will not infringe any third party patents,
copyrights, trademarks or other rights. Copyright holders will bear no
liability for any use of this software or documentation.
The name and trademarks of copyright holders may not be used in advertising
or publicity pertaining to the software without specific, written prior
permission. Title to copyright in this software and any associated
documentation will at all times remain with copyright holders.
***********************************************************************************/
/*
* helper.c -- helper functions for data structure code.
*/
#include <stdlib.h>
#include "al_helper.h"
#include "instr.h"
void *alloc(int size) {
void *new = malloc(size);
if(new == NULL) {
fprintf(stderr,"ALLOC: Out of memory\n");
}
return (new);
}
void *zalloc(int size) {
int i;
char *new = (char *)alloc(size);
if(new == NULL) {
fprintf(stderr,"ZALLOC: Out of memory\n");
}
for(i = 0; i < size; i++) {
new[i]= 0x0;
}
return (new);
}
void dealloc(void *ptr) {
free(ptr);
}
void *resize(void *address, int newsize) {
void *mem;
mem = realloc(address, newsize);
if (mem == NULL) {
fprintf(stderr,"RESIZE: Out of memory\n");
}
return (mem);
}
/*
* intCompare(o1,o2) -- compare ints for sorting.
*
* #%#% Does not work in case of overflow?
*/
int intCompare(void *o1, void *o2) {
return refCompare(o1, o2);
}
/*
* refCompare(o1,o2) -- sorting comparison for ints and pointers.
*
* Subtracts value of o1 from o2.
* #%#% Does not work in case of overflow?
*/
int refCompare(void *o1, void *o2) {
return (long int) o1 - (long int) o2;
}
/*
* strCompare(o1,o2) -- compare strings for sorting.
*/
int strCompare(void *o1, void *o2) {
return strcmp(o1, o2);
}
/*
* insCompareByAddr(i1, i2) -- compare instructions by address for sorting
*/
int insCompareByAddr(void *i1, void *i2) {
Instruction *ins1, *ins2;
ins1 = (Instruction *)i1;
ins2 = (Instruction *)i2;
if (ins1->addr < ins2->addr) {
return -1;
}
if (ins1->addr > ins2-> addr) {
return 1;
}
return 0;
}
/*
* insCompareByOrder(i1, i2) -- compare instructions by order # for sorting
*/
int insCompareByOrder(void *i1, void *i2) {
Instruction *ins1, *ins2;
ins1 = (Instruction *)i1;
ins2 = (Instruction *)i2;
if (ins1->order < ins2->order) {
return -1;
}
if (ins1->order > ins2->order) {
return 1;
}
return 0;
}
/*
* intPrint(item) -- print an integer.
*/
void intPrint(void *item) {
printf("%ld", (long int) item);
}
/*
* refPrint(item) -- print memory addresses.
*/
void refPrint(void *item) {
printf("%16lX", (unsigned long int) item);
}
/*
* strPrint(key) -- print a string.
*/
void strPrint(void *key) {
printf("\"%s\"", (char *) key);
}