forked from allinurl/goaccess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitmap.c
174 lines (140 loc) · 4.29 KB
/
bitmap.c
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
/**
* bitmap.c -- A quick bitmap implementation
* ______ ___
* / ____/___ / | _____________ __________
* / / __/ __ \/ /| |/ ___/ ___/ _ \/ ___/ ___/
* / /_/ / /_/ / ___ / /__/ /__/ __(__ |__ )
* \____/\____/_/ |_\___/\___/\___/____/____/
*
* The MIT License (MIT)
* Copyright (c) 2009-2020 Gerardo Orellana <hello @ goaccess.io>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdlib.h>
#include <string.h>
#include <limits.h> /* for CHAR_BIT */
#include <stdint.h>
#include "error.h"
#include "xmalloc.h"
#include "bitmap.h"
/*
* Returns the hamming weight (i.e. the number of bits set) in a word.
* NOTE: This routine borrowed from Linux 2.4.9 <linux/bitops.h>.
*/
static uint32_t
hweight (uint32_t w) {
uint32_t res;
res = (w & 0x55555555) + ((w >> 1) & 0x55555555);
res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F);
res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF);
res = (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF);
return res;
}
/* Returns the number of bytes required for bm->len bits. */
inline uint32_t
bitmap_sizeof (uint32_t i) {
return bitmap_word (i) * sizeof (word_t);
}
void
free_bitmap (bitmap * bm) {
free ((uint32_t *) bm->bmp);
free (bm);
}
int
bitmap_set_bit (word_t * words, uint32_t n) {
words[WORD_OFFSET (n)] |= ((word_t) 1 << BIT_OFFSET (n));
return 0;
}
int
bitmap_get_bit (word_t * words, uint32_t n) {
word_t bit = words[WORD_OFFSET (n)] & ((word_t) 1 << BIT_OFFSET (n));
return bit != 0;
}
uint32_t
bitmap_count_set (const bitmap * bm) {
uint32_t i, n = 0, len = 0;
if (!bm)
return 0;
len = bitmap_word (bm->len);
for (i = 0; i < len; ++i)
n += hweight (bm->bmp[i]);
return n;
}
uint32_t
bitmap_ffs (bitmap * bm) {
uint32_t i, pos = 1, len = 0;
uint32_t __bitset;
if (!bm)
return 0;
len = bitmap_word (bm->len);
for (i = 0; i < len; ++i) {
__bitset = bm->bmp[i];
if ((pos = __builtin_ffs (__bitset)))
break;
}
return (BITS_PER_WORD * i) + pos;
}
bitmap *
bitmap_create (uint32_t bit) {
bitmap *bm = xcalloc (1, sizeof (bitmap));
uint32_t bytes = bitmap_sizeof (bit);
bm->bmp = xmalloc (bytes);
memset (bm->bmp, 0, bytes);
bm->len = bit;
return bm;
}
int
bitmap_realloc (bitmap * bm, uint32_t bit) {
uint32_t *tmp = NULL;
uint32_t oldlen = 0, newlen = 0;
newlen = bitmap_sizeof (bit);
oldlen = bitmap_sizeof (bm->len);
if (newlen <= oldlen)
return 1;
tmp = realloc (bm->bmp, newlen);
if ((tmp == NULL && newlen > 0) || bit < bm->len)
FATAL ("Unable to realloc bitmap hash value %u %u", newlen, bm->len);
LOG_DEBUG (("bit: %d, bm->len: %d, oldlen: %d, newlen: %d\n", bit, bm->len, oldlen, newlen));
memset (tmp + bitmap_word (bm->len), 0, (newlen - oldlen));
bm->len = bit;
bm->bmp = tmp;
return 0;
}
bitmap *
bitmap_copy (const bitmap * bm) {
bitmap *ret;
if (!bm)
return NULL;
if (!(ret = bitmap_create (bm->len)))
return NULL;
memcpy (ret->bmp, bm->bmp, bitmap_sizeof (bm->len));
return ret;
}
int
bitmap_key_exists (bitmap * bm, uint32_t bit) {
if (bm->len < bit)
bitmap_realloc (bm, bit);
/* if bit set, then it's the same visitor */
if (bitmap_get_bit (bm->bmp, bit - 1))
return 1;
bitmap_set_bit (bm->bmp, bit - 1);
return 0;
}