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 #undef PRINT_MAP_ERROR
40 /* #define MAP_DEBUG */
41 /* #define NEED_READ_WRITE */
44 #define MSHIFT 8 /* XXX make this 8 in the production version... */
45 #define MDATA (1<<MSHIFT)
47 struct bitmap *m_next;
52 #define MAP(p) ((struct bitmap*)((intptr_t)(p)&~1))
53 #define NEGMAP(p) (((intptr_t)(p))&1)
54 #define POSMAP(p) (!NEGMAP(p))
55 #define NOT_MAP(mp) ((struct map *) (((intptr_t)(mp)) ^ 1))
57 #define NUMBERTOBIT(n) ((n) & ((1<<LSHIFT)-1))
58 #define NUMBERTOINDEX(n) ((n>>LSHIFT) & ((1<<MSHIFT)-1))
59 #define NUMBERTOPAGE(n) ((n>>(LSHIFT+MSHIFT)))
60 #define TONUMBER(p,x,b) ((b) + ((x)<<LSHIFT) + ((p)<<((LSHIFT+MSHIFT))))
62 #define Mflag (debug_mask & (1L<<('Y'-'@')))
63 #define Aflag (debug_mask & (1L<<('Z'-'@')))
64 extern int debug_mask;
67 in_map(struct map *parm, int node)
76 printf("in_map: is %d in [", node);
81 bit = NUMBERTOBIT(node);
82 x = NUMBERTOINDEX(node);
83 page = NUMBERTOPAGE(node);
86 if (TONUMBER(page, x, bit) != node) {
88 ("bxp mixup: node=%d -> p=%d x=%d b=%d -> %d, %d, %d = %d\n",
89 node, page, x, bit, TONUMBER(page, 0, 0), TONUMBER(0, x, 0),
90 TONUMBER(0, 0, bit), TONUMBER(page, x, bit));
95 for (map = MAP(parm); map; map = map->m_next)
96 if (map->m_page == page)
97 return NEGMAP(parm) ^ (!!(map->m_data[x] & bit));
98 result = !POSMAP(parm);
101 printf(" -> %s\n", result ? "yes" : "no");
107 free_map(struct map *parm)
109 struct bitmap *map, *next;
112 printf("Freeing map");
126 add_map(struct map *parm, int node)
134 printf("add_map: adding %d to [", node);
139 bit = NUMBERTOBIT(node);
140 x = NUMBERTOINDEX(node);
141 page = NUMBERTOPAGE(node);
145 for (map = MAP(parm); map; map = map->m_next)
146 if (map->m_page == page)
149 map = malloc(sizeof *map);
151 #ifdef PRINT_MAP_ERROR
152 printf("No memory!\n");
154 free_map((struct map *)map);
158 memset( map->m_data, 0, sizeof map->m_data);
161 for (i = 0; i < MDATA; ++i)
164 map->m_next = MAP(parm);
166 parm = (struct map *)map;
171 map->m_data[x] |= bit;
173 map->m_data[x] &= ~bit;
181 return (struct map *)parm;
185 simplify_bitmap(struct bitmap *map)
187 struct bitmap **mpp, *mp2;
189 for (mpp = ↦ (mp2 = *mpp);) {
190 for (i = 0; i < MDATA; ++i)
194 #ifdef PRINT_MAP_ERROR
195 printf("simplify_bitmap: failed to eliminate zero page %d\n",
207 or_bitmap(struct bitmap *left, struct bitmap *right)
209 struct bitmap **rightmp, *lmap, *rmap;
211 for (lmap = left; lmap; lmap = lmap->m_next) {
212 for (rightmp = &right; (rmap = *rightmp); rightmp = &rmap->m_next)
213 if (rmap->m_page == lmap->m_page) {
214 for (i = 0; i < MDATA; ++i)
215 lmap->m_data[i] |= rmap->m_data[i];
216 *rightmp = rmap->m_next;
221 for (rightmp = &left; *rightmp; rightmp = &(*rightmp)->m_next)
228 and_bitmap(struct bitmap *left, struct bitmap *right)
230 struct bitmap **rightmp, *lmap, *rmap, **leftmp;
233 for (leftmp = &left; (lmap = *leftmp);) {
235 for (rightmp = &right; (rmap = *rightmp); rightmp = &rmap->m_next)
236 if (rmap->m_page == lmap->m_page) {
237 for (i = 0; i < MDATA; ++i)
238 sig |= (lmap->m_data[i] &= rmap->m_data[i]);
239 *rightmp = rmap->m_next;
244 leftmp = &lmap->m_next;
246 *leftmp = lmap->m_next;
250 free_map((struct map *)right);
251 return simplify_bitmap(left);
254 /* bit set in left, but not in right */
256 bic_bitmap(struct bitmap *left, struct bitmap *right)
258 struct bitmap **rightmp, *lmap, *rmap, **leftmp;
263 printf("bic_bitmap: left=%#lx right=%#lx\n", (long)left, (long)right);
266 for (leftmp = &left; (lmap = *leftmp);) {
268 for (rightmp = &right; (rmap = *rightmp); rightmp = &rmap->m_next)
269 if (rmap->m_page == lmap->m_page) {
270 for (i = 0; i < MDATA; ++i)
271 sig |= (lmap->m_data[i] &= ~rmap->m_data[i]);
272 *rightmp = rmap->m_next;
277 leftmp = &lmap->m_next;
279 *leftmp = lmap->m_next;
283 free_map((struct map *)right);
284 left = simplify_bitmap(left);
287 printf("bic_bitmap: result=%#lx\n", (long) left);
294 and_map(struct map *mp1, struct map *mp2)
298 printf("Anding maps");
306 mp1 = (struct map *)and_bitmap((struct bitmap *) mp1,
307 (struct bitmap *) mp2);
309 mp1 = (struct map *)bic_bitmap((struct bitmap *) mp1, MAP(mp2));
310 else if (POSMAP(mp2))
311 mp1 = (struct map *)bic_bitmap((struct bitmap *) mp2, MAP(mp1));
313 mp1 = NOT_MAP(or_bitmap(MAP(mp1), MAP(mp2)));
325 or_map(struct map *mp1, struct map *mp2)
329 printf("Oring maps");
337 mp1 = (struct map *)or_bitmap((struct bitmap *) mp1,
338 (struct bitmap *) mp2);
340 mp1 = NOT_MAP(bic_bitmap(MAP(mp2), (struct bitmap *) mp1));
341 else if (POSMAP(mp2))
342 mp1 = NOT_MAP(bic_bitmap(MAP(mp1), (struct bitmap *) mp2));
344 mp1 = NOT_MAP(and_bitmap(MAP(mp1), MAP(mp2)));
356 not_map(struct map *map)
360 printf("Notting map");
369 copy_map(struct map *parm)
371 struct bitmap *result, **mpp, *map;
380 for (mpp = &result; (*mpp = 0), map; map = map->m_next) {
381 *mpp = malloc(sizeof **mpp);
385 printf("copy_map: out of memory\n");
387 free_map((struct map *)result);
392 mpp = &(*mpp)->m_next;
395 return NOT_MAP(result);
397 return (struct map *)result;
401 count_map(struct map *parm)
408 for (map = MAP(parm); map; map = map->m_next) {
409 for (i = 0; i < MDATA; ++i) {
412 else if (!~map->m_data[i])
415 for (j = 0; j < (1L << LSHIFT); ++j)
416 if (map->m_data[i] & (1L << j))
421 nf = ~nf; /* note 1's complement */
426 printf(" -> %d\n", nf);
433 next_map(struct map *parm, int node)
435 struct bitmap *map, *lowest;
444 printf("next_map: selecting next after %d from", node);
451 printf("next_map failed; negative map\n");
457 bit = NUMBERTOBIT(node);
458 x = NUMBERTOINDEX(node);
459 page = NUMBERTOPAGE(node);
463 for (map = MAP(parm); map; map = map->m_next)
464 if (map->m_page >= page && (!lowest || lowest->m_page > map->m_page)) {
467 if (page == map->m_page)
469 for (; i < MDATA; ++i, mask = ~0)
470 if (map->m_data[i] & mask)
473 for (bn = 0; bn < (1 << LSHIFT); ++bn)
474 if (map->m_data[i] & mask & (1L << bn)) {
479 if (bn == (1 << LSHIFT)) {
481 ("next_map: botch; pageno %d index %d data %#x mask %#x x,bit %d,%#x\n",
482 map->m_page, i, map->m_data[i], mask, x, bit);
487 best = bn + ((i + ((map->m_page) << MSHIFT)
494 printf(" -> %d\n", best);
495 if (best >= 0 && !in_map(parm, best)) {
496 printf("next_map: botch; %d not in map\n", best);
505 first_map(struct map *parm)
507 return next_map(parm, -9999);
511 prev_map(struct map *parm, int node)
513 struct bitmap *map, *lowest;
522 printf("prev_map: selecting previous before %d from", node);
529 printf("prev_map failed; negative map\n");
535 node = ((unsigned int)~0) >> 1;
538 bit = NUMBERTOBIT(node);
539 x = NUMBERTOINDEX(node);
540 page = NUMBERTOPAGE(node);
544 for (map = MAP(parm); map; map = map->m_next)
545 if (map->m_page <= page && (!lowest || lowest->m_page < map->m_page)) {
548 if (page == map->m_page)
549 i = x, mask = (bit << 1) - 1;
550 for (; i >= 0; --i, mask = ~0)
551 if (map->m_data[i] & mask)
554 for (bn = (1 << LSHIFT) - 1; bn >= 0; --bn)
555 if (map->m_data[i] & mask & (1L << bn)) {
562 ("prev_map: botch; pageno %d index %d data %#x mask %#x x,bit %d,%#x\n",
563 map->m_page, i, map->m_data[i], mask, x, bit);
568 best = bn + ((i + ((map->m_page) << MSHIFT)
575 printf(" -> %d\n", best);
576 if (best >= 0 && !in_map(parm, best)) {
577 printf("prev_map: botch; %d not in map\n", best);
586 last_map(struct map *parm)
588 return prev_map(parm, 0x7fffffff);
592 negative_map(struct map *map)
594 return (struct map *)NEGMAP(map);
598 bic_map(struct map *mp1, struct map *mp2)
608 mp1 = and_map(mp1, NOT_MAP(mp2));
619 #ifdef PRINT_MAP_ERROR
621 print_map(struct map *parm)
625 int bitno, firstbitno, lastbitno;
631 printf(" nil(%lx)", (long)parm);
633 printf(" %lx", (long)parm);
636 for (; map; map = map->m_next)
637 for (i = 0; i < MDATA; ++i)
639 for (j = 0; j < (1 << LSHIFT); ++j) {
642 ((map->m_page) << (LSHIFT + MSHIFT));
643 if (map->m_data[i] & (1 << j)) {
644 if (bitno == lastbitno + 1) {
648 if (bitno != (lastbitno + 1)) {
649 if (firstbitno >= 0 && firstbitno != lastbitno)
650 printf("-%d", lastbitno);
653 printf(" %d", bitno);
654 firstbitno = lastbitno = bitno;
656 if (firstbitno >= 0 && firstbitno != lastbitno)
657 printf("-%d", lastbitno);
661 if (firstbitno >= 0 && firstbitno != lastbitno)
662 printf("-%d", lastbitno);
666 #ifdef NEED_READ_WRITE
668 read_map(int (*f) (void *), char *arg)
670 struct bitmap *map, *result, **mp;
675 /* count, then startbitno, then bits. */
676 lastno = ((*f) (arg));
679 if (lastno & ((1 << LSHIFT) - 1)) {
680 #ifdef PRINT_MAP_ERROR
681 printf("read_map: bad count\n");
685 bitno = ((*f) (arg));
686 if (bitno & ((1 << LSHIFT) - 1)) {
687 #ifdef PRINT_MAP_ERROR
688 printf("read_map: bad start\n");
694 for (; bitno < lastno; bitno += (1 << LSHIFT)) {
698 page = NUMBERTOPAGE(bitno);
699 if (!map || map->m_page != page)
700 for (mp = &result; (map = *mp); mp = &map->m_next)
701 if (map->m_page == page)
704 map = malloc(sizeof *map);
706 #ifdef PRINT_MAP_ERROR
707 printf("No memory!\n");
710 free_map((struct map *)result);
713 memset( map->m_data, 0, sizeof map->m_data);
718 map->m_data[NUMBERTOINDEX(bitno)] = data;
720 return (struct map *)result;
724 write_map(struct map *parm, int (*f) (void *, int), char *arg)
728 int bitno, lastno, thisno, prevno;
731 bitno = first_map(parm);
732 bitno &= ~((1 << LSHIFT) - 1);
734 lastno = last_map(parm);
736 lastno += ((1 << LSHIFT));
737 lastno &= ~((1 << LSHIFT) - 1);
738 /* count, then startbitno, then bits. */
739 if ((*f) (arg, lastno) == -1)
741 /* word: number of bits */
742 if ((*f) (arg, bitno) == -1)
746 for (bitno = first_map(parm); bitno >= 0; bitno = next_map(parm, bitno)) {
747 page = NUMBERTOPAGE(bitno);
748 for (map = MAP(parm); map; map = map->m_next)
749 if (map->m_page == page)
752 #ifdef PRINT_MAP_ERROR
753 printf("write_map: botch #1: can't find page %d\n", page);
757 for (i = 0; i < MDATA; ++i) {
760 thisno = TONUMBER(page, i, 0);
761 for (j = thisno - prevno; j > 0; j -= (1 << LSHIFT))
762 if ((*f) (arg, 0) == -1)
766 if ((*f) (arg, map->m_data[i]) == -1)
768 prevno += (1 << LSHIFT);
770 if (i >= MDATA || !map->m_data[i])
775 bitno = TONUMBER(page, i, 0) - 1;
777 #ifdef PRINT_MAP_ERROR
778 if (prevno != lastno)
779 printf("write_map: botch #2: bitno=%d prevno=%d lastno=%d\n", bitno,