From 2e76f4fc58d0b71be835d23f518bcfeec704f281 Mon Sep 17 00:00:00 2001 From: Norman Feske Date: Sat, 5 Apr 2025 13:27:05 +0200 Subject: [PATCH] base: move common exception types to exception.h The univerally used exception types Out_of_ram, Out_of_caps, and Denied used to be defined at quota_guard.h and ram_allocator.h whereas the types are broadly used. This patch gathers those type definitions at the central place exception.h instead, to gradually untangle the reliance on exceptions, i.e., in quota_goard.h, and to make the output of error messages printing exception types ('abort') more concise. Issue #5245 --- repos/base/include/base/allocator.h | 7 ++----- repos/base/include/base/exception.h | 12 ++++++++---- repos/base/include/base/quota_guard.h | 10 +++++----- repos/base/include/base/ram_allocator.h | 5 ++--- repos/base/include/session/session.h | 3 ++- repos/base/src/core/include/account.h | 16 ++++++++-------- 6 files changed, 27 insertions(+), 26 deletions(-) diff --git a/repos/base/include/base/allocator.h b/repos/base/include/base/allocator.h index 48e4b4d3e9..365369cea2 100644 --- a/repos/base/include/base/allocator.h +++ b/repos/base/include/base/allocator.h @@ -60,16 +60,13 @@ struct Genode::Deallocator : Interface struct Genode::Allocator : Deallocator { - /** - * Exception type - */ using Out_of_memory = Out_of_ram; - using Denied = Ram_allocator::Denied; + using Denied = Genode::Denied; /** * Return type of 'try_alloc' */ - using Alloc_error = Ram_allocator::Alloc_error; + using Alloc_error = Genode::Alloc_error; using Alloc_result = Attempt; /** diff --git a/repos/base/include/base/exception.h b/repos/base/include/base/exception.h index 8ea9375734..b539164eec 100644 --- a/repos/base/include/base/exception.h +++ b/repos/base/include/base/exception.h @@ -1,11 +1,11 @@ /* - * \brief Exception base class + * \brief Common exception types * \author Norman Feske * \date 2008-03-22 */ /* - * Copyright (C) 2008-2017 Genode Labs GmbH + * Copyright (C) 2008-2025 Genode Labs GmbH * * This file is part of the Genode OS framework, which is distributed * under the terms of the GNU Affero General Public License version 3. @@ -14,8 +14,12 @@ #ifndef _INCLUDE__BASE__EXCEPTION_H_ #define _INCLUDE__BASE__EXCEPTION_H_ -namespace Genode { class Exception; } +namespace Genode { -class Genode::Exception { }; + struct Exception { }; + struct Out_of_ram : Exception { }; + struct Out_of_caps : Exception { }; + struct Denied : Exception { }; +} #endif /* _INCLUDE__BASE__EXCEPTION_H_ */ diff --git a/repos/base/include/base/quota_guard.h b/repos/base/include/base/quota_guard.h index 394bc6c7f1..cbdae81320 100644 --- a/repos/base/include/base/quota_guard.h +++ b/repos/base/include/base/quota_guard.h @@ -22,6 +22,8 @@ namespace Genode { struct Ram_quota { + using Exhausted_exception = Out_of_ram; + size_t value; static char const *name() { return "bytes"; } @@ -31,6 +33,8 @@ namespace Genode { struct Cap_quota { + using Exhausted_exception = Out_of_caps; + size_t value; static char const *name() { return "caps"; } @@ -171,7 +175,7 @@ class Genode::Quota_guard public: - struct Limit_exceeded : Exception { }; + using Limit_exceeded = typename UNIT::Exhausted_exception; Quota_guard() { } Quota_guard(UNIT amount) { upgrade(amount); } @@ -288,12 +292,8 @@ class Genode::Quota_guard namespace Genode { - using Ram_quota_guard = Quota_guard; using Cap_quota_guard = Quota_guard; - - using Out_of_ram = Ram_quota_guard::Limit_exceeded; - using Out_of_caps = Cap_quota_guard::Limit_exceeded; } #endif /* _INCLUDE__BASE__QUOTA_GUARD_H_ */ diff --git a/repos/base/include/base/ram_allocator.h b/repos/base/include/base/ram_allocator.h index e9739417fb..b7ffe9e704 100644 --- a/repos/base/include/base/ram_allocator.h +++ b/repos/base/include/base/ram_allocator.h @@ -31,8 +31,6 @@ namespace Genode { struct Genode::Ram_allocator : Ram::Unmapped_allocator { - struct Denied : Exception { }; - /** * Allocate RAM * @@ -75,6 +73,7 @@ struct Genode::Ram_allocator : Ram::Unmapped_allocator /* type aliases used for API transition */ using Alloc_result = Ram::Unmapped_allocator::Result; using Alloc_error = Ram::Error; + using Denied = Genode::Denied; }; @@ -87,7 +86,7 @@ namespace Genode { case Ram::Error::OUT_OF_CAPS: throw Out_of_caps(); case Ram::Error::DENIED: break; } - throw Ram_allocator::Denied(); + throw Denied(); } } diff --git a/repos/base/include/session/session.h b/repos/base/include/session/session.h index 7a4eec48fa..a627ea0707 100644 --- a/repos/base/include/session/session.h +++ b/repos/base/include/session/session.h @@ -14,6 +14,7 @@ #ifndef _INCLUDE__SESSION__SESSION_H_ #define _INCLUDE__SESSION__SESSION_H_ +#include #include #include #include @@ -34,7 +35,7 @@ namespace Genode { */ struct Insufficient_ram_quota : Exception { }; struct Insufficient_cap_quota : Exception { }; - struct Service_denied : Exception { }; + using Service_denied = Denied; } diff --git a/repos/base/src/core/include/account.h b/repos/base/src/core/include/account.h index 7d3b8dec9a..1bcdb60b2a 100644 --- a/repos/base/src/core/include/account.h +++ b/repos/base/src/core/include/account.h @@ -27,6 +27,11 @@ namespace Core { template class Account; } template class Core::Account { + public: + + using Limit_exceeded = UNIT::Exhausted_exception; + using Guard = Quota_guard; + private: /* @@ -35,7 +40,7 @@ class Core::Account Account(Account const &); Account &operator = (Account const &); - Quota_guard &_quota_guard; + Guard &_quota_guard; Session::Label const &_label; @@ -88,16 +93,11 @@ class Core::Account public: - using Limit_exceeded = typename Quota_guard::Limit_exceeded; - - class Unrelated_account : Exception { }; - /** * Constructor for creating a regular account that is rechargeable by * the specified reference account */ - Account(Quota_guard "a_guard, Session_label const &label, - Account &ref_account) + Account(Guard "a_guard, Session_label const &label, Account &ref_account) : _quota_guard(quota_guard), _label(label), _initial_limit(_quota_guard.limit()) @@ -108,7 +108,7 @@ class Core::Account /** * Constructor used for creating the initial account */ - Account(Quota_guard "a_guard, Session_label const &label) + Account(Guard "a_guard, Session_label const &label) : _quota_guard(quota_guard), _label(label), _initial_limit(UNIT{0}) { } ~Account()