rf_utils.c revision 1.8 1 1.8 lukem /* $NetBSD: rf_utils.c,v 1.8 2001/11/13 07:11:17 lukem 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.8 lukem __KERNEL_RCSID(0, "$NetBSD: rf_utils.c,v 1.8 2001/11/13 07:11:17 lukem Exp $");
37 1.1 oster
38 1.1 oster #include "rf_threadstuff.h"
39 1.1 oster
40 1.1 oster #include <sys/time.h>
41 1.1 oster
42 1.7 oster #include "rf_archs.h"
43 1.1 oster #include "rf_utils.h"
44 1.1 oster #include "rf_debugMem.h"
45 1.1 oster #include "rf_alloclist.h"
46 1.1 oster #include "rf_general.h"
47 1.1 oster
48 1.1 oster /* creates & zeros 2-d array with b rows and k columns (MCH) */
49 1.3 oster RF_RowCol_t **
50 1.3 oster rf_make_2d_array(b, k, allocList)
51 1.3 oster int b;
52 1.3 oster int k;
53 1.3 oster RF_AllocListElem_t *allocList;
54 1.3 oster {
55 1.3 oster RF_RowCol_t **retval, i;
56 1.3 oster
57 1.3 oster RF_MallocAndAdd(retval, b * sizeof(RF_RowCol_t *), (RF_RowCol_t **), allocList);
58 1.3 oster for (i = 0; i < b; i++) {
59 1.3 oster RF_MallocAndAdd(retval[i], k * sizeof(RF_RowCol_t), (RF_RowCol_t *), allocList);
60 1.6 thorpej (void) memset((char *) retval[i], 0, k * sizeof(RF_RowCol_t));
61 1.3 oster }
62 1.3 oster return (retval);
63 1.3 oster }
64 1.3 oster
65 1.3 oster void
66 1.3 oster rf_free_2d_array(a, b, k)
67 1.3 oster RF_RowCol_t **a;
68 1.3 oster int b;
69 1.3 oster int k;
70 1.3 oster {
71 1.3 oster RF_RowCol_t i;
72 1.3 oster
73 1.3 oster for (i = 0; i < b; i++)
74 1.3 oster RF_Free(a[i], k * sizeof(RF_RowCol_t));
75 1.3 oster RF_Free(a, b * sizeof(RF_RowCol_t));
76 1.1 oster }
77 1.1 oster
78 1.1 oster
79 1.1 oster /* creates & zeros a 1-d array with c columns */
80 1.3 oster RF_RowCol_t *
81 1.3 oster rf_make_1d_array(c, allocList)
82 1.3 oster int c;
83 1.3 oster RF_AllocListElem_t *allocList;
84 1.1 oster {
85 1.3 oster RF_RowCol_t *retval;
86 1.1 oster
87 1.3 oster RF_MallocAndAdd(retval, c * sizeof(RF_RowCol_t), (RF_RowCol_t *), allocList);
88 1.6 thorpej (void) memset((char *) retval, 0, c * sizeof(RF_RowCol_t));
89 1.3 oster return (retval);
90 1.1 oster }
91 1.1 oster
92 1.3 oster void
93 1.3 oster rf_free_1d_array(a, n)
94 1.3 oster RF_RowCol_t *a;
95 1.3 oster int n;
96 1.1 oster {
97 1.3 oster RF_Free(a, n * sizeof(RF_RowCol_t));
98 1.1 oster }
99 1.1 oster /* Euclid's algorithm: finds and returns the greatest common divisor
100 1.1 oster * between a and b. (MCH)
101 1.1 oster */
102 1.3 oster int
103 1.3 oster rf_gcd(m, n)
104 1.3 oster int m;
105 1.3 oster int n;
106 1.3 oster {
107 1.3 oster int t;
108 1.3 oster
109 1.3 oster while (m > 0) {
110 1.3 oster t = n % m;
111 1.3 oster n = m;
112 1.3 oster m = t;
113 1.3 oster }
114 1.3 oster return (n);
115 1.1 oster }
116 1.1 oster /* these convert between text and integer. Apparently the regular C macros
117 1.1 oster * for doing this are not available in the kernel
118 1.1 oster */
119 1.1 oster
120 1.1 oster #define ISDIGIT(x) ( (x) >= '0' && (x) <= '9' )
121 1.1 oster #define ISHEXCHAR(x) ( ((x) >= 'a' && (x) <= 'f') || ((x) >= 'A' && (x) <= 'F') )
122 1.1 oster #define ISHEX(x) ( ISDIGIT(x) || ISHEXCHAR(x) )
123 1.1 oster #define HC2INT(x) ( ((x) >= 'a' && (x) <= 'f') ? (x) - 'a' + 10 : \
124 1.1 oster ( ((x) >= 'A' && (x) <= 'F') ? (x) - 'A' + 10 : (x - '0') ) )
125 1.1 oster
126 1.3 oster int
127 1.3 oster rf_atoi(p)
128 1.3 oster char *p;
129 1.3 oster {
130 1.3 oster int val = 0, negate = 0;
131 1.3 oster
132 1.3 oster if (*p == '-') {
133 1.3 oster negate = 1;
134 1.3 oster p++;
135 1.3 oster }
136 1.3 oster for (; ISDIGIT(*p); p++)
137 1.3 oster val = 10 * val + (*p - '0');
138 1.3 oster return ((negate) ? -val : val);
139 1.3 oster }
140 1.3 oster
141 1.3 oster int
142 1.3 oster rf_htoi(p)
143 1.3 oster char *p;
144 1.3 oster {
145 1.3 oster int val = 0;
146 1.3 oster for (; ISHEXCHAR(*p); p++)
147 1.3 oster val = 16 * val + HC2INT(*p);
148 1.3 oster return (val);
149 1.1 oster }
150