Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
8247759: ZGC: Replace ZGC specific array implementations with Growabl…
…eArray

Reviewed-by: pliden
  • Loading branch information
stefank committed Aug 27, 2020
1 parent c24e7ba commit 46b5560bd0bef136b6c56dc9b3fcd0030ee97456
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 288 deletions.
@@ -83,7 +83,7 @@ void ZMountPoint::get_mountpoints(const char* filesystem, ZArray<char*>* mountpo
while (getline(&line, &length, fd) != -1) {
char* const mountpoint = get_mountpoint(line, filesystem);
if (mountpoint != NULL) {
mountpoints->add(mountpoint);
mountpoints->append(mountpoint);
}
}

@@ -129,10 +129,10 @@ char* ZMountPoint::find_mountpoint(const char* filesystem, const char** preferre

get_mountpoints(filesystem, &mountpoints);

if (mountpoints.size() == 0) {
if (mountpoints.length() == 0) {
// No mount point found
log_error_p(gc)("Failed to find an accessible %s filesystem", filesystem);
} else if (mountpoints.size() == 1) {
} else if (mountpoints.length() == 1) {
// One mount point found
path = strdup(mountpoints.at(0));
} else {
@@ -26,39 +26,21 @@

#include "memory/allocation.hpp"
#include "utilities/globalDefinitions.hpp"
#include "utilities/growableArray.hpp"

template <typename T>
class ZArray {
private:
static const size_t initial_capacity = 32;

T* _array;
size_t _size;
size_t _capacity;

NONCOPYABLE(ZArray);

void expand(size_t new_capacity);

class ZArray : public GrowableArrayCHeap<T, mtGC> {
public:
ZArray();
~ZArray();

size_t size() const;
bool is_empty() const;

T at(size_t index) const;

void add(T value);
void transfer(ZArray<T>* from);
void clear();
};

template <typename T, bool parallel>
class ZArrayIteratorImpl : public StackObj {
private:
ZArray<T>* const _array;
size_t _next;
int _next;

public:
ZArrayIteratorImpl(ZArray<T>* array);
@@ -70,16 +52,7 @@ class ZArrayIteratorImpl : public StackObj {
#define ZARRAY_SERIAL false
#define ZARRAY_PARALLEL true

template <typename T>
class ZArrayIterator : public ZArrayIteratorImpl<T, ZARRAY_SERIAL> {
public:
ZArrayIterator(ZArray<T>* array);
};

template <typename T>
class ZArrayParallelIterator : public ZArrayIteratorImpl<T, ZARRAY_PARALLEL> {
public:
ZArrayParallelIterator(ZArray<T>* array);
};
template <typename T> using ZArrayIterator = ZArrayIteratorImpl<T, ZARRAY_SERIAL>;
template <typename T> using ZArrayParallelIterator = ZArrayIteratorImpl<T, ZARRAY_PARALLEL>;

#endif // SHARE_GC_Z_ZARRAY_HPP
@@ -30,67 +30,17 @@

template <typename T>
inline ZArray<T>::ZArray() :
_array(NULL),
_size(0),
_capacity(0) {}

template <typename T>
inline ZArray<T>::~ZArray() {
FREE_C_HEAP_ARRAY(T, _array);
}

template <typename T>
inline size_t ZArray<T>::size() const {
return _size;
}

template <typename T>
inline bool ZArray<T>::is_empty() const {
return size() == 0;
}

template <typename T>
inline T ZArray<T>::at(size_t index) const {
assert(index < _size, "Index out of bounds");
return _array[index];
}

template <typename T>
inline void ZArray<T>::expand(size_t new_capacity) {
T* new_array = NEW_C_HEAP_ARRAY(T, new_capacity, mtGC);
if (_array != NULL) {
memcpy(new_array, _array, sizeof(T) * _capacity);
FREE_C_HEAP_ARRAY(T, _array);
}

_array = new_array;
_capacity = new_capacity;
}

template <typename T>
inline void ZArray<T>::add(T value) {
if (_size == _capacity) {
const size_t new_capacity = (_capacity > 0) ? _capacity * 2 : initial_capacity;
expand(new_capacity);
}

_array[_size++] = value;
}
GrowableArrayCHeap<T, mtGC>(0) {}

template <typename T>
inline void ZArray<T>::transfer(ZArray<T>* from) {
assert(_array == NULL, "Should be empty");
_array = from->_array;
_size = from->_size;
_capacity = from->_capacity;
from->_array = NULL;
from->_size = 0;
from->_capacity = 0;
}

template <typename T>
inline void ZArray<T>::clear() {
_size = 0;
assert(this->_data == NULL, "Should be empty");
this->_data = from->_data;
this->_len = from->_len;
this->_max = from->_max;
from->_data = NULL;
from->_len = 0;
from->_max = 0;
}

template <typename T, bool parallel>
@@ -101,13 +51,13 @@ inline ZArrayIteratorImpl<T, parallel>::ZArrayIteratorImpl(ZArray<T>* array) :
template <typename T, bool parallel>
inline bool ZArrayIteratorImpl<T, parallel>::next(T* elem) {
if (parallel) {
const size_t next = Atomic::fetch_and_add(&_next, 1u);
if (next < _array->size()) {
const int next = Atomic::fetch_and_add(&_next, 1);
if (next < _array->length()) {
*elem = _array->at(next);
return true;
}
} else {
if (_next < _array->size()) {
if (_next < _array->length()) {
*elem = _array->at(_next++);
return true;
}
@@ -117,12 +67,4 @@ inline bool ZArrayIteratorImpl<T, parallel>::next(T* elem) {
return false;
}

template <typename T>
inline ZArrayIterator<T>::ZArrayIterator(ZArray<T>* array) :
ZArrayIteratorImpl<T, ZARRAY_SERIAL>(array) {}

template <typename T>
inline ZArrayParallelIterator<T>::ZArrayParallelIterator(ZArray<T>* array) :
ZArrayIteratorImpl<T, ZARRAY_PARALLEL>(array) {}

#endif // SHARE_GC_Z_ZARRAY_INLINE_HPP

0 comments on commit 46b5560

Please sign in to comment.