Path: blob/main/sys/contrib/ck/include/ck_bitmap.h
48254 views
/*1* Copyright 2012-2015 Samy Al Bahra.2* Copyright 2012-2014 AppNexus, Inc.3* Copyright 2014 Paul Khuong.4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/2728#ifndef CK_BITMAP_H29#define CK_BITMAP_H3031#include <ck_cc.h>32#include <ck_limits.h>33#include <ck_pr.h>34#include <ck_stdint.h>35#include <ck_stdbool.h>36#include <ck_stddef.h>37#include <ck_stdbool.h>38#include <ck_stddef.h>39#include <ck_string.h>4041#if !defined(CK_F_PR_LOAD_UINT) || !defined(CK_F_PR_STORE_UINT) || \42!defined(CK_F_PR_AND_UINT) || !defined(CK_F_PR_OR_UINT) || \43!defined(CK_F_CC_CTZ)44#error "ck_bitmap is not supported on your platform."45#endif4647#define CK_BITMAP_BLOCK (sizeof(unsigned int) * CHAR_BIT)48#define CK_BITMAP_OFFSET(i) ((i) % CK_BITMAP_BLOCK)49#define CK_BITMAP_BIT(i) (1U << CK_BITMAP_OFFSET(i))50#define CK_BITMAP_PTR(x, i) ((x) + ((i) / CK_BITMAP_BLOCK))51#define CK_BITMAP_BLOCKS(n) (((n) + CK_BITMAP_BLOCK - 1) / CK_BITMAP_BLOCK)5253#define CK_BITMAP_INSTANCE(n_entries) \54union { \55struct { \56unsigned int n_bits; \57unsigned int map[CK_BITMAP_BLOCKS(n_entries)]; \58} content; \59struct ck_bitmap bitmap; \60}6162#define CK_BITMAP_ITERATOR_INIT(a, b) \63ck_bitmap_iterator_init((a), &(b)->bitmap)6465#define CK_BITMAP_INIT(a, b, c) \66ck_bitmap_init(&(a)->bitmap, (b), (c))6768#define CK_BITMAP_NEXT(a, b, c) \69ck_bitmap_next(&(a)->bitmap, (b), (c))7071#define CK_BITMAP_SET(a, b) \72ck_bitmap_set(&(a)->bitmap, (b))7374#define CK_BITMAP_BTS(a, b) \75ck_bitmap_bts(&(a)->bitmap, (b))7677#define CK_BITMAP_RESET(a, b) \78ck_bitmap_reset(&(a)->bitmap, (b))7980#define CK_BITMAP_TEST(a, b) \81ck_bitmap_test(&(a)->bitmap, (b))8283#define CK_BITMAP_UNION(a, b) \84ck_bitmap_union(&(a)->bitmap, &(b)->bitmap)8586#define CK_BITMAP_INTERSECTION(a, b) \87ck_bitmap_intersection(&(a)->bitmap, &(b)->bitmap)8889#define CK_BITMAP_INTERSECTION_NEGATE(a, b) \90ck_bitmap_intersection_negate(&(a)->bitmap, &(b)->bitmap)9192#define CK_BITMAP_CLEAR(a) \93ck_bitmap_clear(&(a)->bitmap)9495#define CK_BITMAP_EMPTY(a, b) \96ck_bitmap_empty(&(a)->bitmap, b)9798#define CK_BITMAP_FULL(a, b) \99ck_bitmap_full(&(a)->bitmap, b)100101#define CK_BITMAP_COUNT(a, b) \102ck_bitmap_count(&(a)->bitmap, b)103104#define CK_BITMAP_COUNT_INTERSECT(a, b, c) \105ck_bitmap_count_intersect(&(a)->bitmap, b, c)106107#define CK_BITMAP_BITS(a) \108ck_bitmap_bits(&(a)->bitmap)109110#define CK_BITMAP_BUFFER(a) \111ck_bitmap_buffer(&(a)->bitmap)112113#define CK_BITMAP(a) \114(&(a)->bitmap)115116struct ck_bitmap {117unsigned int n_bits;118unsigned int map[];119};120typedef struct ck_bitmap ck_bitmap_t;121122struct ck_bitmap_iterator {123unsigned int cache;124unsigned int n_block;125unsigned int n_limit;126};127typedef struct ck_bitmap_iterator ck_bitmap_iterator_t;128129CK_CC_INLINE static unsigned int130ck_bitmap_base(unsigned int n_bits)131{132133return CK_BITMAP_BLOCKS(n_bits) * sizeof(unsigned int);134}135136/*137* Returns the required number of bytes for a ck_bitmap_t object supporting the138* specified number of bits.139*/140CK_CC_INLINE static unsigned int141ck_bitmap_size(unsigned int n_bits)142{143144return ck_bitmap_base(n_bits) + sizeof(struct ck_bitmap);145}146147/*148* Returns total number of bits in specified bitmap.149*/150CK_CC_INLINE static unsigned int151ck_bitmap_bits(const struct ck_bitmap *bitmap)152{153154return bitmap->n_bits;155}156157/*158* Returns a pointer to the bit buffer associated159* with the specified bitmap.160*/161CK_CC_INLINE static void *162ck_bitmap_buffer(struct ck_bitmap *bitmap)163{164165return bitmap->map;166}167168/*169* Sets the bit at the offset specified in the second argument.170*/171CK_CC_INLINE static void172ck_bitmap_set(struct ck_bitmap *bitmap, unsigned int n)173{174175ck_pr_or_uint(CK_BITMAP_PTR(bitmap->map, n), CK_BITMAP_BIT(n));176return;177}178179/*180* Performs a test-and-set operation at the offset specified in the181* second argument.182* Returns true if the bit at the specified offset was already set,183* false otherwise.184*/185CK_CC_INLINE static bool186ck_bitmap_bts(struct ck_bitmap *bitmap, unsigned int n)187{188189return ck_pr_bts_uint(CK_BITMAP_PTR(bitmap->map, n),190CK_BITMAP_OFFSET(n));191}192193/*194* Resets the bit at the offset specified in the second argument.195*/196CK_CC_INLINE static void197ck_bitmap_reset(struct ck_bitmap *bitmap, unsigned int n)198{199200ck_pr_and_uint(CK_BITMAP_PTR(bitmap->map, n), ~CK_BITMAP_BIT(n));201return;202}203204/*205* Determines whether the bit at offset specified in the206* second argument is set.207*/208CK_CC_INLINE static bool209ck_bitmap_test(const struct ck_bitmap *bitmap, unsigned int n)210{211unsigned int block;212213block = ck_pr_load_uint(CK_BITMAP_PTR(bitmap->map, n));214return block & CK_BITMAP_BIT(n);215}216217/*218* Combines bits from second bitmap into the first bitmap. This is not a219* linearized operation with respect to the complete bitmap.220*/221CK_CC_INLINE static void222ck_bitmap_union(struct ck_bitmap *dst, const struct ck_bitmap *src)223{224unsigned int n;225unsigned int n_buckets = dst->n_bits;226227if (src->n_bits < dst->n_bits)228n_buckets = src->n_bits;229230n_buckets = CK_BITMAP_BLOCKS(n_buckets);231for (n = 0; n < n_buckets; n++) {232ck_pr_or_uint(&dst->map[n],233ck_pr_load_uint(&src->map[n]));234}235236return;237}238239/*240* Intersects bits from second bitmap into the first bitmap. This is241* not a linearized operation with respect to the complete bitmap.242* Any trailing bit in dst is cleared.243*/244CK_CC_INLINE static void245ck_bitmap_intersection(struct ck_bitmap *dst, const struct ck_bitmap *src)246{247unsigned int n;248unsigned int n_buckets = dst->n_bits;249unsigned int n_intersect = n_buckets;250251if (src->n_bits < n_intersect)252n_intersect = src->n_bits;253254n_buckets = CK_BITMAP_BLOCKS(n_buckets);255n_intersect = CK_BITMAP_BLOCKS(n_intersect);256for (n = 0; n < n_intersect; n++) {257ck_pr_and_uint(&dst->map[n],258ck_pr_load_uint(&src->map[n]));259}260261for (; n < n_buckets; n++)262ck_pr_store_uint(&dst->map[n], 0);263264return;265}266267/*268* Intersects the complement of bits from second bitmap into the first269* bitmap. This is not a linearized operation with respect to the270* complete bitmap. Any trailing bit in dst is left as is.271*/272CK_CC_INLINE static void273ck_bitmap_intersection_negate(struct ck_bitmap *dst,274const struct ck_bitmap *src)275{276unsigned int n;277unsigned int n_intersect = dst->n_bits;278279if (src->n_bits < n_intersect)280n_intersect = src->n_bits;281282n_intersect = CK_BITMAP_BLOCKS(n_intersect);283for (n = 0; n < n_intersect; n++) {284ck_pr_and_uint(&dst->map[n],285(~ck_pr_load_uint(&src->map[n])));286}287288return;289}290291/*292* Resets all bits in the provided bitmap. This is not a linearized293* operation in ck_bitmap.294*/295CK_CC_INLINE static void296ck_bitmap_clear(struct ck_bitmap *bitmap)297{298unsigned int i;299unsigned int n_buckets = ck_bitmap_base(bitmap->n_bits) /300sizeof(unsigned int);301302for (i = 0; i < n_buckets; i++)303ck_pr_store_uint(&bitmap->map[i], 0);304305return;306}307308/*309* Returns true if the first limit bits in bitmap are cleared. If310* limit is greater than the bitmap size, limit is truncated to that311* size.312*/313CK_CC_INLINE static bool314ck_bitmap_empty(const ck_bitmap_t *bitmap, unsigned int limit)315{316unsigned int i, words, slop;317318if (limit > bitmap->n_bits)319limit = bitmap->n_bits;320321words = limit / CK_BITMAP_BLOCK;322slop = limit % CK_BITMAP_BLOCK;323for (i = 0; i < words; i++) {324if (ck_pr_load_uint(&bitmap->map[i]) != 0) {325return false;326}327}328329if (slop > 0) {330unsigned int word;331332word = ck_pr_load_uint(&bitmap->map[i]);333if ((word & ((1U << slop) - 1)) != 0)334return false;335}336337return true;338}339340/*341* Returns true if the first limit bits in bitmap are set. If limit342* is greater than the bitmap size, limit is truncated to that size.343*/344CK_CC_UNUSED static bool345ck_bitmap_full(const ck_bitmap_t *bitmap, unsigned int limit)346{347unsigned int i, slop, words;348349if (limit > bitmap->n_bits) {350limit = bitmap->n_bits;351}352353words = limit / CK_BITMAP_BLOCK;354slop = limit % CK_BITMAP_BLOCK;355for (i = 0; i < words; i++) {356if (ck_pr_load_uint(&bitmap->map[i]) != -1U)357return false;358}359360if (slop > 0) {361unsigned int word;362363word = ~ck_pr_load_uint(&bitmap->map[i]);364if ((word & ((1U << slop) - 1)) != 0)365return false;366}367return true;368}369370/*371* Returns the number of set bit in bitmap, upto (and excluding)372* limit. If limit is greater than the bitmap size, it is truncated373* to that size.374*/375CK_CC_INLINE static unsigned int376ck_bitmap_count(const ck_bitmap_t *bitmap, unsigned int limit)377{378unsigned int count, i, slop, words;379380if (limit > bitmap->n_bits)381limit = bitmap->n_bits;382383words = limit / CK_BITMAP_BLOCK;384slop = limit % CK_BITMAP_BLOCK;385for (i = 0, count = 0; i < words; i++)386count += ck_cc_popcount(ck_pr_load_uint(&bitmap->map[i]));387388if (slop > 0) {389unsigned int word;390391word = ck_pr_load_uint(&bitmap->map[i]);392count += ck_cc_popcount(word & ((1U << slop) - 1));393}394return count;395}396397/*398* Returns the number of set bit in the intersection of two bitmaps,399* upto (and excluding) limit. If limit is greater than either bitmap400* size, it is truncated to the smallest.401*/402CK_CC_INLINE static unsigned int403ck_bitmap_count_intersect(const ck_bitmap_t *x, const ck_bitmap_t *y,404unsigned int limit)405{406unsigned int count, i, slop, words;407408if (limit > x->n_bits)409limit = x->n_bits;410411if (limit > y->n_bits)412limit = y->n_bits;413414words = limit / CK_BITMAP_BLOCK;415slop = limit % CK_BITMAP_BLOCK;416for (i = 0, count = 0; i < words; i++) {417unsigned int xi, yi;418419xi = ck_pr_load_uint(&x->map[i]);420yi = ck_pr_load_uint(&y->map[i]);421count += ck_cc_popcount(xi & yi);422}423424if (slop > 0) {425unsigned int word, xi, yi;426427xi = ck_pr_load_uint(&x->map[i]);428yi = ck_pr_load_uint(&y->map[i]);429word = xi & yi;430count += ck_cc_popcount(word & ((1U << slop) - 1));431}432return count;433}434435/*436* Initializes a ck_bitmap pointing to a region of memory with437* ck_bitmap_size(n_bits) bytes. Third argument determines whether438* default bit value is 1 (true) or 0 (false).439*/440CK_CC_INLINE static void441ck_bitmap_init(struct ck_bitmap *bitmap,442unsigned int n_bits,443bool set)444{445unsigned int base = ck_bitmap_base(n_bits);446447bitmap->n_bits = n_bits;448memset(bitmap->map, -(int)set, base);449450if (set == true) {451unsigned int b = n_bits % CK_BITMAP_BLOCK;452453if (b == 0)454return;455456*CK_BITMAP_PTR(bitmap->map, n_bits - 1) &= (1U << b) - 1U;457}458459return;460}461462/*463* Initialize iterator for use with provided bitmap.464*/465CK_CC_INLINE static void466ck_bitmap_iterator_init(struct ck_bitmap_iterator *i,467const struct ck_bitmap *bitmap)468{469470i->n_block = 0;471i->n_limit = CK_BITMAP_BLOCKS(bitmap->n_bits);472if (i->n_limit > 0) {473i->cache = ck_pr_load_uint(&bitmap->map[0]);474} else {475i->cache = 0;476}477return;478}479480/*481* Iterate to next bit.482*/483CK_CC_INLINE static bool484ck_bitmap_next(const struct ck_bitmap *bitmap,485struct ck_bitmap_iterator *i,486unsigned int *bit)487{488unsigned int cache = i->cache;489unsigned int n_block = i->n_block;490unsigned int n_limit = i->n_limit;491492if (cache == 0) {493if (n_block >= n_limit)494return false;495496for (n_block++; n_block < n_limit; n_block++) {497cache = ck_pr_load_uint(&bitmap->map[n_block]);498if (cache != 0)499goto non_zero;500}501502i->cache = 0;503i->n_block = n_block;504return false;505}506507non_zero:508*bit = CK_BITMAP_BLOCK * n_block + ck_cc_ctz(cache);509i->cache = cache & (cache - 1);510i->n_block = n_block;511return true;512}513514#endif /* CK_BITMAP_H */515516517