Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / dev / raidframe / rf_utils.c
blob958071fd5d5d247e04c2170576034136adcdbba2
1 /* $NetBSD: rf_utils.c,v 1.15 2006/10/12 01:31:52 christos Exp $ */
2 /*
3 * Copyright (c) 1995 Carnegie-Mellon University.
4 * All rights reserved.
6 * Author: Mark Holland
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.
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.
18 * Carnegie Mellon requests users of this software to return to
20 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
21 * School of Computer Science
22 * Carnegie Mellon University
23 * Pittsburgh PA 15213-3890
25 * any improvements or extensions that they make and grant Carnegie the
26 * rights to redistribute these changes.
29 /****************************************
31 * rf_utils.c -- various support routines
33 ****************************************/
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: rf_utils.c,v 1.15 2006/10/12 01:31:52 christos Exp $");
38 #include "rf_archs.h"
39 #include "rf_utils.h"
40 #include "rf_debugMem.h"
41 #include "rf_alloclist.h"
43 /* creates & zeros 2-d array with b rows and k columns (MCH) */
44 RF_RowCol_t **
45 rf_make_2d_array(int b, int k, RF_AllocListElem_t *allocList)
47 RF_RowCol_t **retval, i;
49 RF_MallocAndAdd(retval, b * sizeof(RF_RowCol_t *), (RF_RowCol_t **), allocList);
50 for (i = 0; i < b; i++) {
51 RF_MallocAndAdd(retval[i], k * sizeof(RF_RowCol_t), (RF_RowCol_t *), allocList);
52 (void) memset((char *) retval[i], 0, k * sizeof(RF_RowCol_t));
54 return (retval);
57 #if (RF_INCLUDE_PARITY_DECLUSTERING > 0) || (RF_INCLUDE_PARITY_DECLUSTERING_PQ > 0)
59 void
60 rf_free_2d_array(RF_RowCol_t **a, int b, int k)
62 RF_RowCol_t i;
64 for (i = 0; i < b; i++)
65 RF_Free(a[i], k * sizeof(RF_RowCol_t));
66 RF_Free(a, b * sizeof(RF_RowCol_t));
70 /* creates & zeros a 1-d array with c columns */
71 RF_RowCol_t *
72 rf_make_1d_array(int c, RF_AllocListElem_t *allocList)
74 RF_RowCol_t *retval;
76 RF_MallocAndAdd(retval, c * sizeof(RF_RowCol_t), (RF_RowCol_t *), allocList);
77 (void) memset((char *) retval, 0, c * sizeof(RF_RowCol_t));
78 return (retval);
81 void
82 rf_free_1d_array(RF_RowCol_t *a, int n)
84 RF_Free(a, n * sizeof(RF_RowCol_t));
87 /* Euclid's algorithm: finds and returns the greatest common divisor
88 * between a and b. (MCH)
90 int
91 rf_gcd(int m, int n)
93 int t;
95 while (m > 0) {
96 t = n % m;
97 n = m;
98 m = t;
100 return (n);
102 #endif
103 /* these convert between text and integer. Apparently the regular C macros
104 * for doing this are not available in the kernel
107 #define ISDIGIT(x) ( (x) >= '0' && (x) <= '9' )
108 #define ISHEXCHAR(x) ( ((x) >= 'a' && (x) <= 'f') || ((x) >= 'A' && (x) <= 'F') )
109 #define ISHEX(x) ( ISDIGIT(x) || ISHEXCHAR(x) )
110 #define HC2INT(x) ( ((x) >= 'a' && (x) <= 'f') ? (x) - 'a' + 10 : \
111 ( ((x) >= 'A' && (x) <= 'F') ? (x) - 'A' + 10 : (x - '0') ) )
114 rf_atoi(char *p)
116 int val = 0, negate = 0;
118 if (*p == '-') {
119 negate = 1;
120 p++;
122 for (; ISDIGIT(*p); p++)
123 val = 10 * val + (*p - '0');
124 return ((negate) ? -val : val);
128 rf_htoi(char *p)
130 int val = 0;
131 for (; ISHEXCHAR(*p); p++)
132 val = 16 * val + HC2INT(*p);
133 return (val);