1 /*
2 * Copyright (c) 1997, 2010, 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 *
60 /* If the GrowableArrays you are creating is C_Heap allocated then it */
61 /* hould not old handles since the handles could trivially try and */
62 /* outlive their HandleMark. In some situations you might need to do */
63 /* this and it would be legal but be very careful and see if you can do */
64 /* the code in some other manner. */
65 /* */
66 /*************************************************************************/
67
68 // To call default constructor the placement operator new() is used.
69 // It should be empty (it only returns the passed void* pointer).
70 // The definition of placement operator new(size_t, void*) in the <new>.
71
72 #include <new>
73
74 // Need the correct linkage to call qsort without warnings
75 extern "C" {
76 typedef int (*_sort_Fn)(const void *, const void *);
77 }
78
79 class GenericGrowableArray : public ResourceObj {
80 protected:
81 int _len; // current length
82 int _max; // maximum length
83 Arena* _arena; // Indicates where allocation occurs:
84 // 0 means default ResourceArea
85 // 1 means on C heap
86 // otherwise, allocate in _arena
87 #ifdef ASSERT
88 int _nesting; // resource area nesting at creation
89 void set_nesting();
90 void check_nesting();
91 #else
92 #define set_nesting();
93 #define check_nesting();
94 #endif
95
96 // Where are we going to allocate memory?
97 bool on_C_heap() { return _arena == (Arena*)1; }
98 bool on_stack () { return _arena == NULL; }
99 bool on_arena () { return _arena > (Arena*)1; }
119 _max = initial_size;
120 assert(_len >= 0 && _len <= _max, "initial_len too big");
121 _arena = arena;
122 assert(on_arena(), "arena has taken on reserved value 0 or 1");
123 // Relax next assert to allow object allocation on resource area,
124 // on stack or embedded into an other object.
125 assert(allocated_on_arena() || allocated_on_stack(),
126 "growable array must be on arena or on stack if elements are on arena");
127 }
128
129 void* raw_allocate(int elementSize);
130
131 // some uses pass the Thread explicitly for speed (4990299 tuning)
132 void* raw_allocate(Thread* thread, int elementSize) {
133 assert(on_stack(), "fast ResourceObj path only");
134 return (void*)resource_allocate_bytes(thread, elementSize * _max);
135 }
136 };
137
138 template<class E> class GrowableArray : public GenericGrowableArray {
139 private:
140 E* _data; // data array
141
142 void grow(int j);
143 void raw_at_put_grow(int i, const E& p, const E& fill);
144 void clear_and_deallocate();
145 public:
146 GrowableArray(Thread* thread, int initial_size) : GenericGrowableArray(initial_size, 0, false) {
147 _data = (E*)raw_allocate(thread, sizeof(E));
148 for (int i = 0; i < _max; i++) ::new ((void*)&_data[i]) E();
149 }
150
151 GrowableArray(int initial_size, bool C_heap = false) : GenericGrowableArray(initial_size, 0, C_heap) {
152 _data = (E*)raw_allocate(sizeof(E));
153 for (int i = 0; i < _max; i++) ::new ((void*)&_data[i]) E();
154 }
155
156 GrowableArray(int initial_size, int initial_len, const E& filler, bool C_heap = false) : GenericGrowableArray(initial_size, initial_len, C_heap) {
157 _data = (E*)raw_allocate(sizeof(E));
158 int i = 0;
|
1 /*
2 * Copyright (c) 1997, 2011, 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 *
60 /* If the GrowableArrays you are creating is C_Heap allocated then it */
61 /* hould not old handles since the handles could trivially try and */
62 /* outlive their HandleMark. In some situations you might need to do */
63 /* this and it would be legal but be very careful and see if you can do */
64 /* the code in some other manner. */
65 /* */
66 /*************************************************************************/
67
68 // To call default constructor the placement operator new() is used.
69 // It should be empty (it only returns the passed void* pointer).
70 // The definition of placement operator new(size_t, void*) in the <new>.
71
72 #include <new>
73
74 // Need the correct linkage to call qsort without warnings
75 extern "C" {
76 typedef int (*_sort_Fn)(const void *, const void *);
77 }
78
79 class GenericGrowableArray : public ResourceObj {
80 friend class VMStructs;
81
82 protected:
83 int _len; // current length
84 int _max; // maximum length
85 Arena* _arena; // Indicates where allocation occurs:
86 // 0 means default ResourceArea
87 // 1 means on C heap
88 // otherwise, allocate in _arena
89 #ifdef ASSERT
90 int _nesting; // resource area nesting at creation
91 void set_nesting();
92 void check_nesting();
93 #else
94 #define set_nesting();
95 #define check_nesting();
96 #endif
97
98 // Where are we going to allocate memory?
99 bool on_C_heap() { return _arena == (Arena*)1; }
100 bool on_stack () { return _arena == NULL; }
101 bool on_arena () { return _arena > (Arena*)1; }
121 _max = initial_size;
122 assert(_len >= 0 && _len <= _max, "initial_len too big");
123 _arena = arena;
124 assert(on_arena(), "arena has taken on reserved value 0 or 1");
125 // Relax next assert to allow object allocation on resource area,
126 // on stack or embedded into an other object.
127 assert(allocated_on_arena() || allocated_on_stack(),
128 "growable array must be on arena or on stack if elements are on arena");
129 }
130
131 void* raw_allocate(int elementSize);
132
133 // some uses pass the Thread explicitly for speed (4990299 tuning)
134 void* raw_allocate(Thread* thread, int elementSize) {
135 assert(on_stack(), "fast ResourceObj path only");
136 return (void*)resource_allocate_bytes(thread, elementSize * _max);
137 }
138 };
139
140 template<class E> class GrowableArray : public GenericGrowableArray {
141 friend class VMStructs;
142
143 private:
144 E* _data; // data array
145
146 void grow(int j);
147 void raw_at_put_grow(int i, const E& p, const E& fill);
148 void clear_and_deallocate();
149 public:
150 GrowableArray(Thread* thread, int initial_size) : GenericGrowableArray(initial_size, 0, false) {
151 _data = (E*)raw_allocate(thread, sizeof(E));
152 for (int i = 0; i < _max; i++) ::new ((void*)&_data[i]) E();
153 }
154
155 GrowableArray(int initial_size, bool C_heap = false) : GenericGrowableArray(initial_size, 0, C_heap) {
156 _data = (E*)raw_allocate(sizeof(E));
157 for (int i = 0; i < _max; i++) ::new ((void*)&_data[i]) E();
158 }
159
160 GrowableArray(int initial_size, int initial_len, const E& filler, bool C_heap = false) : GenericGrowableArray(initial_size, initial_len, C_heap) {
161 _data = (E*)raw_allocate(sizeof(E));
162 int i = 0;
|