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
This commit is contained in:
Norman Feske 2025-04-05 13:27:05 +02:00
parent 7e0d2084fb
commit 2e76f4fc58
6 changed files with 27 additions and 26 deletions

View File

@ -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<void *, Alloc_error>;
/**

View File

@ -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_ */

View File

@ -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<Ram_quota>;
using Cap_quota_guard = Quota_guard<Cap_quota>;
using Out_of_ram = Ram_quota_guard::Limit_exceeded;
using Out_of_caps = Cap_quota_guard::Limit_exceeded;
}
#endif /* _INCLUDE__BASE__QUOTA_GUARD_H_ */

View File

@ -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();
}
}

View File

@ -14,6 +14,7 @@
#ifndef _INCLUDE__SESSION__SESSION_H_
#define _INCLUDE__SESSION__SESSION_H_
#include <base/exception.h>
#include <base/quota_guard.h>
#include <base/session_label.h>
#include <util/arg_string.h>
@ -34,7 +35,7 @@ namespace Genode {
*/
struct Insufficient_ram_quota : Exception { };
struct Insufficient_cap_quota : Exception { };
struct Service_denied : Exception { };
using Service_denied = Denied;
}

View File

@ -27,6 +27,11 @@ namespace Core { template <typename> class Account; }
template <typename UNIT>
class Core::Account
{
public:
using Limit_exceeded = UNIT::Exhausted_exception;
using Guard = Quota_guard<UNIT>;
private:
/*
@ -35,7 +40,7 @@ class Core::Account
Account(Account const &);
Account &operator = (Account const &);
Quota_guard<UNIT> &_quota_guard;
Guard &_quota_guard;
Session::Label const &_label;
@ -88,16 +93,11 @@ class Core::Account
public:
using Limit_exceeded = typename Quota_guard<UNIT>::Limit_exceeded;
class Unrelated_account : Exception { };
/**
* Constructor for creating a regular account that is rechargeable by
* the specified reference account
*/
Account(Quota_guard<UNIT> &quota_guard, Session_label const &label,
Account &ref_account)
Account(Guard &quota_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<UNIT> &quota_guard, Session_label const &label)
Account(Guard &quota_guard, Session_label const &label)
: _quota_guard(quota_guard), _label(label), _initial_limit(UNIT{0}) { }
~Account()