Home | History | Annotate | Line # | Download | only in config
pack.c revision 1.2
      1  1.2     cube /*	$NetBSD: pack.c,v 1.2 2005/09/30 22:36:20 cube Exp $	*/
      2  1.1  thorpej 
      3  1.1  thorpej /*
      4  1.1  thorpej  * Copyright (c) 1992, 1993
      5  1.1  thorpej  *	The Regents of the University of California.  All rights reserved.
      6  1.1  thorpej  *
      7  1.1  thorpej  * This software was developed by the Computer Systems Engineering group
      8  1.1  thorpej  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9  1.1  thorpej  * contributed to Berkeley.
     10  1.1  thorpej  *
     11  1.1  thorpej  * All advertising materials mentioning features or use of this software
     12  1.1  thorpej  * must display the following acknowledgement:
     13  1.1  thorpej  *	This product includes software developed by the University of
     14  1.1  thorpej  *	California, Lawrence Berkeley Laboratories.
     15  1.1  thorpej  *
     16  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     17  1.1  thorpej  * modification, are permitted provided that the following conditions
     18  1.1  thorpej  * are met:
     19  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     20  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     21  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     22  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     23  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     24  1.1  thorpej  * 3. Neither the name of the University nor the names of its contributors
     25  1.1  thorpej  *    may be used to endorse or promote products derived from this software
     26  1.1  thorpej  *    without specific prior written permission.
     27  1.1  thorpej  *
     28  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  1.1  thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  1.1  thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  1.1  thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  1.1  thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  1.1  thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  1.1  thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  1.1  thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  1.1  thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  1.1  thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  1.1  thorpej  * SUCH DAMAGE.
     39  1.1  thorpej  *
     40  1.1  thorpej  *	from: @(#)pack.c	8.1 (Berkeley) 6/6/93
     41  1.1  thorpej  */
     42  1.1  thorpej 
     43  1.1  thorpej #if HAVE_NBTOOL_CONFIG_H
     44  1.1  thorpej #include "nbtool_config.h"
     45  1.1  thorpej #endif
     46  1.1  thorpej 
     47  1.1  thorpej #include <sys/param.h>
     48  1.1  thorpej #include <stdlib.h>
     49  1.1  thorpej #include <string.h>
     50  1.1  thorpej #include "defs.h"
     51  1.1  thorpej 
     52  1.1  thorpej /*
     53  1.1  thorpej  * Packing.  We have three separate kinds of packing here.
     54  1.1  thorpej  *
     55  1.1  thorpej  * First, we pack device instances which have identical parent specs.
     56  1.1  thorpej  *
     57  1.1  thorpej  * Second, we pack locators.  Given something like
     58  1.1  thorpej  *
     59  1.1  thorpej  *	hp0 at mba0 drive 0
     60  1.1  thorpej  *	hp* at mba* drive ?
     61  1.1  thorpej  *	ht0 at mba0 drive 0
     62  1.1  thorpej  *	tu0 at ht0 slave 0
     63  1.1  thorpej  *	ht* at mba* drive ?
     64  1.1  thorpej  *	tu* at ht* slave ?
     65  1.1  thorpej  *
     66  1.1  thorpej  * (where the default drive and slave numbers are -1), we have three
     67  1.1  thorpej  * locators whose value is 0 and three whose value is -1.  Rather than
     68  1.1  thorpej  * emitting six integers, we emit just two.
     69  1.1  thorpej  *
     70  1.1  thorpej  * When packing locators, we would like to find sequences such as
     71  1.1  thorpej  *	{1 2 3} {2 3 4} {3} {4 5}
     72  1.1  thorpej  * and turn this into the flat sequence {1 2 3 4 5}, with each subsequence
     73  1.1  thorpej  * given by the appropriate offset (here 0, 1, 2, and 3 respectively).
     74  1.1  thorpej  * Non-overlapping packing is much easier, and so we use that here
     75  1.1  thorpej  * and miss out on the chance to squeeze the locator sequence optimally.
     76  1.1  thorpej  * (So it goes.)
     77  1.1  thorpej  */
     78  1.1  thorpej 
     79  1.1  thorpej typedef int (*vec_cmp_func)(const void *, int, int);
     80  1.1  thorpej 
     81  1.1  thorpej #define	TAILHSIZE	128
     82  1.1  thorpej #define	PVHASH(i)	((i) & (TAILHSIZE - 1))
     83  1.1  thorpej #define	LOCHASH(l)	(((long)(l) >> 2) & (TAILHSIZE - 1))
     84  1.1  thorpej struct tails {
     85  1.1  thorpej 	struct	tails *t_next;
     86  1.1  thorpej 	int	t_ends_at;
     87  1.1  thorpej };
     88  1.1  thorpej 
     89  1.1  thorpej static struct tails *tails[TAILHSIZE];
     90  1.1  thorpej static int locspace;
     91  1.1  thorpej 
     92  1.1  thorpej static void packdevi(void);
     93  1.1  thorpej static void packlocs(void);
     94  1.1  thorpej 
     95  1.1  thorpej static int sameas(struct devi *, struct devi *);
     96  1.1  thorpej static int findvec(const void *, int, int, vec_cmp_func, int);
     97  1.1  thorpej static int samelocs(const void *, int, int);
     98  1.1  thorpej static int addlocs(const char **, int);
     99  1.1  thorpej static int loclencmp(const void *, const void *);
    100  1.1  thorpej static void resettails(void);
    101  1.1  thorpej 
    102  1.1  thorpej void
    103  1.1  thorpej pack(void)
    104  1.1  thorpej {
    105  1.1  thorpej 	struct pspec *p;
    106  1.1  thorpej 	struct devi *i;
    107  1.1  thorpej 
    108  1.1  thorpej 	/* Pack instances and make parent vectors. */
    109  1.1  thorpej 	packdevi();
    110  1.1  thorpej 
    111  1.1  thorpej 	/*
    112  1.1  thorpej 	 * Now that we know what we have, find upper limits on space
    113  1.1  thorpej 	 * needed for the loc[] table.  The loc table size is bounded by
    114  1.1  thorpej 	 * what we would get if no packing occurred.
    115  1.1  thorpej 	 */
    116  1.1  thorpej 	locspace = 0;
    117  1.1  thorpej 	TAILQ_FOREACH(i, &alldevi, i_next) {
    118  1.2     cube 		if (!i->i_active || i->i_collapsed)
    119  1.1  thorpej 			continue;
    120  1.1  thorpej 		if ((p = i->i_pspec) == NULL)
    121  1.1  thorpej 			continue;
    122  1.1  thorpej 		locspace += p->p_iattr->a_loclen;
    123  1.1  thorpej 	}
    124  1.1  thorpej 
    125  1.1  thorpej 	/* Allocate and pack loc[]. */
    126  1.1  thorpej 	locators.vec = ecalloc(locspace, sizeof(*locators.vec));
    127  1.1  thorpej 	locators.used = 0;
    128  1.1  thorpej 	packlocs();
    129  1.1  thorpej }
    130  1.1  thorpej 
    131  1.1  thorpej /*
    132  1.1  thorpej  * Pack device instances together wherever possible.
    133  1.1  thorpej  */
    134  1.1  thorpej void
    135  1.1  thorpej packdevi(void)
    136  1.1  thorpej {
    137  1.1  thorpej 	struct devi *firststar, *i, **ip, *l, *p;
    138  1.1  thorpej 	struct devbase *d;
    139  1.1  thorpej 	int j, m, n;
    140  1.1  thorpej 
    141  1.1  thorpej 	/*
    142  1.1  thorpej 	 * Sort all the cloning units to after the non-cloning units,
    143  1.1  thorpej 	 * preserving order of cloning and non-cloning units with
    144  1.1  thorpej 	 * respect to other units of the same type.
    145  1.1  thorpej 	 *
    146  1.1  thorpej 	 * Algorithm: Walk down the list until the first cloning unit is
    147  1.1  thorpej 	 * seen for the second time (or until the end of the list, if there
    148  1.1  thorpej 	 * are no cloning units on the list), moving starred units to the
    149  1.1  thorpej 	 * end of the list.
    150  1.1  thorpej 	 */
    151  1.1  thorpej 	TAILQ_FOREACH(d, &allbases, d_next) {
    152  1.1  thorpej 		ip = &d->d_ihead;
    153  1.1  thorpej 		firststar = NULL;
    154  1.1  thorpej 
    155  1.1  thorpej 		for (i = *ip; i != firststar; i = *ip) {
    156  1.1  thorpej 			if (i->i_unit != STAR) {
    157  1.1  thorpej 				/* try i->i_bsame next */
    158  1.1  thorpej 				ip = &i->i_bsame;
    159  1.1  thorpej 			} else {
    160  1.1  thorpej 				if (firststar == NULL)
    161  1.1  thorpej 					firststar = i;
    162  1.1  thorpej 
    163  1.1  thorpej 				*d->d_ipp = i;
    164  1.1  thorpej 				d->d_ipp = &i->i_bsame;
    165  1.1  thorpej 
    166  1.1  thorpej 				*ip = i->i_bsame;
    167  1.1  thorpej 				i->i_bsame = NULL;
    168  1.1  thorpej 
    169  1.1  thorpej 				/* leave ip alone; try (old) i->i_bsame next */
    170  1.1  thorpej 			}
    171  1.1  thorpej 		}
    172  1.1  thorpej 	}
    173  1.1  thorpej 
    174  1.1  thorpej 	packed = ecalloc(ndevi + 1, sizeof *packed);
    175  1.1  thorpej 	n = 0;
    176  1.1  thorpej 	TAILQ_FOREACH(d, &allbases, d_next) {
    177  1.1  thorpej 		/*
    178  1.1  thorpej 		 * For each instance of each device, add or collapse
    179  1.1  thorpej 		 * all its aliases.
    180  1.2     cube 		 *
    181  1.2     cube 		 * Pseudo-devices have a non-empty d_ihead for convenience.
    182  1.2     cube 		 * Ignore them.
    183  1.1  thorpej 		 */
    184  1.2     cube 		if (d->d_ispseudo)
    185  1.2     cube 			continue;
    186  1.1  thorpej 		for (i = d->d_ihead; i != NULL; i = i->i_bsame) {
    187  1.1  thorpej 			m = n;
    188  1.1  thorpej 			for (l = i; l != NULL; l = l->i_alias) {
    189  1.2     cube 				if (!l->i_active)
    190  1.2     cube 					continue;
    191  1.1  thorpej 				l->i_locoff = -1;
    192  1.1  thorpej 				/* try to find an equivalent for l */
    193  1.1  thorpej 				for (j = m; j < n; j++) {
    194  1.1  thorpej 					p = packed[j];
    195  1.1  thorpej 					if (sameas(l, p)) {
    196  1.1  thorpej 						l->i_collapsed = 1;
    197  1.1  thorpej 						l->i_cfindex = p->i_cfindex;
    198  1.1  thorpej 						goto nextalias;
    199  1.1  thorpej 					}
    200  1.1  thorpej 				}
    201  1.1  thorpej 				/* could not find a suitable alias */
    202  1.1  thorpej 				l->i_collapsed = 0;
    203  1.1  thorpej 				l->i_cfindex = n;
    204  1.1  thorpej 				packed[n++] = l;
    205  1.1  thorpej  nextalias:;
    206  1.1  thorpej 			}
    207  1.1  thorpej 		}
    208  1.1  thorpej 	}
    209  1.1  thorpej 	npacked = n;
    210  1.1  thorpej 	packed[n] = NULL;
    211  1.1  thorpej }
    212  1.1  thorpej 
    213  1.1  thorpej /*
    214  1.1  thorpej  * Return true if two aliases are "the same".  In this case, they need
    215  1.1  thorpej  * to have the same parent spec, have the same config flags, and have
    216  1.1  thorpej  * the same locators.
    217  1.1  thorpej  */
    218  1.1  thorpej static int
    219  1.1  thorpej sameas(struct devi *i1, struct devi *i2)
    220  1.1  thorpej {
    221  1.1  thorpej 	const char **p1, **p2;
    222  1.1  thorpej 
    223  1.1  thorpej 	if (i1->i_pspec != i2->i_pspec)
    224  1.1  thorpej 		return (0);
    225  1.1  thorpej 	if (i1->i_cfflags != i2->i_cfflags)
    226  1.1  thorpej 		return (0);
    227  1.1  thorpej 	for (p1 = i1->i_locs, p2 = i2->i_locs; *p1 == *p2; p2++)
    228  1.1  thorpej 		if (*p1++ == 0)
    229  1.1  thorpej 			return (1);
    230  1.1  thorpej 	return (0);
    231  1.1  thorpej }
    232  1.1  thorpej 
    233  1.1  thorpej static void
    234  1.1  thorpej packlocs(void)
    235  1.1  thorpej {
    236  1.1  thorpej 	struct pspec *ps;
    237  1.1  thorpej 	struct devi **p, *i;
    238  1.1  thorpej 	int l,o;
    239  1.1  thorpej 	extern int Pflag;
    240  1.1  thorpej 
    241  1.1  thorpej 	qsort(packed, npacked, sizeof *packed, loclencmp);
    242  1.1  thorpej 	for (p = packed; (i = *p) != NULL; p++) {
    243  1.1  thorpej 		if ((ps = i->i_pspec) != NULL &&
    244  1.1  thorpej 		    (l = ps->p_iattr->a_loclen) > 0) {
    245  1.1  thorpej 			if (Pflag) {
    246  1.1  thorpej 				o = findvec(i->i_locs,
    247  1.1  thorpej 				    LOCHASH(i->i_locs[l - 1]), l,
    248  1.1  thorpej 				    samelocs, locators.used);
    249  1.1  thorpej 				i->i_locoff = o < 0 ?
    250  1.1  thorpej 				    addlocs(i->i_locs, l) : o;
    251  1.1  thorpej 			} else
    252  1.1  thorpej 				i->i_locoff = addlocs(i->i_locs, l);
    253  1.1  thorpej 		} else
    254  1.1  thorpej 			i->i_locoff = -1;
    255  1.1  thorpej 	}
    256  1.1  thorpej 	resettails();
    257  1.1  thorpej }
    258  1.1  thorpej 
    259  1.1  thorpej /*
    260  1.1  thorpej  * Return the index at which the given vector already exists, or -1
    261  1.1  thorpej  * if it is not anywhere in the current set.  If we return -1, we assume
    262  1.1  thorpej  * our caller will add it at the end of the current set, and we make
    263  1.1  thorpej  * sure that next time, we will find it there.
    264  1.1  thorpej  */
    265  1.1  thorpej static int
    266  1.1  thorpej findvec(const void *ptr, int hash, int len, vec_cmp_func cmp, int nextplace)
    267  1.1  thorpej {
    268  1.1  thorpej 	struct tails *t, **hp;
    269  1.1  thorpej 	int off;
    270  1.1  thorpej 
    271  1.1  thorpej 	hp = &tails[hash];
    272  1.1  thorpej 	for (t = *hp; t != NULL; t = t->t_next) {
    273  1.1  thorpej 		off = t->t_ends_at - len;
    274  1.1  thorpej 		if (off >= 0 && (*cmp)(ptr, off, len))
    275  1.1  thorpej 			return (off);
    276  1.1  thorpej 	}
    277  1.1  thorpej 	t = ecalloc(1, sizeof(*t));
    278  1.1  thorpej 	t->t_next = *hp;
    279  1.1  thorpej 	*hp = t;
    280  1.1  thorpej 	t->t_ends_at = nextplace + len;
    281  1.1  thorpej 	return (-1);
    282  1.1  thorpej }
    283  1.1  thorpej 
    284  1.1  thorpej /*
    285  1.1  thorpej  * Comparison function for locators.
    286  1.1  thorpej  */
    287  1.1  thorpej static int
    288  1.1  thorpej samelocs(const void *ptr, int off, int len)
    289  1.1  thorpej {
    290  1.1  thorpej 	const char **p, **q;
    291  1.1  thorpej 
    292  1.1  thorpej 	for (p = &locators.vec[off], q = (const char **)ptr; --len >= 0;)
    293  1.1  thorpej 		if (*p++ != *q++)
    294  1.1  thorpej 			return (0);	/* different */
    295  1.1  thorpej 	return (1);			/* same */
    296  1.1  thorpej }
    297  1.1  thorpej 
    298  1.1  thorpej /*
    299  1.1  thorpej  * Add the given locators at the end of the global loc[] table.
    300  1.1  thorpej  */
    301  1.1  thorpej static int
    302  1.1  thorpej addlocs(const char **locs, int len)
    303  1.1  thorpej {
    304  1.1  thorpej 	const char **p;
    305  1.1  thorpej 	int ret;
    306  1.1  thorpej 
    307  1.1  thorpej 	ret = locators.used;
    308  1.1  thorpej 	if ((locators.used = ret + len) > locspace)
    309  1.1  thorpej 		panic("addlocs: overrun");
    310  1.1  thorpej 	for (p = &locators.vec[ret]; --len >= 0;)
    311  1.1  thorpej 		*p++ = *locs++;
    312  1.1  thorpej 	return (ret);
    313  1.1  thorpej }
    314  1.1  thorpej 
    315  1.1  thorpej /*
    316  1.1  thorpej  * Comparison function for qsort-by-locator-length, longest first.
    317  1.1  thorpej  * We rashly assume that subtraction of these lengths does not overflow.
    318  1.1  thorpej  */
    319  1.1  thorpej static int
    320  1.1  thorpej loclencmp(const void *a, const void *b)
    321  1.1  thorpej {
    322  1.1  thorpej 	struct pspec *p1, *p2;
    323  1.1  thorpej 	int l1, l2;
    324  1.1  thorpej 
    325  1.1  thorpej 	p1 = (*(struct devi **)a)->i_pspec;
    326  1.1  thorpej 	l1 = p1 != NULL ? p1->p_iattr->a_loclen : 0;
    327  1.1  thorpej 
    328  1.1  thorpej 	p2 = (*(struct devi **)b)->i_pspec;
    329  1.1  thorpej 	l2 = p2 != NULL ? p2->p_iattr->a_loclen : 0;
    330  1.1  thorpej 
    331  1.1  thorpej 	return (l2 - l1);
    332  1.1  thorpej }
    333  1.1  thorpej 
    334  1.1  thorpej static void
    335  1.1  thorpej resettails(void)
    336  1.1  thorpej {
    337  1.1  thorpej 	struct tails **p, *t, *next;
    338  1.1  thorpej 	int i;
    339  1.1  thorpej 
    340  1.1  thorpej 	for (p = tails, i = TAILHSIZE; --i >= 0; p++) {
    341  1.1  thorpej 		for (t = *p; t != NULL; t = next) {
    342  1.1  thorpej 			next = t->t_next;
    343  1.1  thorpej 			free(t);
    344  1.1  thorpej 		}
    345  1.1  thorpej 		*p = NULL;
    346  1.1  thorpej 	}
    347  1.1  thorpej }
    348