From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 70DBF3858D1E for ; Thu, 17 Aug 2023 10:27:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 70DBF3858D1E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692268021; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=9Hfk1ESxpTjWgXibzarRuPuounaC5SRK2ZgVTAD8ufM=; b=gQmu8P4pvXntr7gtklFql+hbpdeiXkqjHaqZT9fksZbbDAy6ONblD2q3W1+2S2jM61hwck Ejr5SEMqOGOCpwxx8iUnwL1gO9BAhOnUXWaHgH+CcFV3FJuj/ma7+L1kE1c7hFOaByiih5 WxELdpYBlTfxedmKuKwr54AJ7J0QltI= Received: from mail-ej1-f69.google.com (mail-ej1-f69.google.com [209.85.218.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-577-sNd9uquxNxSkk_t7W4hPjQ-1; Thu, 17 Aug 2023 06:26:59 -0400 X-MC-Unique: sNd9uquxNxSkk_t7W4hPjQ-1 Received: by mail-ej1-f69.google.com with SMTP id a640c23a62f3a-99df53612d2so96911066b.1 for ; Thu, 17 Aug 2023 03:26:59 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692268018; x=1692872818; h=mime-version:message-id:date:references:in-reply-to:subject:cc:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=9Hfk1ESxpTjWgXibzarRuPuounaC5SRK2ZgVTAD8ufM=; b=b7Nl+hM7iqFma8afdD6mO6EIgrQzNUsL+4NAPskoF0kDjNcj3wIZfOzjOn6woN32Ft NNlNzzMhBD759+iVcD9xjrdAriC5cmcQvmVtYWYvIBCguE0Pq3IA9yzx8J6bphBHwxdg A4vKOI1ZaGa/b6gqZrGtuHmhbihBHa+BZn5wjDMb8uxKnceKPN+FrrV8MyQT3+X3Ix7d WLarkklmaTLE1LauDoi1IvdAR1sR5m7po5K1XHx2ltpoA3619fuldV7IG5nlNxn05ArW P9mmWIw6CUiNsH2wOPHATYVXqksqaaLTgjrMPGL5jkYJgHr3xFumvq/AiXfp6WZRsoGI 5Szw== X-Gm-Message-State: AOJu0YxFT9mg82hE0NeM0NG2r6muEjKhup8ahxueaF2gTKLmYJSpaShk 4EhHKI9gcBw7554JD31JYdkO2i8rHGhRRFGluO5KcwlVvGQRe0o2cspck3pdwPSJH5TasRUPvLu ElCF2lKdwKYl5cHCYReqvd6ydPjxczQ== X-Received: by 2002:a17:907:3e0b:b0:96f:9cea:a34d with SMTP id hp11-20020a1709073e0b00b0096f9ceaa34dmr2764224ejc.21.1692268017875; Thu, 17 Aug 2023 03:26:57 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGht1iyWIbGirzxFB5luu7w6nwYhb0oojCdWcWtluNL6D3FoL7Tmg8MSFg78PyM2RIWJIlxeA== X-Received: by 2002:a17:907:3e0b:b0:96f:9cea:a34d with SMTP id hp11-20020a1709073e0b00b0096f9ceaa34dmr2764207ejc.21.1692268017469; Thu, 17 Aug 2023 03:26:57 -0700 (PDT) Received: from localhost ([31.111.84.232]) by smtp.gmail.com with ESMTPSA id g8-20020a17090613c800b00992bea2e9d2sm9793416ejc.62.2023.08.17.03.26.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Aug 2023 03:26:56 -0700 (PDT) From: Andrew Burgess To: Tom Tromey Cc: gdb-patches@sourceware.org, Tom Tromey Subject: Re: [PATCH 1/3] gdb: add gdb::make_unique function In-Reply-To: <874jl11vnu.fsf@tromey.com> References: <874jl11vnu.fsf@tromey.com> Date: Thu, 17 Aug 2023 11:26:55 +0100 Message-ID: <87zg2qarrk.fsf@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Tom Tromey writes: >>>>>> "Andrew" == Andrew Burgess writes: > > Andrew> While GDB is still C++11, lets add a gdb::make_unique template > Andrew> function that can be used to create std::unique_ptr objects, just like > Andrew> the C++14 std::make_unique. > > Andrew> When we move to C++14 we can either alias gdb::make_unique to > Andrew> std::make_unique, or just replace the 'gdb::' prefix throughout. > > This looks totally fine to me, but I wonder if this is a situation where > we'd want to use the standard make_unique when it is available, like we > do for some other things. Hi Tom, I couldn't figure out a way to truly 'alias' gdb::make_unique to std::make_unique. For non template functions it is possible to do something like: const auto alias_func = original_func; But I couldn't get anything working for template functions, so I just made gdb::make_unique wrap around a call to std::make_unique (when using C++14), with -O1 this optimises out, so hopefully this should be enough. I figure that at some point GDB will move off C++11, at which time it's easy enough to globally: s/gdb::make_unique/std::make_unique/. Let me know what you think of this. Thanks, Andrew --- commit 7a7a2f37fa6152afd5f7d0b3fb668e081a7f3ed0 Author: Andrew Burgess Date: Thu Aug 10 17:57:46 2023 +0100 gdb: add gdb::make_unique function While GDB is still C++11, lets add a gdb::make_unique template function that can be used to create std::unique_ptr objects, just like the C++14 std::make_unique. If GDB is being compiled with a C++14 compiler then the new gdb::make_unique function will delegate to the std::make_unique. I checked with gcc, and at -O1 and above gdb::make_unique will be optimised away completely in this case. If C++14 (or later) becomes our minimum, then it will be easy enough to go through the code and replace gdb::make_unique with std::make_unique later on. I've make use of this function in all the places I think this can easily be used, though I'm sure I've probably missed some. Should be no user visible changes after this commit. diff --git a/gdb/addrmap.c b/gdb/addrmap.c index 33dc7762d91..d16775d49d4 100644 --- a/gdb/addrmap.c +++ b/gdb/addrmap.c @@ -428,7 +428,7 @@ test_addrmap () /* Create mutable addrmap. */ auto_obstack temp_obstack; - std::unique_ptr map (new addrmap_mutable); + auto map = gdb::make_unique (); SELF_CHECK (map != nullptr); /* Check initial state. */ diff --git a/gdb/break-catch-load.c b/gdb/break-catch-load.c index 440b42852bb..94d8b420d32 100644 --- a/gdb/break-catch-load.c +++ b/gdb/break-catch-load.c @@ -230,9 +230,8 @@ add_solib_catchpoint (const char *arg, bool is_load, bool is_temp, bool enabled) if (*arg == '\0') arg = nullptr; - std::unique_ptr c (new solib_catchpoint (gdbarch, is_temp, - nullptr, - is_load, arg)); + auto c = gdb::make_unique (gdbarch, is_temp, nullptr, + is_load, arg); c->enable_state = enabled ? bp_enabled : bp_disabled; diff --git a/gdb/compile/compile-c-support.c b/gdb/compile/compile-c-support.c index f9b32205b7a..53b7285b366 100644 --- a/gdb/compile/compile-c-support.c +++ b/gdb/compile/compile-c-support.c @@ -118,7 +118,7 @@ get_compile_context (const char *fe_libcc, const char *fe_context, error (_("The loaded version of GCC does not support the required version " "of the API.")); - return std::unique_ptr (new INSTTYPE (context)); + return gdb::make_unique (context); } /* A C-language implementation of get_compile_context. */ diff --git a/gdb/cp-name-parser.y b/gdb/cp-name-parser.y index 80188074202..324166a03ff 100644 --- a/gdb/cp-name-parser.y +++ b/gdb/cp-name-parser.y @@ -2038,7 +2038,7 @@ cp_demangled_name_to_comp (const char *demangled_name, state.demangle_info = allocate_info (); - std::unique_ptr result (new demangle_parse_info); + auto result = gdb::make_unique (); result->info = state.demangle_info; if (yyparse (&state)) diff --git a/gdb/cp-support.c b/gdb/cp-support.c index 0300727434d..2af0218dba0 100644 --- a/gdb/cp-support.c +++ b/gdb/cp-support.c @@ -674,7 +674,7 @@ mangled_name_to_comp (const char *mangled_name, int options, options, memory); if (ret) { - std::unique_ptr info (new demangle_parse_info); + auto info = gdb::make_unique (); info->tree = ret; *demangled_p = NULL; return info; diff --git a/gdb/dwarf2/frame.c b/gdb/dwarf2/frame.c index 940a01e9612..abc8d613482 100644 --- a/gdb/dwarf2/frame.c +++ b/gdb/dwarf2/frame.c @@ -2126,7 +2126,7 @@ dwarf2_build_frame_info (struct objfile *objfile) struct gdbarch *gdbarch = objfile->arch (); /* Build a minimal decoding of the DWARF2 compilation unit. */ - std::unique_ptr unit (new comp_unit (objfile)); + auto unit = gdb::make_unique (objfile); if (objfile->separate_debug_objfile_backlink == NULL) { diff --git a/gdb/dwarf2/read-debug-names.c b/gdb/dwarf2/read-debug-names.c index 3d96bf476f8..2e5067efb3d 100644 --- a/gdb/dwarf2/read-debug-names.c +++ b/gdb/dwarf2/read-debug-names.c @@ -462,7 +462,7 @@ create_cus_from_debug_names (dwarf2_per_bfd *per_bfd, bool dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile) { - std::unique_ptr map (new mapped_debug_names); + auto map = gdb::make_unique (); mapped_debug_names dwz_map; struct objfile *objfile = per_objfile->objfile; dwarf2_per_bfd *per_bfd = per_objfile->per_bfd; diff --git a/gdb/dwarf2/read-gdb-index.c b/gdb/dwarf2/read-gdb-index.c index 1127643e53a..9bfc5302b0e 100644 --- a/gdb/dwarf2/read-gdb-index.c +++ b/gdb/dwarf2/read-gdb-index.c @@ -778,7 +778,7 @@ dwarf2_read_gdb_index if (main_index_contents.empty ()) return 0; - std::unique_ptr map (new mapped_gdb_index); + auto map = gdb::make_unique (); if (!read_gdb_index_from_buffer (objfile_name (objfile), use_deprecated_index_sections, main_index_contents, map.get (), &cu_list, diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c index a64f82bd24a..7d27ff14128 100644 --- a/gdb/dwarf2/read.c +++ b/gdb/dwarf2/read.c @@ -4535,7 +4535,7 @@ allocate_type_unit_groups_table () static std::unique_ptr create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct) { - std::unique_ptr tu_group (new type_unit_group); + auto tu_group = gdb::make_unique (); tu_group->hash.dwo_unit = cu->dwo_unit; tu_group->hash.line_sect_off = line_offset_struct; diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c index d7db7beb554..939fcc23b82 100644 --- a/gdb/gdbtypes.c +++ b/gdb/gdbtypes.c @@ -5853,7 +5853,7 @@ static const struct registry::key void allocate_fixed_point_type_info (struct type *type) { - std::unique_ptr up (new fixed_point_type_info); + auto up = gdb::make_unique (); fixed_point_type_info *info; if (type->is_objfile_owned ()) diff --git a/gdb/python/py-varobj.c b/gdb/python/py-varobj.c index 08e790b7e7e..98603cec009 100644 --- a/gdb/python/py-varobj.c +++ b/gdb/python/py-varobj.c @@ -170,7 +170,5 @@ py_varobj_get_iterator (struct varobj *var, PyObject *printer, error (_("Could not get children iterator")); } - return std::unique_ptr (new py_varobj_iter (var, - std::move (iter), - opts)); + return gdb::make_unique (var, std::move (iter), opts); } diff --git a/gdb/ui-out.c b/gdb/ui-out.c index 9380630c320..b4b8e486cb8 100644 --- a/gdb/ui-out.c +++ b/gdb/ui-out.c @@ -236,9 +236,9 @@ void ui_out_table::append_header (int width, ui_align alignment, internal_error (_("table header must be specified after table_begin and " "before table_body.")); - std::unique_ptr header (new ui_out_hdr (m_headers.size () + 1, - width, alignment, - col_name, col_hdr)); + auto header = gdb::make_unique (m_headers.size () + 1, + width, alignment, + col_name, col_hdr); m_headers.push_back (std::move (header)); } @@ -328,7 +328,7 @@ ui_out::current_level () const void ui_out::push_level (ui_out_type type) { - std::unique_ptr level (new ui_out_level (type)); + auto level = gdb::make_unique (type); m_levels.push_back (std::move (level)); } diff --git a/gdb/unittests/parallel-for-selftests.c b/gdb/unittests/parallel-for-selftests.c index 15a095ae62b..1ad7eaa701c 100644 --- a/gdb/unittests/parallel-for-selftests.c +++ b/gdb/unittests/parallel-for-selftests.c @@ -160,7 +160,7 @@ TEST (int n_threads) { if (start == end) any_empty_tasks = true; - return std::unique_ptr (new int (end - start)); + return gdb::make_unique (end - start); }); SELF_CHECK (!any_empty_tasks); SELF_CHECK (std::all_of (intresults.begin (), @@ -178,7 +178,7 @@ TEST (int n_threads) { if (start == end) any_empty_tasks = true; - return std::unique_ptr (new int (end - start)); + return gdb::make_unique (end - start); }, task_size_one); SELF_CHECK (!any_empty_tasks); diff --git a/gdb/varobj.c b/gdb/varobj.c index 81b8e61f304..e7323bed127 100644 --- a/gdb/varobj.c +++ b/gdb/varobj.c @@ -261,7 +261,7 @@ varobj_create (const char *objname, const char *expression, CORE_ADDR frame, enum varobj_type type) { /* Fill out a varobj structure for the (root) variable being constructed. */ - std::unique_ptr var (new varobj (new varobj_root)); + auto var = gdb::make_unique (new varobj_root); if (expression != NULL) { diff --git a/gdbsupport/gdb_unique_ptr.h b/gdbsupport/gdb_unique_ptr.h index a3ab62405d4..9a0ff0b48e0 100644 --- a/gdbsupport/gdb_unique_ptr.h +++ b/gdbsupport/gdb_unique_ptr.h @@ -56,6 +56,19 @@ struct noop_deleter void operator() (T *ptr) const { } }; +/* Create simple std::unique_ptr objects. */ + +template +std::unique_ptr +make_unique (Arg... args) +{ +#if __cplusplus >= 201402L + return std::make_unique (std::forward (args)...); +#else + return std::unique_ptr (new T (std::forward (args)...)); +#endif /* __cplusplus < 201402L */ +} + } /* namespace gdb */ /* Dup STR and return a unique_xmalloc_ptr for the result. */