root/trunk/varnish-cache/include/vbm.h @ 4475

Revision 4475, 2.9 KB (checked in by phk, 8 months ago)

Various flexelint silencings.

  • Property svn:keywords set to Id
Line 
1/*-
2 * Copyright (c) 2006 Verdens Gang AS
3 * Copyright (c) 2006-2009 Linpro AS
4 * All rights reserved.
5 *
6 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $Id$
30 *
31 * Self-sizeing bitmap operations
32 */
33
34#include "config.h"
35
36/**********************************************************************
37 * Generic bitmap functions, may be generalized at some point.
38 */
39
40#define VBITMAP_TYPE    unsigned        /* Our preferred wordsize */
41#define VBITMAP_LUMP    (1024)          /* How many bits we alloc at a time */
42#define VBITMAP_WORD    (sizeof(VBITMAP_TYPE) * 8)
43#define VBITMAP_IDX(n)  (n / VBITMAP_WORD)
44#define VBITMAP_BIT(n)  (1U << (n % VBITMAP_WORD))
45
46struct vbitmap {
47        VBITMAP_TYPE    *bits;
48        unsigned        nbits;
49};
50
51static inline void
52vbit_expand(struct vbitmap *vb, unsigned bit)
53{
54        unsigned char *p;
55
56        bit += VBITMAP_LUMP - 1;
57        bit -= (bit % VBITMAP_LUMP);
58        p = realloc(vb->bits, bit / 8);
59        assert(p != NULL);
60        memset(p + vb->nbits / 8, 0, (bit - vb->nbits) / 8);
61        vb->bits = (void*)p;
62        vb->nbits = bit;
63}
64
65static inline struct vbitmap *
66vbit_init(unsigned initial)
67{
68        struct vbitmap *vb;
69
70        vb = calloc(sizeof *vb, 1);
71        assert(vb != NULL);
72        if (initial == 0)
73                initial = VBITMAP_LUMP;
74        vbit_expand(vb, initial);
75        return (vb);
76}
77
78static inline void
79vbit_set(struct vbitmap *vb, unsigned bit)
80{
81
82        if (bit >= vb->nbits)
83                vbit_expand(vb, bit);
84        vb->bits[VBITMAP_IDX(bit)] |= VBITMAP_BIT(bit);
85}
86
87static inline void
88vbit_clr(const struct vbitmap *vb, unsigned bit)
89{
90
91        if (bit < vb->nbits)
92                vb->bits[VBITMAP_IDX(bit)] &= ~VBITMAP_BIT(bit);
93}
94
95static inline int
96vbit_test(const struct vbitmap *vb, unsigned bit)
97{
98
99        if (bit >= vb->nbits)
100                return (0);
101        return (vb->bits[VBITMAP_IDX(bit)] & VBITMAP_BIT(bit));
102}
Note: See TracBrowser for help on using the browser.