1 /* $NetBSD: rf_utils.c,v 1.8 2001/11/13 07:11:17 lukem Exp $ */ 2 /* 3 * Copyright (c) 1995 Carnegie-Mellon University. 4 * All rights reserved. 5 * 6 * Author: Mark Holland 7 * 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. 13 * 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. 17 * 18 * Carnegie Mellon requests users of this software to return to 19 * 20 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 21 * School of Computer Science 22 * Carnegie Mellon University 23 * Pittsburgh PA 15213-3890 24 * 25 * any improvements or extensions that they make and grant Carnegie the 26 * rights to redistribute these changes. 27 */ 28 29 /**************************************** 30 * 31 * rf_utils.c -- various support routines 32 * 33 ****************************************/ 34 35 #include <sys/cdefs.h> 36 __KERNEL_RCSID(0, "$NetBSD: rf_utils.c,v 1.8 2001/11/13 07:11:17 lukem Exp $"); 37 38 #include "rf_threadstuff.h" 39 40 #include <sys/time.h> 41 42 #include "rf_archs.h" 43 #include "rf_utils.h" 44 #include "rf_debugMem.h" 45 #include "rf_alloclist.h" 46 #include "rf_general.h" 47 48 /* creates & zeros 2-d array with b rows and k columns (MCH) */ 49 RF_RowCol_t ** 50 rf_make_2d_array(b, k, allocList) 51 int b; 52 int k; 53 RF_AllocListElem_t *allocList; 54 { 55 RF_RowCol_t **retval, i; 56 57 RF_MallocAndAdd(retval, b * sizeof(RF_RowCol_t *), (RF_RowCol_t **), allocList); 58 for (i = 0; i < b; i++) { 59 RF_MallocAndAdd(retval[i], k * sizeof(RF_RowCol_t), (RF_RowCol_t *), allocList); 60 (void) memset((char *) retval[i], 0, k * sizeof(RF_RowCol_t)); 61 } 62 return (retval); 63 } 64 65 void 66 rf_free_2d_array(a, b, k) 67 RF_RowCol_t **a; 68 int b; 69 int k; 70 { 71 RF_RowCol_t i; 72 73 for (i = 0; i < b; i++) 74 RF_Free(a[i], k * sizeof(RF_RowCol_t)); 75 RF_Free(a, b * sizeof(RF_RowCol_t)); 76 } 77 78 79 /* creates & zeros a 1-d array with c columns */ 80 RF_RowCol_t * 81 rf_make_1d_array(c, allocList) 82 int c; 83 RF_AllocListElem_t *allocList; 84 { 85 RF_RowCol_t *retval; 86 87 RF_MallocAndAdd(retval, c * sizeof(RF_RowCol_t), (RF_RowCol_t *), allocList); 88 (void) memset((char *) retval, 0, c * sizeof(RF_RowCol_t)); 89 return (retval); 90 } 91 92 void 93 rf_free_1d_array(a, n) 94 RF_RowCol_t *a; 95 int n; 96 { 97 RF_Free(a, n * sizeof(RF_RowCol_t)); 98 } 99 /* Euclid's algorithm: finds and returns the greatest common divisor 100 * between a and b. (MCH) 101 */ 102 int 103 rf_gcd(m, n) 104 int m; 105 int n; 106 { 107 int t; 108 109 while (m > 0) { 110 t = n % m; 111 n = m; 112 m = t; 113 } 114 return (n); 115 } 116 /* these convert between text and integer. Apparently the regular C macros 117 * for doing this are not available in the kernel 118 */ 119 120 #define ISDIGIT(x) ( (x) >= '0' && (x) <= '9' ) 121 #define ISHEXCHAR(x) ( ((x) >= 'a' && (x) <= 'f') || ((x) >= 'A' && (x) <= 'F') ) 122 #define ISHEX(x) ( ISDIGIT(x) || ISHEXCHAR(x) ) 123 #define HC2INT(x) ( ((x) >= 'a' && (x) <= 'f') ? (x) - 'a' + 10 : \ 124 ( ((x) >= 'A' && (x) <= 'F') ? (x) - 'A' + 10 : (x - '0') ) ) 125 126 int 127 rf_atoi(p) 128 char *p; 129 { 130 int val = 0, negate = 0; 131 132 if (*p == '-') { 133 negate = 1; 134 p++; 135 } 136 for (; ISDIGIT(*p); p++) 137 val = 10 * val + (*p - '0'); 138 return ((negate) ? -val : val); 139 } 140 141 int 142 rf_htoi(p) 143 char *p; 144 { 145 int val = 0; 146 for (; ISHEXCHAR(*p); p++) 147 val = 16 * val + HC2INT(*p); 148 return (val); 149 } 150