1/* Copyright (c) 2001, 2011, Oracle and/or its affiliates.
2 Copyright (c) 2009-2011, Monty Program Ab
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
16
17#ifndef _my_bitmap_h_
18#define _my_bitmap_h_
19
20#define MY_BIT_NONE (~(uint) 0)
21
22#include <m_string.h>
23#include <my_pthread.h>
24
25typedef uint32 my_bitmap_map;
26
27typedef struct st_bitmap
28{
29 my_bitmap_map *bitmap;
30 my_bitmap_map *last_word_ptr;
31 /*
32 mutex will be acquired for the duration of each bitmap operation if
33 thread_safe flag in bitmap_init was set. Otherwise, we optimize by not
34 acquiring the mutex
35 */
36 mysql_mutex_t *mutex;
37 my_bitmap_map last_word_mask;
38 uint32 n_bits; /* number of bits occupied by the above */
39} MY_BITMAP;
40
41#ifdef __cplusplus
42extern "C" {
43#endif
44
45/* compatibility functions */
46#define bitmap_init(A,B,C,D) my_bitmap_init(A,B,C,D)
47#define bitmap_free(A) my_bitmap_free(A)
48/* Reset memory. Faster then doing a full bzero */
49#define my_bitmap_clear(A) ((A)->bitmap= 0)
50
51extern void create_last_word_mask(MY_BITMAP *map);
52extern my_bool my_bitmap_init(MY_BITMAP *map, my_bitmap_map *buf, uint n_bits,
53 my_bool thread_safe);
54extern my_bool bitmap_is_clear_all(const MY_BITMAP *map);
55extern my_bool bitmap_is_prefix(const MY_BITMAP *map, uint prefix_size);
56extern my_bool bitmap_is_set_all(const MY_BITMAP *map);
57extern my_bool bitmap_is_subset(const MY_BITMAP *map1, const MY_BITMAP *map2);
58extern my_bool bitmap_is_overlapping(const MY_BITMAP *map1,
59 const MY_BITMAP *map2);
60extern my_bool bitmap_test_and_set(MY_BITMAP *map, uint bitmap_bit);
61extern my_bool bitmap_test_and_clear(MY_BITMAP *map, uint bitmap_bit);
62extern my_bool bitmap_fast_test_and_set(MY_BITMAP *map, uint bitmap_bit);
63extern my_bool bitmap_fast_test_and_clear(MY_BITMAP *map, uint bitmap_bit);
64extern my_bool bitmap_union_is_set_all(const MY_BITMAP *map1,
65 const MY_BITMAP *map2);
66extern my_bool bitmap_exists_intersection(const MY_BITMAP **bitmap_array,
67 uint bitmap_count,
68 uint start_bit, uint end_bit);
69
70extern uint bitmap_set_next(MY_BITMAP *map);
71extern uint bitmap_get_first(const MY_BITMAP *map);
72extern uint bitmap_get_first_set(const MY_BITMAP *map);
73extern uint bitmap_bits_set(const MY_BITMAP *map);
74extern uint bitmap_get_next_set(const MY_BITMAP *map, uint bitmap_bit);
75extern void my_bitmap_free(MY_BITMAP *map);
76extern void bitmap_set_above(MY_BITMAP *map, uint from_byte, uint use_bit);
77extern void bitmap_set_prefix(MY_BITMAP *map, uint prefix_size);
78extern void bitmap_intersect(MY_BITMAP *map, const MY_BITMAP *map2);
79extern void bitmap_subtract(MY_BITMAP *map, const MY_BITMAP *map2);
80extern void bitmap_union(MY_BITMAP *map, const MY_BITMAP *map2);
81extern void bitmap_xor(MY_BITMAP *map, const MY_BITMAP *map2);
82extern void bitmap_invert(MY_BITMAP *map);
83extern void bitmap_copy(MY_BITMAP *map, const MY_BITMAP *map2);
84
85extern uint bitmap_lock_set_next(MY_BITMAP *map);
86extern void bitmap_lock_clear_bit(MY_BITMAP *map, uint bitmap_bit);
87/* Fast, not thread safe, bitmap functions */
88#define bitmap_buffer_size(bits) (((bits)+31)/32)*4
89#define no_bytes_in_map(map) (((map)->n_bits + 7)/8)
90#define no_words_in_map(map) (((map)->n_bits + 31)/32)
91#define bytes_word_aligned(bytes) (4*((bytes + 3)/4))
92#define _bitmap_set_bit(MAP, BIT) (((uchar*)(MAP)->bitmap)[(BIT) / 8] \
93 |= (1 << ((BIT) & 7)))
94#define _bitmap_flip_bit(MAP, BIT) (((uchar*)(MAP)->bitmap)[(BIT) / 8] \
95 ^= (1 << ((BIT) & 7)))
96#define _bitmap_clear_bit(MAP, BIT) (((uchar*)(MAP)->bitmap)[(BIT) / 8] \
97 &= ~ (1 << ((BIT) & 7)))
98#define _bitmap_is_set(MAP, BIT) (uint) (((uchar*)(MAP)->bitmap)[(BIT) / 8] \
99 & (1 << ((BIT) & 7)))
100/*
101 WARNING!
102
103 The below symbols are inline functions in DEBUG builds and macros in
104 non-DEBUG builds. The latter evaluate their 'bit' argument twice.
105
106 NEVER use an increment/decrement operator with the 'bit' argument.
107 It would work with DEBUG builds, but fails later in production builds!
108
109 FORBIDDEN: bitmap_set_bit($my_bitmap, (field++)->field_index);
110*/
111#ifndef DBUG_OFF
112static inline void
113bitmap_set_bit(MY_BITMAP *map,uint bit)
114{
115 DBUG_ASSERT(bit < (map)->n_bits);
116 _bitmap_set_bit(map,bit);
117}
118static inline void
119bitmap_flip_bit(MY_BITMAP *map,uint bit)
120{
121 DBUG_ASSERT(bit < (map)->n_bits);
122 _bitmap_flip_bit(map,bit);
123}
124static inline void
125bitmap_clear_bit(MY_BITMAP *map,uint bit)
126{
127 DBUG_ASSERT(bit < (map)->n_bits);
128 _bitmap_clear_bit(map,bit);
129}
130static inline uint
131bitmap_is_set(const MY_BITMAP *map,uint bit)
132{
133 DBUG_ASSERT(bit < (map)->n_bits);
134 return _bitmap_is_set(map,bit);
135}
136#else
137#define bitmap_set_bit(MAP, BIT) _bitmap_set_bit(MAP, BIT)
138#define bitmap_flip_bit(MAP, BIT) _bitmap_flip_bit(MAP, BIT)
139#define bitmap_clear_bit(MAP, BIT) _bitmap_clear_bit(MAP, BIT)
140#define bitmap_is_set(MAP, BIT) _bitmap_is_set(MAP, BIT)
141#endif
142
143static inline my_bool bitmap_cmp(const MY_BITMAP *map1, const MY_BITMAP *map2)
144{
145 if (memcmp(map1->bitmap, map2->bitmap, 4*(no_words_in_map(map1)-1)) != 0)
146 return FALSE;
147 return ((*map1->last_word_ptr | map1->last_word_mask) ==
148 (*map2->last_word_ptr | map2->last_word_mask));
149}
150
151#define bitmap_clear_all(MAP) \
152 { memset((MAP)->bitmap, 0, 4*no_words_in_map((MAP))); }
153#define bitmap_set_all(MAP) \
154 (memset((MAP)->bitmap, 0xFF, 4*no_words_in_map((MAP))))
155
156#ifdef __cplusplus
157}
158#endif
159
160#endif /* _my_bitmap_h_ */
161