mirror of
https://github.com/genodelabs/genode.git
synced 2025-04-08 03:45:24 +00:00
nic_router: simplify the pointer utility
Instead of Pointer<T>::set use assignment operator with implicit constructor from T-reference. Instead of Pointer<T>::unset use assignment operator with Pointer<T>(). Instead of Pointer<T>::deref provide () operator. Issue #2730
This commit is contained in:
parent
a3b04c55f3
commit
1044c2fcab
@ -70,17 +70,17 @@ Configuration::Configuration(Env &env,
|
||||
Xml_node const report_node = node.sub_node("report");
|
||||
try {
|
||||
/* try to re-use existing reporter */
|
||||
_reporter.set(legacy._reporter.deref());
|
||||
legacy._reporter.unset();
|
||||
_reporter = legacy._reporter();
|
||||
legacy._reporter = Pointer<Reporter>();
|
||||
}
|
||||
catch (Pointer<Reporter>::Invalid) {
|
||||
|
||||
/* there is no reporter by now, create a new one */
|
||||
_reporter.set(*new (_alloc) Reporter(env, "state"));
|
||||
_reporter = *new (_alloc) Reporter(env, "state");
|
||||
}
|
||||
/* create report generator */
|
||||
_report.set(*new (_alloc)
|
||||
Report(report_node, timer, _domains, _reporter.deref()));
|
||||
_report = *new (_alloc)
|
||||
Report(report_node, timer, _domains, _reporter());
|
||||
}
|
||||
catch (Genode::Xml_node::Nonexistent_sub_node) { }
|
||||
}
|
||||
@ -89,11 +89,11 @@ Configuration::Configuration(Env &env,
|
||||
Configuration::~Configuration()
|
||||
{
|
||||
/* destroy reporter */
|
||||
try { destroy(_alloc, &_reporter.deref()); }
|
||||
try { destroy(_alloc, &_reporter()); }
|
||||
catch (Pointer<Reporter>::Invalid) { }
|
||||
|
||||
/* destroy report generator */
|
||||
try { destroy(_alloc, &_report.deref()); }
|
||||
try { destroy(_alloc, &_report()); }
|
||||
catch (Pointer<Report>::Invalid) { }
|
||||
|
||||
/* destroy domains */
|
||||
|
@ -81,7 +81,7 @@ class Net::Configuration
|
||||
Genode::Microseconds tcp_idle_timeout() const { return _tcp_idle_timeout; }
|
||||
Genode::Microseconds tcp_max_segm_lifetime() const { return _tcp_max_segm_lifetime; }
|
||||
Domain_tree &domains() { return _domains; }
|
||||
Report &report() { return _report.deref(); }
|
||||
Report &report() { return _report(); }
|
||||
Genode::Xml_node node() const { return _node; }
|
||||
};
|
||||
|
||||
|
@ -157,12 +157,12 @@ void Domain::_ip_config_changed()
|
||||
}
|
||||
/* try to find configuration for DHCP server role */
|
||||
try {
|
||||
_dhcp_server.set(*new (_alloc)
|
||||
_dhcp_server = *new (_alloc)
|
||||
Dhcp_server(_node.sub_node("dhcp-server"), _alloc,
|
||||
ip_config().interface));
|
||||
ip_config().interface);
|
||||
|
||||
if (_config.verbose()) {
|
||||
log("DHCP server at domain \"", *this, "\": ", _dhcp_server.deref()); }
|
||||
log("DHCP server at domain \"", *this, "\": ", _dhcp_server()); }
|
||||
}
|
||||
catch (Xml_node::Nonexistent_sub_node) { }
|
||||
catch (Dhcp_server::Invalid) {
|
||||
@ -181,7 +181,7 @@ Domain::~Domain()
|
||||
_tcp_forward_rules.destroy_each(_alloc);
|
||||
|
||||
/* destroy DHCP server and IP config */
|
||||
try { destroy(_alloc, &_dhcp_server.deref()); }
|
||||
try { destroy(_alloc, &_dhcp_server()); }
|
||||
catch (Pointer<Dhcp_server>::Invalid) { }
|
||||
_ip_config.destruct();
|
||||
}
|
||||
|
@ -179,7 +179,7 @@ class Net::Domain : public Domain_base
|
||||
Interface_list &interfaces() { return _interfaces; }
|
||||
Configuration &config() const { return _config; }
|
||||
Domain_avl_member &avl_member() { return _avl_member; }
|
||||
Dhcp_server &dhcp_server() { return _dhcp_server.deref(); }
|
||||
Dhcp_server &dhcp_server() { return _dhcp_server(); }
|
||||
Arp_cache &arp_cache() { return _arp_cache; }
|
||||
Arp_waiter_list &foreign_arp_waiters() { return _foreign_arp_waiters; }
|
||||
Link_side_tree &tcp_links() { return _tcp_links; }
|
||||
|
@ -226,7 +226,7 @@ void Interface::_attach_to_domain_raw(Domain_name const &domain_name)
|
||||
|
||||
void Interface::_detach_from_domain_raw()
|
||||
{
|
||||
Domain &domain = _domain.deref();
|
||||
Domain &domain = _domain();
|
||||
domain.detach_interface(*this);
|
||||
_interfaces.insert(this);
|
||||
_domain = Pointer<Domain>();
|
||||
@ -263,7 +263,7 @@ void Interface::_apply_foreign_arp()
|
||||
bool Interface::link_state()
|
||||
{
|
||||
try {
|
||||
_domain.deref();
|
||||
_domain();
|
||||
return true;
|
||||
}
|
||||
catch (Pointer<Domain>::Invalid) { }
|
||||
@ -290,7 +290,7 @@ void Interface::handle_config_aftermath()
|
||||
void Interface::detach_from_ip_config()
|
||||
{
|
||||
/* destroy our own ARP waiters */
|
||||
Domain &domain = _domain.deref();
|
||||
Domain &domain = _domain();
|
||||
while (_own_arp_waiters.first()) {
|
||||
cancel_arp_waiting(*_own_arp_waiters.first()->object());
|
||||
}
|
||||
@ -342,7 +342,7 @@ Interface::_new_link(L3_protocol const protocol,
|
||||
|
||||
void Interface::dhcp_allocation_expired(Dhcp_allocation &allocation)
|
||||
{
|
||||
_release_dhcp_allocation(allocation, _domain.deref());
|
||||
_release_dhcp_allocation(allocation, _domain());
|
||||
_released_dhcp_allocations.insert(&allocation);
|
||||
}
|
||||
|
||||
@ -406,7 +406,7 @@ void Interface::_nat_link_and_pass(Ethernet_frame ð,
|
||||
|
||||
_src_port(prot, prot_base, nat.port_alloc(prot).alloc());
|
||||
ip.src(remote_domain.ip_config().interface.address);
|
||||
remote_port_alloc.set(nat.port_alloc(prot));
|
||||
remote_port_alloc = nat.port_alloc(prot);
|
||||
}
|
||||
catch (Nat_rule_tree::No_match) { }
|
||||
Link_side_id const remote_id = { ip.dst(), _dst_port(prot, prot_base),
|
||||
@ -1014,7 +1014,7 @@ void Interface::_handle_eth(void *const eth_base,
|
||||
Packet_descriptor const &pkt)
|
||||
{
|
||||
try {
|
||||
Domain &local_domain = _domain.deref();
|
||||
Domain &local_domain = _domain();
|
||||
try {
|
||||
local_domain.raise_rx_bytes(eth_size);
|
||||
|
||||
@ -1103,7 +1103,7 @@ void Interface::_send_submit_pkt(Packet_descriptor &pkt,
|
||||
void * &pkt_base,
|
||||
size_t pkt_size)
|
||||
{
|
||||
Domain &local_domain = _domain.deref();
|
||||
Domain &local_domain = _domain();
|
||||
_source().submit_packet(pkt);
|
||||
local_domain.raise_tx_bytes(pkt_size);
|
||||
if (local_domain.verbose_packets()) {
|
||||
@ -1173,7 +1173,7 @@ void Interface::_update_link_check_nat(Link &link,
|
||||
Nat_rule &nat = new_srv_dom.nat_rules().find_by_domain(cln_dom);
|
||||
Port_allocator_guard &remote_port_alloc = nat.port_alloc(prot);
|
||||
remote_port_alloc.alloc(link.server().dst_port());
|
||||
remote_port_alloc_ptr.set(remote_port_alloc);
|
||||
remote_port_alloc_ptr = remote_port_alloc;
|
||||
link.handle_config(cln_dom, new_srv_dom, remote_port_alloc_ptr, _config());
|
||||
return;
|
||||
}
|
||||
|
@ -327,7 +327,7 @@ class Net::Interface : private Interface_list::Element
|
||||
** Accessors **
|
||||
***************/
|
||||
|
||||
Domain &domain() { return _domain.deref(); }
|
||||
Domain &domain() { return _domain(); }
|
||||
Mac_address router_mac() const { return _router_mac; }
|
||||
Arp_waiter_list &own_arp_waiters() { return _own_arp_waiters; }
|
||||
};
|
||||
|
@ -155,7 +155,7 @@ void Link::dissolve()
|
||||
log("Dissolve ", l3_protocol_name(_protocol), " link: ", *this); }
|
||||
|
||||
try {
|
||||
_server_port_alloc.deref().free(_server.dst_port());
|
||||
_server_port_alloc().free(_server.dst_port());
|
||||
if (_config().verbose()) {
|
||||
log("Free ", l3_protocol_name(_protocol),
|
||||
" port ", _server.dst_port(),
|
||||
|
@ -23,72 +23,54 @@ namespace Net {
|
||||
template <typename> class Const_pointer;
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
class Net::Pointer
|
||||
{
|
||||
private:
|
||||
|
||||
T *_ptr;
|
||||
T *_obj;
|
||||
|
||||
public:
|
||||
|
||||
struct Valid : Genode::Exception { };
|
||||
struct Invalid : Genode::Exception { };
|
||||
|
||||
Pointer() : _ptr(nullptr) { }
|
||||
Pointer() : _obj(nullptr) { }
|
||||
|
||||
Pointer(T &ref) : _ptr(&ref) { }
|
||||
Pointer(T &obj) : _obj(&obj) { }
|
||||
|
||||
T &deref() const
|
||||
T &operator () () const
|
||||
{
|
||||
if (_ptr == nullptr) {
|
||||
throw Invalid(); }
|
||||
if (_obj == nullptr)
|
||||
throw Invalid();
|
||||
|
||||
return *_ptr;
|
||||
return *_obj;
|
||||
}
|
||||
|
||||
void set(T &ptr)
|
||||
{
|
||||
if (_ptr != nullptr) {
|
||||
throw Valid(); }
|
||||
|
||||
_ptr = &ptr;
|
||||
}
|
||||
|
||||
void unset() { _ptr = nullptr; }
|
||||
};
|
||||
|
||||
|
||||
template <typename T>
|
||||
class Net::Const_pointer
|
||||
{
|
||||
private:
|
||||
|
||||
T const *_ptr;
|
||||
bool _valid;
|
||||
T const *_obj;
|
||||
|
||||
public:
|
||||
|
||||
struct Valid : Genode::Exception { };
|
||||
struct Invalid : Genode::Exception { };
|
||||
|
||||
Const_pointer() : _ptr(nullptr), _valid(false) { }
|
||||
Const_pointer() : _obj(nullptr) { }
|
||||
|
||||
Const_pointer(T const &ref) : _ptr(&ref), _valid(true) { }
|
||||
Const_pointer(T const &obj) : _obj(&obj) { }
|
||||
|
||||
T const &deref() const
|
||||
T const &operator () () const
|
||||
{
|
||||
if (!_valid) { throw Invalid(); }
|
||||
return *_ptr;
|
||||
}
|
||||
if (_obj == nullptr)
|
||||
throw Invalid();
|
||||
|
||||
void set(T const &ptr)
|
||||
{
|
||||
if (_valid) { throw Valid(); }
|
||||
_ptr = &ptr;
|
||||
_valid = true;
|
||||
return *_obj;
|
||||
}
|
||||
|
||||
void unset() { _valid = false; }
|
||||
};
|
||||
|
||||
#endif /* _POINTER_H_ */
|
||||
|
@ -52,7 +52,7 @@ Transport_rule::Transport_rule(Domain_tree &domains,
|
||||
{
|
||||
/* skip specific permit rules if all ports are permitted anyway */
|
||||
try {
|
||||
Permit_any_rule &permit_any_rule = _permit_any_rule.deref();
|
||||
Permit_any_rule &permit_any_rule = _permit_any_rule();
|
||||
if (config.verbose()) {
|
||||
log(" ", protocol, " rule: ", _dst, " ", permit_any_rule); }
|
||||
|
||||
@ -80,14 +80,14 @@ Transport_rule::Transport_rule(Domain_tree &domains,
|
||||
Transport_rule::~Transport_rule()
|
||||
{
|
||||
_permit_single_rules.destroy_each(_alloc);
|
||||
try { destroy(_alloc, &_permit_any_rule.deref()); }
|
||||
try { destroy(_alloc, &_permit_any_rule()); }
|
||||
catch (Pointer<Permit_any_rule>::Invalid) { }
|
||||
}
|
||||
|
||||
|
||||
Permit_rule const &Transport_rule::permit_rule(Port const port) const
|
||||
{
|
||||
try { return _permit_any_rule.deref(); }
|
||||
try { return _permit_any_rule(); }
|
||||
catch (Pointer<Permit_any_rule>::Invalid) { }
|
||||
return _permit_single_rules.find_by_port(port);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user