]>
git.proxmox.com Git - mirror_ovs.git/blob - lib/bitmap.h
2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
24 #define BITMAP_ULONG_BITS (sizeof(unsigned long) * CHAR_BIT)
26 static inline unsigned long *
27 bitmap_unit__(const unsigned long *bitmap
, size_t offset
)
29 return CONST_CAST(unsigned long *, &bitmap
[offset
/ BITMAP_ULONG_BITS
]);
32 static inline unsigned long
33 bitmap_bit__(size_t offset
)
35 return 1UL << (offset
% BITMAP_ULONG_BITS
);
38 #define BITMAP_N_LONGS(N_BITS) DIV_ROUND_UP(N_BITS, BITMAP_ULONG_BITS)
41 bitmap_n_longs(size_t n_bits
)
43 return BITMAP_N_LONGS(n_bits
);
47 bitmap_n_bytes(size_t n_bits
)
49 return bitmap_n_longs(n_bits
) * sizeof(unsigned long int);
52 static inline unsigned long *
53 bitmap_allocate(size_t n_bits
)
55 return xzalloc(bitmap_n_bytes(n_bits
));
58 unsigned long *bitmap_allocate1(size_t n_bits
);
60 static inline unsigned long *
61 bitmap_clone(const unsigned long *bitmap
, size_t n_bits
)
63 return xmemdup(bitmap
, bitmap_n_bytes(n_bits
));
67 bitmap_free(unsigned long *bitmap
)
73 bitmap_is_set(const unsigned long *bitmap
, size_t offset
)
75 return (*bitmap_unit__(bitmap
, offset
) & bitmap_bit__(offset
)) != 0;
79 bitmap_set1(unsigned long *bitmap
, size_t offset
)
81 *bitmap_unit__(bitmap
, offset
) |= bitmap_bit__(offset
);
85 bitmap_set0(unsigned long *bitmap
, size_t offset
)
87 *bitmap_unit__(bitmap
, offset
) &= ~bitmap_bit__(offset
);
91 bitmap_set(unsigned long *bitmap
, size_t offset
, bool value
)
94 bitmap_set1(bitmap
, offset
);
96 bitmap_set0(bitmap
, offset
);
100 void bitmap_set_multiple(unsigned long *, size_t start
, size_t count
,
102 bool bitmap_equal(const unsigned long *, const unsigned long *, size_t n
);
103 size_t bitmap_scan(const unsigned long int *, size_t start
, size_t end
);
105 #define BITMAP_FOR_EACH_1(IDX, SIZE, BITMAP) \
106 for ((IDX) = bitmap_scan(BITMAP, 0, SIZE); (IDX) < (SIZE); \
107 (IDX) = bitmap_scan(BITMAP, (IDX) + 1, SIZE))
109 #endif /* bitmap.h */