summaryrefslogtreecommitdiff
path: root/src/lib/bitmap.c
blob: 9d08221aa3e7a077a5cf948917a2dd864ca01ef2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
 * Ouroboros - Copyright (C) 2016 - 2021
 *
 * Bitmap implementation
 *
 *    Dimitri Staessens <dimitri.staessens@ugent.be>
 *    Sander Vrijders   <sander.vrijders@ugent.be>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., http://www.fsf.org/about/contact/.
 */

#include <ouroboros/bitmap.h>

#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

#define BITS_PER_BYTE CHAR_BIT

#define BITS_PER_LONG (sizeof(size_t) *  BITS_PER_BYTE)

#define BIT_WORD(nr) ((nr) / BITS_PER_LONG)

#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))

#define BITS_TO_LONGS(nr) \
        DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(size_t))

static size_t find_next_zero_bit(const size_t * addr,
                                 size_t         nbits)
{
        size_t tmp;
        size_t start = 0;
        size_t pos = 0;
        size_t mask;

        /* First find correct word */
        tmp = ~addr[start];
        while (!tmp) {
                start++;
                if (start >= DIV_ROUND_UP(nbits, BITS_PER_LONG))
                        return nbits;

                tmp = ~addr[start];
        }

        /* Find the free bit in the word */
        mask = 1UL;
        while (!(tmp & mask)) {
                pos++;
                mask = 1UL << pos;
        }

        return (start * BITS_PER_LONG) + pos;
}

static void bitmap_zero(size_t * dst,
                        size_t   nbits)
{
        memset(dst, 0, BITS_TO_LONGS(nbits) * sizeof(size_t));
}

static void bitmap_clear(size_t * map,
                         size_t   start)
{
        size_t * p = map + BIT_WORD(start);
        size_t mask = ~(1UL << (start % (BITS_PER_LONG)));

        *p &= mask;
}

static void bitmap_set(size_t * map,
                       size_t   start)
{
        size_t * p = map + BIT_WORD(start);
        size_t mask = 1UL << (start % (BITS_PER_LONG));

        *p |= mask;
}

struct bmp {
        ssize_t  offset;
        size_t   size;

        size_t * bitmap;
};

struct bmp * bmp_create(size_t  bits,
                        ssize_t offset)
{
        struct bmp * bmp;

        assert(bits);

        bmp = malloc(sizeof(*bmp));
        if (bmp == NULL)
                return NULL;

        bmp->bitmap = malloc(BITS_TO_LONGS(bits) * sizeof(size_t));
        if (bmp->bitmap == NULL) {
                free(bmp);
                return NULL;
        }

        bmp->size   = bits;
        bmp->offset = offset;
        bitmap_zero(bmp->bitmap, bits);

        return bmp;
}

void bmp_destroy(struct bmp * bmp)
{
        assert(bmp);

        if (bmp->bitmap != NULL)
                free(bmp->bitmap);

        free(bmp);
}

static ssize_t bad_id(struct bmp * bmp)
{
        assert(bmp);

        return bmp->offset - 1;
}

ssize_t bmp_allocate(struct bmp * bmp)
{
        size_t id;

        assert(bmp);

        id = find_next_zero_bit(bmp->bitmap, bmp->size);
        if (id >= bmp->size)
                return bad_id(bmp);

        bitmap_set(bmp->bitmap, id);

        return id + bmp->offset;
}

static bool is_id_valid(struct bmp * bmp,
                        ssize_t      id)
{
        assert(bmp);

        if ((id < bmp->offset) || (id > (ssize_t) (bmp->offset + bmp->size)))
                return false;

        return true;
}

static bool is_id_used(size_t * map,
                       size_t   start)
{
        size_t * p = map + BIT_WORD(start);
        size_t mask = 1UL << (start % (BITS_PER_LONG));

        return (*p & mask) != 0;
}

bool bmp_is_id_valid(struct bmp * bmp,
                     ssize_t      id)
{
        assert(bmp);

        return is_id_valid(bmp, id);
}

int bmp_release(struct bmp * bmp,
                ssize_t      id)
{
        assert(bmp);

        if (!is_id_valid(bmp, id))
                return -1;

        bitmap_clear(bmp->bitmap, id - bmp->offset);

        return 0;
}

bool bmp_is_id_used(struct bmp * bmp,
                    ssize_t      id)
{
        assert(bmp);

        return is_id_used(bmp->bitmap, id - bmp->offset);
}