rf_utils.c revision 1.1 1 /* $NetBSD: rf_utils.c,v 1.1 1998/11/13 04:20:35 oster Exp $ */
2 /*
3 * Copyright (c) 1995 Carnegie-Mellon University.
4 * All rights reserved.
5 *
6 * Author: Mark Holland
7 *
8 * Permission to use, copy, modify and distribute this software and
9 * its documentation is hereby granted, provided that both the copyright
10 * notice and this permission notice appear in all copies of the
11 * software, derivative works or modified versions, and any portions
12 * thereof, and that both notices appear in supporting documentation.
13 *
14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
15 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
16 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17 *
18 * Carnegie Mellon requests users of this software to return to
19 *
20 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
21 * School of Computer Science
22 * Carnegie Mellon University
23 * Pittsburgh PA 15213-3890
24 *
25 * any improvements or extensions that they make and grant Carnegie the
26 * rights to redistribute these changes.
27 */
28
29 /****************************************
30 *
31 * rf_utils.c -- various support routines
32 *
33 ****************************************/
34
35 /* :
36 * Log: rf_utils.c,v
37 * Revision 1.20 1996/07/27 23:36:08 jimz
38 * Solaris port of simulator
39 *
40 * Revision 1.19 1996/07/22 19:52:16 jimz
41 * switched node params to RF_DagParam_t, a union of
42 * a 64-bit int and a void *, for better portability
43 * attempted hpux port, but failed partway through for
44 * lack of a single C compiler capable of compiling all
45 * source files
46 *
47 * Revision 1.18 1996/07/15 17:22:18 jimz
48 * nit-pick code cleanup
49 * resolve stdlib problems on DEC OSF
50 *
51 * Revision 1.17 1996/06/09 02:36:46 jimz
52 * lots of little crufty cleanup- fixup whitespace
53 * issues, comment #ifdefs, improve typing in some
54 * places (esp size-related)
55 *
56 * Revision 1.16 1996/06/07 21:33:04 jimz
57 * begin using consistent types for sector numbers,
58 * stripe numbers, row+col numbers, recon unit numbers
59 *
60 * Revision 1.15 1996/06/03 23:28:26 jimz
61 * more bugfixes
62 * check in tree to sync for IPDS runs with current bugfixes
63 * there still may be a problem with threads in the script test
64 * getting I/Os stuck- not trivially reproducible (runs ~50 times
65 * in a row without getting stuck)
66 *
67 * Revision 1.14 1996/06/02 17:31:48 jimz
68 * Moved a lot of global stuff into array structure, where it belongs.
69 * Fixed up paritylogging, pss modules in this manner. Some general
70 * code cleanup. Removed lots of dead code, some dead files.
71 *
72 * Revision 1.13 1996/05/27 18:56:37 jimz
73 * more code cleanup
74 * better typing
75 * compiles in all 3 environments
76 *
77 * Revision 1.12 1996/05/23 21:46:35 jimz
78 * checkpoint in code cleanup (release prep)
79 * lots of types, function names have been fixed
80 *
81 * Revision 1.11 1996/05/18 19:51:34 jimz
82 * major code cleanup- fix syntax, make some types consistent,
83 * add prototypes, clean out dead code, et cetera
84 *
85 * Revision 1.10 1995/12/06 15:17:44 root
86 * added copyright info
87 *
88 */
89
90 #include "rf_threadstuff.h"
91
92 #ifdef _KERNEL
93 #define KERNEL
94 #endif
95
96 #ifndef KERNEL
97 #include <stdio.h>
98 #endif /* !KERNEL */
99 #include <sys/time.h>
100
101 #include "rf_threadid.h"
102 #include "rf_utils.h"
103 #include "rf_debugMem.h"
104 #include "rf_alloclist.h"
105 #include "rf_general.h"
106 #include "rf_sys.h"
107
108 #ifndef KERNEL
109 #include "rf_randmacros.h"
110 #endif /* !KERNEL */
111
112 /* creates & zeros 2-d array with b rows and k columns (MCH) */
113 RF_RowCol_t **rf_make_2d_array(b, k, allocList)
114 int b;
115 int k;
116 RF_AllocListElem_t *allocList;
117 {
118 RF_RowCol_t **retval, i;
119
120 RF_MallocAndAdd(retval, b * sizeof(RF_RowCol_t *), (RF_RowCol_t **), allocList);
121 for (i=0; i<b; i++) {
122 RF_MallocAndAdd(retval[i], k * sizeof(RF_RowCol_t), (RF_RowCol_t *), allocList);
123 (void) bzero((char *) retval[i], k*sizeof(RF_RowCol_t));
124 }
125 return(retval);
126 }
127
128 void rf_free_2d_array(a, b, k)
129 RF_RowCol_t **a;
130 int b;
131 int k;
132 {
133 RF_RowCol_t i;
134
135 for (i=0; i<b; i++)
136 RF_Free(a[i], k*sizeof(RF_RowCol_t));
137 RF_Free(a, b*sizeof(RF_RowCol_t));
138 }
139
140
141 /* creates & zeros a 1-d array with c columns */
142 RF_RowCol_t *rf_make_1d_array(c, allocList)
143 int c;
144 RF_AllocListElem_t *allocList;
145 {
146 RF_RowCol_t *retval;
147
148 RF_MallocAndAdd(retval, c * sizeof(RF_RowCol_t), (RF_RowCol_t *), allocList);
149 (void) bzero((char *) retval, c*sizeof(RF_RowCol_t));
150 return(retval);
151 }
152
153 void rf_free_1d_array(a, n)
154 RF_RowCol_t *a;
155 int n;
156 {
157 RF_Free(a, n * sizeof(RF_RowCol_t));
158 }
159
160 /* Euclid's algorithm: finds and returns the greatest common divisor
161 * between a and b. (MCH)
162 */
163 int rf_gcd(m, n)
164 int m;
165 int n;
166 {
167 int t;
168
169 while (m>0) {
170 t = n % m;
171 n = m;
172 m = t;
173 }
174 return(n);
175 }
176
177 #if !defined(KERNEL) && !defined(SIMULATE) && defined(__osf__)
178 /* this is used to generate a random number when _FASTRANDOM is off
179 * in randmacros.h
180 */
181 long rf_do_random(rval, rdata)
182 long *rval;
183 struct random_data *rdata;
184 {
185 int a, b;
186 long c;
187 /*
188 * random_r() generates random 32-bit values. OR them together.
189 */
190 if (random_r(&a, rdata)!=0) {
191 fprintf(stderr,"Yikes! call to random_r failed\n");
192 exit(1);
193 }
194 if (random_r(&b, rdata)!=0) {
195 fprintf(stderr,"Yikes! call to random_r failed\n");
196 exit(1);
197 }
198 c = ((long)a)<<32;
199 *rval = c|b;
200 return(*rval);
201 }
202 #endif /* !KERNEL && !SIMULATE && __osf__ */
203
204 /* these convert between text and integer. Apparently the regular C macros
205 * for doing this are not available in the kernel
206 */
207
208 #define ISDIGIT(x) ( (x) >= '0' && (x) <= '9' )
209 #define ISHEXCHAR(x) ( ((x) >= 'a' && (x) <= 'f') || ((x) >= 'A' && (x) <= 'F') )
210 #define ISHEX(x) ( ISDIGIT(x) || ISHEXCHAR(x) )
211 #define HC2INT(x) ( ((x) >= 'a' && (x) <= 'f') ? (x) - 'a' + 10 : \
212 ( ((x) >= 'A' && (x) <= 'F') ? (x) - 'A' + 10 : (x - '0') ) )
213
214 int rf_atoi(p)
215 char *p;
216 {
217 int val = 0, negate = 0;
218
219 if (*p == '-') {negate=1; p++;}
220 for ( ; ISDIGIT(*p); p++) val = 10 * val + (*p - '0');
221 return((negate) ? -val : val);
222 }
223
224 int rf_htoi(p)
225 char *p;
226 {
227 int val = 0;
228 for ( ; ISHEXCHAR(*p); p++) val = 16 * val + HC2INT(*p);
229 return(val);
230 }
231