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