bitmap.c revision 50479
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: head/usr.sbin/pw/bitmap.c 50479 1999-08-28 01:35:59Z peter $"; 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{ 5320253Sjoerg if (bm->map) 5420253Sjoerg free(bm->map); 5520253Sjoerg} 5620253Sjoerg 5720253Sjoergstatic void 5820253Sjoergbm_getmask(int *pos, unsigned char *bmask) 5920253Sjoerg{ 6020253Sjoerg *bmask = (unsigned char) (1 << (*pos % 8)); 6120253Sjoerg *pos /= 8; 6220253Sjoerg} 6320253Sjoerg 6420253Sjoergvoid 6520253Sjoergbm_setbit(struct bitmap * bm, int pos) 6620253Sjoerg{ 6720253Sjoerg unsigned char bmask; 6820253Sjoerg 6920253Sjoerg bm_getmask(&pos, &bmask); 7020253Sjoerg bm->map[pos] |= bmask; 7120253Sjoerg} 7220253Sjoerg 7320253Sjoergvoid 7420253Sjoergbm_clrbit(struct bitmap * bm, int pos) 7520253Sjoerg{ 7620253Sjoerg unsigned char bmask; 7720253Sjoerg 7820253Sjoerg bm_getmask(&pos, &bmask); 7920253Sjoerg bm->map[pos] &= ~bmask; 8020253Sjoerg} 8120253Sjoerg 8220253Sjoergint 8320253Sjoergbm_isset(struct bitmap * bm, int pos) 8420253Sjoerg{ 8520253Sjoerg unsigned char bmask; 8620253Sjoerg 8720253Sjoerg bm_getmask(&pos, &bmask); 8820253Sjoerg return !!(bm->map[pos] & bmask); 8920253Sjoerg} 9020253Sjoerg 9120253Sjoergint 9220253Sjoergbm_firstunset(struct bitmap * bm) 9320253Sjoerg{ 9420253Sjoerg int szmap = (bm->size / 8) + !!(bm->size % 8); 9520253Sjoerg int at = 0; 9620253Sjoerg int pos = 0; 9720253Sjoerg 9820253Sjoerg while (pos < szmap) { 9920253Sjoerg unsigned char bmv = bm->map[pos++]; 10020253Sjoerg unsigned char bmask = 1; 10120253Sjoerg 10220253Sjoerg while (bmask & 0xff) { 10320253Sjoerg if ((bmv & bmask) == 0) 10420253Sjoerg return at; 10520253Sjoerg bmask <<= 1; 10620253Sjoerg ++at; 10720253Sjoerg } 10820253Sjoerg } 10920253Sjoerg return at; 11020253Sjoerg} 11120253Sjoerg 11220253Sjoergint 11320253Sjoergbm_lastset(struct bitmap * bm) 11420253Sjoerg{ 11520253Sjoerg int szmap = (bm->size / 8) + !!(bm->size % 8); 11620253Sjoerg int at = 0; 11720253Sjoerg int pos = 0; 11820253Sjoerg int ofs = 0; 11920253Sjoerg 12020253Sjoerg while (pos < szmap) { 12120253Sjoerg unsigned char bmv = bm->map[pos++]; 12220253Sjoerg unsigned char bmask = 1; 12320253Sjoerg 12420253Sjoerg while (bmask & 0xff) { 12520253Sjoerg if ((bmv & bmask) != 0) 12620253Sjoerg ofs = at; 12720253Sjoerg bmask <<= 1; 12820253Sjoerg ++at; 12920253Sjoerg } 13020253Sjoerg } 13120253Sjoerg return ofs; 13220253Sjoerg} 133