rf_utils.c revision 1.2 1 /* $NetBSD: rf_utils.c,v 1.2 1999/01/26 02:34:03 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 #include "rf_threadstuff.h"
37
38 #include <sys/time.h>
39
40 #include "rf_threadid.h"
41 #include "rf_utils.h"
42 #include "rf_debugMem.h"
43 #include "rf_alloclist.h"
44 #include "rf_general.h"
45 #include "rf_sys.h"
46
47 /* creates & zeros 2-d array with b rows and k columns (MCH) */
48 RF_RowCol_t **rf_make_2d_array(b, k, allocList)
49 int b;
50 int k;
51 RF_AllocListElem_t *allocList;
52 {
53 RF_RowCol_t **retval, i;
54
55 RF_MallocAndAdd(retval, b * sizeof(RF_RowCol_t *), (RF_RowCol_t **), allocList);
56 for (i=0; i<b; i++) {
57 RF_MallocAndAdd(retval[i], k * sizeof(RF_RowCol_t), (RF_RowCol_t *), allocList);
58 (void) bzero((char *) retval[i], k*sizeof(RF_RowCol_t));
59 }
60 return(retval);
61 }
62
63 void rf_free_2d_array(a, b, k)
64 RF_RowCol_t **a;
65 int b;
66 int k;
67 {
68 RF_RowCol_t i;
69
70 for (i=0; i<b; i++)
71 RF_Free(a[i], k*sizeof(RF_RowCol_t));
72 RF_Free(a, b*sizeof(RF_RowCol_t));
73 }
74
75
76 /* creates & zeros a 1-d array with c columns */
77 RF_RowCol_t *rf_make_1d_array(c, allocList)
78 int c;
79 RF_AllocListElem_t *allocList;
80 {
81 RF_RowCol_t *retval;
82
83 RF_MallocAndAdd(retval, c * sizeof(RF_RowCol_t), (RF_RowCol_t *), allocList);
84 (void) bzero((char *) retval, c*sizeof(RF_RowCol_t));
85 return(retval);
86 }
87
88 void rf_free_1d_array(a, n)
89 RF_RowCol_t *a;
90 int n;
91 {
92 RF_Free(a, n * sizeof(RF_RowCol_t));
93 }
94
95 /* Euclid's algorithm: finds and returns the greatest common divisor
96 * between a and b. (MCH)
97 */
98 int rf_gcd(m, n)
99 int m;
100 int n;
101 {
102 int t;
103
104 while (m>0) {
105 t = n % m;
106 n = m;
107 m = t;
108 }
109 return(n);
110 }
111
112 /* these convert between text and integer. Apparently the regular C macros
113 * for doing this are not available in the kernel
114 */
115
116 #define ISDIGIT(x) ( (x) >= '0' && (x) <= '9' )
117 #define ISHEXCHAR(x) ( ((x) >= 'a' && (x) <= 'f') || ((x) >= 'A' && (x) <= 'F') )
118 #define ISHEX(x) ( ISDIGIT(x) || ISHEXCHAR(x) )
119 #define HC2INT(x) ( ((x) >= 'a' && (x) <= 'f') ? (x) - 'a' + 10 : \
120 ( ((x) >= 'A' && (x) <= 'F') ? (x) - 'A' + 10 : (x - '0') ) )
121
122 int rf_atoi(p)
123 char *p;
124 {
125 int val = 0, negate = 0;
126
127 if (*p == '-') {negate=1; p++;}
128 for ( ; ISDIGIT(*p); p++) val = 10 * val + (*p - '0');
129 return((negate) ? -val : val);
130 }
131
132 int rf_htoi(p)
133 char *p;
134 {
135 int val = 0;
136 for ( ; ISHEXCHAR(*p); p++) val = 16 * val + HC2INT(*p);
137 return(val);
138 }
139