Home | History | Annotate | Line # | Download | only in config
pack.c revision 1.5.20.1
      1  1.5.20.1       jym /*	$NetBSD: pack.c,v 1.5.20.1 2009/05/13 19:19:47 jym 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.4  christos #include <util.h>
     51       1.1   thorpej #include "defs.h"
     52       1.1   thorpej 
     53       1.1   thorpej /*
     54       1.1   thorpej  * Packing.  We have three separate kinds of packing here.
     55       1.1   thorpej  *
     56       1.1   thorpej  * First, we pack device instances which have identical parent specs.
     57       1.1   thorpej  *
     58       1.1   thorpej  * Second, we pack locators.  Given something like
     59       1.1   thorpej  *
     60       1.1   thorpej  *	hp0 at mba0 drive 0
     61       1.1   thorpej  *	hp* at mba* drive ?
     62       1.1   thorpej  *	ht0 at mba0 drive 0
     63       1.1   thorpej  *	tu0 at ht0 slave 0
     64       1.1   thorpej  *	ht* at mba* drive ?
     65       1.1   thorpej  *	tu* at ht* slave ?
     66       1.1   thorpej  *
     67       1.1   thorpej  * (where the default drive and slave numbers are -1), we have three
     68       1.1   thorpej  * locators whose value is 0 and three whose value is -1.  Rather than
     69       1.1   thorpej  * emitting six integers, we emit just two.
     70       1.1   thorpej  *
     71       1.1   thorpej  * When packing locators, we would like to find sequences such as
     72       1.1   thorpej  *	{1 2 3} {2 3 4} {3} {4 5}
     73       1.1   thorpej  * and turn this into the flat sequence {1 2 3 4 5}, with each subsequence
     74       1.1   thorpej  * given by the appropriate offset (here 0, 1, 2, and 3 respectively).
     75       1.1   thorpej  * Non-overlapping packing is much easier, and so we use that here
     76       1.1   thorpej  * and miss out on the chance to squeeze the locator sequence optimally.
     77       1.1   thorpej  * (So it goes.)
     78       1.1   thorpej  */
     79       1.1   thorpej 
     80       1.1   thorpej typedef int (*vec_cmp_func)(const void *, int, int);
     81       1.1   thorpej 
     82       1.1   thorpej #define	TAILHSIZE	128
     83       1.1   thorpej #define	PVHASH(i)	((i) & (TAILHSIZE - 1))
     84       1.1   thorpej #define	LOCHASH(l)	(((long)(l) >> 2) & (TAILHSIZE - 1))
     85       1.1   thorpej struct tails {
     86       1.1   thorpej 	struct	tails *t_next;
     87       1.1   thorpej 	int	t_ends_at;
     88       1.1   thorpej };
     89       1.1   thorpej 
     90       1.1   thorpej static struct tails *tails[TAILHSIZE];
     91       1.1   thorpej static int locspace;
     92       1.1   thorpej 
     93       1.1   thorpej static void packdevi(void);
     94       1.1   thorpej static void packlocs(void);
     95       1.1   thorpej 
     96       1.1   thorpej static int sameas(struct devi *, struct devi *);
     97       1.1   thorpej static int findvec(const void *, int, int, vec_cmp_func, int);
     98       1.1   thorpej static int samelocs(const void *, int, int);
     99       1.1   thorpej static int addlocs(const char **, int);
    100       1.1   thorpej static int loclencmp(const void *, const void *);
    101       1.1   thorpej static void resettails(void);
    102       1.1   thorpej 
    103       1.1   thorpej void
    104       1.1   thorpej pack(void)
    105       1.1   thorpej {
    106       1.1   thorpej 	struct pspec *p;
    107       1.1   thorpej 	struct devi *i;
    108       1.1   thorpej 
    109       1.1   thorpej 	/* Pack instances and make parent vectors. */
    110       1.1   thorpej 	packdevi();
    111       1.1   thorpej 
    112       1.1   thorpej 	/*
    113       1.1   thorpej 	 * Now that we know what we have, find upper limits on space
    114       1.1   thorpej 	 * needed for the loc[] table.  The loc table size is bounded by
    115       1.1   thorpej 	 * what we would get if no packing occurred.
    116       1.1   thorpej 	 */
    117       1.1   thorpej 	locspace = 0;
    118       1.1   thorpej 	TAILQ_FOREACH(i, &alldevi, i_next) {
    119       1.3      cube 		if (!i->i_active == DEVI_ACTIVE || i->i_collapsed)
    120       1.1   thorpej 			continue;
    121       1.1   thorpej 		if ((p = i->i_pspec) == NULL)
    122       1.1   thorpej 			continue;
    123       1.1   thorpej 		locspace += p->p_iattr->a_loclen;
    124       1.1   thorpej 	}
    125       1.1   thorpej 
    126       1.1   thorpej 	/* Allocate and pack loc[]. */
    127       1.5  christos 	locators.vec = ecalloc((size_t)locspace, sizeof(*locators.vec));
    128       1.1   thorpej 	locators.used = 0;
    129       1.1   thorpej 	packlocs();
    130       1.1   thorpej }
    131       1.1   thorpej 
    132       1.1   thorpej /*
    133       1.1   thorpej  * Pack device instances together wherever possible.
    134       1.1   thorpej  */
    135       1.1   thorpej void
    136       1.1   thorpej packdevi(void)
    137       1.1   thorpej {
    138       1.1   thorpej 	struct devi *firststar, *i, **ip, *l, *p;
    139       1.1   thorpej 	struct devbase *d;
    140       1.1   thorpej 	int j, m, n;
    141       1.1   thorpej 
    142       1.1   thorpej 	/*
    143       1.1   thorpej 	 * Sort all the cloning units to after the non-cloning units,
    144       1.1   thorpej 	 * preserving order of cloning and non-cloning units with
    145       1.1   thorpej 	 * respect to other units of the same type.
    146       1.1   thorpej 	 *
    147       1.1   thorpej 	 * Algorithm: Walk down the list until the first cloning unit is
    148       1.1   thorpej 	 * seen for the second time (or until the end of the list, if there
    149       1.1   thorpej 	 * are no cloning units on the list), moving starred units to the
    150       1.1   thorpej 	 * end of the list.
    151       1.1   thorpej 	 */
    152       1.1   thorpej 	TAILQ_FOREACH(d, &allbases, d_next) {
    153       1.1   thorpej 		ip = &d->d_ihead;
    154       1.1   thorpej 		firststar = NULL;
    155       1.1   thorpej 
    156       1.1   thorpej 		for (i = *ip; i != firststar; i = *ip) {
    157       1.1   thorpej 			if (i->i_unit != STAR) {
    158       1.1   thorpej 				/* try i->i_bsame next */
    159       1.1   thorpej 				ip = &i->i_bsame;
    160       1.1   thorpej 			} else {
    161       1.1   thorpej 				if (firststar == NULL)
    162       1.1   thorpej 					firststar = i;
    163       1.1   thorpej 
    164       1.1   thorpej 				*d->d_ipp = i;
    165       1.1   thorpej 				d->d_ipp = &i->i_bsame;
    166       1.1   thorpej 
    167       1.1   thorpej 				*ip = i->i_bsame;
    168       1.1   thorpej 				i->i_bsame = NULL;
    169       1.1   thorpej 
    170       1.1   thorpej 				/* leave ip alone; try (old) i->i_bsame next */
    171       1.1   thorpej 			}
    172       1.1   thorpej 		}
    173       1.1   thorpej 	}
    174       1.1   thorpej 
    175       1.5  christos 	packed = ecalloc((size_t)ndevi + 1, sizeof *packed);
    176       1.1   thorpej 	n = 0;
    177       1.1   thorpej 	TAILQ_FOREACH(d, &allbases, d_next) {
    178       1.1   thorpej 		/*
    179       1.1   thorpej 		 * For each instance of each device, add or collapse
    180       1.1   thorpej 		 * all its aliases.
    181       1.2      cube 		 *
    182       1.2      cube 		 * Pseudo-devices have a non-empty d_ihead for convenience.
    183       1.2      cube 		 * Ignore them.
    184       1.1   thorpej 		 */
    185       1.2      cube 		if (d->d_ispseudo)
    186       1.2      cube 			continue;
    187       1.1   thorpej 		for (i = d->d_ihead; i != NULL; i = i->i_bsame) {
    188       1.1   thorpej 			m = n;
    189       1.1   thorpej 			for (l = i; l != NULL; l = l->i_alias) {
    190       1.3      cube 				if (l->i_active != DEVI_ACTIVE)
    191       1.2      cube 					continue;
    192       1.1   thorpej 				l->i_locoff = -1;
    193       1.1   thorpej 				/* try to find an equivalent for l */
    194       1.1   thorpej 				for (j = m; j < n; j++) {
    195       1.1   thorpej 					p = packed[j];
    196       1.1   thorpej 					if (sameas(l, p)) {
    197       1.1   thorpej 						l->i_collapsed = 1;
    198       1.1   thorpej 						l->i_cfindex = p->i_cfindex;
    199       1.1   thorpej 						goto nextalias;
    200       1.1   thorpej 					}
    201       1.1   thorpej 				}
    202       1.1   thorpej 				/* could not find a suitable alias */
    203       1.1   thorpej 				l->i_collapsed = 0;
    204       1.1   thorpej 				l->i_cfindex = n;
    205       1.1   thorpej 				packed[n++] = l;
    206       1.1   thorpej  nextalias:;
    207       1.1   thorpej 			}
    208       1.1   thorpej 		}
    209       1.1   thorpej 	}
    210       1.1   thorpej 	npacked = n;
    211       1.1   thorpej 	packed[n] = NULL;
    212       1.1   thorpej }
    213       1.1   thorpej 
    214       1.1   thorpej /*
    215       1.1   thorpej  * Return true if two aliases are "the same".  In this case, they need
    216       1.1   thorpej  * to have the same parent spec, have the same config flags, and have
    217       1.1   thorpej  * the same locators.
    218       1.1   thorpej  */
    219       1.1   thorpej static int
    220       1.1   thorpej sameas(struct devi *i1, struct devi *i2)
    221       1.1   thorpej {
    222       1.1   thorpej 	const char **p1, **p2;
    223       1.1   thorpej 
    224       1.1   thorpej 	if (i1->i_pspec != i2->i_pspec)
    225       1.1   thorpej 		return (0);
    226       1.1   thorpej 	if (i1->i_cfflags != i2->i_cfflags)
    227       1.1   thorpej 		return (0);
    228       1.1   thorpej 	for (p1 = i1->i_locs, p2 = i2->i_locs; *p1 == *p2; p2++)
    229       1.1   thorpej 		if (*p1++ == 0)
    230       1.1   thorpej 			return (1);
    231       1.1   thorpej 	return (0);
    232       1.1   thorpej }
    233       1.1   thorpej 
    234       1.1   thorpej static void
    235       1.1   thorpej packlocs(void)
    236       1.1   thorpej {
    237       1.1   thorpej 	struct pspec *ps;
    238       1.1   thorpej 	struct devi **p, *i;
    239       1.1   thorpej 	int l,o;
    240       1.1   thorpej 	extern int Pflag;
    241       1.1   thorpej 
    242       1.1   thorpej 	qsort(packed, npacked, sizeof *packed, loclencmp);
    243       1.1   thorpej 	for (p = packed; (i = *p) != NULL; p++) {
    244       1.1   thorpej 		if ((ps = i->i_pspec) != NULL &&
    245       1.1   thorpej 		    (l = ps->p_iattr->a_loclen) > 0) {
    246       1.1   thorpej 			if (Pflag) {
    247       1.1   thorpej 				o = findvec(i->i_locs,
    248       1.1   thorpej 				    LOCHASH(i->i_locs[l - 1]), l,
    249       1.1   thorpej 				    samelocs, locators.used);
    250       1.1   thorpej 				i->i_locoff = o < 0 ?
    251       1.1   thorpej 				    addlocs(i->i_locs, l) : o;
    252       1.1   thorpej 			} else
    253       1.1   thorpej 				i->i_locoff = addlocs(i->i_locs, l);
    254       1.1   thorpej 		} else
    255       1.1   thorpej 			i->i_locoff = -1;
    256       1.1   thorpej 	}
    257       1.1   thorpej 	resettails();
    258       1.1   thorpej }
    259       1.1   thorpej 
    260       1.1   thorpej /*
    261       1.1   thorpej  * Return the index at which the given vector already exists, or -1
    262       1.1   thorpej  * if it is not anywhere in the current set.  If we return -1, we assume
    263       1.1   thorpej  * our caller will add it at the end of the current set, and we make
    264       1.1   thorpej  * sure that next time, we will find it there.
    265       1.1   thorpej  */
    266       1.1   thorpej static int
    267       1.1   thorpej findvec(const void *ptr, int hash, int len, vec_cmp_func cmp, int nextplace)
    268       1.1   thorpej {
    269       1.1   thorpej 	struct tails *t, **hp;
    270       1.1   thorpej 	int off;
    271       1.1   thorpej 
    272       1.1   thorpej 	hp = &tails[hash];
    273       1.1   thorpej 	for (t = *hp; t != NULL; t = t->t_next) {
    274       1.1   thorpej 		off = t->t_ends_at - len;
    275       1.1   thorpej 		if (off >= 0 && (*cmp)(ptr, off, len))
    276       1.1   thorpej 			return (off);
    277       1.1   thorpej 	}
    278       1.1   thorpej 	t = ecalloc(1, sizeof(*t));
    279       1.1   thorpej 	t->t_next = *hp;
    280       1.1   thorpej 	*hp = t;
    281       1.1   thorpej 	t->t_ends_at = nextplace + len;
    282       1.1   thorpej 	return (-1);
    283       1.1   thorpej }
    284       1.1   thorpej 
    285       1.1   thorpej /*
    286       1.1   thorpej  * Comparison function for locators.
    287       1.1   thorpej  */
    288       1.1   thorpej static int
    289       1.1   thorpej samelocs(const void *ptr, int off, int len)
    290       1.1   thorpej {
    291  1.5.20.1       jym 	const char * const *p, * const *q;
    292       1.1   thorpej 
    293  1.5.20.1       jym 	for (p = &locators.vec[off], q = (const char * const *)ptr; --len >= 0;)
    294       1.1   thorpej 		if (*p++ != *q++)
    295       1.1   thorpej 			return (0);	/* different */
    296       1.1   thorpej 	return (1);			/* same */
    297       1.1   thorpej }
    298       1.1   thorpej 
    299       1.1   thorpej /*
    300       1.1   thorpej  * Add the given locators at the end of the global loc[] table.
    301       1.1   thorpej  */
    302       1.1   thorpej static int
    303       1.1   thorpej addlocs(const char **locs, int len)
    304       1.1   thorpej {
    305       1.1   thorpej 	const char **p;
    306       1.1   thorpej 	int ret;
    307       1.1   thorpej 
    308       1.1   thorpej 	ret = locators.used;
    309       1.1   thorpej 	if ((locators.used = ret + len) > locspace)
    310       1.1   thorpej 		panic("addlocs: overrun");
    311       1.1   thorpej 	for (p = &locators.vec[ret]; --len >= 0;)
    312       1.1   thorpej 		*p++ = *locs++;
    313       1.1   thorpej 	return (ret);
    314       1.1   thorpej }
    315       1.1   thorpej 
    316       1.1   thorpej /*
    317       1.1   thorpej  * Comparison function for qsort-by-locator-length, longest first.
    318       1.1   thorpej  * We rashly assume that subtraction of these lengths does not overflow.
    319       1.1   thorpej  */
    320       1.1   thorpej static int
    321       1.1   thorpej loclencmp(const void *a, const void *b)
    322       1.1   thorpej {
    323       1.5  christos 	const struct pspec *p1, *p2;
    324       1.1   thorpej 	int l1, l2;
    325       1.1   thorpej 
    326  1.5.20.1       jym 	p1 = (*(const struct devi * const *)a)->i_pspec;
    327       1.1   thorpej 	l1 = p1 != NULL ? p1->p_iattr->a_loclen : 0;
    328       1.1   thorpej 
    329  1.5.20.1       jym 	p2 = (*(const struct devi * const *)b)->i_pspec;
    330       1.1   thorpej 	l2 = p2 != NULL ? p2->p_iattr->a_loclen : 0;
    331       1.1   thorpej 
    332       1.1   thorpej 	return (l2 - l1);
    333       1.1   thorpej }
    334       1.1   thorpej 
    335       1.1   thorpej static void
    336       1.1   thorpej resettails(void)
    337       1.1   thorpej {
    338       1.1   thorpej 	struct tails **p, *t, *next;
    339       1.1   thorpej 	int i;
    340       1.1   thorpej 
    341       1.1   thorpej 	for (p = tails, i = TAILHSIZE; --i >= 0; p++) {
    342       1.1   thorpej 		for (t = *p; t != NULL; t = next) {
    343       1.1   thorpej 			next = t->t_next;
    344       1.1   thorpej 			free(t);
    345       1.1   thorpej 		}
    346       1.1   thorpej 		*p = NULL;
    347       1.1   thorpej 	}
    348       1.1   thorpej }
    349