< prev index next >

src/hotspot/share/utilities/growableArray.hpp

roman_version

8  * This code is distributed in the hope that it will be useful, but WITHOUT                                                          
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or                                                             
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License                                                             
11  * version 2 for more details (a copy is included in the LICENSE file that                                                           
12  * accompanied this code).                                                                                                           
13  *                                                                                                                                   
14  * You should have received a copy of the GNU General Public License version                                                         
15  * 2 along with this work; if not, write to the Free Software Foundation,                                                            
16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.                                                                     
17  *                                                                                                                                   
18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA                                                           
19  * or visit www.oracle.com if you need additional information or have any                                                            
20  * questions.                                                                                                                        
21  *                                                                                                                                   
22  */                                                                                                                                  
23 
24 #ifndef SHARE_VM_UTILITIES_GROWABLEARRAY_HPP                                                                                         
25 #define SHARE_VM_UTILITIES_GROWABLEARRAY_HPP                                                                                         
26 
27 #include "memory/allocation.hpp"                                                                                                     
                                                                                                                                     
28 #include "utilities/debug.hpp"                                                                                                       
29 #include "utilities/globalDefinitions.hpp"                                                                                           
30 #include "utilities/ostream.hpp"                                                                                                     
31 
32 // A growable array.                                                                                                                 
33 
34 /*************************************************************************/                                                          
35 /*                                                                       */                                                          
36 /*     WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING   */                                                          
37 /*                                                                       */                                                          
38 /* Should you use GrowableArrays to contain handles you must be certain  */                                                          
39 /* the the GrowableArray does not outlive the HandleMark that contains   */                                                          
40 /* the handles. Since GrowableArrays are typically resource allocated    */                                                          
41 /* the following is an example of INCORRECT CODE,                        */                                                          
42 /*                                                                       */                                                          
43 /* ResourceMark rm;                                                      */                                                          
44 /* GrowableArray<Handle>* arr = new GrowableArray<Handle>(size);         */                                                          
45 /* if (blah) {                                                           */                                                          
46 /*    while (...) {                                                      */                                                          

8  * This code is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11  * version 2 for more details (a copy is included in the LICENSE file that
12  * accompanied this code).
13  *
14  * You should have received a copy of the GNU General Public License version
15  * 2 along with this work; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
19  * or visit www.oracle.com if you need additional information or have any
20  * questions.
21  *
22  */
23 
24 #ifndef SHARE_VM_UTILITIES_GROWABLEARRAY_HPP
25 #define SHARE_VM_UTILITIES_GROWABLEARRAY_HPP
26 
27 #include "memory/allocation.hpp"
28 #include "oops/oop.hpp"
29 #include "utilities/debug.hpp"
30 #include "utilities/globalDefinitions.hpp"
31 #include "utilities/ostream.hpp"
32 
33 // A growable array.
34 
35 /*************************************************************************/
36 /*                                                                       */
37 /*     WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING   */
38 /*                                                                       */
39 /* Should you use GrowableArrays to contain handles you must be certain  */
40 /* the the GrowableArray does not outlive the HandleMark that contains   */
41 /* the handles. Since GrowableArrays are typically resource allocated    */
42 /* the following is an example of INCORRECT CODE,                        */
43 /*                                                                       */
44 /* ResourceMark rm;                                                      */
45 /* GrowableArray<Handle>* arr = new GrowableArray<Handle>(size);         */
46 /* if (blah) {                                                           */
47 /*    while (...) {                                                      */

193   GrowableArray() : GenericGrowableArray(2, 0, false) {                                                                              
194     _data = (E*)raw_allocate(sizeof(E));                                                                                             
195     ::new ((void*)&_data[0]) E();                                                                                                    
196     ::new ((void*)&_data[1]) E();                                                                                                    
197   }                                                                                                                                  
198 
199                                 // Does nothing for resource and arena objects                                                       
200   ~GrowableArray()              { if (on_C_heap()) clear_and_deallocate(); }                                                         
201 
202   void  clear()                 { _len = 0; }                                                                                        
203   int   length() const          { return _len; }                                                                                     
204   int   max_length() const      { return _max; }                                                                                     
205   void  trunc_to(int l)         { assert(l <= _len,"cannot increase length"); _len = l; }                                            
206   bool  is_empty() const        { return _len == 0; }                                                                                
207   bool  is_nonempty() const     { return _len != 0; }                                                                                
208   bool  is_full() const         { return _len == _max; }                                                                             
209   DEBUG_ONLY(E* data_addr() const      { return _data; })                                                                            
210 
211   void print();                                                                                                                      
212 
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
                                                                                                                                     
213   int append(const E& elem) {                                                                                                        
214     check_nesting();                                                                                                                 
215     if (_len == _max) grow(_len);                                                                                                    
216     int idx = _len++;                                                                                                                
217     _data[idx] = elem;                                                                                                               
218     return idx;                                                                                                                      
219   }                                                                                                                                  
220 
221   bool append_if_missing(const E& elem) {                                                                                            
222     // Returns TRUE if elem is added.                                                                                                
223     bool missed = !contains(elem);                                                                                                   
224     if (missed) append(elem);                                                                                                        
225     return missed;                                                                                                                   
226   }                                                                                                                                  
227 
228   E& at(int i) {                                                                                                                     
229     assert(0 <= i && i < _len, "illegal index");                                                                                     
230     return _data[i];                                                                                                                 
231   }                                                                                                                                  

194   GrowableArray() : GenericGrowableArray(2, 0, false) {
195     _data = (E*)raw_allocate(sizeof(E));
196     ::new ((void*)&_data[0]) E();
197     ::new ((void*)&_data[1]) E();
198   }
199 
200                                 // Does nothing for resource and arena objects
201   ~GrowableArray()              { if (on_C_heap()) clear_and_deallocate(); }
202 
203   void  clear()                 { _len = 0; }
204   int   length() const          { return _len; }
205   int   max_length() const      { return _max; }
206   void  trunc_to(int l)         { assert(l <= _len,"cannot increase length"); _len = l; }
207   bool  is_empty() const        { return _len == 0; }
208   bool  is_nonempty() const     { return _len != 0; }
209   bool  is_full() const         { return _len == _max; }
210   DEBUG_ONLY(E* data_addr() const      { return _data; })
211 
212   void print();
213 
214   inline static bool safe_equals(oop obj1, oop obj2) {
215     return oopDesc::equals(obj1, obj2);
216   }
217 
218   template <class X>
219   inline static bool safe_equals(X i1, X i2) {
220     return i1 == i2;
221   }
222 
223   int append(const E& elem) {
224     check_nesting();
225     if (_len == _max) grow(_len);
226     int idx = _len++;
227     _data[idx] = elem;
228     return idx;
229   }
230 
231   bool append_if_missing(const E& elem) {
232     // Returns TRUE if elem is added.
233     bool missed = !contains(elem);
234     if (missed) append(elem);
235     return missed;
236   }
237 
238   E& at(int i) {
239     assert(0 <= i && i < _len, "illegal index");
240     return _data[i];
241   }

277   E at_grow(int i, const E& fill = E()) {                                                                                            
278     assert(0 <= i, "negative index");                                                                                                
279     check_nesting();                                                                                                                 
280     if (i >= _len) {                                                                                                                 
281       if (i >= _max) grow(i);                                                                                                        
282       for (int j = _len; j <= i; j++)                                                                                                
283         _data[j] = fill;                                                                                                             
284       _len = i+1;                                                                                                                    
285     }                                                                                                                                
286     return _data[i];                                                                                                                 
287   }                                                                                                                                  
288 
289   void at_put_grow(int i, const E& elem, const E& fill = E()) {                                                                      
290     assert(0 <= i, "negative index");                                                                                                
291     check_nesting();                                                                                                                 
292     raw_at_put_grow(i, elem, fill);                                                                                                  
293   }                                                                                                                                  
294 
295   bool contains(const E& elem) const {                                                                                               
296     for (int i = 0; i < _len; i++) {                                                                                                 
297       if (_data[i] == elem) return true;                                                                                             
298     }                                                                                                                                
299     return false;                                                                                                                    
300   }                                                                                                                                  
301 
302   int  find(const E& elem) const {                                                                                                   
303     for (int i = 0; i < _len; i++) {                                                                                                 
304       if (_data[i] == elem) return i;                                                                                                
305     }                                                                                                                                
306     return -1;                                                                                                                       
307   }                                                                                                                                  
308 
309   int  find_from_end(const E& elem) const {                                                                                          
310     for (int i = _len-1; i >= 0; i--) {                                                                                              
311       if (_data[i] == elem) return i;                                                                                                
312     }                                                                                                                                
313     return -1;                                                                                                                       
314   }                                                                                                                                  
315 
316   int  find(void* token, bool f(void*, E)) const {                                                                                   

287   E at_grow(int i, const E& fill = E()) {
288     assert(0 <= i, "negative index");
289     check_nesting();
290     if (i >= _len) {
291       if (i >= _max) grow(i);
292       for (int j = _len; j <= i; j++)
293         _data[j] = fill;
294       _len = i+1;
295     }
296     return _data[i];
297   }
298 
299   void at_put_grow(int i, const E& elem, const E& fill = E()) {
300     assert(0 <= i, "negative index");
301     check_nesting();
302     raw_at_put_grow(i, elem, fill);
303   }
304 
305   bool contains(const E& elem) const {
306     for (int i = 0; i < _len; i++) {
307       if (safe_equals(_data[i], elem)) return true;
308     }
309     return false;
310   }
311 
312   int  find(const E& elem) const {
313     for (int i = 0; i < _len; i++) {
314       if (_data[i] == elem) return i;
315     }
316     return -1;
317   }
318 
319   int  find_from_end(const E& elem) const {
320     for (int i = _len-1; i >= 0; i--) {
321       if (_data[i] == elem) return i;
322     }
323     return -1;
324   }
325 
326   int  find(void* token, bool f(void*, E)) const {
< prev index next >