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