Home | History | Annotate | Line # | Download | only in raidframe
rf_utils.c revision 1.5
      1  1.5  oster /*	$NetBSD: rf_utils.c,v 1.5 2000/01/07 03:41:03 oster Exp $	*/
      2  1.1  oster /*
      3  1.1  oster  * Copyright (c) 1995 Carnegie-Mellon University.
      4  1.1  oster  * All rights reserved.
      5  1.1  oster  *
      6  1.1  oster  * Author: Mark Holland
      7  1.1  oster  *
      8  1.1  oster  * Permission to use, copy, modify and distribute this software and
      9  1.1  oster  * its documentation is hereby granted, provided that both the copyright
     10  1.1  oster  * notice and this permission notice appear in all copies of the
     11  1.1  oster  * software, derivative works or modified versions, and any portions
     12  1.1  oster  * thereof, and that both notices appear in supporting documentation.
     13  1.1  oster  *
     14  1.1  oster  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     15  1.1  oster  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     16  1.1  oster  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     17  1.1  oster  *
     18  1.1  oster  * Carnegie Mellon requests users of this software to return to
     19  1.1  oster  *
     20  1.1  oster  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     21  1.1  oster  *  School of Computer Science
     22  1.1  oster  *  Carnegie Mellon University
     23  1.1  oster  *  Pittsburgh PA 15213-3890
     24  1.1  oster  *
     25  1.1  oster  * any improvements or extensions that they make and grant Carnegie the
     26  1.1  oster  * rights to redistribute these changes.
     27  1.1  oster  */
     28  1.1  oster 
     29  1.1  oster /****************************************
     30  1.1  oster  *
     31  1.1  oster  * rf_utils.c -- various support routines
     32  1.1  oster  *
     33  1.1  oster  ****************************************/
     34  1.1  oster 
     35  1.1  oster 
     36  1.1  oster #include "rf_threadstuff.h"
     37  1.1  oster 
     38  1.1  oster #include <sys/time.h>
     39  1.1  oster 
     40  1.1  oster #include "rf_utils.h"
     41  1.1  oster #include "rf_debugMem.h"
     42  1.1  oster #include "rf_alloclist.h"
     43  1.1  oster #include "rf_general.h"
     44  1.1  oster 
     45  1.1  oster /* creates & zeros 2-d array with b rows and k columns (MCH) */
     46  1.3  oster RF_RowCol_t **
     47  1.3  oster rf_make_2d_array(b, k, allocList)
     48  1.3  oster 	int     b;
     49  1.3  oster 	int     k;
     50  1.3  oster 	RF_AllocListElem_t *allocList;
     51  1.3  oster {
     52  1.3  oster 	RF_RowCol_t **retval, i;
     53  1.3  oster 
     54  1.3  oster 	RF_MallocAndAdd(retval, b * sizeof(RF_RowCol_t *), (RF_RowCol_t **), allocList);
     55  1.3  oster 	for (i = 0; i < b; i++) {
     56  1.3  oster 		RF_MallocAndAdd(retval[i], k * sizeof(RF_RowCol_t), (RF_RowCol_t *), allocList);
     57  1.3  oster 		(void) bzero((char *) retval[i], k * sizeof(RF_RowCol_t));
     58  1.3  oster 	}
     59  1.3  oster 	return (retval);
     60  1.3  oster }
     61  1.3  oster 
     62  1.3  oster void
     63  1.3  oster rf_free_2d_array(a, b, k)
     64  1.3  oster 	RF_RowCol_t **a;
     65  1.3  oster 	int     b;
     66  1.3  oster 	int     k;
     67  1.3  oster {
     68  1.3  oster 	RF_RowCol_t i;
     69  1.3  oster 
     70  1.3  oster 	for (i = 0; i < b; i++)
     71  1.3  oster 		RF_Free(a[i], k * sizeof(RF_RowCol_t));
     72  1.3  oster 	RF_Free(a, b * sizeof(RF_RowCol_t));
     73  1.1  oster }
     74  1.1  oster 
     75  1.1  oster 
     76  1.1  oster /* creates & zeros a 1-d array with c columns */
     77  1.3  oster RF_RowCol_t *
     78  1.3  oster rf_make_1d_array(c, allocList)
     79  1.3  oster 	int     c;
     80  1.3  oster 	RF_AllocListElem_t *allocList;
     81  1.1  oster {
     82  1.3  oster 	RF_RowCol_t *retval;
     83  1.1  oster 
     84  1.3  oster 	RF_MallocAndAdd(retval, c * sizeof(RF_RowCol_t), (RF_RowCol_t *), allocList);
     85  1.3  oster 	(void) bzero((char *) retval, c * sizeof(RF_RowCol_t));
     86  1.3  oster 	return (retval);
     87  1.1  oster }
     88  1.1  oster 
     89  1.3  oster void
     90  1.3  oster rf_free_1d_array(a, n)
     91  1.3  oster 	RF_RowCol_t *a;
     92  1.3  oster 	int     n;
     93  1.1  oster {
     94  1.3  oster 	RF_Free(a, n * sizeof(RF_RowCol_t));
     95  1.1  oster }
     96  1.1  oster /* Euclid's algorithm:  finds and returns the greatest common divisor
     97  1.1  oster  * between a and b.     (MCH)
     98  1.1  oster  */
     99  1.3  oster int
    100  1.3  oster rf_gcd(m, n)
    101  1.3  oster 	int     m;
    102  1.3  oster 	int     n;
    103  1.3  oster {
    104  1.3  oster 	int     t;
    105  1.3  oster 
    106  1.3  oster 	while (m > 0) {
    107  1.3  oster 		t = n % m;
    108  1.3  oster 		n = m;
    109  1.3  oster 		m = t;
    110  1.3  oster 	}
    111  1.3  oster 	return (n);
    112  1.1  oster }
    113  1.1  oster /* these convert between text and integer.  Apparently the regular C macros
    114  1.1  oster  * for doing this are not available in the kernel
    115  1.1  oster  */
    116  1.1  oster 
    117  1.1  oster #define ISDIGIT(x)   ( (x) >= '0' && (x) <= '9' )
    118  1.1  oster #define ISHEXCHAR(x) ( ((x) >= 'a' && (x) <= 'f') || ((x) >= 'A' && (x) <= 'F') )
    119  1.1  oster #define ISHEX(x)     ( ISDIGIT(x) || ISHEXCHAR(x) )
    120  1.1  oster #define HC2INT(x)    ( ((x) >= 'a' && (x) <= 'f') ? (x) - 'a' + 10 :                    \
    121  1.1  oster 		       ( ((x) >= 'A' && (x) <= 'F') ? (x) - 'A' + 10 : (x - '0') ) )
    122  1.1  oster 
    123  1.3  oster int
    124  1.3  oster rf_atoi(p)
    125  1.3  oster 	char   *p;
    126  1.3  oster {
    127  1.3  oster 	int     val = 0, negate = 0;
    128  1.3  oster 
    129  1.3  oster 	if (*p == '-') {
    130  1.3  oster 		negate = 1;
    131  1.3  oster 		p++;
    132  1.3  oster 	}
    133  1.3  oster 	for (; ISDIGIT(*p); p++)
    134  1.3  oster 		val = 10 * val + (*p - '0');
    135  1.3  oster 	return ((negate) ? -val : val);
    136  1.3  oster }
    137  1.3  oster 
    138  1.3  oster int
    139  1.3  oster rf_htoi(p)
    140  1.3  oster 	char   *p;
    141  1.3  oster {
    142  1.3  oster 	int     val = 0;
    143  1.3  oster 	for (; ISHEXCHAR(*p); p++)
    144  1.3  oster 		val = 16 * val + HC2INT(*p);
    145  1.3  oster 	return (val);
    146  1.1  oster }
    147