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