1 /* $NetBSD: rf_utils.c,v 1.15 2006/10/12 01:31:52 christos Exp $ */
3 * Copyright (c) 1995 Carnegie-Mellon University.
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 $");
40 #include "rf_debugMem.h"
41 #include "rf_alloclist.h"
43 /* creates & zeros 2-d array with b rows and k columns (MCH) */
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
));
57 #if (RF_INCLUDE_PARITY_DECLUSTERING > 0) || (RF_INCLUDE_PARITY_DECLUSTERING_PQ > 0)
60 rf_free_2d_array(RF_RowCol_t
**a
, int b
, int k
)
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 */
72 rf_make_1d_array(int c
, RF_AllocListElem_t
*allocList
)
76 RF_MallocAndAdd(retval
, c
* sizeof(RF_RowCol_t
), (RF_RowCol_t
*), allocList
);
77 (void) memset((char *) retval
, 0, c
* sizeof(RF_RowCol_t
));
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)
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') ) )
116 int val
= 0, negate
= 0;
122 for (; ISDIGIT(*p
); p
++)
123 val
= 10 * val
+ (*p
- '0');
124 return ((negate
) ? -val
: val
);
131 for (; ISHEXCHAR(*p
); p
++)
132 val
= 16 * val
+ HC2INT(*p
);