2 * bit map routines (in-core).
5 * Copyright (c) 1995, 1996, 2007 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. The name of the developer may not be used to endorse or promote
16 * products derived from this software without specific prior written
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 * MARCUS D. WATTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
25 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
27 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
28 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <afsconfig.h>
32 #include <afs/param.h>
42 #undef PRINT_MAP_ERROR
43 /* #define MAP_DEBUG /**/
44 /* #define NEED_READ_WRITE /**/
47 #define MSHIFT 8 /* XXX make this 8 in the production version... */
48 #define MDATA (1<<MSHIFT)
50 struct bitmap *m_next;
55 #define MAP(p) ((struct bitmap*)((int)(p)&~1))
56 #define NEGMAP(p) (((int)(p))&1)
57 #define POSMAP(p) (!NEGMAP(p))
58 #define NOT_MAP(mp) ((struct map *) (((int)(mp)) ^ 1))
60 #define NUMBERTOBIT(n) ((n) & ((1<<LSHIFT)-1))
61 #define NUMBERTOINDEX(n) ((n>>LSHIFT) & ((1<<MSHIFT)-1))
62 #define NUMBERTOPAGE(n) ((n>>(LSHIFT+MSHIFT)))
63 #define TONUMBER(p,x,b) ((b) + ((x)<<LSHIFT) + ((p)<<((LSHIFT+MSHIFT))))
65 #define Mflag (debug_mask & (1L<<('Y'-'@')))
66 #define Aflag (debug_mask & (1L<<('Z'-'@')))
67 extern int debug_mask;
70 in_map(struct map *parm, int node)
79 printf("in_map: is %d in [", node);
84 bit = NUMBERTOBIT(node);
85 x = NUMBERTOINDEX(node);
86 page = NUMBERTOPAGE(node);
89 if (TONUMBER(page, x, bit) != node) {
91 ("bxp mixup: node=%d -> p=%d x=%d b=%d -> %d, %d, %d = %d\n",
92 node, page, x, bit, TONUMBER(page, 0, 0), TONUMBER(0, x, 0),
93 TONUMBER(0, 0, bit), TONUMBER(page, x, bit));
98 for (map = MAP(parm); map; map = map->m_next)
99 if (map->m_page == page)
100 return NEGMAP(parm) ^ (!!(map->m_data[x] & bit));
101 result = !POSMAP(parm);
104 printf(" -> %s\n", result ? "yes" : "no");
110 free_map(struct map *parm)
112 struct bitmap *map, *next;
115 printf("Freeing map");
129 add_map(struct map *parm, int node)
137 printf("add_map: adding %d to [", node);
142 bit = NUMBERTOBIT(node);
143 x = NUMBERTOINDEX(node);
144 page = NUMBERTOPAGE(node);
148 for (map = MAP(parm); map; map = map->m_next)
149 if (map->m_page == page)
152 map = (struct bitmap *)malloc(sizeof *map);
154 #ifdef PRINT_MAP_ERROR
155 printf("No memory!\n");
157 free_map((struct map *)map);
161 memset((char *) map->m_data, 0, sizeof map->m_data);
164 for (i = 0; i < MDATA; ++i)
167 map->m_next = MAP(parm);
169 parm = (struct map *)map;
174 map->m_data[x] |= bit;
176 map->m_data[x] &= ~bit;
184 return (struct map *)parm;
188 simplify_bitmap(struct bitmap *map)
190 struct bitmap **mpp, *mp2;
192 for (mpp = ↦ (mp2 = *mpp);) {
193 for (i = 0; i < MDATA; ++i)
197 #ifdef PRINT_MAP_ERROR
198 printf("simplify_bitmap: failed to eliminate zero page %d\n",
210 or_bitmap(struct bitmap *left, struct bitmap *right)
212 struct bitmap **rightmp, *lmap, *rmap;
214 for (lmap = left; lmap; lmap = lmap->m_next) {
215 for (rightmp = &right; (rmap = *rightmp); rightmp = &rmap->m_next)
216 if (rmap->m_page == lmap->m_page) {
217 for (i = 0; i < MDATA; ++i)
218 lmap->m_data[i] |= rmap->m_data[i];
219 *rightmp = rmap->m_next;
224 for (rightmp = &left; *rightmp; rightmp = &(*rightmp)->m_next)
231 and_bitmap(struct bitmap *left, struct bitmap *right)
233 struct bitmap **rightmp, *lmap, *rmap, **leftmp;
236 for (leftmp = &left; (lmap = *leftmp);) {
238 for (rightmp = &right; (rmap = *rightmp); rightmp = &rmap->m_next)
239 if (rmap->m_page == lmap->m_page) {
240 for (i = 0; i < MDATA; ++i)
241 sig |= (lmap->m_data[i] &= rmap->m_data[i]);
242 *rightmp = rmap->m_next;
247 leftmp = &lmap->m_next;
249 *leftmp = lmap->m_next;
253 free_map((struct map *)right);
254 return simplify_bitmap(left);
257 /* bit set in left, but not in right */
259 bic_bitmap(struct bitmap *left, struct bitmap *right)
261 struct bitmap **rightmp, *lmap, *rmap, **leftmp;
266 printf("bic_bitmap: left=%#lx right=%#lx\n", (long)left, (long)right);
269 for (leftmp = &left; (lmap = *leftmp);) {
271 for (rightmp = &right; (rmap = *rightmp); rightmp = &rmap->m_next)
272 if (rmap->m_page == lmap->m_page) {
273 for (i = 0; i < MDATA; ++i)
274 sig |= (lmap->m_data[i] &= ~rmap->m_data[i]);
275 *rightmp = rmap->m_next;
280 leftmp = &lmap->m_next;
282 *leftmp = lmap->m_next;
286 free_map((struct map *)right);
287 left = simplify_bitmap(left);
290 printf("bic_bitmap: result=%#lx\n", (long) left);
297 and_map(struct map *mp1, struct map *mp2)
301 printf("Anding maps");
309 mp1 = (struct map *)and_bitmap((struct bitmap *) mp1,
310 (struct bitmap *) mp2);
312 mp1 = (struct map *)bic_bitmap((struct bitmap *) mp1, MAP(mp2));
313 else if (POSMAP(mp2))
314 mp1 = (struct map *)bic_bitmap((struct bitmap *) mp2, MAP(mp1));
316 mp1 = NOT_MAP(or_bitmap(MAP(mp1), MAP(mp2)));
328 or_map(struct map *mp1, struct map *mp2)
332 printf("Oring maps");
340 mp1 = (struct map *)or_bitmap((struct bitmap *) mp1,
341 (struct bitmap *) mp2);
343 mp1 = NOT_MAP(bic_bitmap(MAP(mp2), (struct bitmap *) mp1));
344 else if (POSMAP(mp2))
345 mp1 = NOT_MAP(bic_bitmap(MAP(mp1), (struct bitmap *) 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) {
415 else if (!~map->m_data[i])
418 for (j = 0; j < (1L << LSHIFT); ++j)
419 if (map->m_data[i] & (1L << j))
424 nf = ~nf; /* note 1's complement */
429 printf(" -> %d\n", nf);
436 next_map(struct map *parm, int node)
438 struct bitmap *map, *lowest;
447 printf("next_map: selecting next after %d from", node);
454 printf("next_map failed; negative map\n");
460 bit = NUMBERTOBIT(node);
461 x = NUMBERTOINDEX(node);
462 page = NUMBERTOPAGE(node);
466 for (map = MAP(parm); map; map = map->m_next)
467 if (map->m_page >= page && (!lowest || lowest->m_page > map->m_page)) {
470 if (page == map->m_page)
472 for (; i < MDATA; ++i, mask = ~0)
473 if (map->m_data[i] & mask)
476 for (bn = 0; bn < (1 << LSHIFT); ++bn)
477 if (map->m_data[i] & mask & (1L << bn)) {
482 if (bn == (1 << LSHIFT)) {
484 ("next_map: botch; pageno %d index %d data %#x mask %#x x,bit %d,%#x\n",
485 map->m_page, i, map->m_data[i], mask, x, bit);
490 best = bn + ((i + ((map->m_page) << MSHIFT)
497 printf(" -> %d\n", best);
498 if (best >= 0 && !in_map(parm, best)) {
499 printf("next_map: botch; %d not in map\n", best);
508 first_map(struct map *parm)
510 return next_map(parm, -9999);
514 prev_map(struct map *parm, int node)
516 struct bitmap *map, *lowest;
525 printf("prev_map: selecting previous before %d from", node);
532 printf("prev_map failed; negative map\n");
538 node = ((unsigned int)~0) >> 1;
541 bit = NUMBERTOBIT(node);
542 x = NUMBERTOINDEX(node);
543 page = NUMBERTOPAGE(node);
547 for (map = MAP(parm); map; map = map->m_next)
548 if (map->m_page <= page && (!lowest || lowest->m_page < map->m_page)) {
551 if (page == map->m_page)
552 i = x, mask = (bit << 1) - 1;
553 for (; i >= 0; --i, mask = ~0)
554 if (map->m_data[i] & mask)
557 for (bn = (1 << LSHIFT) - 1; bn >= 0; --bn)
558 if (map->m_data[i] & mask & (1L << bn)) {
565 ("prev_map: botch; pageno %d index %d data %#x mask %#x x,bit %d,%#x\n",
566 map->m_page, i, map->m_data[i], mask, x, bit);
571 best = bn + ((i + ((map->m_page) << MSHIFT)
578 printf(" -> %d\n", best);
579 if (best >= 0 && !in_map(parm, best)) {
580 printf("prev_map: botch; %d not in map\n", best);
589 last_map(struct map *parm)
591 return prev_map(parm, 0x7fffffff);
595 negative_map(struct map *map)
597 return (struct map *)NEGMAP(map);
601 bic_map(struct map *mp1, struct map *mp2)
611 mp1 = and_map(mp1, NOT_MAP(mp2));
622 #ifdef PRINT_MAP_ERROR
624 print_map(struct map *parm)
628 int bitno, firstbitno, lastbitno;
634 printf(" nil(%lx)", (long)parm);
636 printf(" %lx", (long)parm);
639 for (; map; map = map->m_next)
640 for (i = 0; i < MDATA; ++i)
642 for (j = 0; j < (1 << LSHIFT); ++j) {
645 ((map->m_page) << (LSHIFT + MSHIFT));
646 if (map->m_data[i] & (1 << j)) {
647 if (bitno == lastbitno + 1) {
651 if (bitno != (lastbitno + 1)) {
652 if (firstbitno >= 0 && firstbitno != lastbitno)
653 printf("-%d", lastbitno);
656 printf(" %d", bitno);
657 firstbitno = lastbitno = bitno;
659 if (firstbitno >= 0 && firstbitno != lastbitno)
660 printf("-%d", lastbitno);
664 if (firstbitno >= 0 && firstbitno != lastbitno)
665 printf("-%d", lastbitno);
669 #ifdef NEED_READ_WRITE
671 read_map(int (*f) (void *), char *arg)
673 struct bitmap *map, *result, **mp;
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 memset((char *) map->m_data, 0, sizeof map->m_data);
721 map->m_data[NUMBERTOINDEX(bitno)] = data;
723 return (struct map *)result;
727 write_map(struct map *parm, int (*f) (void *, int), 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,