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