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