1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (C) 1996
5 *	David L. Nugent.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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 *
16 * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <stdlib.h>
30#include <string.h>
31
32#include "bitmap.h"
33
34struct bitmap
35bm_alloc(int size)
36{
37	struct bitmap   bm;
38	int             szmap = (size / 8) + !!(size % 8);
39
40	bm.size = size;
41	bm.map = malloc(szmap);
42	if (bm.map)
43		memset(bm.map, 0, szmap);
44	return bm;
45}
46
47void
48bm_dealloc(struct bitmap * bm)
49{
50	free(bm->map);
51}
52
53static void
54bm_getmask(int *pos, unsigned char *bmask)
55{
56	*bmask = (unsigned char) (1 << (*pos % 8));
57	*pos /= 8;
58}
59
60void
61bm_setbit(struct bitmap * bm, int pos)
62{
63	unsigned char   bmask;
64
65	bm_getmask(&pos, &bmask);
66	bm->map[pos] |= bmask;
67}
68
69void
70bm_clrbit(struct bitmap * bm, int pos)
71{
72	unsigned char   bmask;
73
74	bm_getmask(&pos, &bmask);
75	bm->map[pos] &= ~bmask;
76}
77
78int
79bm_isset(struct bitmap * bm, int pos)
80{
81	unsigned char   bmask;
82
83	bm_getmask(&pos, &bmask);
84	return !!(bm->map[pos] & bmask);
85}
86
87int
88bm_firstunset(struct bitmap * bm)
89{
90	int             szmap = (bm->size / 8) + !!(bm->size % 8);
91	int             at = 0;
92	int             pos = 0;
93
94	while (pos < szmap) {
95		unsigned char   bmv = bm->map[pos++];
96		unsigned char   bmask = 1;
97
98		while (bmask & 0xff) {
99			if ((bmv & bmask) == 0)
100				return at;
101			bmask <<= 1;
102			++at;
103		}
104	}
105	return at;
106}
107
108int
109bm_lastset(struct bitmap * bm)
110{
111	int             szmap = (bm->size / 8) + !!(bm->size % 8);
112	int             at = 0;
113	int             pos = 0;
114	int             ofs = 0;
115
116	while (pos < szmap) {
117		unsigned char   bmv = bm->map[pos++];
118		unsigned char   bmask = 1;
119
120		while (bmask & 0xff) {
121			if ((bmv & bmask) != 0)
122				ofs = at;
123			bmask <<= 1;
124			++at;
125		}
126	}
127	return ofs;
128}
129