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