diff --git a/repos/os/src/server/nic_router/nat_rule.cc b/repos/os/src/server/nic_router/nat_rule.cc index dd05062ba7..1839b8ce22 100644 --- a/repos/os/src/server/nic_router/nat_rule.cc +++ b/repos/os/src/server/nic_router/nat_rule.cc @@ -84,9 +84,9 @@ Nat_rule &Nat_rule_tree::find_by_domain(Domain &domain) void Nat_rule::print(Output &output) const { Genode::print(output, "domain ", _domain, - " tcp-ports ", _tcp_port_alloc.max(), - " udp-ports ", _udp_port_alloc.max(), - " icmp-ids ", _icmp_port_alloc.max()); + " tcp-ports ", _tcp_port_alloc.max_nr_of_ports(), + " udp-ports ", _udp_port_alloc.max_nr_of_ports(), + " icmp-ids ", _icmp_port_alloc.max_nr_of_ports()); } diff --git a/repos/os/src/server/nic_router/port_allocator.cc b/repos/os/src/server/nic_router/port_allocator.cc index 65a9e4e69d..d43b18216f 100644 --- a/repos/os/src/server/nic_router/port_allocator.cc +++ b/repos/os/src/server/nic_router/port_allocator.cc @@ -21,8 +21,10 @@ using namespace Net; using namespace Genode; -bool Net::dynamic_port(Port const port) { - return port.value >= Port_allocator::FIRST; } +bool Net::dynamic_port(Port const port) +{ + return port.value >= Port_allocator::FIRST_PORT; +} /******************** @@ -32,16 +34,16 @@ bool Net::dynamic_port(Port const port) { Port Net::Port_allocator::alloc() { for (unsigned nr_of_trials { 0 }; - nr_of_trials < COUNT; + nr_of_trials < NR_OF_PORTS; nr_of_trials++) { uint16_t const port_offset = _next_port_offset; - _next_port_offset = (_next_port_offset + 1) % COUNT; + _next_port_offset = (_next_port_offset + 1) % NR_OF_PORTS; try { - _alloc.alloc_addr(port_offset); - return Port { (uint16_t)(port_offset + FIRST) }; + _bit_allocator.alloc_addr(port_offset); + return Port { (uint16_t)(port_offset + FIRST_PORT) }; } - catch (Bit_allocator::Range_conflict) { } + catch (Bit_allocator::Range_conflict) { } } throw Out_of_indices(); } @@ -49,15 +51,19 @@ Port Net::Port_allocator::alloc() void Net::Port_allocator::alloc(Port const port) { - try { _alloc.alloc_addr(port.value - FIRST); } - catch (Genode::Bit_allocator::Range_conflict) { - throw Allocation_conflict(); } + try { + _bit_allocator.alloc_addr(port.value - FIRST_PORT); + } + catch (Bit_allocator::Range_conflict) { + + throw Allocation_conflict(); + } } void Port_allocator::free(Port const port) { - _alloc.free(port.value - FIRST); + _bit_allocator.free(port.value - FIRST_PORT); } @@ -67,40 +73,48 @@ void Port_allocator::free(Port const port) Port Port_allocator_guard::alloc() { - if (_used == _max) { - throw Out_of_indices(); } - - Port const port = _port_alloc.alloc(); - _used++; - return port; + if (_used_nr_of_ports == _max_nr_of_ports) { + throw Out_of_indices(); + } + try { + Port const port = _port_alloc.alloc(); + _used_nr_of_ports++; + return port; + } + catch (Port_allocator::Out_of_indices) { + throw Out_of_indices(); + } } void Port_allocator_guard::alloc(Port const port) { - if (_used == _max) { - throw Out_of_indices(); } - + if (_used_nr_of_ports == _max_nr_of_ports) { + throw Out_of_indices(); + } _port_alloc.alloc(port); - _used++; + _used_nr_of_ports++; } void Port_allocator_guard::free(Port const port) { _port_alloc.free(port); - _used = _used ? _used - 1 : 0; + _used_nr_of_ports = _used_nr_of_ports ? _used_nr_of_ports - 1 : 0; } Port_allocator_guard::Port_allocator_guard(Port_allocator &port_alloc, - unsigned const max, + unsigned const max_nr_of_ports, bool const verbose) : - _port_alloc(port_alloc), - _max(min(max, static_cast(Port_allocator::COUNT))) + _port_alloc { port_alloc }, + _max_nr_of_ports { + min(max_nr_of_ports, + static_cast(Port_allocator::NR_OF_PORTS)) } { - if (verbose && max > (Port_allocator::COUNT)) { + if (verbose && + max_nr_of_ports > (Port_allocator::NR_OF_PORTS)) { warning("number of ports was truncated to capacity of allocator"); } diff --git a/repos/os/src/server/nic_router/port_allocator.h b/repos/os/src/server/nic_router/port_allocator.h index 73f86658fb..a2b7b65cd0 100644 --- a/repos/os/src/server/nic_router/port_allocator.h +++ b/repos/os/src/server/nic_router/port_allocator.h @@ -34,12 +34,12 @@ class Net::Port_allocator { public: - enum { FIRST = 49152, COUNT = 16384 }; + enum { FIRST_PORT = 49152, NR_OF_PORTS = 16384 }; private: - Genode::Bit_allocator _alloc { }; - Genode::uint16_t _next_port_offset { 0 }; + Genode::Bit_allocator _bit_allocator { }; + Genode::uint16_t _next_port_offset { 0 }; public: @@ -59,8 +59,8 @@ class Net::Port_allocator_guard private: Port_allocator &_port_alloc; - unsigned const _max; - unsigned _used = 0; + unsigned const _max_nr_of_ports; + unsigned _used_nr_of_ports { 0 }; public: @@ -73,10 +73,10 @@ class Net::Port_allocator_guard void free(Port const port); Port_allocator_guard(Port_allocator &port_alloc, - unsigned const max, + unsigned const max_nr_of_ports, bool const verbose); - unsigned max() const { return _max; } + unsigned max_nr_of_ports() const { return _max_nr_of_ports; } }; #endif /* _PORT_ALLOCATOR_H_ */