rf_utils.c revision 1.17 1 1.17 christos /* $NetBSD: rf_utils.c,v 1.17 2019/02/09 03:34:00 christos Exp $ */
2 1.1 oster /*
3 1.1 oster * Copyright (c) 1995 Carnegie-Mellon University.
4 1.1 oster * All rights reserved.
5 1.1 oster *
6 1.1 oster * Author: Mark Holland
7 1.1 oster *
8 1.1 oster * Permission to use, copy, modify and distribute this software and
9 1.1 oster * its documentation is hereby granted, provided that both the copyright
10 1.1 oster * notice and this permission notice appear in all copies of the
11 1.1 oster * software, derivative works or modified versions, and any portions
12 1.1 oster * thereof, and that both notices appear in supporting documentation.
13 1.1 oster *
14 1.1 oster * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
15 1.1 oster * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
16 1.1 oster * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17 1.1 oster *
18 1.1 oster * Carnegie Mellon requests users of this software to return to
19 1.1 oster *
20 1.1 oster * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
21 1.1 oster * School of Computer Science
22 1.1 oster * Carnegie Mellon University
23 1.1 oster * Pittsburgh PA 15213-3890
24 1.1 oster *
25 1.1 oster * any improvements or extensions that they make and grant Carnegie the
26 1.1 oster * rights to redistribute these changes.
27 1.1 oster */
28 1.1 oster
29 1.1 oster /****************************************
30 1.1 oster *
31 1.1 oster * rf_utils.c -- various support routines
32 1.1 oster *
33 1.1 oster ****************************************/
34 1.1 oster
35 1.8 lukem #include <sys/cdefs.h>
36 1.17 christos __KERNEL_RCSID(0, "$NetBSD: rf_utils.c,v 1.17 2019/02/09 03:34:00 christos Exp $");
37 1.1 oster
38 1.7 oster #include "rf_archs.h"
39 1.1 oster #include "rf_utils.h"
40 1.1 oster #include "rf_debugMem.h"
41 1.1 oster #include "rf_alloclist.h"
42 1.1 oster
43 1.1 oster /* creates & zeros 2-d array with b rows and k columns (MCH) */
44 1.3 oster RF_RowCol_t **
45 1.12 oster rf_make_2d_array(int b, int k, RF_AllocListElem_t *allocList)
46 1.3 oster {
47 1.3 oster RF_RowCol_t **retval, i;
48 1.3 oster
49 1.17 christos retval = RF_MallocAndAdd(b * sizeof(*retval), allocList);
50 1.3 oster for (i = 0; i < b; i++) {
51 1.17 christos retval[i] = RF_MallocAndAdd(k * sizeof(*retval[i]), allocList);
52 1.3 oster }
53 1.3 oster return (retval);
54 1.3 oster }
55 1.3 oster
56 1.10 oster #if (RF_INCLUDE_PARITY_DECLUSTERING > 0) || (RF_INCLUDE_PARITY_DECLUSTERING_PQ > 0)
57 1.10 oster
58 1.13 perry void
59 1.16 christos rf_free_2d_array(RF_RowCol_t **a, int b, int k)
60 1.3 oster {
61 1.3 oster RF_RowCol_t i;
62 1.3 oster
63 1.3 oster for (i = 0; i < b; i++)
64 1.3 oster RF_Free(a[i], k * sizeof(RF_RowCol_t));
65 1.3 oster RF_Free(a, b * sizeof(RF_RowCol_t));
66 1.1 oster }
67 1.1 oster
68 1.1 oster
69 1.1 oster /* creates & zeros a 1-d array with c columns */
70 1.3 oster RF_RowCol_t *
71 1.12 oster rf_make_1d_array(int c, RF_AllocListElem_t *allocList)
72 1.1 oster {
73 1.3 oster RF_RowCol_t *retval;
74 1.1 oster
75 1.17 christos retval = RF_MallocAndAdd(c * sizeof(*retval), allocList);
76 1.3 oster return (retval);
77 1.1 oster }
78 1.1 oster
79 1.13 perry void
80 1.16 christos rf_free_1d_array(RF_RowCol_t *a, int n)
81 1.1 oster {
82 1.3 oster RF_Free(a, n * sizeof(RF_RowCol_t));
83 1.1 oster }
84 1.11 junyoung
85 1.1 oster /* Euclid's algorithm: finds and returns the greatest common divisor
86 1.1 oster * between a and b. (MCH)
87 1.1 oster */
88 1.13 perry int
89 1.12 oster rf_gcd(int m, int n)
90 1.3 oster {
91 1.3 oster int t;
92 1.3 oster
93 1.3 oster while (m > 0) {
94 1.3 oster t = n % m;
95 1.3 oster n = m;
96 1.3 oster m = t;
97 1.3 oster }
98 1.3 oster return (n);
99 1.1 oster }
100 1.10 oster #endif
101 1.1 oster /* these convert between text and integer. Apparently the regular C macros
102 1.1 oster * for doing this are not available in the kernel
103 1.1 oster */
104 1.1 oster
105 1.1 oster #define ISDIGIT(x) ( (x) >= '0' && (x) <= '9' )
106 1.1 oster #define ISHEXCHAR(x) ( ((x) >= 'a' && (x) <= 'f') || ((x) >= 'A' && (x) <= 'F') )
107 1.1 oster #define ISHEX(x) ( ISDIGIT(x) || ISHEXCHAR(x) )
108 1.1 oster #define HC2INT(x) ( ((x) >= 'a' && (x) <= 'f') ? (x) - 'a' + 10 : \
109 1.1 oster ( ((x) >= 'A' && (x) <= 'F') ? (x) - 'A' + 10 : (x - '0') ) )
110 1.1 oster
111 1.13 perry int
112 1.12 oster rf_atoi(char *p)
113 1.3 oster {
114 1.3 oster int val = 0, negate = 0;
115 1.3 oster
116 1.3 oster if (*p == '-') {
117 1.3 oster negate = 1;
118 1.3 oster p++;
119 1.3 oster }
120 1.3 oster for (; ISDIGIT(*p); p++)
121 1.3 oster val = 10 * val + (*p - '0');
122 1.3 oster return ((negate) ? -val : val);
123 1.3 oster }
124 1.3 oster
125 1.13 perry int
126 1.12 oster rf_htoi(char *p)
127 1.3 oster {
128 1.3 oster int val = 0;
129 1.3 oster for (; ISHEXCHAR(*p); p++)
130 1.3 oster val = 16 * val + HC2INT(*p);
131 1.3 oster return (val);
132 1.1 oster }
133