Path: blob/main/sys/compat/linuxkpi/common/include/linux/bitmap.h
39604 views
/*1* Copyright (c) 2013-2017 Mellanox Technologies, Ltd.2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice unmodified, this list of conditions, and the following9* disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR15* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES16* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.17* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,18* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT19* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,20* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY21* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT22* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF23* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.24*/2526#ifndef _LINUXKPI_LINUX_BITMAP_H_27#define _LINUXKPI_LINUX_BITMAP_H_2829#include <linux/bitops.h>30#include <linux/slab.h>3132static inline void33bitmap_zero(unsigned long *addr, const unsigned int size)34{35memset(addr, 0, BITS_TO_LONGS(size) * sizeof(long));36}3738static inline void39bitmap_fill(unsigned long *addr, const unsigned int size)40{41const unsigned int tail = size & (BITS_PER_LONG - 1);4243memset(addr, 0xff, BIT_WORD(size) * sizeof(long));4445if (tail)46addr[BIT_WORD(size)] = BITMAP_LAST_WORD_MASK(tail);47}4849static inline int50bitmap_full(unsigned long *addr, const unsigned int size)51{52const unsigned int end = BIT_WORD(size);53const unsigned int tail = size & (BITS_PER_LONG - 1);54unsigned int i;5556for (i = 0; i != end; i++) {57if (addr[i] != ~0UL)58return (0);59}6061if (tail) {62const unsigned long mask = BITMAP_LAST_WORD_MASK(tail);6364if ((addr[end] & mask) != mask)65return (0);66}67return (1);68}6970static inline int71bitmap_empty(unsigned long *addr, const unsigned int size)72{73const unsigned int end = BIT_WORD(size);74const unsigned int tail = size & (BITS_PER_LONG - 1);75unsigned int i;7677for (i = 0; i != end; i++) {78if (addr[i] != 0)79return (0);80}8182if (tail) {83const unsigned long mask = BITMAP_LAST_WORD_MASK(tail);8485if ((addr[end] & mask) != 0)86return (0);87}88return (1);89}9091static inline void92bitmap_set(unsigned long *map, unsigned int start, int nr)93{94const unsigned int size = start + nr;95int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);96unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);9798map += BIT_WORD(start);99100while (nr - bits_to_set >= 0) {101*map |= mask_to_set;102nr -= bits_to_set;103bits_to_set = BITS_PER_LONG;104mask_to_set = ~0UL;105map++;106}107108if (nr) {109mask_to_set &= BITMAP_LAST_WORD_MASK(size);110*map |= mask_to_set;111}112}113114static inline void115bitmap_clear(unsigned long *map, unsigned int start, int nr)116{117const unsigned int size = start + nr;118int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);119unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);120121map += BIT_WORD(start);122123while (nr - bits_to_clear >= 0) {124*map &= ~mask_to_clear;125nr -= bits_to_clear;126bits_to_clear = BITS_PER_LONG;127mask_to_clear = ~0UL;128map++;129}130131if (nr) {132mask_to_clear &= BITMAP_LAST_WORD_MASK(size);133*map &= ~mask_to_clear;134}135}136137static inline unsigned int138bitmap_find_next_zero_area_off(const unsigned long *map,139const unsigned int size, unsigned int start,140unsigned int nr, unsigned int align_mask,141unsigned int align_offset)142{143unsigned int index;144unsigned int end;145unsigned int i;146147retry:148index = find_next_zero_bit(map, size, start);149150index = (((index + align_offset) + align_mask) & ~align_mask) - align_offset;151152end = index + nr;153if (end > size)154return (end);155156i = find_next_bit(map, end, index);157if (i < end) {158start = i + 1;159goto retry;160}161return (index);162}163164static inline unsigned int165bitmap_find_next_zero_area(const unsigned long *map,166const unsigned int size, unsigned int start,167unsigned int nr, unsigned int align_mask)168{169return (bitmap_find_next_zero_area_off(map, size,170start, nr, align_mask, 0));171}172173static inline int174bitmap_find_free_region(unsigned long *bitmap, int bits, int order)175{176int pos;177int end;178179for (pos = 0; (end = pos + (1 << order)) <= bits; pos = end) {180if (!linux_reg_op(bitmap, pos, order, REG_OP_ISFREE))181continue;182linux_reg_op(bitmap, pos, order, REG_OP_ALLOC);183return (pos);184}185return (-ENOMEM);186}187188static inline int189bitmap_allocate_region(unsigned long *bitmap, int pos, int order)190{191if (!linux_reg_op(bitmap, pos, order, REG_OP_ISFREE))192return (-EBUSY);193linux_reg_op(bitmap, pos, order, REG_OP_ALLOC);194return (0);195}196197static inline void198bitmap_release_region(unsigned long *bitmap, int pos, int order)199{200linux_reg_op(bitmap, pos, order, REG_OP_RELEASE);201}202203static inline unsigned int204bitmap_weight(unsigned long *addr, const unsigned int size)205{206const unsigned int end = BIT_WORD(size);207const unsigned int tail = size & (BITS_PER_LONG - 1);208unsigned int retval = 0;209unsigned int i;210211for (i = 0; i != end; i++)212retval += hweight_long(addr[i]);213214if (tail) {215const unsigned long mask = BITMAP_LAST_WORD_MASK(tail);216217retval += hweight_long(addr[end] & mask);218}219return (retval);220}221222static inline int223bitmap_equal(const unsigned long *pa,224const unsigned long *pb, unsigned size)225{226const unsigned int end = BIT_WORD(size);227const unsigned int tail = size & (BITS_PER_LONG - 1);228unsigned int i;229230for (i = 0; i != end; i++) {231if (pa[i] != pb[i])232return (0);233}234235if (tail) {236const unsigned long mask = BITMAP_LAST_WORD_MASK(tail);237238if ((pa[end] ^ pb[end]) & mask)239return (0);240}241return (1);242}243244static inline int245bitmap_subset(const unsigned long *pa,246const unsigned long *pb, unsigned size)247{248const unsigned end = BIT_WORD(size);249const unsigned tail = size & (BITS_PER_LONG - 1);250unsigned i;251252for (i = 0; i != end; i++) {253if (pa[i] & ~pb[i])254return (0);255}256257if (tail) {258const unsigned long mask = BITMAP_LAST_WORD_MASK(tail);259260if (pa[end] & ~pb[end] & mask)261return (0);262}263return (1);264}265266static inline bool267bitmap_intersects(const unsigned long *pa, const unsigned long *pb,268unsigned size)269{270const unsigned end = BIT_WORD(size);271const unsigned tail = size & (BITS_PER_LONG - 1);272unsigned i;273274for (i = 0; i != end; i++)275if (pa[i] & pb[i])276return (true);277278if (tail) {279const unsigned long mask = BITMAP_LAST_WORD_MASK(tail);280281if (pa[end] & pb[end] & mask)282return (true);283}284return (false);285}286287static inline void288bitmap_complement(unsigned long *dst, const unsigned long *src,289const unsigned int size)290{291const unsigned int end = BITS_TO_LONGS(size);292unsigned int i;293294for (i = 0; i != end; i++)295dst[i] = ~src[i];296}297298static inline void299bitmap_copy(unsigned long *dst, const unsigned long *src,300const unsigned int size)301{302const unsigned int end = BITS_TO_LONGS(size);303unsigned int i;304305for (i = 0; i != end; i++)306dst[i] = src[i];307}308309static inline void310bitmap_to_arr32(uint32_t *dst, const unsigned long *src, unsigned int size)311{312const unsigned int end = howmany(size, 32);313314#ifdef __LP64__315unsigned int i = 0;316while (i < end) {317dst[i++] = (uint32_t)(*src & UINT_MAX);318if (i < end)319dst[i++] = (uint32_t)(*src >> 32);320src++;321}322#else323bitmap_copy((unsigned long *)dst, src, size);324#endif325if ((size % 32) != 0) /* Linux uses BITS_PER_LONG. Seems to be a bug */326dst[end - 1] &= (uint32_t)(UINT_MAX >> (32 - (size % 32)));327}328329static inline void330bitmap_from_arr32(unsigned long *dst, const uint32_t *src,331unsigned int size)332{333const unsigned int end = BIT_WORD(size);334const unsigned int tail = size & (BITS_PER_LONG - 1);335336#ifdef __LP64__337const unsigned int end32 = howmany(size, 32);338unsigned int i = 0;339340while (i < end32) {341dst[i++/2] = (unsigned long) *(src++);342if (i < end32)343dst[i++/2] |= ((unsigned long) *(src++)) << 32;344}345#else346bitmap_copy(dst, (const unsigned long *)src, size);347#endif348if ((size % BITS_PER_LONG) != 0)349dst[end] &= BITMAP_LAST_WORD_MASK(tail);350}351352static inline void353bitmap_or(unsigned long *dst, const unsigned long *src1,354const unsigned long *src2, const unsigned int size)355{356const unsigned int end = BITS_TO_LONGS(size);357unsigned int i;358359for (i = 0; i != end; i++)360dst[i] = src1[i] | src2[i];361}362363static inline void364bitmap_and(unsigned long *dst, const unsigned long *src1,365const unsigned long *src2, const unsigned int size)366{367const unsigned int end = BITS_TO_LONGS(size);368unsigned int i;369370for (i = 0; i != end; i++)371dst[i] = src1[i] & src2[i];372}373374static inline void375bitmap_andnot(unsigned long *dst, const unsigned long *src1,376const unsigned long *src2, const unsigned int size)377{378const unsigned int end = BITS_TO_LONGS(size);379unsigned int i;380381for (i = 0; i != end; i++)382dst[i] = src1[i] & ~src2[i];383}384385static inline void386bitmap_xor(unsigned long *dst, const unsigned long *src1,387const unsigned long *src2, const unsigned int size)388{389const unsigned int end = BITS_TO_LONGS(size);390unsigned int i;391392for (i = 0; i != end; i++)393dst[i] = src1[i] ^ src2[i];394}395396static inline void397bitmap_shift_right(unsigned long *dst, const unsigned long *src,398unsigned int shift, unsigned int size)399{400const unsigned int end = BITS_TO_LONGS(size);401const unsigned int tail = size & (BITS_PER_LONG - 1);402const unsigned long mask = BITMAP_LAST_WORD_MASK(tail);403const unsigned int off = BIT_WORD(shift);404const unsigned int rem = shift & (BITS_PER_LONG - 1);405unsigned long left, right;406unsigned int i, srcpos;407408for (i = 0, srcpos = off; srcpos < end; i++, srcpos++) {409right = src[srcpos];410left = 0;411412if (srcpos == end - 1)413right &= mask;414415if (rem != 0) {416right >>= rem;417if (srcpos + 1 < end) {418left = src[srcpos + 1];419if (srcpos + 1 == end - 1)420left &= mask;421left <<= (BITS_PER_LONG - rem);422}423}424dst[i] = left | right;425}426if (off != 0)427memset(dst + end - off, 0, off * sizeof(unsigned long));428}429430static inline unsigned long *431bitmap_alloc(unsigned int size, gfp_t flags)432{433return (kmalloc_array(BITS_TO_LONGS(size),434sizeof(unsigned long), flags));435}436437static inline unsigned long *438bitmap_zalloc(unsigned int size, gfp_t flags)439{440return (bitmap_alloc(size, flags | __GFP_ZERO));441}442443static inline void444bitmap_free(const unsigned long *bitmap)445{446kfree(bitmap);447}448449#endif /* _LINUXKPI_LINUX_BITMAP_H_ */450451452