src/share/vm/utilities/bitMap.hpp

Thu, 22 May 2014 15:52:41 -0400

author
drchase
date
Thu, 22 May 2014 15:52:41 -0400
changeset 6680
78bbf4d43a14
parent 6198
55fb97c4c58d
child 6876
710a3c8b516e
child 9616
faa71d8b8ab5
permissions
-rw-r--r--

8037816: Fix for 8036122 breaks build with Xcode5/clang
8043029: Change 8037816 breaks HS build with older GCC versions which don't support diagnostic pragmas
8043164: Format warning in traceStream.hpp
Summary: Backport of main fix + two corrections, enables clang compilation, turns on format attributes, corrects/mutes warnings
Reviewed-by: kvn, coleenp, iveresov, twisti

     1 /*
     2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  *
    23  */
    25 #ifndef SHARE_VM_UTILITIES_BITMAP_HPP
    26 #define SHARE_VM_UTILITIES_BITMAP_HPP
    28 #include "memory/allocation.hpp"
    29 #include "utilities/top.hpp"
    31 // Forward decl;
    32 class BitMapClosure;
    34 // Operations for bitmaps represented as arrays of unsigned integers.
    35 // Bit offsets are numbered from 0 to size-1.
    37 class BitMap VALUE_OBJ_CLASS_SPEC {
    38   friend class BitMap2D;
    40  public:
    41   typedef size_t idx_t;         // Type used for bit and word indices.
    42   typedef uintptr_t bm_word_t;  // Element type of array that represents
    43                                 // the bitmap.
    45   // Hints for range sizes.
    46   typedef enum {
    47     unknown_range, small_range, large_range
    48   } RangeSizeHint;
    50  private:
    51   ArrayAllocator<bm_word_t, mtInternal> _map_allocator;
    52   bm_word_t* _map;     // First word in bitmap
    53   idx_t      _size;    // Size of bitmap (in bits)
    55   // Puts the given value at the given offset, using resize() to size
    56   // the bitmap appropriately if needed using factor-of-two expansion.
    57   void at_put_grow(idx_t index, bool value);
    59  protected:
    60   // Return the position of bit within the word that contains it (e.g., if
    61   // bitmap words are 32 bits, return a number 0 <= n <= 31).
    62   static idx_t bit_in_word(idx_t bit) { return bit & (BitsPerWord - 1); }
    64   // Return a mask that will select the specified bit, when applied to the word
    65   // containing the bit.
    66   static bm_word_t bit_mask(idx_t bit) { return (bm_word_t)1 << bit_in_word(bit); }
    68   // Return the index of the word containing the specified bit.
    69   static idx_t word_index(idx_t bit)  { return bit >> LogBitsPerWord; }
    71   // Return the bit number of the first bit in the specified word.
    72   static idx_t bit_index(idx_t word)  { return word << LogBitsPerWord; }
    74   // Return the array of bitmap words, or a specific word from it.
    75   bm_word_t* map() const           { return _map; }
    76   bm_word_t  map(idx_t word) const { return _map[word]; }
    78   // Return a pointer to the word containing the specified bit.
    79   bm_word_t* word_addr(idx_t bit) const { return map() + word_index(bit); }
    81   // Set a word to a specified value or to all ones; clear a word.
    82   void set_word  (idx_t word, bm_word_t val) { _map[word] = val; }
    83   void set_word  (idx_t word)            { set_word(word, ~(uintptr_t)0); }
    84   void clear_word(idx_t word)            { _map[word] = 0; }
    86   // Utilities for ranges of bits.  Ranges are half-open [beg, end).
    88   // Ranges within a single word.
    89   bm_word_t inverted_bit_mask_for_range(idx_t beg, idx_t end) const;
    90   void  set_range_within_word      (idx_t beg, idx_t end);
    91   void  clear_range_within_word    (idx_t beg, idx_t end);
    92   void  par_put_range_within_word  (idx_t beg, idx_t end, bool value);
    94   // Ranges spanning entire words.
    95   void      set_range_of_words         (idx_t beg, idx_t end);
    96   void      clear_range_of_words       (idx_t beg, idx_t end);
    97   void      set_large_range_of_words   (idx_t beg, idx_t end);
    98   void      clear_large_range_of_words (idx_t beg, idx_t end);
   100   // The index of the first full word in a range.
   101   idx_t word_index_round_up(idx_t bit) const;
   103   // Verification.
   104   inline void verify_index(idx_t index) const NOT_DEBUG_RETURN;
   105   inline void verify_range(idx_t beg_index, idx_t end_index) const
   106     NOT_DEBUG_RETURN;
   108   // Statistics.
   109   static idx_t* _pop_count_table;
   110   static void init_pop_count_table();
   111   static idx_t num_set_bits(bm_word_t w);
   112   static idx_t num_set_bits_from_table(unsigned char c);
   114  public:
   116   // Constructs a bitmap with no map, and size 0.
   117   BitMap() : _map(NULL), _size(0), _map_allocator(false) {}
   119   // Constructs a bitmap with the given map and size.
   120   BitMap(bm_word_t* map, idx_t size_in_bits);
   122   // Constructs an empty bitmap of the given size (that is, this clears the
   123   // new bitmap).  Allocates the map array in resource area if
   124   // "in_resource_area" is true, else in the C heap.
   125   BitMap(idx_t size_in_bits, bool in_resource_area = true);
   127   // Set the map and size.
   128   void set_map(bm_word_t* map)      { _map = map; }
   129   void set_size(idx_t size_in_bits) { _size = size_in_bits; }
   131   // Allocates necessary data structure, either in the resource area
   132   // or in the C heap, as indicated by "in_resource_area."
   133   // Preserves state currently in bit map by copying data.
   134   // Zeros any newly-addressable bits.
   135   // If "in_resource_area" is false, frees the current map.
   136   // (Note that this assumes that all calls to "resize" on the same BitMap
   137   // use the same value for "in_resource_area".)
   138   void resize(idx_t size_in_bits, bool in_resource_area = true);
   140   // Accessing
   141   idx_t size() const                    { return _size; }
   142   idx_t size_in_words() const           {
   143     return word_index(size() + BitsPerWord - 1);
   144   }
   146   bool at(idx_t index) const {
   147     verify_index(index);
   148     return (*word_addr(index) & bit_mask(index)) != 0;
   149   }
   151   // Align bit index up or down to the next bitmap word boundary, or check
   152   // alignment.
   153   static idx_t word_align_up(idx_t bit) {
   154     return align_size_up(bit, BitsPerWord);
   155   }
   156   static idx_t word_align_down(idx_t bit) {
   157     return align_size_down(bit, BitsPerWord);
   158   }
   159   static bool is_word_aligned(idx_t bit) {
   160     return word_align_up(bit) == bit;
   161   }
   163   // Set or clear the specified bit.
   164   inline void set_bit(idx_t bit);
   165   inline void clear_bit(idx_t bit);
   167   // Atomically set or clear the specified bit.
   168   inline bool par_set_bit(idx_t bit);
   169   inline bool par_clear_bit(idx_t bit);
   171   // Put the given value at the given offset. The parallel version
   172   // will CAS the value into the bitmap and is quite a bit slower.
   173   // The parallel version also returns a value indicating if the
   174   // calling thread was the one that changed the value of the bit.
   175   void at_put(idx_t index, bool value);
   176   bool par_at_put(idx_t index, bool value);
   178   // Update a range of bits.  Ranges are half-open [beg, end).
   179   void set_range   (idx_t beg, idx_t end);
   180   void clear_range (idx_t beg, idx_t end);
   181   void set_large_range   (idx_t beg, idx_t end);
   182   void clear_large_range (idx_t beg, idx_t end);
   183   void at_put_range(idx_t beg, idx_t end, bool value);
   184   void par_at_put_range(idx_t beg, idx_t end, bool value);
   185   void at_put_large_range(idx_t beg, idx_t end, bool value);
   186   void par_at_put_large_range(idx_t beg, idx_t end, bool value);
   188   // Update a range of bits, using a hint about the size.  Currently only
   189   // inlines the predominant case of a 1-bit range.  Works best when hint is a
   190   // compile-time constant.
   191   void set_range(idx_t beg, idx_t end, RangeSizeHint hint);
   192   void clear_range(idx_t beg, idx_t end, RangeSizeHint hint);
   193   void par_set_range(idx_t beg, idx_t end, RangeSizeHint hint);
   194   void par_clear_range  (idx_t beg, idx_t end, RangeSizeHint hint);
   196   // Clearing
   197   void clear_large();
   198   inline void clear();
   200   // Iteration support.  Returns "true" if the iteration completed, false
   201   // if the iteration terminated early (because the closure "blk" returned
   202   // false).
   203   bool iterate(BitMapClosure* blk, idx_t leftIndex, idx_t rightIndex);
   204   bool iterate(BitMapClosure* blk) {
   205     // call the version that takes an interval
   206     return iterate(blk, 0, size());
   207   }
   209   // Looking for 1's and 0's at indices equal to or greater than "l_index",
   210   // stopping if none has been found before "r_index", and returning
   211   // "r_index" (which must be at most "size") in that case.
   212   idx_t get_next_one_offset_inline (idx_t l_index, idx_t r_index) const;
   213   idx_t get_next_zero_offset_inline(idx_t l_index, idx_t r_index) const;
   215   // Like "get_next_one_offset_inline", except requires that "r_index" is
   216   // aligned to bitsizeof(bm_word_t).
   217   idx_t get_next_one_offset_inline_aligned_right(idx_t l_index,
   218                                                         idx_t r_index) const;
   220   // Non-inline versionsof the above.
   221   idx_t get_next_one_offset (idx_t l_index, idx_t r_index) const;
   222   idx_t get_next_zero_offset(idx_t l_index, idx_t r_index) const;
   224   idx_t get_next_one_offset(idx_t offset) const {
   225     return get_next_one_offset(offset, size());
   226   }
   227   idx_t get_next_zero_offset(idx_t offset) const {
   228     return get_next_zero_offset(offset, size());
   229   }
   231   // Returns the number of bits set in the bitmap.
   232   idx_t count_one_bits() const;
   234   // Set operations.
   235   void set_union(BitMap bits);
   236   void set_difference(BitMap bits);
   237   void set_intersection(BitMap bits);
   238   // Returns true iff "this" is a superset of "bits".
   239   bool contains(const BitMap bits) const;
   240   // Returns true iff "this and "bits" have a non-empty intersection.
   241   bool intersects(const BitMap bits) const;
   243   // Returns result of whether this map changed
   244   // during the operation
   245   bool set_union_with_result(BitMap bits);
   246   bool set_difference_with_result(BitMap bits);
   247   bool set_intersection_with_result(BitMap bits);
   249   // Requires the submap of "bits" starting at offset to be at least as
   250   // large as "this".  Modifies "this" to be the intersection of its
   251   // current contents and the submap of "bits" starting at "offset" of the
   252   // same length as "this."
   253   // (For expedience, currently requires the offset to be aligned to the
   254   // bitsize of a uintptr_t.  This should go away in the future though it
   255   // will probably remain a good case to optimize.)
   256   void set_intersection_at_offset(BitMap bits, idx_t offset);
   258   void set_from(BitMap bits);
   260   bool is_same(BitMap bits);
   262   // Test if all bits are set or cleared
   263   bool is_full() const;
   264   bool is_empty() const;
   266   void print_on_error(outputStream* st, const char* prefix) const;
   268 #ifndef PRODUCT
   269  public:
   270   // Printing
   271   void print_on(outputStream* st) const;
   272 #endif
   273 };
   275 // Convenience class wrapping BitMap which provides multiple bits per slot.
   276 class BitMap2D VALUE_OBJ_CLASS_SPEC {
   277  public:
   278   typedef BitMap::idx_t idx_t;          // Type used for bit and word indices.
   279   typedef BitMap::bm_word_t bm_word_t;  // Element type of array that
   280                                         // represents the bitmap.
   281  private:
   282   BitMap _map;
   283   idx_t  _bits_per_slot;
   285   idx_t bit_index(idx_t slot_index, idx_t bit_within_slot_index) const {
   286     return slot_index * _bits_per_slot + bit_within_slot_index;
   287   }
   289   void verify_bit_within_slot_index(idx_t index) const {
   290     assert(index < _bits_per_slot, "bit_within_slot index out of bounds");
   291   }
   293  public:
   294   // Construction. bits_per_slot must be greater than 0.
   295   BitMap2D(bm_word_t* map, idx_t size_in_slots, idx_t bits_per_slot);
   297   // Allocates necessary data structure in resource area. bits_per_slot must be greater than 0.
   298   BitMap2D(idx_t size_in_slots, idx_t bits_per_slot);
   300   idx_t size_in_bits() {
   301     return _map.size();
   302   }
   304   // Returns number of full slots that have been allocated
   305   idx_t size_in_slots() {
   306     // Round down
   307     return _map.size() / _bits_per_slot;
   308   }
   310   bool is_valid_index(idx_t slot_index, idx_t bit_within_slot_index) {
   311     verify_bit_within_slot_index(bit_within_slot_index);
   312     return (bit_index(slot_index, bit_within_slot_index) < size_in_bits());
   313   }
   315   bool at(idx_t slot_index, idx_t bit_within_slot_index) const {
   316     verify_bit_within_slot_index(bit_within_slot_index);
   317     return _map.at(bit_index(slot_index, bit_within_slot_index));
   318   }
   320   void set_bit(idx_t slot_index, idx_t bit_within_slot_index) {
   321     verify_bit_within_slot_index(bit_within_slot_index);
   322     _map.set_bit(bit_index(slot_index, bit_within_slot_index));
   323   }
   325   void clear_bit(idx_t slot_index, idx_t bit_within_slot_index) {
   326     verify_bit_within_slot_index(bit_within_slot_index);
   327     _map.clear_bit(bit_index(slot_index, bit_within_slot_index));
   328   }
   330   void at_put(idx_t slot_index, idx_t bit_within_slot_index, bool value) {
   331     verify_bit_within_slot_index(bit_within_slot_index);
   332     _map.at_put(bit_index(slot_index, bit_within_slot_index), value);
   333   }
   335   void at_put_grow(idx_t slot_index, idx_t bit_within_slot_index, bool value) {
   336     verify_bit_within_slot_index(bit_within_slot_index);
   337     _map.at_put_grow(bit_index(slot_index, bit_within_slot_index), value);
   338   }
   340   void clear();
   341 };
   343 // Closure for iterating over BitMaps
   345 class BitMapClosure VALUE_OBJ_CLASS_SPEC {
   346  public:
   347   // Callback when bit in map is set.  Should normally return "true";
   348   // return of false indicates that the bitmap iteration should terminate.
   349   virtual bool do_bit(BitMap::idx_t offset) = 0;
   350 };
   352 #endif // SHARE_VM_UTILITIES_BITMAP_HPP

mercurial