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