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