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>
39 #ifdef STDLIB_HAS_MALLOC_PROTOS
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*)((int)(p)&~1))
59 #define NEGMAP(p) (((int)(p))&1)
60 #define POSMAP(p) (!NEGMAP(p))
61 #define NOT_MAP(mp) ((struct map *) (((int)(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, int 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=%d -> p=%d x=%d b=%d -> %d, %d, %d = %d\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, int 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 memset( map->m_data, 0, 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)
234 and_bitmap(struct bitmap *left, struct bitmap *right)
236 struct bitmap **rightmp, *lmap, *rmap, **leftmp;
239 for (leftmp = &left; (lmap = *leftmp);) {
241 for (rightmp = &right; (rmap = *rightmp); rightmp = &rmap->m_next)
242 if (rmap->m_page == lmap->m_page) {
243 for (i = 0; i < MDATA; ++i)
244 sig |= (lmap->m_data[i] &= rmap->m_data[i]);
245 *rightmp = rmap->m_next;
250 leftmp = &lmap->m_next;
252 *leftmp = lmap->m_next;
256 free_map((struct map *)right);
257 return simplify_bitmap(left);
260 /* bit set in left, but not in right */
262 bic_bitmap(struct bitmap *left, struct bitmap *right)
264 struct bitmap **rightmp, *lmap, *rmap, **leftmp;
269 printf("bic_bitmap: left=%#lx right=%#lx\n", (long)left, (long)right);
272 for (leftmp = &left; (lmap = *leftmp);) {
274 for (rightmp = &right; (rmap = *rightmp); rightmp = &rmap->m_next)
275 if (rmap->m_page == lmap->m_page) {
276 for (i = 0; i < MDATA; ++i)
277 sig |= (lmap->m_data[i] &= ~rmap->m_data[i]);
278 *rightmp = rmap->m_next;
283 leftmp = &lmap->m_next;
285 *leftmp = lmap->m_next;
289 free_map((struct map *)right);
290 left = simplify_bitmap(left);
293 printf("bic_bitmap: result=%#lx\n", (long) left);
300 and_map(struct map *mp1, struct map *mp2)
304 printf("Anding maps");
312 mp1 = (struct map *)and_bitmap((struct bitmap *) mp1,
313 (struct bitmap *) mp2);
315 mp1 = (struct map *)bic_bitmap((struct bitmap *) mp1, MAP(mp2));
316 else if (POSMAP(mp2))
317 mp1 = (struct map *)bic_bitmap((struct bitmap *) mp2, MAP(mp1));
319 mp1 = NOT_MAP(or_bitmap(MAP(mp1), MAP(mp2)));
331 or_map(struct map *mp1, struct map *mp2)
335 printf("Oring maps");
343 mp1 = (struct map *)or_bitmap((struct bitmap *) mp1,
344 (struct bitmap *) mp2);
346 mp1 = NOT_MAP(bic_bitmap(MAP(mp2), (struct bitmap *) mp1));
347 else if (POSMAP(mp2))
348 mp1 = NOT_MAP(bic_bitmap(MAP(mp1), (struct bitmap *) mp2));
350 mp1 = NOT_MAP(and_bitmap(MAP(mp1), MAP(mp2)));
362 not_map(struct map *map)
366 printf("Notting map");
375 copy_map(struct map *parm)
377 struct bitmap *result, **mpp, *map;
386 for (mpp = &result; (*mpp = 0), map; map = map->m_next) {
387 *mpp = (struct bitmap *)malloc(sizeof **mpp);
391 printf("copy_map: out of memory\n");
393 free_map((struct map *)result);
398 mpp = &(*mpp)->m_next;
401 return NOT_MAP(result);
403 return (struct map *)result;
407 count_map(struct map *parm)
414 for (map = MAP(parm); map; map = map->m_next) {
415 for (i = 0; i < MDATA; ++i) {
418 else if (!~map->m_data[i])
421 for (j = 0; j < (1L << LSHIFT); ++j)
422 if (map->m_data[i] & (1L << j))
427 nf = ~nf; /* note 1's complement */
432 printf(" -> %d\n", nf);
439 next_map(struct map *parm, int node)
441 struct bitmap *map, *lowest;
450 printf("next_map: selecting next after %d from", node);
457 printf("next_map failed; negative map\n");
463 bit = NUMBERTOBIT(node);
464 x = NUMBERTOINDEX(node);
465 page = NUMBERTOPAGE(node);
469 for (map = MAP(parm); map; map = map->m_next)
470 if (map->m_page >= page && (!lowest || lowest->m_page > map->m_page)) {
473 if (page == map->m_page)
475 for (; i < MDATA; ++i, mask = ~0)
476 if (map->m_data[i] & mask)
479 for (bn = 0; bn < (1 << LSHIFT); ++bn)
480 if (map->m_data[i] & mask & (1L << bn)) {
485 if (bn == (1 << LSHIFT)) {
487 ("next_map: botch; pageno %d index %d data %#x mask %#x x,bit %d,%#x\n",
488 map->m_page, i, map->m_data[i], mask, x, bit);
493 best = bn + ((i + ((map->m_page) << MSHIFT)
500 printf(" -> %d\n", best);
501 if (best >= 0 && !in_map(parm, best)) {
502 printf("next_map: botch; %d not in map\n", best);
511 first_map(struct map *parm)
513 return next_map(parm, -9999);
517 prev_map(struct map *parm, int node)
519 struct bitmap *map, *lowest;
528 printf("prev_map: selecting previous before %d from", node);
535 printf("prev_map failed; negative map\n");
541 node = ((unsigned int)~0) >> 1;
544 bit = NUMBERTOBIT(node);
545 x = NUMBERTOINDEX(node);
546 page = NUMBERTOPAGE(node);
550 for (map = MAP(parm); map; map = map->m_next)
551 if (map->m_page <= page && (!lowest || lowest->m_page < map->m_page)) {
554 if (page == map->m_page)
555 i = x, mask = (bit << 1) - 1;
556 for (; i >= 0; --i, mask = ~0)
557 if (map->m_data[i] & mask)
560 for (bn = (1 << LSHIFT) - 1; bn >= 0; --bn)
561 if (map->m_data[i] & mask & (1L << bn)) {
568 ("prev_map: botch; pageno %d index %d data %#x mask %#x x,bit %d,%#x\n",
569 map->m_page, i, map->m_data[i], mask, x, bit);
574 best = bn + ((i + ((map->m_page) << MSHIFT)
581 printf(" -> %d\n", best);
582 if (best >= 0 && !in_map(parm, best)) {
583 printf("prev_map: botch; %d not in map\n", best);
592 last_map(struct map *parm)
594 return prev_map(parm, 0x7fffffff);
598 negative_map(struct map *map)
600 return (struct map *)NEGMAP(map);
604 bic_map(struct map *mp1, struct map *mp2)
614 mp1 = and_map(mp1, NOT_MAP(mp2));
625 #ifdef PRINT_MAP_ERROR
627 print_map(struct map *parm)
631 int bitno, firstbitno, lastbitno;
637 printf(" nil(%lx)", (long)parm);
639 printf(" %lx", (long)parm);
642 for (; map; map = map->m_next)
643 for (i = 0; i < MDATA; ++i)
645 for (j = 0; j < (1 << LSHIFT); ++j) {
648 ((map->m_page) << (LSHIFT + MSHIFT));
649 if (map->m_data[i] & (1 << j)) {
650 if (bitno == lastbitno + 1) {
654 if (bitno != (lastbitno + 1)) {
655 if (firstbitno >= 0 && firstbitno != lastbitno)
656 printf("-%d", lastbitno);
659 printf(" %d", bitno);
660 firstbitno = lastbitno = bitno;
662 if (firstbitno >= 0 && firstbitno != lastbitno)
663 printf("-%d", lastbitno);
667 if (firstbitno >= 0 && firstbitno != lastbitno)
668 printf("-%d", lastbitno);
672 #ifdef NEED_READ_WRITE
674 read_map(int (*f) (void *), char *arg)
676 struct bitmap *map, *result, **mp;
681 /* count, then startbitno, then bits. */
682 lastno = ((*f) (arg));
685 if (lastno & ((1 << LSHIFT) - 1)) {
686 #ifdef PRINT_MAP_ERROR
687 printf("read_map: bad count\n");
691 bitno = ((*f) (arg));
692 if (bitno & ((1 << LSHIFT) - 1)) {
693 #ifdef PRINT_MAP_ERROR
694 printf("read_map: bad start\n");
700 for (; bitno < lastno; bitno += (1 << LSHIFT)) {
704 page = NUMBERTOPAGE(bitno);
705 if (!map || map->m_page != page)
706 for (mp = &result; (map = *mp); mp = &map->m_next)
707 if (map->m_page == page)
710 map = (struct bitmap *)malloc(sizeof *map);
712 #ifdef PRINT_MAP_ERROR
713 printf("No memory!\n");
716 free_map((struct map *)result);
719 memset( map->m_data, 0, sizeof map->m_data);
724 map->m_data[NUMBERTOINDEX(bitno)] = data;
726 return (struct map *)result;
730 write_map(struct map *parm, int (*f) (void *, int), char *arg)
734 int bitno, lastno, thisno, prevno;
737 bitno = first_map(parm);
738 bitno &= ~((1 << LSHIFT) - 1);
740 lastno = last_map(parm);
742 lastno += ((1 << LSHIFT));
743 lastno &= ~((1 << LSHIFT) - 1);
744 /* count, then startbitno, then bits. */
745 if ((*f) (arg, lastno) == -1)
747 /* word: number of bits */
748 if ((*f) (arg, bitno) == -1)
752 for (bitno = first_map(parm); bitno >= 0; bitno = next_map(parm, bitno)) {
753 page = NUMBERTOPAGE(bitno);
754 for (map = MAP(parm); map; map = map->m_next)
755 if (map->m_page == page)
758 #ifdef PRINT_MAP_ERROR
759 printf("write_map: botch #1: can't find page %d\n", page);
763 for (i = 0; i < MDATA; ++i) {
766 thisno = TONUMBER(page, i, 0);
767 for (j = thisno - prevno; j > 0; j -= (1 << LSHIFT))
768 if ((*f) (arg, 0) == -1)
772 if ((*f) (arg, map->m_data[i]) == -1)
774 prevno += (1 << LSHIFT);
776 if (i >= MDATA || !map->m_data[i])
781 bitno = TONUMBER(page, i, 0) - 1;
783 #ifdef PRINT_MAP_ERROR
784 if (prevno != lastno)
785 printf("write_map: botch #2: bitno=%d prevno=%d lastno=%d\n", bitno,