From 4060b35b1d00fccbec4b20353063f77c43ecc686 Mon Sep 17 00:00:00 2001 From: Kim Barrett Date: Tue, 2 Jul 2024 08:58:20 +0000 Subject: [PATCH] 8335298: Fix -Wzero-as-null-pointer-constant warning in G1CardSetContainers Reviewed-by: iwalulya, ayang --- .../share/gc/g1/g1CardSetContainers.hpp | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/hotspot/share/gc/g1/g1CardSetContainers.hpp b/src/hotspot/share/gc/g1/g1CardSetContainers.hpp index 43e6c8a3bf7..84e6e3e9274 100644 --- a/src/hotspot/share/gc/g1/g1CardSetContainers.hpp +++ b/src/hotspot/share/gc/g1/g1CardSetContainers.hpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2023, 2024, 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 @@ -86,13 +86,23 @@ class G1CardSetInlinePtr : public StackObj { uint find(uint const card_idx, uint const bits_per_card, uint start_at, uint num_cards); -public: - G1CardSetInlinePtr() : _value_addr(nullptr), _value((ContainerPtr)G1CardSet::ContainerInlinePtr) { } - - G1CardSetInlinePtr(ContainerPtr value) : _value_addr(nullptr), _value(value) { - assert(G1CardSet::container_type(_value) == G1CardSet::ContainerInlinePtr, "Value " PTR_FORMAT " is not a valid G1CardSetInlinePtr.", p2i(_value)); + static ContainerPtr empty_card_set() { + // Work around https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114573 + // gcc issues -Wzero-as-null-pointer-constant here, even though + // ContainerInlinePtr is a *non-literal* constant 0. We cast a non-const + // copy, and let the compiler's constant propagation optimize into + // equivalent code. + static_assert(G1CardSet::ContainerInlinePtr == 0, "unnecessary warning dodge"); + auto value = G1CardSet::ContainerInlinePtr; + return reinterpret_cast(value); } +public: + G1CardSetInlinePtr() : G1CardSetInlinePtr(empty_card_set()) {} + + explicit G1CardSetInlinePtr(ContainerPtr value) : + G1CardSetInlinePtr(nullptr, value) {} + G1CardSetInlinePtr(ContainerPtr volatile* value_addr, ContainerPtr value) : _value_addr(value_addr), _value(value) { assert(G1CardSet::container_type(_value) == G1CardSet::ContainerInlinePtr, "Value " PTR_FORMAT " is not a valid G1CardSetInlinePtr.", p2i(_value)); }