2 * bit map routines (in-core).
5 * Copyright (c) 1995, 1996 Marcus D. Watts All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Marcus D. Watts.
18 * 4. The name of the developer may not be used to endorse or promote
19 * products derived from this software without specific prior written
22 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
23 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
24 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
25 * MARCUS D. WATTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <afsconfig.h>
35 #include <afs/param.h>
45 #undef PRINT_MAP_ERROR
46 /* #define MAP_DEBUG /**/
47 /* #define NEED_READ_WRITE /**/
50 #define MSHIFT 8 /* XXX make this 8 in the production version... */
51 #define MDATA (1<<MSHIFT)
53 struct bitmap *m_next;
58 #define MAP(p) ((struct bitmap*)((long)(p)&~1))
59 #define NEGMAP(p) (((int)(p))&1)
60 #define POSMAP(p) (!NEGMAP(p))
61 #define NOT_MAP(mp) ((struct map *) (((long)(mp)) ^ 1))
63 #define NUMBERTOBIT(n) ((n) & ((1<<LSHIFT)-1))
64 #define NUMBERTOINDEX(n) ((n>>LSHIFT) & ((1<<MSHIFT)-1))
65 #define NUMBERTOPAGE(n) ((n>>(LSHIFT+MSHIFT)))
66 #define TONUMBER(p,x,b) ((b) + ((x)<<LSHIFT) + ((p)<<((LSHIFT+MSHIFT))))
68 #define Mflag (debug_mask & (1L<<('Y'-'@')))
69 #define Aflag (debug_mask & (1L<<('Z'-'@')))
70 extern int debug_mask;
73 in_map(struct map *parm, long node)
82 printf("in_map: is %d in [", node);
87 bit = NUMBERTOBIT(node);
88 x = NUMBERTOINDEX(node);
89 page = NUMBERTOPAGE(node);
92 if (TONUMBER(page, x, bit) != node) {
94 ("bxp mixup: node=%ld -> p=%d x=%d b=%d -> %ld, %ld, %ld = %ld\n",
95 node, page, x, bit, TONUMBER(page, 0, 0), TONUMBER(0, x, 0),
96 TONUMBER(0, 0, bit), TONUMBER(page, x, bit));
101 for (map = MAP(parm); map; map = map->m_next)
102 if (map->m_page == page)
103 return NEGMAP(parm) ^ (!!(map->m_data[x] & bit));
104 result = !POSMAP(parm);
107 printf(" -> %s\n", result ? "yes" : "no");
113 free_map(struct map *parm)
115 struct bitmap *map, *next;
118 printf("Freeing map");
132 add_map(struct map *parm, long node)
140 printf("add_map: adding %d to [", node);
145 bit = NUMBERTOBIT(node);
146 x = NUMBERTOINDEX(node);
147 page = NUMBERTOPAGE(node);
151 for (map = MAP(parm); map; map = map->m_next)
152 if (map->m_page == page)
155 map = (struct bitmap *)malloc(sizeof *map);
157 #ifdef PRINT_MAP_ERROR
158 printf("No memory!\n");
160 free_map((struct map *)map);
164 bzero((char *)map->m_data, sizeof map->m_data);
167 for (i = 0; i < MDATA; ++i)
170 map->m_next = MAP(parm);
172 parm = (struct map *)map;
177 map->m_data[x] |= bit;
179 map->m_data[x] &= ~bit;
187 return (struct map *)parm;
191 simplify_bitmap(struct bitmap *map)
193 struct bitmap **mpp, *mp2;
195 for (mpp = ↦ mp2 = *mpp;) {
196 for (i = 0; i < MDATA; ++i)
200 #ifdef PRINT_MAP_ERROR
201 printf("simplify_bitmap: failed to eliminate zero page %d\n",
213 or_bitmap(struct bitmap *left, struct bitmap *right)
215 struct bitmap **rightmp, *lmap, *rmap;
217 for (lmap = left; lmap; lmap = lmap->m_next) {
218 for (rightmp = &right; rmap = *rightmp; rightmp = &rmap->m_next)
219 if (rmap->m_page == lmap->m_page) {
220 for (i = 0; i < MDATA; ++i)
221 lmap->m_data[i] |= rmap->m_data[i];
222 *rightmp = rmap->m_next;
227 for (rightmp = &left; *rightmp; rightmp = &(*rightmp)->m_next);
233 and_bitmap(struct bitmap *left, struct bitmap *right)
235 struct bitmap **rightmp, *lmap, *rmap, **leftmp;
238 for (leftmp = &left; lmap = *leftmp;) {
240 for (rightmp = &right; rmap = *rightmp; rightmp = &rmap->m_next)
241 if (rmap->m_page == lmap->m_page) {
242 for (i = 0; i < MDATA; ++i)
243 sig |= (lmap->m_data[i] &= rmap->m_data[i]);
244 *rightmp = rmap->m_next;
249 leftmp = &lmap->m_next;
251 *leftmp = lmap->m_next;
255 free_map((struct map *)right);
256 return simplify_bitmap(left);
259 /* bit set in left, but not in right */
261 bic_bitmap(struct bitmap *left, struct bitmap *right)
263 struct bitmap **rightmp, *lmap, *rmap, **leftmp;
268 printf("bic_bitmap: left=%#lx right=%#lx\n", left, right);
271 for (leftmp = &left; lmap = *leftmp;) {
273 for (rightmp = &right; rmap = *rightmp; rightmp = &rmap->m_next)
274 if (rmap->m_page == lmap->m_page) {
275 for (i = 0; i < MDATA; ++i)
276 sig |= (lmap->m_data[i] &= ~rmap->m_data[i]);
277 *rightmp = rmap->m_next;
282 leftmp = &lmap->m_next;
284 *leftmp = lmap->m_next;
288 free_map((struct map *)right);
289 left = simplify_bitmap(left);
292 printf("bic_bitmap: result=%#lx\n", left);
299 and_map(struct map *mp1, struct map *mp2)
303 printf("Anding maps");
311 mp1 = (struct map *)and_bitmap(mp1, mp2);
313 mp1 = (struct map *)bic_bitmap(mp1, MAP(mp2));
314 else if (POSMAP(mp2))
315 mp1 = (struct map *)bic_bitmap(mp2, MAP(mp1));
317 mp1 = NOT_MAP(or_bitmap(MAP(mp1), MAP(mp2)));
329 or_map(struct map *mp1, struct map *mp2)
333 printf("Oring maps");
341 mp1 = (struct map *)or_bitmap(mp1, mp2);
343 mp1 = NOT_MAP(bic_bitmap(MAP(mp2), mp1));
344 else if (POSMAP(mp2))
345 mp1 = NOT_MAP(bic_bitmap(MAP(mp1), mp2));
347 mp1 = NOT_MAP(and_bitmap(MAP(mp1), MAP(mp2)));
359 not_map(struct map *map)
363 printf("Notting map");
372 copy_map(struct map *parm)
374 struct bitmap *result, **mpp, *map;
383 for (mpp = &result; *mpp = 0, map; map = map->m_next) {
384 *mpp = (struct bitmap *)malloc(sizeof **mpp);
388 printf("copy_map: out of memory\n");
390 free_map((struct map *)result);
395 mpp = &(*mpp)->m_next;
398 return NOT_MAP(result);
400 return (struct map *)result;
404 count_map(struct map *parm)
411 for (map = MAP(parm); map; map = map->m_next) {
412 for (i = 0; i < MDATA; ++i) {
413 if (!map->m_data[i]);
414 else if (!~map->m_data[i])
417 for (j = 0; j < (1L << LSHIFT); ++j)
418 if (map->m_data[i] & (1L << j))
423 nf = ~nf; /* note 1's complement */
428 printf(" -> %d\n", nf);
435 next_map(struct map *parm, long node)
437 struct bitmap *map, *lowest;
446 printf("next_map: selecting next after %d from", node);
453 printf("next_map failed; negative map\n");
459 bit = NUMBERTOBIT(node);
460 x = NUMBERTOINDEX(node);
461 page = NUMBERTOPAGE(node);
465 for (map = MAP(parm); map; map = map->m_next)
466 if (map->m_page >= page && (!lowest || lowest->m_page > map->m_page)) {
469 if (page == map->m_page)
471 for (; i < MDATA; ++i, mask = ~0)
472 if (map->m_data[i] & mask)
475 for (bn = 0; bn < (1 << LSHIFT); ++bn)
476 if (map->m_data[i] & mask & (1L << bn)) {
481 if (bn == (1 << LSHIFT)) {
483 ("next_map: botch; pageno %d index %d data %#lx mask %#lx x,bit %d,%#lx\n",
484 map->m_page, i, map->m_data[i], mask, x, bit);
489 best = bn + ((i + ((map->m_page) << MSHIFT)
496 printf(" -> %d\n", best);
497 if (best >= 0 && !in_map((struct map *)parm, best)) {
498 printf("next_map: botch; %d not in map\n", best);
507 first_map(struct map *parm)
509 return next_map(parm, -9999);
513 prev_map(struct map *parm, long node)
515 struct bitmap *map, *lowest;
524 printf("prev_map: selecting previous before %d from", node);
531 printf("prev_map failed; negative map\n");
537 node = ((unsigned long)~0) >> 1;
540 bit = NUMBERTOBIT(node);
541 x = NUMBERTOINDEX(node);
542 page = NUMBERTOPAGE(node);
546 for (map = MAP(parm); map; map = map->m_next)
547 if (map->m_page <= page && (!lowest || lowest->m_page < map->m_page)) {
550 if (page == map->m_page)
551 i = x, mask = (bit << 1) - 1;
552 for (; i >= 0; --i, mask = ~0)
553 if (map->m_data[i] & mask)
556 for (bn = (1 << LSHIFT) - 1; bn >= 0; --bn)
557 if (map->m_data[i] & mask & (1L << bn)) {
564 ("prev_map: botch; pageno %d index %d data %#lx mask %#lx x,bit %d,%#lx\n",
565 map->m_page, i, map->m_data[i], mask, x, bit);
570 best = bn + ((i + ((map->m_page) << MSHIFT)
577 printf(" -> %d\n", best);
578 if (best >= 0 && !in_map(parm, best)) {
579 printf("prev_map: botch; %d not in map\n", best);
588 last_map(struct map *parm)
590 return prev_map(parm, 0x7fffffff);
594 negative_map(struct map *map)
596 return (struct map *)NEGMAP(map);
600 bic_map(struct map *mp1, struct map *mp2)
610 mp1 = and_map(mp1, NOT_MAP(mp2));
621 #ifdef PRINT_MAP_ERROR
623 print_map(struct map *parm)
627 int bitno, firstbitno, lastbitno;
633 printf(" nil(%lx)", parm);
635 printf(" %lx", parm);
638 for (; map; map = map->m_next)
639 for (i = 0; i < MDATA; ++i)
641 for (j = 0; j < (1 << LSHIFT); ++j) {
644 ((map->m_page) << (LSHIFT + MSHIFT));
645 if (map->m_data[i] & (1 << j)) {
646 if (bitno == lastbitno + 1) {
650 if (bitno != (lastbitno + 1)) {
651 if (firstbitno >= 0 && firstbitno != lastbitno)
652 printf("-%d", lastbitno);
655 printf(" %d", bitno);
656 firstbitno = lastbitno = bitno;
658 if (firstbitno >= 0 && firstbitno != lastbitno)
659 printf("-%d", lastbitno);
663 if (firstbitno >= 0 && firstbitno != lastbitno)
664 printf("-%d", lastbitno);
668 #ifdef NEED_READ_WRITE
670 read_map(int (*f) (), char *arg)
672 struct bitmap *map, *result, **mp;
674 int bitno, lastno, thisno, prevno;
678 /* count, then startbitno, then bits. */
679 lastno = ((*f) (arg));
682 if (lastno & ((1 << LSHIFT) - 1)) {
683 #ifdef PRINT_MAP_ERROR
684 printf("read_map: bad count\n");
688 bitno = ((*f) (arg));
689 if (bitno & ((1 << LSHIFT) - 1)) {
690 #ifdef PRINT_MAP_ERROR
691 printf("read_map: bad start\n");
697 for (; bitno < lastno; bitno += (1 << LSHIFT)) {
701 page = NUMBERTOPAGE(bitno);
702 if (!map || map->m_page != page)
703 for (mp = &result; map = *mp; mp = &map->m_next)
704 if (map->m_page == page)
707 map = (struct bitmap *)malloc(sizeof *map);
709 #ifdef PRINT_MAP_ERROR
710 printf("No memory!\n");
713 free_map((struct map *)result);
716 bzero((char *)map->m_data, sizeof map->m_data);
721 map->m_data[NUMBERTOINDEX(bitno)] = data;
723 return (struct map *)result;
727 write_map(struct map *parm, int (*f) (), char *arg)
731 int bitno, lastno, thisno, prevno;
734 bitno = first_map(parm);
735 bitno &= ~((1 << LSHIFT) - 1);
737 lastno = last_map(parm);
739 lastno += ((1 << LSHIFT));
740 lastno &= ~((1 << LSHIFT) - 1);
741 /* count, then startbitno, then bits. */
742 if ((*f) (arg, lastno) == -1)
744 /* word: number of bits */
745 if ((*f) (arg, bitno) == -1)
749 for (bitno = first_map(parm); bitno >= 0; bitno = next_map(parm, bitno)) {
750 page = NUMBERTOPAGE(bitno);
751 for (map = MAP(parm); map; map = map->m_next)
752 if (map->m_page == page)
755 #ifdef PRINT_MAP_ERROR
756 printf("write_map: botch #1: can't find page %d\n", page);
760 for (i = 0; i < MDATA; ++i) {
763 thisno = TONUMBER(page, i, 0);
764 for (j = thisno - prevno; j > 0; j -= (1 << LSHIFT))
765 if ((*f) (arg, 0) == -1)
769 if ((*f) (arg, map->m_data[i]) == -1)
771 prevno += (1 << LSHIFT);
773 if (i >= MDATA || !map->m_data[i])
778 bitno = TONUMBER(page, i, 0) - 1;
780 #ifdef PRINT_MAP_ERROR
781 if (prevno != lastno)
782 printf("write_map: botch #2: bitno=%d prevno=%d lastno=%d\n", bitno,