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.

Approved-By: Tom Tromey <tom@tromey.com>
This commit is contained in:
Andrew Burgess
2023-08-10 17:57:46 +01:00
parent adc5f8b99a
commit 0b72cde372
15 changed files with 32 additions and 22 deletions

View File

@@ -428,7 +428,7 @@ test_addrmap ()
/* Create mutable addrmap. */ /* Create mutable addrmap. */
auto_obstack temp_obstack; auto_obstack temp_obstack;
std::unique_ptr<struct addrmap_mutable> map (new addrmap_mutable); auto map = gdb::make_unique<struct addrmap_mutable> ();
SELF_CHECK (map != nullptr); SELF_CHECK (map != nullptr);
/* Check initial state. */ /* Check initial state. */

View File

@@ -230,9 +230,8 @@ add_solib_catchpoint (const char *arg, bool is_load, bool is_temp, bool enabled)
if (*arg == '\0') if (*arg == '\0')
arg = nullptr; arg = nullptr;
std::unique_ptr<solib_catchpoint> c (new solib_catchpoint (gdbarch, is_temp, auto c = gdb::make_unique<solib_catchpoint> (gdbarch, is_temp, nullptr,
nullptr, is_load, arg);
is_load, arg));
c->enable_state = enabled ? bp_enabled : bp_disabled; c->enable_state = enabled ? bp_enabled : bp_disabled;

View File

@@ -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 " error (_("The loaded version of GCC does not support the required version "
"of the API.")); "of the API."));
return std::unique_ptr<compile_instance> (new INSTTYPE (context)); return gdb::make_unique<INSTTYPE> (context);
} }
/* A C-language implementation of get_compile_context. */ /* A C-language implementation of get_compile_context. */

View File

@@ -2038,7 +2038,7 @@ cp_demangled_name_to_comp (const char *demangled_name,
state.demangle_info = allocate_info (); state.demangle_info = allocate_info ();
std::unique_ptr<demangle_parse_info> result (new demangle_parse_info); auto result = gdb::make_unique<demangle_parse_info> ();
result->info = state.demangle_info; result->info = state.demangle_info;
if (yyparse (&state)) if (yyparse (&state))

View File

@@ -674,7 +674,7 @@ mangled_name_to_comp (const char *mangled_name, int options,
options, memory); options, memory);
if (ret) if (ret)
{ {
std::unique_ptr<demangle_parse_info> info (new demangle_parse_info); auto info = gdb::make_unique<demangle_parse_info> ();
info->tree = ret; info->tree = ret;
*demangled_p = NULL; *demangled_p = NULL;
return info; return info;

View File

@@ -2126,7 +2126,7 @@ dwarf2_build_frame_info (struct objfile *objfile)
struct gdbarch *gdbarch = objfile->arch (); struct gdbarch *gdbarch = objfile->arch ();
/* Build a minimal decoding of the DWARF2 compilation unit. */ /* Build a minimal decoding of the DWARF2 compilation unit. */
std::unique_ptr<comp_unit> unit (new comp_unit (objfile)); auto unit = gdb::make_unique<comp_unit> (objfile);
if (objfile->separate_debug_objfile_backlink == NULL) if (objfile->separate_debug_objfile_backlink == NULL)
{ {

View File

@@ -462,7 +462,7 @@ create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
bool bool
dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile) dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
{ {
std::unique_ptr<mapped_debug_names> map (new mapped_debug_names); auto map = gdb::make_unique<mapped_debug_names> ();
mapped_debug_names dwz_map; mapped_debug_names dwz_map;
struct objfile *objfile = per_objfile->objfile; struct objfile *objfile = per_objfile->objfile;
dwarf2_per_bfd *per_bfd = per_objfile->per_bfd; dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;

View File

@@ -778,7 +778,7 @@ dwarf2_read_gdb_index
if (main_index_contents.empty ()) if (main_index_contents.empty ())
return 0; return 0;
std::unique_ptr<mapped_gdb_index> map (new mapped_gdb_index); auto map = gdb::make_unique<mapped_gdb_index> ();
if (!read_gdb_index_from_buffer (objfile_name (objfile), if (!read_gdb_index_from_buffer (objfile_name (objfile),
use_deprecated_index_sections, use_deprecated_index_sections,
main_index_contents, map.get (), &cu_list, main_index_contents, map.get (), &cu_list,

View File

@@ -4535,7 +4535,7 @@ allocate_type_unit_groups_table ()
static std::unique_ptr<type_unit_group> static std::unique_ptr<type_unit_group>
create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct) create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
{ {
std::unique_ptr<type_unit_group> tu_group (new type_unit_group); auto tu_group = gdb::make_unique<type_unit_group> ();
tu_group->hash.dwo_unit = cu->dwo_unit; tu_group->hash.dwo_unit = cu->dwo_unit;
tu_group->hash.line_sect_off = line_offset_struct; tu_group->hash.line_sect_off = line_offset_struct;

View File

@@ -5853,7 +5853,7 @@ static const struct registry<objfile>::key<fixed_point_type_storage>
void void
allocate_fixed_point_type_info (struct type *type) allocate_fixed_point_type_info (struct type *type)
{ {
std::unique_ptr<fixed_point_type_info> up (new fixed_point_type_info); auto up = gdb::make_unique<fixed_point_type_info> ();
fixed_point_type_info *info; fixed_point_type_info *info;
if (type->is_objfile_owned ()) if (type->is_objfile_owned ())

View File

@@ -170,7 +170,5 @@ py_varobj_get_iterator (struct varobj *var, PyObject *printer,
error (_("Could not get children iterator")); error (_("Could not get children iterator"));
} }
return std::unique_ptr<varobj_iter> (new py_varobj_iter (var, return gdb::make_unique<py_varobj_iter> (var, std::move (iter), opts);
std::move (iter),
opts));
} }

View File

@@ -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 " internal_error (_("table header must be specified after table_begin and "
"before table_body.")); "before table_body."));
std::unique_ptr<ui_out_hdr> header (new ui_out_hdr (m_headers.size () + 1, auto header = gdb::make_unique<ui_out_hdr> (m_headers.size () + 1,
width, alignment, width, alignment,
col_name, col_hdr)); col_name, col_hdr);
m_headers.push_back (std::move (header)); m_headers.push_back (std::move (header));
} }
@@ -328,7 +328,7 @@ ui_out::current_level () const
void void
ui_out::push_level (ui_out_type type) ui_out::push_level (ui_out_type type)
{ {
std::unique_ptr<ui_out_level> level (new ui_out_level (type)); auto level = gdb::make_unique<ui_out_level> (type);
m_levels.push_back (std::move (level)); m_levels.push_back (std::move (level));
} }

View File

@@ -160,7 +160,7 @@ TEST (int n_threads)
{ {
if (start == end) if (start == end)
any_empty_tasks = true; any_empty_tasks = true;
return std::unique_ptr<int> (new int (end - start)); return gdb::make_unique<int> (end - start);
}); });
SELF_CHECK (!any_empty_tasks); SELF_CHECK (!any_empty_tasks);
SELF_CHECK (std::all_of (intresults.begin (), SELF_CHECK (std::all_of (intresults.begin (),
@@ -178,7 +178,7 @@ TEST (int n_threads)
{ {
if (start == end) if (start == end)
any_empty_tasks = true; any_empty_tasks = true;
return std::unique_ptr<int> (new int (end - start)); return gdb::make_unique<int> (end - start);
}, },
task_size_one); task_size_one);
SELF_CHECK (!any_empty_tasks); SELF_CHECK (!any_empty_tasks);

View File

@@ -261,7 +261,7 @@ varobj_create (const char *objname,
const char *expression, CORE_ADDR frame, enum varobj_type type) const char *expression, CORE_ADDR frame, enum varobj_type type)
{ {
/* Fill out a varobj structure for the (root) variable being constructed. */ /* Fill out a varobj structure for the (root) variable being constructed. */
std::unique_ptr<varobj> var (new varobj (new varobj_root)); auto var = gdb::make_unique<varobj> (new varobj_root);
if (expression != NULL) if (expression != NULL)
{ {

View File

@@ -56,6 +56,19 @@ struct noop_deleter
void operator() (T *ptr) const { } void operator() (T *ptr) const { }
}; };
/* Create simple std::unique_ptr<T> objects. */
template<typename T, typename... Arg>
std::unique_ptr<T>
make_unique (Arg &&...args)
{
#if __cplusplus >= 201402L
return std::make_unique<T> (std::forward<Arg> (args)...);
#else
return std::unique_ptr<T> (new T (std::forward<Arg> (args)...));
#endif /* __cplusplus < 201402L */
}
} /* namespace gdb */ } /* namespace gdb */
/* Dup STR and return a unique_xmalloc_ptr for the result. */ /* Dup STR and return a unique_xmalloc_ptr for the result. */