Home | History | Annotate | Line # | Download | only in raidframe
rf_utils.c revision 1.10
      1  1.10    oster /*	$NetBSD: rf_utils.c,v 1.10 2002/09/21 00:47:59 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.8    lukem #include <sys/cdefs.h>
     36  1.10    oster __KERNEL_RCSID(0, "$NetBSD: rf_utils.c,v 1.10 2002/09/21 00:47:59 oster Exp $");
     37   1.1    oster 
     38   1.7    oster #include "rf_archs.h"
     39   1.1    oster #include "rf_utils.h"
     40   1.1    oster #include "rf_debugMem.h"
     41   1.1    oster #include "rf_alloclist.h"
     42   1.1    oster 
     43   1.1    oster /* creates & zeros 2-d array with b rows and k columns (MCH) */
     44   1.3    oster RF_RowCol_t **
     45   1.3    oster rf_make_2d_array(b, k, allocList)
     46   1.3    oster 	int     b;
     47   1.3    oster 	int     k;
     48   1.3    oster 	RF_AllocListElem_t *allocList;
     49   1.3    oster {
     50   1.3    oster 	RF_RowCol_t **retval, i;
     51   1.3    oster 
     52   1.3    oster 	RF_MallocAndAdd(retval, b * sizeof(RF_RowCol_t *), (RF_RowCol_t **), allocList);
     53   1.3    oster 	for (i = 0; i < b; i++) {
     54   1.3    oster 		RF_MallocAndAdd(retval[i], k * sizeof(RF_RowCol_t), (RF_RowCol_t *), allocList);
     55   1.6  thorpej 		(void) memset((char *) retval[i], 0, k * sizeof(RF_RowCol_t));
     56   1.3    oster 	}
     57   1.3    oster 	return (retval);
     58   1.3    oster }
     59   1.3    oster 
     60  1.10    oster #if (RF_INCLUDE_PARITY_DECLUSTERING > 0) || (RF_INCLUDE_PARITY_DECLUSTERING_PQ > 0)
     61  1.10    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.6  thorpej 	(void) memset((char *) retval, 0, 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.10    oster  0)
     97   1.1    oster /* Euclid's algorithm:  finds and returns the greatest common divisor
     98   1.1    oster  * between a and b.     (MCH)
     99   1.1    oster  */
    100   1.3    oster int
    101   1.3    oster rf_gcd(m, n)
    102   1.3    oster 	int     m;
    103   1.3    oster 	int     n;
    104   1.3    oster {
    105   1.3    oster 	int     t;
    106   1.3    oster 
    107   1.3    oster 	while (m > 0) {
    108   1.3    oster 		t = n % m;
    109   1.3    oster 		n = m;
    110   1.3    oster 		m = t;
    111   1.3    oster 	}
    112   1.3    oster 	return (n);
    113   1.1    oster }
    114  1.10    oster #endif
    115   1.1    oster /* these convert between text and integer.  Apparently the regular C macros
    116   1.1    oster  * for doing this are not available in the kernel
    117   1.1    oster  */
    118   1.1    oster 
    119   1.1    oster #define ISDIGIT(x)   ( (x) >= '0' && (x) <= '9' )
    120   1.1    oster #define ISHEXCHAR(x) ( ((x) >= 'a' && (x) <= 'f') || ((x) >= 'A' && (x) <= 'F') )
    121   1.1    oster #define ISHEX(x)     ( ISDIGIT(x) || ISHEXCHAR(x) )
    122   1.1    oster #define HC2INT(x)    ( ((x) >= 'a' && (x) <= 'f') ? (x) - 'a' + 10 :                    \
    123   1.1    oster 		       ( ((x) >= 'A' && (x) <= 'F') ? (x) - 'A' + 10 : (x - '0') ) )
    124   1.1    oster 
    125   1.3    oster int
    126   1.3    oster rf_atoi(p)
    127   1.3    oster 	char   *p;
    128   1.3    oster {
    129   1.3    oster 	int     val = 0, negate = 0;
    130   1.3    oster 
    131   1.3    oster 	if (*p == '-') {
    132   1.3    oster 		negate = 1;
    133   1.3    oster 		p++;
    134   1.3    oster 	}
    135   1.3    oster 	for (; ISDIGIT(*p); p++)
    136   1.3    oster 		val = 10 * val + (*p - '0');
    137   1.3    oster 	return ((negate) ? -val : val);
    138   1.3    oster }
    139   1.3    oster 
    140   1.3    oster int
    141   1.3    oster rf_htoi(p)
    142   1.3    oster 	char   *p;
    143   1.3    oster {
    144   1.3    oster 	int     val = 0;
    145   1.3    oster 	for (; ISHEXCHAR(*p); p++)
    146   1.3    oster 		val = 16 * val + HC2INT(*p);
    147   1.3    oster 	return (val);
    148   1.1    oster }
    149