growableArray.hpp revision 0
/*
* Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*
*/
// A growable array.
/*************************************************************************/
/* */
/* WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING */
/* */
/* Should you use GrowableArrays to contain handles you must be certain */
/* the the GrowableArray does not outlive the HandleMark that contains */
/* the handles. Since GrowableArrays are typically resource allocated */
/* the following is an example of INCORRECT CODE, */
/* */
/* ResourceMark rm; */
/* GrowableArray<Handle>* arr = new GrowableArray<Handle>(size); */
/* if (blah) { */
/* while (...) { */
/* HandleMark hm; */
/* ... */
/* Handle h(THREAD, some_oop); */
/* arr->append(h); */
/* } */
/* } */
/* if (arr->length() != 0 ) { */
/* oop bad_oop = arr->at(0)(); // Handle is BAD HERE. */
/* ... */
/* } */
/* */
/* If the GrowableArrays you are creating is C_Heap allocated then it */
/* hould not old handles since the handles could trivially try and */
/* outlive their HandleMark. In some situations you might need to do */
/* this and it would be legal but be very careful and see if you can do */
/* the code in some other manner. */
/* */
/*************************************************************************/
// To call default constructor the placement operator new() is used.
// It should be empty (it only returns the passed void* pointer).
// The definition of placement operator new(size_t, void*) in the <new>.
#include <new>
// Need the correct linkage to call qsort without warnings
extern "C" {
typedef int (*_sort_Fn)(const void *, const void *);
}
class GenericGrowableArray : public ResourceObj {
protected:
int _len; // current length
int _max; // maximum length
// 0 means default ResourceArea
// 1 means on C heap
// otherwise, allocate in _arena
#ifdef ASSERT
int _nesting; // resource area nesting at creation
void set_nesting();
void check_nesting();
#else
#define set_nesting();
#define check_nesting();
#endif
// Where are we going to allocate memory?
// This GA will use the resource stack for storage if c_heap==false,
// Else it will use the C heap. Use clear_and_deallocate to avoid leaks.
_len = initial_len;
_max = initial_size;
set_nesting();
}
// This GA will use the given arena for storage.
// Consider using new(arena) GrowableArray<T> to allocate the header.
_len = initial_len;
_max = initial_size;
}
void* raw_allocate(int elementSize);
};
template<class E> class GrowableArray : public GenericGrowableArray {
private:
E* _data; // data array
void grow(int j);
void raw_at_put_grow(int i, const E& p, const E& fill);
void clear_and_deallocate();
public:
GrowableArray(int initial_size, bool C_heap = false) : GenericGrowableArray(initial_size, 0, C_heap) {
_data = (E*)raw_allocate(sizeof(E));
}
GrowableArray(int initial_size, int initial_len, const E& filler, bool C_heap = false) : GenericGrowableArray(initial_size, initial_len, C_heap) {
_data = (E*)raw_allocate(sizeof(E));
int i = 0;
}
GrowableArray(Arena* arena, int initial_size, int initial_len, const E& filler) : GenericGrowableArray(arena, initial_size, initial_len) {
_data = (E*)raw_allocate(sizeof(E));
int i = 0;
}
_data = (E*)raw_allocate(sizeof(E));
::new ((void*)&_data[0]) E();
::new ((void*)&_data[1]) E();
}
// Does nothing for resource and arena objects
bool is_nonempty() const { return _len != 0; }
void print();
}
void append_if_missing(const E& elem) {
}
E at(int i) const {
return _data[i];
}
E* adr_at(int i) const {
return &_data[i];
}
E first() const {
return _data[0];
}
E top() const {
}
E pop() {
}
}
assert(0 <= i, "negative index");
if (i >= _len) {
for (int j = _len; j <= i; j++)
_len = i+1;
}
return _data[i];
}
assert(0 <= i, "negative index");
}
for (int i = 0; i < _len; i++) {
}
return false;
}
for (int i = 0; i < _len; i++) {
}
return -1;
}
for (int i = 0; i < _len; i++) {
}
return -1;
}
int find_at_end(void* token, bool f(void*, E)) const {
// start at the end of the array
for (int i = _len-1; i >= 0; i--) {
}
return -1;
}
for (int i = 0; i < _len; i++) {
_len--;
return;
}
}
}
_len--;
}
void appendAll(const GrowableArray<E>* l) {
for (int i = 0; i < l->_len; i++) {
}
}
void sort(int f(E*,E*)) {
}
// sort by fixed-stride sub arrays:
}
};
// Global GrowableArray methods (one instance in the library per each 'E' type).
template<class E> void GrowableArray<E>::grow(int j) {
// grow the array by doubling its size (amortized growth)
// j < _max
E* newData = (E*)raw_allocate(sizeof(E));
int i = 0;
}
}
if (i >= _len) {
for (int j = _len; j < i; j++)
_len = i+1;
}
_data[i] = p;
}
// This function clears and deallocate the data in the growable array that
// has been allocated on the C heap. It's not public - called by the
// destructor.
template<class E> void GrowableArray<E>::clear_and_deallocate() {
"clear_and_deallocate should only be called when on C heap");
clear();
}
}
template<class E> void GrowableArray<E>::print() {
}