8220599: ZGC: Introduce ZSafeDelete
authorpliden
Mon, 18 Mar 2019 11:50:40 +0100
changeset 54173 e6a92f2f37a9
parent 54172 f92f1f1045ad
child 54174 6d1caebf8d37
8220599: ZGC: Introduce ZSafeDelete Reviewed-by: stefank, eosterlund
src/hotspot/share/gc/z/zArray.hpp
src/hotspot/share/gc/z/zArray.inline.hpp
src/hotspot/share/gc/z/zSafeDelete.hpp
src/hotspot/share/gc/z/zSafeDelete.inline.hpp
--- a/src/hotspot/share/gc/z/zArray.hpp	Mon Mar 18 11:50:40 2019 +0100
+++ b/src/hotspot/share/gc/z/zArray.hpp	Mon Mar 18 11:50:40 2019 +0100
@@ -51,6 +51,7 @@
   T at(size_t index) const;
 
   void add(T value);
+  void transfer(ZArray<T>* from);
   void clear();
 };
 
--- a/src/hotspot/share/gc/z/zArray.inline.hpp	Mon Mar 18 11:50:40 2019 +0100
+++ b/src/hotspot/share/gc/z/zArray.inline.hpp	Mon Mar 18 11:50:40 2019 +0100
@@ -80,6 +80,17 @@
 }
 
 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;
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/hotspot/share/gc/z/zSafeDelete.hpp	Mon Mar 18 11:50:40 2019 +0100
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+#ifndef SHARE_GC_Z_ZSAFEDELETE_HPP
+#define SHARE_GC_Z_ZSAFEDELETE_HPP
+
+#include "gc/z/zArray.hpp"
+#include "gc/z/zLock.hpp"
+#include "metaprogramming/removeExtent.hpp"
+
+template <typename T>
+class ZSafeDelete {
+private:
+  typedef typename RemoveExtent<T>::type ItemT;
+
+  ZLock          _lock;
+  uint64_t       _enabled;
+  ZArray<ItemT*> _deferred;
+
+  bool deferred_delete(ItemT* item);
+  void immediate_delete(ItemT* item);
+
+public:
+  ZSafeDelete();
+
+  void enable_deferred_delete();
+  void disable_deferred_delete();
+
+  void operator()(ItemT* item);
+};
+
+#endif // SHARE_GC_Z_ZSAFEDELETE_HPP
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/hotspot/share/gc/z/zSafeDelete.inline.hpp	Mon Mar 18 11:50:40 2019 +0100
@@ -0,0 +1,89 @@
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+#ifndef SHARE_GC_Z_ZSAFEDELETE_INLINE_HPP
+#define SHARE_GC_Z_ZSAFEDELETE_INLINE_HPP
+
+#include "gc/z/zArray.inline.hpp"
+#include "gc/z/zSafeDelete.hpp"
+#include "metaprogramming/isArray.hpp"
+#include "utilities/debug.hpp"
+
+template <typename T>
+ZSafeDelete<T>::ZSafeDelete() :
+    _lock(),
+    _enabled(0),
+    _deferred() {}
+
+template <typename T>
+bool ZSafeDelete<T>::deferred_delete(ItemT* item) {
+  ZLocker<ZLock> locker(&_lock);
+  if (_enabled > 0) {
+    _deferred.add(item);
+    return true;
+  }
+
+  return false;
+}
+
+template <typename T>
+void ZSafeDelete<T>::immediate_delete(ItemT* item) {
+  if (IsArray<T>::value) {
+    delete [] item;
+  } else {
+    delete item;
+  }
+}
+
+template <typename T>
+void ZSafeDelete<T>::enable_deferred_delete() {
+  ZLocker<ZLock> locker(&_lock);
+  _enabled++;
+}
+
+template <typename T>
+void ZSafeDelete<T>::disable_deferred_delete() {
+  ZArray<ItemT*> deferred;
+
+  {
+    ZLocker<ZLock> locker(&_lock);
+    assert(_enabled > 0, "Invalid state");
+    if (--_enabled == 0) {
+      deferred.transfer(&_deferred);
+    }
+  }
+
+  ZArrayIterator<ItemT*> iter(&deferred);
+  for (ItemT* item; iter.next(&item);) {
+    immediate_delete(item);
+  }
+}
+
+template <typename T>
+void ZSafeDelete<T>::operator()(ItemT* item) {
+  if (!deferred_delete(item)) {
+    immediate_delete(item);
+  }
+}
+
+#endif // SHARE_GC_Z_ZSAFEDELETE_INLINE_HPP