120253Sjoerg/*-
220302Sjoerg * Copyright (C) 1996
320302Sjoerg *	David L. Nugent.  All rights reserved.
420253Sjoerg *
520253Sjoerg * Redistribution and use in source and binary forms, with or without
620253Sjoerg * modification, are permitted provided that the following conditions
720253Sjoerg * are met:
820253Sjoerg * 1. Redistributions of source code must retain the above copyright
920302Sjoerg *    notice, this list of conditions and the following disclaimer.
1020253Sjoerg * 2. Redistributions in binary form must reproduce the above copyright
1120253Sjoerg *    notice, this list of conditions and the following disclaimer in the
1220253Sjoerg *    documentation and/or other materials provided with the distribution.
1320253Sjoerg *
1420302Sjoerg * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
1520253Sjoerg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1620253Sjoerg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1720302Sjoerg * ARE DISCLAIMED.  IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
1820253Sjoerg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1920253Sjoerg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2020253Sjoerg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2120253Sjoerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2220253Sjoerg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2320253Sjoerg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2420253Sjoerg * SUCH DAMAGE.
2520253Sjoerg */
2620253Sjoerg
2730259Scharnier#ifndef lint
2830259Scharnierstatic const char rcsid[] =
2950479Speter  "$FreeBSD$";
3030259Scharnier#endif /* not lint */
3130259Scharnier
3220253Sjoerg#include <stdlib.h>
3320253Sjoerg#include <string.h>
3420253Sjoerg
3520253Sjoerg#include "bitmap.h"
3620253Sjoerg
3720253Sjoergstruct bitmap
3820253Sjoergbm_alloc(int size)
3920253Sjoerg{
4020253Sjoerg	struct bitmap   bm;
4120253Sjoerg	int             szmap = (size / 8) + !!(size % 8);
4220253Sjoerg
4320253Sjoerg	bm.size = size;
4420253Sjoerg	bm.map = malloc(szmap);
4520253Sjoerg	if (bm.map)
4620253Sjoerg		memset(bm.map, 0, szmap);
4720253Sjoerg	return bm;
4820253Sjoerg}
4920253Sjoerg
5020253Sjoergvoid
5120253Sjoergbm_dealloc(struct bitmap * bm)
5220253Sjoerg{
53244459Seadler	free(bm->map);
5420253Sjoerg}
5520253Sjoerg
5620253Sjoergstatic void
5720253Sjoergbm_getmask(int *pos, unsigned char *bmask)
5820253Sjoerg{
5920253Sjoerg	*bmask = (unsigned char) (1 << (*pos % 8));
6020253Sjoerg	*pos /= 8;
6120253Sjoerg}
6220253Sjoerg
6320253Sjoergvoid
6420253Sjoergbm_setbit(struct bitmap * bm, int pos)
6520253Sjoerg{
6620253Sjoerg	unsigned char   bmask;
6720253Sjoerg
6820253Sjoerg	bm_getmask(&pos, &bmask);
6920253Sjoerg	bm->map[pos] |= bmask;
7020253Sjoerg}
7120253Sjoerg
7220253Sjoergvoid
7320253Sjoergbm_clrbit(struct bitmap * bm, int pos)
7420253Sjoerg{
7520253Sjoerg	unsigned char   bmask;
7620253Sjoerg
7720253Sjoerg	bm_getmask(&pos, &bmask);
7820253Sjoerg	bm->map[pos] &= ~bmask;
7920253Sjoerg}
8020253Sjoerg
8120253Sjoergint
8220253Sjoergbm_isset(struct bitmap * bm, int pos)
8320253Sjoerg{
8420253Sjoerg	unsigned char   bmask;
8520253Sjoerg
8620253Sjoerg	bm_getmask(&pos, &bmask);
8720253Sjoerg	return !!(bm->map[pos] & bmask);
8820253Sjoerg}
8920253Sjoerg
9020253Sjoergint
9120253Sjoergbm_firstunset(struct bitmap * bm)
9220253Sjoerg{
9320253Sjoerg	int             szmap = (bm->size / 8) + !!(bm->size % 8);
9420253Sjoerg	int             at = 0;
9520253Sjoerg	int             pos = 0;
9620253Sjoerg
9720253Sjoerg	while (pos < szmap) {
9820253Sjoerg		unsigned char   bmv = bm->map[pos++];
9920253Sjoerg		unsigned char   bmask = 1;
10020253Sjoerg
10120253Sjoerg		while (bmask & 0xff) {
10220253Sjoerg			if ((bmv & bmask) == 0)
10320253Sjoerg				return at;
10420253Sjoerg			bmask <<= 1;
10520253Sjoerg			++at;
10620253Sjoerg		}
10720253Sjoerg	}
10820253Sjoerg	return at;
10920253Sjoerg}
11020253Sjoerg
11120253Sjoergint
11220253Sjoergbm_lastset(struct bitmap * bm)
11320253Sjoerg{
11420253Sjoerg	int             szmap = (bm->size / 8) + !!(bm->size % 8);
11520253Sjoerg	int             at = 0;
11620253Sjoerg	int             pos = 0;
11720253Sjoerg	int             ofs = 0;
11820253Sjoerg
11920253Sjoerg	while (pos < szmap) {
12020253Sjoerg		unsigned char   bmv = bm->map[pos++];
12120253Sjoerg		unsigned char   bmask = 1;
12220253Sjoerg
12320253Sjoerg		while (bmask & 0xff) {
12420253Sjoerg			if ((bmv & bmask) != 0)
12520253Sjoerg				ofs = at;
12620253Sjoerg			bmask <<= 1;
12720253Sjoerg			++at;
12820253Sjoerg		}
12920253Sjoerg	}
13020253Sjoerg	return ofs;
13120253Sjoerg}
132