Subversion Repositories Kolibri OS

Rev

Rev 5056 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * lib/bitmap.c
  3.  * Helper functions for bitmap.h.
  4.  *
  5.  * Tlhis source code is licensed under the GNU General Public License,
  6.  * Version 2.  See the file COPYING for more details.
  7.  */
  8. #include <syscall.h>
  9. #include <linux/export.h>
  10. //#include <linux/thread_info.h>
  11. #include <linux/ctype.h>
  12. #include <linux/errno.h>
  13. #include <linux/bitmap.h>
  14. #include <linux/bitops.h>
  15. #include <linux/bug.h>
  16. //#include <asm/uaccess.h>
  17.  
  18. /*
  19.  * bitmaps provide an array of bits, implemented using an an
  20.  * array of unsigned longs.  The number of valid bits in a
  21.  * given bitmap does _not_ need to be an exact multiple of
  22.  * BITS_PER_LONG.
  23.  *
  24.  * The possible unused bits in the last, partially used word
  25.  * of a bitmap are 'don't care'.  The implementation makes
  26.  * no particular effort to keep them zero.  It ensures that
  27.  * their value will not affect the results of any operation.
  28.  * The bitmap operations that return Boolean (bitmap_empty,
  29.  * for example) or scalar (bitmap_weight, for example) results
  30.  * carefully filter out these unused bits from impacting their
  31.  * results.
  32.  *
  33.  * These operations actually hold to a slightly stronger rule:
  34.  * if you don't input any bitmaps to these ops that have some
  35.  * unused bits set, then they won't output any set unused bits
  36.  * in output bitmaps.
  37.  *
  38.  * The byte ordering of bitmaps is more natural on little
  39.  * endian architectures.  See the big-endian headers
  40.  * include/asm-ppc64/bitops.h and include/asm-s390/bitops.h
  41.  * for the best explanations of this ordering.
  42.  */
  43.  
  44. int __bitmap_empty(const unsigned long *bitmap, int bits)
  45. {
  46.         int k, lim = bits/BITS_PER_LONG;
  47.         for (k = 0; k < lim; ++k)
  48.                 if (bitmap[k])
  49.                         return 0;
  50.  
  51.         if (bits % BITS_PER_LONG)
  52.                 if (bitmap[k] & BITMAP_LAST_WORD_MASK(bits))
  53.                         return 0;
  54.  
  55.         return 1;
  56. }
  57. EXPORT_SYMBOL(__bitmap_empty);
  58.  
  59. int __bitmap_full(const unsigned long *bitmap, int bits)
  60. {
  61.         int k, lim = bits/BITS_PER_LONG;
  62.         for (k = 0; k < lim; ++k)
  63.                 if (~bitmap[k])
  64.                         return 0;
  65.  
  66.         if (bits % BITS_PER_LONG)
  67.                 if (~bitmap[k] & BITMAP_LAST_WORD_MASK(bits))
  68.                         return 0;
  69.  
  70.         return 1;
  71. }
  72. EXPORT_SYMBOL(__bitmap_full);
  73.  
  74. int __bitmap_equal(const unsigned long *bitmap1,
  75.                 const unsigned long *bitmap2, int bits)
  76. {
  77.         int k, lim = bits/BITS_PER_LONG;
  78.         for (k = 0; k < lim; ++k)
  79.                 if (bitmap1[k] != bitmap2[k])
  80.                         return 0;
  81.  
  82.         if (bits % BITS_PER_LONG)
  83.                 if ((bitmap1[k] ^ bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
  84.                         return 0;
  85.  
  86.         return 1;
  87. }
  88. EXPORT_SYMBOL(__bitmap_equal);
  89.  
  90. void __bitmap_complement(unsigned long *dst, const unsigned long *src, int bits)
  91. {
  92.         int k, lim = bits/BITS_PER_LONG;
  93.         for (k = 0; k < lim; ++k)
  94.                 dst[k] = ~src[k];
  95.  
  96.         if (bits % BITS_PER_LONG)
  97.                 dst[k] = ~src[k] & BITMAP_LAST_WORD_MASK(bits);
  98. }
  99. EXPORT_SYMBOL(__bitmap_complement);
  100.  
  101. /**
  102.  * __bitmap_shift_right - logical right shift of the bits in a bitmap
  103.  *   @dst : destination bitmap
  104.  *   @src : source bitmap
  105.  *   @shift : shift by this many bits
  106.  *   @bits : bitmap size, in bits
  107.  *
  108.  * Shifting right (dividing) means moving bits in the MS -> LS bit
  109.  * direction.  Zeros are fed into the vacated MS positions and the
  110.  * LS bits shifted off the bottom are lost.
  111.  */
  112. void __bitmap_shift_right(unsigned long *dst,
  113.                         const unsigned long *src, int shift, int bits)
  114. {
  115.         int k, lim = BITS_TO_LONGS(bits), left = bits % BITS_PER_LONG;
  116.         int off = shift/BITS_PER_LONG, rem = shift % BITS_PER_LONG;
  117.         unsigned long mask = (1UL << left) - 1;
  118.         for (k = 0; off + k < lim; ++k) {
  119.                 unsigned long upper, lower;
  120.  
  121.                 /*
  122.                  * If shift is not word aligned, take lower rem bits of
  123.                  * word above and make them the top rem bits of result.
  124.                  */
  125.                 if (!rem || off + k + 1 >= lim)
  126.                         upper = 0;
  127.                 else {
  128.                         upper = src[off + k + 1];
  129.                         if (off + k + 1 == lim - 1 && left)
  130.                                 upper &= mask;
  131.                 }
  132.                 lower = src[off + k];
  133.                 if (left && off + k == lim - 1)
  134.                         lower &= mask;
  135.                 dst[k] = upper << (BITS_PER_LONG - rem) | lower >> rem;
  136.                 if (left && k == lim - 1)
  137.                         dst[k] &= mask;
  138.         }
  139.         if (off)
  140.                 memset(&dst[lim - off], 0, off*sizeof(unsigned long));
  141. }
  142. EXPORT_SYMBOL(__bitmap_shift_right);
  143.  
  144.  
  145. /**
  146.  * __bitmap_shift_left - logical left shift of the bits in a bitmap
  147.  *   @dst : destination bitmap
  148.  *   @src : source bitmap
  149.  *   @shift : shift by this many bits
  150.  *   @bits : bitmap size, in bits
  151.  *
  152.  * Shifting left (multiplying) means moving bits in the LS -> MS
  153.  * direction.  Zeros are fed into the vacated LS bit positions
  154.  * and those MS bits shifted off the top are lost.
  155.  */
  156.  
  157. void __bitmap_shift_left(unsigned long *dst,
  158.                         const unsigned long *src, int shift, int bits)
  159. {
  160.         int k, lim = BITS_TO_LONGS(bits), left = bits % BITS_PER_LONG;
  161.         int off = shift/BITS_PER_LONG, rem = shift % BITS_PER_LONG;
  162.         for (k = lim - off - 1; k >= 0; --k) {
  163.                 unsigned long upper, lower;
  164.  
  165.                 /*
  166.                  * If shift is not word aligned, take upper rem bits of
  167.                  * word below and make them the bottom rem bits of result.
  168.                  */
  169.                 if (rem && k > 0)
  170.                         lower = src[k - 1];
  171.                 else
  172.                         lower = 0;
  173.                 upper = src[k];
  174.                 if (left && k == lim - 1)
  175.                         upper &= (1UL << left) - 1;
  176.                 dst[k + off] = lower  >> (BITS_PER_LONG - rem) | upper << rem;
  177.                 if (left && k + off == lim - 1)
  178.                         dst[k + off] &= (1UL << left) - 1;
  179.         }
  180.         if (off)
  181.                 memset(dst, 0, off*sizeof(unsigned long));
  182. }
  183. EXPORT_SYMBOL(__bitmap_shift_left);
  184.  
  185. int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
  186.                                 const unsigned long *bitmap2, int bits)
  187. {
  188.         int k;
  189.         int nr = BITS_TO_LONGS(bits);
  190.         unsigned long result = 0;
  191.  
  192.         for (k = 0; k < nr; k++)
  193.                 result |= (dst[k] = bitmap1[k] & bitmap2[k]);
  194.         return result != 0;
  195. }
  196. EXPORT_SYMBOL(__bitmap_and);
  197.  
  198. void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
  199.                                 const unsigned long *bitmap2, int bits)
  200. {
  201.         int k;
  202.         int nr = BITS_TO_LONGS(bits);
  203.  
  204.         for (k = 0; k < nr; k++)
  205.                 dst[k] = bitmap1[k] | bitmap2[k];
  206. }
  207. EXPORT_SYMBOL(__bitmap_or);
  208.  
  209. void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
  210.                                 const unsigned long *bitmap2, int bits)
  211. {
  212.         int k;
  213.         int nr = BITS_TO_LONGS(bits);
  214.  
  215.         for (k = 0; k < nr; k++)
  216.                 dst[k] = bitmap1[k] ^ bitmap2[k];
  217. }
  218. EXPORT_SYMBOL(__bitmap_xor);
  219.  
  220. int __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
  221.                                 const unsigned long *bitmap2, int bits)
  222. {
  223.         int k;
  224.         int nr = BITS_TO_LONGS(bits);
  225.         unsigned long result = 0;
  226.  
  227.         for (k = 0; k < nr; k++)
  228.                 result |= (dst[k] = bitmap1[k] & ~bitmap2[k]);
  229.         return result != 0;
  230. }
  231. EXPORT_SYMBOL(__bitmap_andnot);
  232.  
  233. int __bitmap_intersects(const unsigned long *bitmap1,
  234.                                 const unsigned long *bitmap2, int bits)
  235. {
  236.         int k, lim = bits/BITS_PER_LONG;
  237.         for (k = 0; k < lim; ++k)
  238.                 if (bitmap1[k] & bitmap2[k])
  239.                         return 1;
  240.  
  241.         if (bits % BITS_PER_LONG)
  242.                 if ((bitmap1[k] & bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
  243.                         return 1;
  244.         return 0;
  245. }
  246. EXPORT_SYMBOL(__bitmap_intersects);
  247.  
  248. int __bitmap_subset(const unsigned long *bitmap1,
  249.                                 const unsigned long *bitmap2, int bits)
  250. {
  251.         int k, lim = bits/BITS_PER_LONG;
  252.         for (k = 0; k < lim; ++k)
  253.                 if (bitmap1[k] & ~bitmap2[k])
  254.                         return 0;
  255.  
  256.         if (bits % BITS_PER_LONG)
  257.                 if ((bitmap1[k] & ~bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
  258.                         return 0;
  259.         return 1;
  260. }
  261. EXPORT_SYMBOL(__bitmap_subset);
  262.  
  263. int __bitmap_weight(const unsigned long *bitmap, int bits)
  264. {
  265.         int k, w = 0, lim = bits/BITS_PER_LONG;
  266.  
  267.         for (k = 0; k < lim; k++)
  268.                 w += hweight_long(bitmap[k]);
  269.  
  270.         if (bits % BITS_PER_LONG)
  271.                 w += hweight_long(bitmap[k] & BITMAP_LAST_WORD_MASK(bits));
  272.  
  273.         return w;
  274. }
  275. EXPORT_SYMBOL(__bitmap_weight);
  276.  
  277. void bitmap_set(unsigned long *map, int start, int nr)
  278. {
  279.         unsigned long *p = map + BIT_WORD(start);
  280.         const int size = start + nr;
  281.         int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
  282.         unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
  283.  
  284.         while (nr - bits_to_set >= 0) {
  285.                 *p |= mask_to_set;
  286.                 nr -= bits_to_set;
  287.                 bits_to_set = BITS_PER_LONG;
  288.                 mask_to_set = ~0UL;
  289.                 p++;
  290.         }
  291.         if (nr) {
  292.                 mask_to_set &= BITMAP_LAST_WORD_MASK(size);
  293.                 *p |= mask_to_set;
  294.         }
  295. }
  296. EXPORT_SYMBOL(bitmap_set);
  297.  
  298. void bitmap_clear(unsigned long *map, int start, int nr)
  299. {
  300.         unsigned long *p = map + BIT_WORD(start);
  301.         const int size = start + nr;
  302.         int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
  303.         unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
  304.  
  305.         while (nr - bits_to_clear >= 0) {
  306.                 *p &= ~mask_to_clear;
  307.                 nr -= bits_to_clear;
  308.                 bits_to_clear = BITS_PER_LONG;
  309.                 mask_to_clear = ~0UL;
  310.                 p++;
  311.         }
  312.         if (nr) {
  313.                 mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
  314.                 *p &= ~mask_to_clear;
  315.         }
  316. }
  317. EXPORT_SYMBOL(bitmap_clear);
  318.  
  319. /*
  320.  * bitmap_find_next_zero_area - find a contiguous aligned zero area
  321.  * @map: The address to base the search on
  322.  * @size: The bitmap size in bits
  323.  * @start: The bitnumber to start searching at
  324.  * @nr: The number of zeroed bits we're looking for
  325.  * @align_mask: Alignment mask for zero area
  326.  *
  327.  * The @align_mask should be one less than a power of 2; the effect is that
  328.  * the bit offset of all zero areas this function finds is multiples of that
  329.  * power of 2. A @align_mask of 0 means no alignment is required.
  330.  */
  331. unsigned long bitmap_find_next_zero_area(unsigned long *map,
  332.                                          unsigned long size,
  333.                                          unsigned long start,
  334.                                          unsigned int nr,
  335.                                          unsigned long align_mask)
  336. {
  337.         unsigned long index, end, i;
  338. again:
  339.         index = find_next_zero_bit(map, size, start);
  340.  
  341.         /* Align allocation */
  342.         index = __ALIGN_MASK(index, align_mask);
  343.  
  344.         end = index + nr;
  345.         if (end > size)
  346.                 return end;
  347.         i = find_next_bit(map, end, index);
  348.         if (i < end) {
  349.                 start = i + 1;
  350.                 goto again;
  351.         }
  352.         return index;
  353. }
  354. EXPORT_SYMBOL(bitmap_find_next_zero_area);
  355.  
  356. /*
  357.  * Bitmap printing & parsing functions: first version by Nadia Yvette Chambers,
  358.  * second version by Paul Jackson, third by Joe Korty.
  359.  */
  360.  
  361. #define CHUNKSZ                         32
  362. #define nbits_to_hold_value(val)        fls(val)
  363. #define BASEDEC 10              /* fancier cpuset lists input in decimal */
  364.  
  365.  
  366.  
  367.  
  368.  
  369. /**
  370.  * bitmap_pos_to_ord - find ordinal of set bit at given position in bitmap
  371.  *      @buf: pointer to a bitmap
  372.  *      @pos: a bit position in @buf (0 <= @pos < @bits)
  373.  *      @bits: number of valid bit positions in @buf
  374.  *
  375.  * Map the bit at position @pos in @buf (of length @bits) to the
  376.  * ordinal of which set bit it is.  If it is not set or if @pos
  377.  * is not a valid bit position, map to -1.
  378.  *
  379.  * If for example, just bits 4 through 7 are set in @buf, then @pos
  380.  * values 4 through 7 will get mapped to 0 through 3, respectively,
  381.  * and other @pos values will get mapped to 0.  When @pos value 7
  382.  * gets mapped to (returns) @ord value 3 in this example, that means
  383.  * that bit 7 is the 3rd (starting with 0th) set bit in @buf.
  384.  *
  385.  * The bit positions 0 through @bits are valid positions in @buf.
  386.  */
  387. static int bitmap_pos_to_ord(const unsigned long *buf, int pos, int bits)
  388. {
  389.         int i, ord;
  390.  
  391.         if (pos < 0 || pos >= bits || !test_bit(pos, buf))
  392.                 return -1;
  393.  
  394.         i = find_first_bit(buf, bits);
  395.         ord = 0;
  396.         while (i < pos) {
  397.                 i = find_next_bit(buf, bits, i + 1);
  398.                 ord++;
  399.         }
  400.         BUG_ON(i != pos);
  401.  
  402.         return ord;
  403. }
  404.  
  405. /**
  406.  * bitmap_ord_to_pos - find position of n-th set bit in bitmap
  407.  *      @buf: pointer to bitmap
  408.  *      @ord: ordinal bit position (n-th set bit, n >= 0)
  409.  *      @bits: number of valid bit positions in @buf
  410.  *
  411.  * Map the ordinal offset of bit @ord in @buf to its position in @buf.
  412.  * Value of @ord should be in range 0 <= @ord < weight(buf), else
  413.  * results are undefined.
  414.  *
  415.  * If for example, just bits 4 through 7 are set in @buf, then @ord
  416.  * values 0 through 3 will get mapped to 4 through 7, respectively,
  417.  * and all other @ord values return undefined values.  When @ord value 3
  418.  * gets mapped to (returns) @pos value 7 in this example, that means
  419.  * that the 3rd set bit (starting with 0th) is at position 7 in @buf.
  420.  *
  421.  * The bit positions 0 through @bits are valid positions in @buf.
  422.  */
  423. int bitmap_ord_to_pos(const unsigned long *buf, int ord, int bits)
  424. {
  425.         int pos = 0;
  426.  
  427.         if (ord >= 0 && ord < bits) {
  428.                 int i;
  429.  
  430.                 for (i = find_first_bit(buf, bits);
  431.                      i < bits && ord > 0;
  432.                      i = find_next_bit(buf, bits, i + 1))
  433.                         ord--;
  434.                 if (i < bits && ord == 0)
  435.                         pos = i;
  436.         }
  437.  
  438.         return pos;
  439. }
  440.  
  441. /**
  442.  * bitmap_remap - Apply map defined by a pair of bitmaps to another bitmap
  443.  *      @dst: remapped result
  444.  *      @src: subset to be remapped
  445.  *      @old: defines domain of map
  446.  *      @new: defines range of map
  447.  *      @bits: number of bits in each of these bitmaps
  448.  *
  449.  * Let @old and @new define a mapping of bit positions, such that
  450.  * whatever position is held by the n-th set bit in @old is mapped
  451.  * to the n-th set bit in @new.  In the more general case, allowing
  452.  * for the possibility that the weight 'w' of @new is less than the
  453.  * weight of @old, map the position of the n-th set bit in @old to
  454.  * the position of the m-th set bit in @new, where m == n % w.
  455.  *
  456.  * If either of the @old and @new bitmaps are empty, or if @src and
  457.  * @dst point to the same location, then this routine copies @src
  458.  * to @dst.
  459.  *
  460.  * The positions of unset bits in @old are mapped to themselves
  461.  * (the identify map).
  462.  *
  463.  * Apply the above specified mapping to @src, placing the result in
  464.  * @dst, clearing any bits previously set in @dst.
  465.  *
  466.  * For example, lets say that @old has bits 4 through 7 set, and
  467.  * @new has bits 12 through 15 set.  This defines the mapping of bit
  468.  * position 4 to 12, 5 to 13, 6 to 14 and 7 to 15, and of all other
  469.  * bit positions unchanged.  So if say @src comes into this routine
  470.  * with bits 1, 5 and 7 set, then @dst should leave with bits 1,
  471.  * 13 and 15 set.
  472.  */
  473. void bitmap_remap(unsigned long *dst, const unsigned long *src,
  474.                 const unsigned long *old, const unsigned long *new,
  475.                 int bits)
  476. {
  477.         int oldbit, w;
  478.  
  479.         if (dst == src)         /* following doesn't handle inplace remaps */
  480.                 return;
  481.         bitmap_zero(dst, bits);
  482.  
  483.         w = bitmap_weight(new, bits);
  484.         for_each_set_bit(oldbit, src, bits) {
  485.                 int n = bitmap_pos_to_ord(old, oldbit, bits);
  486.  
  487.                 if (n < 0 || w == 0)
  488.                         set_bit(oldbit, dst);   /* identity map */
  489.                 else
  490.                         set_bit(bitmap_ord_to_pos(new, n % w, bits), dst);
  491.         }
  492. }
  493. EXPORT_SYMBOL(bitmap_remap);
  494.  
  495. /**
  496.  * bitmap_bitremap - Apply map defined by a pair of bitmaps to a single bit
  497.  *      @oldbit: bit position to be mapped
  498.  *      @old: defines domain of map
  499.  *      @new: defines range of map
  500.  *      @bits: number of bits in each of these bitmaps
  501.  *
  502.  * Let @old and @new define a mapping of bit positions, such that
  503.  * whatever position is held by the n-th set bit in @old is mapped
  504.  * to the n-th set bit in @new.  In the more general case, allowing
  505.  * for the possibility that the weight 'w' of @new is less than the
  506.  * weight of @old, map the position of the n-th set bit in @old to
  507.  * the position of the m-th set bit in @new, where m == n % w.
  508.  *
  509.  * The positions of unset bits in @old are mapped to themselves
  510.  * (the identify map).
  511.  *
  512.  * Apply the above specified mapping to bit position @oldbit, returning
  513.  * the new bit position.
  514.  *
  515.  * For example, lets say that @old has bits 4 through 7 set, and
  516.  * @new has bits 12 through 15 set.  This defines the mapping of bit
  517.  * position 4 to 12, 5 to 13, 6 to 14 and 7 to 15, and of all other
  518.  * bit positions unchanged.  So if say @oldbit is 5, then this routine
  519.  * returns 13.
  520.  */
  521. int bitmap_bitremap(int oldbit, const unsigned long *old,
  522.                                 const unsigned long *new, int bits)
  523. {
  524.         int w = bitmap_weight(new, bits);
  525.         int n = bitmap_pos_to_ord(old, oldbit, bits);
  526.         if (n < 0 || w == 0)
  527.                 return oldbit;
  528.         else
  529.                 return bitmap_ord_to_pos(new, n % w, bits);
  530. }
  531. EXPORT_SYMBOL(bitmap_bitremap);
  532.  
  533. /**
  534.  * bitmap_onto - translate one bitmap relative to another
  535.  *      @dst: resulting translated bitmap
  536.  *      @orig: original untranslated bitmap
  537.  *      @relmap: bitmap relative to which translated
  538.  *      @bits: number of bits in each of these bitmaps
  539.  *
  540.  * Set the n-th bit of @dst iff there exists some m such that the
  541.  * n-th bit of @relmap is set, the m-th bit of @orig is set, and
  542.  * the n-th bit of @relmap is also the m-th _set_ bit of @relmap.
  543.  * (If you understood the previous sentence the first time your
  544.  * read it, you're overqualified for your current job.)
  545.  *
  546.  * In other words, @orig is mapped onto (surjectively) @dst,
  547.  * using the the map { <n, m> | the n-th bit of @relmap is the
  548.  * m-th set bit of @relmap }.
  549.  *
  550.  * Any set bits in @orig above bit number W, where W is the
  551.  * weight of (number of set bits in) @relmap are mapped nowhere.
  552.  * In particular, if for all bits m set in @orig, m >= W, then
  553.  * @dst will end up empty.  In situations where the possibility
  554.  * of such an empty result is not desired, one way to avoid it is
  555.  * to use the bitmap_fold() operator, below, to first fold the
  556.  * @orig bitmap over itself so that all its set bits x are in the
  557.  * range 0 <= x < W.  The bitmap_fold() operator does this by
  558.  * setting the bit (m % W) in @dst, for each bit (m) set in @orig.
  559.  *
  560.  * Example [1] for bitmap_onto():
  561.  *  Let's say @relmap has bits 30-39 set, and @orig has bits
  562.  *  1, 3, 5, 7, 9 and 11 set.  Then on return from this routine,
  563.  *  @dst will have bits 31, 33, 35, 37 and 39 set.
  564.  *
  565.  *  When bit 0 is set in @orig, it means turn on the bit in
  566.  *  @dst corresponding to whatever is the first bit (if any)
  567.  *  that is turned on in @relmap.  Since bit 0 was off in the
  568.  *  above example, we leave off that bit (bit 30) in @dst.
  569.  *
  570.  *  When bit 1 is set in @orig (as in the above example), it
  571.  *  means turn on the bit in @dst corresponding to whatever
  572.  *  is the second bit that is turned on in @relmap.  The second
  573.  *  bit in @relmap that was turned on in the above example was
  574.  *  bit 31, so we turned on bit 31 in @dst.
  575.  *
  576.  *  Similarly, we turned on bits 33, 35, 37 and 39 in @dst,
  577.  *  because they were the 4th, 6th, 8th and 10th set bits
  578.  *  set in @relmap, and the 4th, 6th, 8th and 10th bits of
  579.  *  @orig (i.e. bits 3, 5, 7 and 9) were also set.
  580.  *
  581.  *  When bit 11 is set in @orig, it means turn on the bit in
  582.  *  @dst corresponding to whatever is the twelfth bit that is
  583.  *  turned on in @relmap.  In the above example, there were
  584.  *  only ten bits turned on in @relmap (30..39), so that bit
  585.  *  11 was set in @orig had no affect on @dst.
  586.  *
  587.  * Example [2] for bitmap_fold() + bitmap_onto():
  588.  *  Let's say @relmap has these ten bits set:
  589.  *              40 41 42 43 45 48 53 61 74 95
  590.  *  (for the curious, that's 40 plus the first ten terms of the
  591.  *  Fibonacci sequence.)
  592.  *
  593.  *  Further lets say we use the following code, invoking
  594.  *  bitmap_fold() then bitmap_onto, as suggested above to
  595.  *  avoid the possitility of an empty @dst result:
  596.  *
  597.  *      unsigned long *tmp;     // a temporary bitmap's bits
  598.  *
  599.  *      bitmap_fold(tmp, orig, bitmap_weight(relmap, bits), bits);
  600.  *      bitmap_onto(dst, tmp, relmap, bits);
  601.  *
  602.  *  Then this table shows what various values of @dst would be, for
  603.  *  various @orig's.  I list the zero-based positions of each set bit.
  604.  *  The tmp column shows the intermediate result, as computed by
  605.  *  using bitmap_fold() to fold the @orig bitmap modulo ten
  606.  *  (the weight of @relmap).
  607.  *
  608.  *      @orig           tmp            @dst
  609.  *      0                0             40
  610.  *      1                1             41
  611.  *      9                9             95
  612.  *      10               0             40 (*)
  613.  *      1 3 5 7          1 3 5 7       41 43 48 61
  614.  *      0 1 2 3 4        0 1 2 3 4     40 41 42 43 45
  615.  *      0 9 18 27        0 9 8 7       40 61 74 95
  616.  *      0 10 20 30       0             40
  617.  *      0 11 22 33       0 1 2 3       40 41 42 43
  618.  *      0 12 24 36       0 2 4 6       40 42 45 53
  619.  *      78 102 211       1 2 8         41 42 74 (*)
  620.  *
  621.  * (*) For these marked lines, if we hadn't first done bitmap_fold()
  622.  *     into tmp, then the @dst result would have been empty.
  623.  *
  624.  * If either of @orig or @relmap is empty (no set bits), then @dst
  625.  * will be returned empty.
  626.  *
  627.  * If (as explained above) the only set bits in @orig are in positions
  628.  * m where m >= W, (where W is the weight of @relmap) then @dst will
  629.  * once again be returned empty.
  630.  *
  631.  * All bits in @dst not set by the above rule are cleared.
  632.  */
  633. void bitmap_onto(unsigned long *dst, const unsigned long *orig,
  634.                         const unsigned long *relmap, int bits)
  635. {
  636.         int n, m;               /* same meaning as in above comment */
  637.  
  638.         if (dst == orig)        /* following doesn't handle inplace mappings */
  639.                 return;
  640.         bitmap_zero(dst, bits);
  641.  
  642.         /*
  643.          * The following code is a more efficient, but less
  644.          * obvious, equivalent to the loop:
  645.          *      for (m = 0; m < bitmap_weight(relmap, bits); m++) {
  646.          *              n = bitmap_ord_to_pos(orig, m, bits);
  647.          *              if (test_bit(m, orig))
  648.          *                      set_bit(n, dst);
  649.          *      }
  650.          */
  651.  
  652.         m = 0;
  653.         for_each_set_bit(n, relmap, bits) {
  654.                 /* m == bitmap_pos_to_ord(relmap, n, bits) */
  655.                 if (test_bit(m, orig))
  656.                         set_bit(n, dst);
  657.                 m++;
  658.         }
  659. }
  660. EXPORT_SYMBOL(bitmap_onto);
  661.  
  662. /**
  663.  * bitmap_fold - fold larger bitmap into smaller, modulo specified size
  664.  *      @dst: resulting smaller bitmap
  665.  *      @orig: original larger bitmap
  666.  *      @sz: specified size
  667.  *      @bits: number of bits in each of these bitmaps
  668.  *
  669.  * For each bit oldbit in @orig, set bit oldbit mod @sz in @dst.
  670.  * Clear all other bits in @dst.  See further the comment and
  671.  * Example [2] for bitmap_onto() for why and how to use this.
  672.  */
  673. void bitmap_fold(unsigned long *dst, const unsigned long *orig,
  674.                         int sz, int bits)
  675. {
  676.         int oldbit;
  677.  
  678.         if (dst == orig)        /* following doesn't handle inplace mappings */
  679.                 return;
  680.         bitmap_zero(dst, bits);
  681.  
  682.         for_each_set_bit(oldbit, orig, bits)
  683.                 set_bit(oldbit % sz, dst);
  684. }
  685. EXPORT_SYMBOL(bitmap_fold);
  686.  
  687. /*
  688.  * Common code for bitmap_*_region() routines.
  689.  *      bitmap: array of unsigned longs corresponding to the bitmap
  690.  *      pos: the beginning of the region
  691.  *      order: region size (log base 2 of number of bits)
  692.  *      reg_op: operation(s) to perform on that region of bitmap
  693.  *
  694.  * Can set, verify and/or release a region of bits in a bitmap,
  695.  * depending on which combination of REG_OP_* flag bits is set.
  696.  *
  697.  * A region of a bitmap is a sequence of bits in the bitmap, of
  698.  * some size '1 << order' (a power of two), aligned to that same
  699.  * '1 << order' power of two.
  700.  *
  701.  * Returns 1 if REG_OP_ISFREE succeeds (region is all zero bits).
  702.  * Returns 0 in all other cases and reg_ops.
  703.  */
  704.  
  705. enum {
  706.         REG_OP_ISFREE,          /* true if region is all zero bits */
  707.         REG_OP_ALLOC,           /* set all bits in region */
  708.         REG_OP_RELEASE,         /* clear all bits in region */
  709. };
  710.  
  711. static int __reg_op(unsigned long *bitmap, int pos, int order, int reg_op)
  712. {
  713.         int nbits_reg;          /* number of bits in region */
  714.         int index;              /* index first long of region in bitmap */
  715.         int offset;             /* bit offset region in bitmap[index] */
  716.         int nlongs_reg;         /* num longs spanned by region in bitmap */
  717.         int nbitsinlong;        /* num bits of region in each spanned long */
  718.         unsigned long mask;     /* bitmask for one long of region */
  719.         int i;                  /* scans bitmap by longs */
  720.         int ret = 0;            /* return value */
  721.  
  722.         /*
  723.          * Either nlongs_reg == 1 (for small orders that fit in one long)
  724.          * or (offset == 0 && mask == ~0UL) (for larger multiword orders.)
  725.          */
  726.         nbits_reg = 1 << order;
  727.         index = pos / BITS_PER_LONG;
  728.         offset = pos - (index * BITS_PER_LONG);
  729.         nlongs_reg = BITS_TO_LONGS(nbits_reg);
  730.         nbitsinlong = min(nbits_reg,  BITS_PER_LONG);
  731.  
  732.         /*
  733.          * Can't do "mask = (1UL << nbitsinlong) - 1", as that
  734.          * overflows if nbitsinlong == BITS_PER_LONG.
  735.          */
  736.         mask = (1UL << (nbitsinlong - 1));
  737.         mask += mask - 1;
  738.         mask <<= offset;
  739.  
  740.         switch (reg_op) {
  741.         case REG_OP_ISFREE:
  742.                 for (i = 0; i < nlongs_reg; i++) {
  743.                         if (bitmap[index + i] & mask)
  744.                                 goto done;
  745.                 }
  746.                 ret = 1;        /* all bits in region free (zero) */
  747.                 break;
  748.  
  749.         case REG_OP_ALLOC:
  750.                 for (i = 0; i < nlongs_reg; i++)
  751.                         bitmap[index + i] |= mask;
  752.                 break;
  753.  
  754.         case REG_OP_RELEASE:
  755.                 for (i = 0; i < nlongs_reg; i++)
  756.                         bitmap[index + i] &= ~mask;
  757.                 break;
  758.         }
  759. done:
  760.         return ret;
  761. }
  762.  
  763. /**
  764.  * bitmap_find_free_region - find a contiguous aligned mem region
  765.  *      @bitmap: array of unsigned longs corresponding to the bitmap
  766.  *      @bits: number of bits in the bitmap
  767.  *      @order: region size (log base 2 of number of bits) to find
  768.  *
  769.  * Find a region of free (zero) bits in a @bitmap of @bits bits and
  770.  * allocate them (set them to one).  Only consider regions of length
  771.  * a power (@order) of two, aligned to that power of two, which
  772.  * makes the search algorithm much faster.
  773.  *
  774.  * Return the bit offset in bitmap of the allocated region,
  775.  * or -errno on failure.
  776.  */
  777. int bitmap_find_free_region(unsigned long *bitmap, int bits, int order)
  778. {
  779.         int pos, end;           /* scans bitmap by regions of size order */
  780.  
  781.         for (pos = 0 ; (end = pos + (1 << order)) <= bits; pos = end) {
  782.                 if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
  783.                         continue;
  784.                 __reg_op(bitmap, pos, order, REG_OP_ALLOC);
  785.                 return pos;
  786.         }
  787.         return -ENOMEM;
  788. }
  789. EXPORT_SYMBOL(bitmap_find_free_region);
  790.  
  791. /**
  792.  * bitmap_release_region - release allocated bitmap region
  793.  *      @bitmap: array of unsigned longs corresponding to the bitmap
  794.  *      @pos: beginning of bit region to release
  795.  *      @order: region size (log base 2 of number of bits) to release
  796.  *
  797.  * This is the complement to __bitmap_find_free_region() and releases
  798.  * the found region (by clearing it in the bitmap).
  799.  *
  800.  * No return value.
  801.  */
  802. void bitmap_release_region(unsigned long *bitmap, int pos, int order)
  803. {
  804.         __reg_op(bitmap, pos, order, REG_OP_RELEASE);
  805. }
  806. EXPORT_SYMBOL(bitmap_release_region);
  807.  
  808. /**
  809.  * bitmap_allocate_region - allocate bitmap region
  810.  *      @bitmap: array of unsigned longs corresponding to the bitmap
  811.  *      @pos: beginning of bit region to allocate
  812.  *      @order: region size (log base 2 of number of bits) to allocate
  813.  *
  814.  * Allocate (set bits in) a specified region of a bitmap.
  815.  *
  816.  * Return 0 on success, or %-EBUSY if specified region wasn't
  817.  * free (not all bits were zero).
  818.  */
  819. int bitmap_allocate_region(unsigned long *bitmap, int pos, int order)
  820. {
  821.         if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
  822.                 return -EBUSY;
  823.         __reg_op(bitmap, pos, order, REG_OP_ALLOC);
  824.         return 0;
  825. }
  826. EXPORT_SYMBOL(bitmap_allocate_region);
  827.  
  828. /**
  829.  * bitmap_copy_le - copy a bitmap, putting the bits into little-endian order.
  830.  * @dst:   destination buffer
  831.  * @src:   bitmap to copy
  832.  * @nbits: number of bits in the bitmap
  833.  *
  834.  * Require nbits % BITS_PER_LONG == 0.
  835.  */
  836. void bitmap_copy_le(void *dst, const unsigned long *src, int nbits)
  837. {
  838.         unsigned long *d = dst;
  839.         int i;
  840.  
  841.         for (i = 0; i < nbits/BITS_PER_LONG; i++) {
  842.                 if (BITS_PER_LONG == 64)
  843.                         d[i] = cpu_to_le64(src[i]);
  844.                 else
  845.                         d[i] = cpu_to_le32(src[i]);
  846.         }
  847. }
  848. EXPORT_SYMBOL(bitmap_copy_le);
  849.