-/* $OpenBSD: subr_blist.c,v 1.2 2022/08/06 13:44:04 semarie Exp $ */
+/* $OpenBSD: subr_blist.c,v 1.3 2022/08/13 16:02:15 semarie Exp $ */
/* DragonFlyBSD:7b80531f545c7d3c51c1660130c71d01f6bccbe0:/sys/kern/subr_blist.c */
/*
* BLIST.C - Bitmap allocator/deallocator, using a radix tree with hinting
#define mallocarray(n,s,t,f) reallocarray(NULL, n, s)
#define free(p,t,s) free(p)
#define KASSERT(exp) assert(exp)
+#define KDASSERT(exp) assert(exp)
#include "../sys/blist.h"
else
blk = blst_meta_alloc(bl->bl_root, 0, 0, count,
bl->bl_radix, bl->bl_skip);
- if (blk != SWAPBLK_NONE)
+ if (blk != SWAPBLK_NONE) {
bl->bl_free -= count;
+
+ KDASSERT(blk < bl->bl_blocks);
+ KDASSERT(bl->bl_free <= bl->bl_blocks);
+ }
}
return(blk);
}
swblk_t blk = SWAPBLK_NONE;
if (bl) {
- KASSERT(blkat < bl->bl_blocks);
- KASSERT(blkat + count <= bl->bl_blocks);
+ KDASSERT(blkat < bl->bl_blocks);
+ KDASSERT(blkat + count <= bl->bl_blocks);
if (bl->bl_radix == BLIST_BMAP_RADIX)
blk = blst_leaf_alloc(bl->bl_root, blkat, 0, count);
else
blk = blst_meta_alloc(bl->bl_root, blkat, 0, count,
bl->bl_radix, bl->bl_skip);
- if (blk != SWAPBLK_NONE)
+ if (blk != SWAPBLK_NONE) {
bl->bl_free -= count;
+
+ KDASSERT(blk < bl->bl_blocks);
+ KDASSERT(bl->bl_free <= bl->bl_blocks);
+ }
}
return(blk);
}
blist_free(blist_t bl, swblk_t blkno, swblk_t count)
{
if (bl) {
- KASSERT(blkno < bl->bl_blocks);
- KASSERT(blkno + count <= bl->bl_blocks);
+ KDASSERT(blkno < bl->bl_blocks);
+ KDASSERT(blkno + count <= bl->bl_blocks);
if (bl->bl_radix == BLIST_BMAP_RADIX)
blst_leaf_free(bl->bl_root, blkno, count);
else
blst_meta_free(bl->bl_root, blkno, count, bl->bl_radix, bl->bl_skip, 0);
bl->bl_free += count;
+
+ KDASSERT(bl->bl_free <= bl->bl_blocks);
}
}
swblk_t filled;
if (bl) {
- KASSERT(blkno < bl->bl_blocks);
- KASSERT(blkno + count <= bl->bl_blocks);
-
+ KDASSERT(blkno < bl->bl_blocks);
+ KDASSERT(blkno + count <= bl->bl_blocks);
+
if (bl->bl_radix == BLIST_BMAP_RADIX) {
filled = blst_leaf_fill(bl->bl_root, blkno, count);
} else {
bl->bl_radix, bl->bl_skip, 0);
}
bl->bl_free -= filled;
+ KDASSERT(bl->bl_free <= bl->bl_blocks);
return (filled);
} else {
return 0;
}
}
- KASSERT(*maxbp <= *maxep);
- KASSERT(*maxbp < bl->bl_blocks);
- KASSERT(*maxep <= bl->bl_blocks);
+ KDASSERT(*maxbp <= *maxep);
+ KDASSERT(*maxbp < bl->bl_blocks);
+ KDASSERT(*maxep <= bl->bl_blocks);
}
/*
}
for (i = 1; i <= skip; i += next_skip) {
+ if (scan[i].bm_bighint == (swblk_t)-1) {
+ /*
+ * Terminator
+ *
+ * note: check it first, as swblk_t may be unsigned.
+ * otherwise, the second if() might match and the
+ * Terminator will be ignored.
+ */
+ break;
+ }
+
if (count <= scan[i].bm_bighint &&
blk + (swblk_t)radix > blkat) {
/*
return(r);
}
/* bighint was updated by recursion */
- } else if (scan[i].bm_bighint == (swblk_t)-1) {
- /*
- * Terminator
- */
- break;
} else if (count > (swblk_t)radix) {
/*
* count does not fit in object even if it were
if (sscanf(buf + 1, "%li %li", &count, &blkat) == 1) {
printf("count %lu\n", count);
swblk_t blk = blist_alloc(bl, count);
- printf(" R=%04lx\n", blk);
+ if (blk == SWAPBLK_NONE)
+ printf(" R=SWAPBLK_NONE\n");
+ else
+ printf(" R=%04lx\n", blk);
} else if (sscanf(buf + 1, "%li %li", &count, &blkat) == 2) {
swblk_t blk = blist_allocat(bl, count, blkat);
- printf(" R=%04lx\n", blk);
+ if (blk == SWAPBLK_NONE)
+ printf(" R=SWAPBLK_NONE\n");
+ else
+ printf(" R=%04lx\n", blk);
} else {
printf("?\n");
}