Ejemplo n.º 1
0
int main(int argc, const char *argv[]) {
        _cleanup_bitmap_free_ Bitmap *b = NULL;
        Iterator it;
        unsigned n = (unsigned) -1, i = 0;

        b = bitmap_new();
        assert_se(b);

        assert_se(bitmap_ensure_allocated(&b) == 0);
        bitmap_free(b);
        b = NULL;
        assert_se(bitmap_ensure_allocated(&b) == 0);

        assert_se(bitmap_isset(b, 0) == false);
        assert_se(bitmap_isset(b, 1) == false);
        assert_se(bitmap_isset(b, 256) == false);
        assert_se(bitmap_isclear(b) == true);

        assert_se(bitmap_set(b, 0) == 0);
        assert_se(bitmap_isset(b, 0) == true);
        assert_se(bitmap_isclear(b) == false);
        bitmap_unset(b, 0);
        assert_se(bitmap_isset(b, 0) == false);
        assert_se(bitmap_isclear(b) == true);

        assert_se(bitmap_set(b, 1) == 0);
        assert_se(bitmap_isset(b, 1) == true);
        assert_se(bitmap_isclear(b) == false);
        bitmap_unset(b, 1);
        assert_se(bitmap_isset(b, 1) == false);
        assert_se(bitmap_isclear(b) == true);

        assert_se(bitmap_set(b, 256) == 0);
        assert_se(bitmap_isset(b, 256) == true);
        assert_se(bitmap_isclear(b) == false);
        bitmap_unset(b, 256);
        assert_se(bitmap_isset(b, 256) == false);
        assert_se(bitmap_isclear(b) == true);

        assert_se(bitmap_set(b, 32) == 0);
        bitmap_unset(b, 0);
        assert_se(bitmap_isset(b, 32) == true);
        bitmap_unset(b, 32);

        BITMAP_FOREACH(n, NULL, it)
                assert_not_reached("NULL bitmap");

        assert_se(bitmap_set(b, 0) == 0);
        assert_se(bitmap_set(b, 1) == 0);
        assert_se(bitmap_set(b, 256) == 0);

        BITMAP_FOREACH(n, b, it) {
                assert_se(n == i);
                if (i == 0)
                        i = 1;
                else if (i == 1)
                        i = 256;
                else if (i == 256)
                        i = (unsigned) -1;
        }
Ejemplo n.º 2
0
TEST_END

TEST_BEGIN(test_bitmap_sfu)
{
	size_t i;

	for (i = 1; i <= BITMAP_MAXBITS; i++) {
		bitmap_info_t binfo;
		bitmap_info_init(&binfo, i);
		{
			size_t j;
			bitmap_t *bitmap = (bitmap_t *)malloc(
			    bitmap_size(&binfo));
			bitmap_init(bitmap, &binfo);

			/* Iteratively set bits starting at the beginning. */
			for (j = 0; j < i; j++) {
				assert_zd_eq(bitmap_sfu(bitmap, &binfo), j,
				    "First unset bit should be just after "
				    "previous first unset bit");
			}
			assert_true(bitmap_full(bitmap, &binfo),
			    "All bits should be set");

			/*
			 * Iteratively unset bits starting at the end, and
			 * verify that bitmap_sfu() reaches the unset bits.
			 */
			for (j = i - 1; j < i; j--) { /* (i..0] */
				bitmap_unset(bitmap, &binfo, j);
				assert_zd_eq(bitmap_sfu(bitmap, &binfo), j,
				    "First unset bit should the bit previously "
				    "unset");
				bitmap_unset(bitmap, &binfo, j);
			}
			assert_false(bitmap_get(bitmap, &binfo, 0),
			    "Bit should be unset");

			/*
			 * Iteratively set bits starting at the beginning, and
			 * verify that bitmap_sfu() looks past them.
			 */
			for (j = 1; j < i; j++) {
				bitmap_set(bitmap, &binfo, j - 1);
				assert_zd_eq(bitmap_sfu(bitmap, &binfo), j,
				    "First unset bit should be just after the "
				    "bit previously set");
				bitmap_unset(bitmap, &binfo, j);
			}
			assert_zd_eq(bitmap_sfu(bitmap, &binfo), i - 1,
			    "First unset bit should be the last bit");
			assert_true(bitmap_full(bitmap, &binfo),
			    "All bits should be set");
			free(bitmap);
		}
	}
}
Ejemplo n.º 3
0
Archivo: main.c Proyecto: pacepi/bitmap
int check(bitmap_t bmp, int x)
{
    int fail = 0;
    int i = 0;

    for (i = 0; i < SIZE; i++) {
        if (i % x)
            bitmap_set(bmp, i);
        else
            bitmap_unset(bmp, i);
    }

    for (i = 0; i < SIZE; i++) {
        if (i % x) {
            if (bitmap_get(bmp, i) != 1)
                fail++;
        }
        else {
            if (bitmap_get(bmp, i) != 0)
                fail++;
        }
    }

    printf("%s : x = %d, fail = %d\n", __FUNCTION__, x, fail);
    return fail;
}
Ejemplo n.º 4
0
TEST_END

TEST_BEGIN(test_bitmap_unset)
{
	size_t i;

	for (i = 1; i <= BITMAP_MAXBITS; i++) {
		bitmap_info_t binfo;
		bitmap_info_init(&binfo, i);
		{
			size_t j;
			bitmap_t *bitmap = (bitmap_t *)malloc(
			    bitmap_size(&binfo));
			bitmap_init(bitmap, &binfo);

			for (j = 0; j < i; j++)
				bitmap_set(bitmap, &binfo, j);
			assert_true(bitmap_full(bitmap, &binfo),
			    "All bits should be set");
			for (j = 0; j < i; j++)
				bitmap_unset(bitmap, &binfo, j);
			for (j = 0; j < i; j++)
				bitmap_set(bitmap, &binfo, j);
			assert_true(bitmap_full(bitmap, &binfo),
			    "All bits should be set");
			free(bitmap);
		}
	}
}
Ejemplo n.º 5
0
static void
test_bitmap_unset(void)
{
    size_t i;

    for (i = 1; i <= MAXBITS; i++) {
        bitmap_info_t binfo;
        bitmap_info_init(&binfo, i);
        {
            size_t j;
            bitmap_t *bitmap = malloc(sizeof(bitmap_t) *
                                      bitmap_info_ngroups(&binfo));
            bitmap_init(bitmap, &binfo);

            for (j = 0; j < i; j++)
                bitmap_set(bitmap, &binfo, j);
            assert(bitmap_full(bitmap, &binfo));
            for (j = 0; j < i; j++)
                bitmap_unset(bitmap, &binfo, j);
            for (j = 0; j < i; j++)
                bitmap_set(bitmap, &binfo, j);
            assert(bitmap_full(bitmap, &binfo));
            free(bitmap);
        }
    }
}
Ejemplo n.º 6
0
TEST_END

static void
test_bitmap_sfu_body(const bitmap_info_t *binfo, size_t nbits)
{
	size_t i;
	bitmap_t *bitmap = (bitmap_t *)malloc(bitmap_size(binfo));
	assert_ptr_not_null(bitmap, "Unexpected malloc() failure");
	bitmap_init(bitmap, binfo);

	/* Iteratively set bits starting at the beginning. */
	for (i = 0; i < nbits; i++) {
		assert_zd_eq(bitmap_sfu(bitmap, binfo), i,
		    "First unset bit should be just after previous first unset "
		    "bit");
	}
	assert_true(bitmap_full(bitmap, binfo), "All bits should be set");

	/*
	 * Iteratively unset bits starting at the end, and verify that
	 * bitmap_sfu() reaches the unset bits.
	 */
	for (i = nbits - 1; i < nbits; i--) { /* (nbits..0] */
		bitmap_unset(bitmap, binfo, i);
		assert_zd_eq(bitmap_sfu(bitmap, binfo), i,
		    "First unset bit should the bit previously unset");
		bitmap_unset(bitmap, binfo, i);
	}
	assert_false(bitmap_get(bitmap, binfo, 0), "Bit should be unset");

	/*
	 * Iteratively set bits starting at the beginning, and verify that
	 * bitmap_sfu() looks past them.
	 */
	for (i = 1; i < nbits; i++) {
		bitmap_set(bitmap, binfo, i - 1);
		assert_zd_eq(bitmap_sfu(bitmap, binfo), i,
		    "First unset bit should be just after the bit previously "
		    "set");
		bitmap_unset(bitmap, binfo, i);
	}
	assert_zd_eq(bitmap_sfu(bitmap, binfo), nbits - 1,
	    "First unset bit should be the last bit");
	assert_true(bitmap_full(bitmap, binfo), "All bits should be set");
	free(bitmap);
}
Ejemplo n.º 7
0
static void
test_bitmap_sfu(void)
{
    size_t i;

    for (i = 1; i <= MAXBITS; i++) {
        bitmap_info_t binfo;
        bitmap_info_init(&binfo, i);
        {
            ssize_t j;
            bitmap_t *bitmap = malloc(sizeof(bitmap_t) *
                                      bitmap_info_ngroups(&binfo));
            bitmap_init(bitmap, &binfo);

            /* Iteratively set bits starting at the beginning. */
            for (j = 0; j < i; j++)
                assert(bitmap_sfu(bitmap, &binfo) == j);
            assert(bitmap_full(bitmap, &binfo));

            /*
             * Iteratively unset bits starting at the end, and
             * verify that bitmap_sfu() reaches the unset bits.
             */
            for (j = i - 1; j >= 0; j--) {
                bitmap_unset(bitmap, &binfo, j);
                assert(bitmap_sfu(bitmap, &binfo) == j);
                bitmap_unset(bitmap, &binfo, j);
            }
            assert(bitmap_get(bitmap, &binfo, 0) == false);

            /*
             * Iteratively set bits starting at the beginning, and
             * verify that bitmap_sfu() looks past them.
             */
            for (j = 1; j < i; j++) {
                bitmap_set(bitmap, &binfo, j - 1);
                assert(bitmap_sfu(bitmap, &binfo) == j);
                bitmap_unset(bitmap, &binfo, j);
            }
            assert(bitmap_sfu(bitmap, &binfo) == i - 1);
            assert(bitmap_full(bitmap, &binfo));
            free(bitmap);
        }
    }
}
Ejemplo n.º 8
0
void  bitmap_region_free_page( region_t *region, void *ptr ){
	uintptr_t vaddr = (uintptr_t)region->addr;
	uintptr_t temp  = (uintptr_t)ptr;
	unsigned index = (temp - vaddr) / PAGE_SIZE;
	unsigned bytepos = index % 8;

	bitmap_unset( region->data, index );

	if ( bytepos <= region->extra_data ){
		region->extra_data = bytepos;
	}

	//printf( "[%s] Freeing ptr at %p, bit %u\n", __func__, ptr, index );
}
Ejemplo n.º 9
0
JEMALLOC_INLINE_C void
arena_slab_reg_dalloc(tsdn_t *tsdn, extent_t *slab,
    arena_slab_data_t *slab_data, void *ptr)
{
	szind_t binind = slab_data->binind;
	const arena_bin_info_t *bin_info = &arena_bin_info[binind];
	size_t regind = arena_slab_regind(slab, binind, ptr);

	assert(slab_data->nfree < bin_info->nregs);
	/* Freeing an unallocated pointer can cause assertion failure. */
	assert(bitmap_get(slab_data->bitmap, &bin_info->bitmap_info, regind));

	bitmap_unset(slab_data->bitmap, &bin_info->bitmap_info, regind);
	slab_data->nfree++;
}
Ejemplo n.º 10
0
TEST_END

static void
test_bitmap_unset_body(const bitmap_info_t *binfo, size_t nbits)
{
	size_t i;
	bitmap_t *bitmap = (bitmap_t *)malloc(bitmap_size(binfo));
	assert_ptr_not_null(bitmap, "Unexpected malloc() failure");
	bitmap_init(bitmap, binfo);

	for (i = 0; i < nbits; i++)
		bitmap_set(bitmap, binfo, i);
	assert_true(bitmap_full(bitmap, binfo), "All bits should be set");
	for (i = 0; i < nbits; i++)
		bitmap_unset(bitmap, binfo, i);
	for (i = 0; i < nbits; i++)
		bitmap_set(bitmap, binfo, i);
	assert_true(bitmap_full(bitmap, binfo), "All bits should be set");
	free(bitmap);
}
Ejemplo n.º 11
0
int main(void)
{
        uint32_t i;
        uint32_t id;
        bitmap_info_t binfo;
        uint32_t buffer[100000] = {0};
        bitmap_info_init(&binfo, 100);
        bitmap_t *bitmap = NULL;
        bitmap = (bitmap_t *)malloc(bitmap_size(&binfo));
        bitmap_init(bitmap, &binfo);
        for (i = 1; i < binfo.nlevels; i++) {
                printf("group_offset%d:%6d %6d\n", i, \
                        binfo.levels[i].group_offset - \
                        binfo.levels[i-1].group_offset, \
                        binfo.levels[i].group_offset);
        }
        
        /* register */
        printf("register: ");
        for(i = 0; i < 65; i++) {
                id = bitmap_sfu(bitmap, &binfo);
                buffer[i] = id;
                print_id(i, id);
        }
#if pbitmap
        /* show bitmap */
        printf("\n");
        for(i = 0; i < 65; i++) {
                printf("%16llx ", bitmap[i]);
                if (!((i+1)%4))
                        printf("\n");
        }
#endif
        /* aged */
        printf("\naged: ");
        for(i = 30; i < 65; ++i) {
                bitmap_unset(bitmap, &binfo, buffer[i]);
                print_id(i, buffer[i]);
        }
#if pbitmap
        /* show bitmap */
        printf("\n");
        for(i = 0; i < 50; i++) {
                printf("%16llx ", bitmap[i]);
                if (!((i+1)%4))
                        printf("\n");
        }
#endif    
        /* register again*/
        printf("\nsecond register: ");
        for(i = 0; i < 50; i++) {
                id = bitmap_sfu(bitmap, &binfo);
                buffer[i+30] = id;
                print_id(i, id);
        }

        /* aged again */
        printf("\naged: ");
        for(i = 0; i < 80; i++) {
                bitmap_unset(bitmap, &binfo, buffer[i]);
                print_id(i, buffer[i]);
        }

        printf("\n");
        free(bitmap);
        return 0;
}