From 7d641d5f1fe4e8a83fd078952431bb8ae30b3443 Mon Sep 17 00:00:00 2001 From: Norman Feske Date: Mon, 1 Oct 2018 14:04:50 +0200 Subject: [PATCH] base: add Reconstructible::conditional method The new 'conditional' method simplifies the typical use case for 'Constructible' objects where the constructed/destructed state depends on a configuration parameter. The method alleviates the need to re-implement the logic again and again. The patch also removes the 'Reconstructible' constructor arguments because they are unused. Fixes #3006 --- repos/base-hw/src/bootstrap/platform.cc | 1 - repos/base-hw/src/bootstrap/platform.h | 2 +- repos/base/include/util/reconstructible.h | 20 +++++++++++++++----- repos/gems/src/app/depot_query/main.cc | 8 ++++---- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/repos/base-hw/src/bootstrap/platform.cc b/repos/base-hw/src/bootstrap/platform.cc index 211b8b98a..4fc5fdf12 100644 --- a/repos/base-hw/src/bootstrap/platform.cc +++ b/repos/base-hw/src/bootstrap/platform.cc @@ -163,7 +163,6 @@ static constexpr Genode::Boot_modules_header & header() { Platform::Platform() : bootstrap_region((addr_t)&_prog_img_beg, ((addr_t)&_prog_img_end - (addr_t)&_prog_img_beg)), - core_pd(ram_alloc), core_elf_addr(header().base), core_elf(core_elf_addr) { diff --git a/repos/base-hw/src/bootstrap/platform.h b/repos/base-hw/src/bootstrap/platform.h index 18b814190..8ce41c450 100644 --- a/repos/base-hw/src/bootstrap/platform.h +++ b/repos/base-hw/src/bootstrap/platform.h @@ -127,7 +127,7 @@ class Bootstrap::Platform Bootstrap::Pic pic { }; Ram_allocator ram_alloc { }; Memory_region const bootstrap_region; - Genode::Constructible core_pd; + Genode::Constructible core_pd { }; addr_t core_elf_addr; Elf core_elf; diff --git a/repos/base/include/util/reconstructible.h b/repos/base/include/util/reconstructible.h index 771ff09b3..1dc413e34 100644 --- a/repos/base/include/util/reconstructible.h +++ b/repos/base/include/util/reconstructible.h @@ -136,6 +136,19 @@ class Genode::Reconstructible : Noncopyable */ bool is_constructed() const { return constructed(); } + /** + * Construct or destruct volatile object according to 'condition' + */ + template + void conditional(bool condition, ARGS &&... args) + { + if (condition && !constructed()) + construct(args...); + + if (!condition && constructed()) + destruct(); + } + /** * Access contained object */ @@ -161,11 +174,8 @@ class Genode::Reconstructible : Noncopyable template struct Genode::Constructible : Reconstructible { - template - Constructible(ARGS &&...) - : - Reconstructible((typename Reconstructible::Lazy *)nullptr) - { } + Constructible() + : Reconstructible((typename Reconstructible::Lazy *)nullptr) { } }; #endif /* _INCLUDE__UTIL__RECONSTRUCTIBLE_H_ */ diff --git a/repos/gems/src/app/depot_query/main.cc b/repos/gems/src/app/depot_query/main.cc index f8372a665..21f43d8a8 100644 --- a/repos/gems/src/app/depot_query/main.cc +++ b/repos/gems/src/app/depot_query/main.cc @@ -166,10 +166,10 @@ struct Depot_query::Main typedef Constructible Constructible_reporter; - Constructible_reporter _directory_reporter { _env, "directory" }; - Constructible_reporter _blueprint_reporter { _env, "blueprint" }; - Constructible_reporter _dependencies_reporter { _env, "dependencies" }; - Constructible_reporter _user_reporter { _env, "user" }; + Constructible_reporter _directory_reporter { }; + Constructible_reporter _blueprint_reporter { }; + Constructible_reporter _dependencies_reporter { }; + Constructible_reporter _user_reporter { }; template static void _construct_if(bool condition, Constructible &obj, ARGS &&... args)