xref: /netbsd/sys/dev/raidframe/rf_utils.c (revision c4a72b64)
1 /*	$NetBSD: rf_utils.c,v 1.10 2002/09/21 00:47:59 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 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: rf_utils.c,v 1.10 2002/09/21 00:47:59 oster Exp $");
37 
38 #include "rf_archs.h"
39 #include "rf_utils.h"
40 #include "rf_debugMem.h"
41 #include "rf_alloclist.h"
42 
43 /* creates & zeros 2-d array with b rows and k columns (MCH) */
44 RF_RowCol_t **
45 rf_make_2d_array(b, k, allocList)
46 	int     b;
47 	int     k;
48 	RF_AllocListElem_t *allocList;
49 {
50 	RF_RowCol_t **retval, i;
51 
52 	RF_MallocAndAdd(retval, b * sizeof(RF_RowCol_t *), (RF_RowCol_t **), allocList);
53 	for (i = 0; i < b; i++) {
54 		RF_MallocAndAdd(retval[i], k * sizeof(RF_RowCol_t), (RF_RowCol_t *), allocList);
55 		(void) memset((char *) retval[i], 0, k * sizeof(RF_RowCol_t));
56 	}
57 	return (retval);
58 }
59 
60 #if (RF_INCLUDE_PARITY_DECLUSTERING > 0) || (RF_INCLUDE_PARITY_DECLUSTERING_PQ > 0)
61 
62 void
63 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 *
78 rf_make_1d_array(c, allocList)
79 	int     c;
80 	RF_AllocListElem_t *allocList;
81 {
82 	RF_RowCol_t *retval;
83 
84 	RF_MallocAndAdd(retval, c * sizeof(RF_RowCol_t), (RF_RowCol_t *), allocList);
85 	(void) memset((char *) retval, 0, c * sizeof(RF_RowCol_t));
86 	return (retval);
87 }
88 
89 void
90 rf_free_1d_array(a, n)
91 	RF_RowCol_t *a;
92 	int     n;
93 {
94 	RF_Free(a, n * sizeof(RF_RowCol_t));
95 }
96  0)
97 /* Euclid's algorithm:  finds and returns the greatest common divisor
98  * between a and b.     (MCH)
99  */
100 int
101 rf_gcd(m, n)
102 	int     m;
103 	int     n;
104 {
105 	int     t;
106 
107 	while (m > 0) {
108 		t = n % m;
109 		n = m;
110 		m = t;
111 	}
112 	return (n);
113 }
114 #endif
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