hotspot/src/share/vm/libadt/vectset.hpp
author kvn
Mon, 07 Feb 2011 10:25:39 -0800
changeset 8319 aedb3bd871bc
parent 7397 5b173b4ca846
child 10975 446510be531a
permissions -rw-r--r--
7013538: Java memory leak with escape analysis Summary: Don't allocate VectorSet iterator on C heap. Reuse resource storage in EA. Reviewed-by: never
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
8319
aedb3bd871bc 7013538: Java memory leak with escape analysis
kvn
parents: 7397
diff changeset
     2
 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    25
#ifndef SHARE_VM_LIBADT_VECTSET_HPP
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    26
#define SHARE_VM_LIBADT_VECTSET_HPP
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    27
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    28
#include "libadt/set.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    29
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
// Vector Sets - An Abstract Data Type
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
//INTERFACE
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
// These sets can grow or shrink, based on the initial size and the largest
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
// element currently in them.  Slow and bulky for sparse sets, these sets
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
// are super for dense sets.  They are fast and compact when dense.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
// TIME:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
// O(1) - Insert, Delete, Member, Sort
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
// O(max_element) - Create, Clear, Size, Copy, Union, Intersect, Difference,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
//                  Equal, ChooseMember, Forall
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
// SPACE: (max_element)/(8*sizeof(int))
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
//------------------------------VectorSet--------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
class VectorSet : public Set {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
friend class VectorSetI;        // Friendly iterator class
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
  uint size;                    // Size of data IN LONGWORDS (32bits)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
  uint32 *data;                 // The data, bit packed
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
  void slamin( const VectorSet& s );     // Initialize one set with another
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
  int compare(const VectorSet &s) const; // Compare set contents
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
  void grow(uint newsize);               // Grow vector to required bitsize
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
  VectorSet(Arena *arena);                      // Creates a new, empty set.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
  VectorSet(const VectorSet &s) : Set(s._set_arena) {slamin(s);} // Set clone; deep-copy guts
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
  Set &operator =(const Set &s);                // Set clone; deep-copy guts
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
  VectorSet &operator =(const VectorSet &s)     // Set clone; deep-copy guts
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
  { if( &s != this ) { slamin(s); } return *this; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
  ~VectorSet() {}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
  Set &clone(void) const { return *(new VectorSet(*this)); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
  Set &operator <<=(uint elem);          // Add member to set
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
  VectorSet operator << (uint elem)      // Add member to new set
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
  { VectorSet foo(*this); foo <<= elem; return foo; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
  Set &operator >>=(uint elem);          // Delete member from set
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
  VectorSet operator >> (uint elem)      // Delete member from new set
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
  { VectorSet foo(*this); foo >>= elem; return foo; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  VectorSet &operator &=(const VectorSet &s); // Intersect sets into first set
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  Set       &operator &=(const Set       &s); // Intersect sets into first set
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  VectorSet operator & (const VectorSet &s) const
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  { VectorSet foo(*this); foo &= s; return foo; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  VectorSet &operator |=(const VectorSet &s); // Intersect sets into first set
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  Set       &operator |=(const Set       &s); // Intersect sets into first set
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
  VectorSet operator | (const VectorSet &s) const
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
  { VectorSet foo(*this); foo |= s; return foo; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
  VectorSet &operator -=(const VectorSet &s); // Intersect sets into first set
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  Set       &operator -=(const Set       &s); // Intersect sets into first set
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
  VectorSet operator - (const VectorSet &s) const
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
  { VectorSet foo(*this); foo -= s; return foo; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  int operator ==(const VectorSet &s) const;  // True if sets are equal
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
  int operator ==(const Set       &s) const;  // True if sets are equal
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
  int operator < (const VectorSet &s) const;  // True if strict subset
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
  int operator < (const Set       &s) const;  // True if strict subset
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  int operator <=(const VectorSet &s) const;  // True if subset relation holds.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
  int operator <=(const Set       &s) const;  // True if subset relation holds.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
  int disjoint   (const Set       &s) const;  // True if sets are disjoint
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
  int operator [](uint elem) const; // Test for membership
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
  uint getelem(void) const;         // Return a random element
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
  void Clear(void);                 // Clear a set
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
  uint Size(void) const;            // Number of elements in the Set.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  void Sort(void);                  // Sort before iterating
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
  int hash() const;                 // Hash function
8319
aedb3bd871bc 7013538: Java memory leak with escape analysis
kvn
parents: 7397
diff changeset
   101
  void Reset(void) {                // Reset a set
aedb3bd871bc 7013538: Java memory leak with escape analysis
kvn
parents: 7397
diff changeset
   102
    memset( data, 0, size*sizeof(uint32) );
aedb3bd871bc 7013538: Java memory leak with escape analysis
kvn
parents: 7397
diff changeset
   103
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  /* Removed for MCC BUG
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
     operator const VectorSet* (void) const { return this; } */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
  const VectorSet *asVectorSet() const { return this; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
  // Expose internals for speed-critical fast iterators
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
  uint word_size() const { return size; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
  uint32 *EXPOSE() const { return data; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
  // Fast inlined "test and set".  Replaces the idiom:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
  //     if( visited[idx] ) return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
  //     visited <<= idx;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  // With:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
  //     if( visited.test_set(idx) ) return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
  //
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  int test_set( uint elem ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
    uint word = elem >> 5;           // Get the longword offset
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
    if( word >= size )               // Beyond the last?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
      return test_set_grow(elem);    // Then grow; set; return 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
    uint32 mask = 1L << (elem & 31); // Get bit mask
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
    uint32 datum = data[word] & mask;// Get bit
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
    data[word] |= mask;              // Set bit
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
    return datum;                    // Return bit
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
  int test_set_grow( uint elem ) {    // Insert & return 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
    (*this) <<= elem;                 // Insert into set
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
    return 0;                         // Return 0!
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
  // Fast inlined test
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
  int test( uint elem ) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
    uint word = elem >> 5;      // Get the longword offset
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
    if( word >= size ) return 0; // Beyond the last?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
    uint32 mask = 1L << (elem & 31); // Get bit mask
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
    return data[word] & mask;   // Get bit
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
  // Fast inlined set
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
  void set( uint elem ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
    uint word = elem >> 5;      // Get the longword offset
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
    if( word >= size ) {        // Beyond the last?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
      test_set_grow(elem);      // Then grow and set
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
      uint32 mask = 1L << (elem & 31); // Get bit mask
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
      data[word] |= mask;       // Set bit
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
private:
8319
aedb3bd871bc 7013538: Java memory leak with escape analysis
kvn
parents: 7397
diff changeset
   154
  SetI_ *iterate(uint&) const { ShouldNotCallThis(); return NULL; } // Removed
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
//------------------------------Iteration--------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
// Loop thru all elements of the set, setting "elem" to the element numbers
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
// in random order.  Inserted or deleted elements during this operation may
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
// or may not be iterated over; untouched elements will be affected once.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
// Usage:  for( VectorSetI i(s); i.test(); i++ ) { body = i.elem; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
8319
aedb3bd871bc 7013538: Java memory leak with escape analysis
kvn
parents: 7397
diff changeset
   163
class VectorSetI : public StackObj {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
  friend class VectorSet;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
  const VectorSet *s;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
  uint i, j;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
  uint32 mask;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
  uint next(void);
8319
aedb3bd871bc 7013538: Java memory leak with escape analysis
kvn
parents: 7397
diff changeset
   169
aedb3bd871bc 7013538: Java memory leak with escape analysis
kvn
parents: 7397
diff changeset
   170
public:
aedb3bd871bc 7013538: Java memory leak with escape analysis
kvn
parents: 7397
diff changeset
   171
  uint elem;                    // The publically accessible element
aedb3bd871bc 7013538: Java memory leak with escape analysis
kvn
parents: 7397
diff changeset
   172
aedb3bd871bc 7013538: Java memory leak with escape analysis
kvn
parents: 7397
diff changeset
   173
  VectorSetI( const VectorSet *vset ) :
aedb3bd871bc 7013538: Java memory leak with escape analysis
kvn
parents: 7397
diff changeset
   174
    s(vset),
aedb3bd871bc 7013538: Java memory leak with escape analysis
kvn
parents: 7397
diff changeset
   175
    i((uint)-1L),
aedb3bd871bc 7013538: Java memory leak with escape analysis
kvn
parents: 7397
diff changeset
   176
    j((uint)-1L),
aedb3bd871bc 7013538: Java memory leak with escape analysis
kvn
parents: 7397
diff changeset
   177
    mask((unsigned)(1L<<31)) {
aedb3bd871bc 7013538: Java memory leak with escape analysis
kvn
parents: 7397
diff changeset
   178
    elem = next();
aedb3bd871bc 7013538: Java memory leak with escape analysis
kvn
parents: 7397
diff changeset
   179
  }
aedb3bd871bc 7013538: Java memory leak with escape analysis
kvn
parents: 7397
diff changeset
   180
aedb3bd871bc 7013538: Java memory leak with escape analysis
kvn
parents: 7397
diff changeset
   181
  void operator ++(void) { elem = next(); }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
  int test(void) { return i < s->size; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
   185
#endif // SHARE_VM_LIBADT_VECTSET_HPP