1/* $NetBSD: rf_utils.c,v 1.17 2019/02/09 03:34:00 christos 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.17 2019/02/09 03:34:00 christos 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) */ 44RF_RowCol_t ** 45rf_make_2d_array(int b, int k, RF_AllocListElem_t *allocList) 46{ 47 RF_RowCol_t **retval, i; 48 49 retval = RF_MallocAndAdd(b * sizeof(*retval), allocList); 50 for (i = 0; i < b; i++) { 51 retval[i] = RF_MallocAndAdd(k * sizeof(*retval[i]), allocList); 52 } 53 return (retval); 54} 55 56#if (RF_INCLUDE_PARITY_DECLUSTERING > 0) || (RF_INCLUDE_PARITY_DECLUSTERING_PQ > 0) 57 58void 59rf_free_2d_array(RF_RowCol_t **a, int b, int k) 60{ 61 RF_RowCol_t i; 62 63 for (i = 0; i < b; i++) 64 RF_Free(a[i], k * sizeof(RF_RowCol_t)); 65 RF_Free(a, b * sizeof(RF_RowCol_t)); 66} 67 68 69/* creates & zeros a 1-d array with c columns */ 70RF_RowCol_t * 71rf_make_1d_array(int c, RF_AllocListElem_t *allocList) 72{ 73 RF_RowCol_t *retval; 74 75 retval = RF_MallocAndAdd(c * sizeof(*retval), allocList); 76 return (retval); 77} 78 79void 80rf_free_1d_array(RF_RowCol_t *a, int n) 81{ 82 RF_Free(a, n * sizeof(RF_RowCol_t)); 83} 84 85/* Euclid's algorithm: finds and returns the greatest common divisor 86 * between a and b. (MCH) 87 */ 88int 89rf_gcd(int m, int n) 90{ 91 int t; 92 93 while (m > 0) { 94 t = n % m; 95 n = m; 96 m = t; 97 } 98 return (n); 99} 100#endif 101/* these convert between text and integer. Apparently the regular C macros 102 * for doing this are not available in the kernel 103 */ 104 105#define ISDIGIT(x) ( (x) >= '0' && (x) <= '9' ) 106#define ISHEXCHAR(x) ( ((x) >= 'a' && (x) <= 'f') || ((x) >= 'A' && (x) <= 'F') ) 107#define ISHEX(x) ( ISDIGIT(x) || ISHEXCHAR(x) ) 108#define HC2INT(x) ( ((x) >= 'a' && (x) <= 'f') ? (x) - 'a' + 10 : \ 109 ( ((x) >= 'A' && (x) <= 'F') ? (x) - 'A' + 10 : (x - '0') ) ) 110 111int 112rf_atoi(char *p) 113{ 114 int val = 0, negate = 0; 115 116 if (*p == '-') { 117 negate = 1; 118 p++; 119 } 120 for (; ISDIGIT(*p); p++) 121 val = 10 * val + (*p - '0'); 122 return ((negate) ? -val : val); 123} 124 125int 126rf_htoi(char *p) 127{ 128 int val = 0; 129 for (; ISHEXCHAR(*p); p++) 130 val = 16 * val + HC2INT(*p); 131 return (val); 132} 133