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