base: Add const qualifiers to 'util/list.h'

This patch adds support for iterating through a const list. This allows
users of lists to be more rigid with regard to constness. Furthermore,
the patch adds the function 'List::insert_at' for inserting an element
at a specified position. By adding this function, we can remove code
duplication in nitpicker.
This commit is contained in:
Norman Feske
2013-09-07 12:54:35 +02:00
parent 664aff2a56
commit 21462cdbd2
7 changed files with 43 additions and 21 deletions

View File

@ -34,7 +34,7 @@ namespace Genode {
friend class List; friend class List;
LT *_next; LT mutable *_next;
public: public:
@ -53,34 +53,44 @@ namespace Genode {
* *
* Start with an empty list. * Start with an empty list.
*/ */
List(): _first(0) { } List() : _first(0) { }
/** /**
* Return first list element * Return first list element
*/ */
LT *first() const { return _first; } LT *first() { return _first; }
LT const *first() const { return _first; }
/** /**
* Insert element into list * Insert element after specified element into list
*
* \param le list element to insert
* \param at target position (preceding list element)
*/ */
void insert(LT *le) void insert(LT const *le, LT const *at = 0)
{ {
le->Element::_next = _first; /* insert at beginning of the list */
_first = le; if (at == 0) {
le->Element::_next = _first;
_first = const_cast<LT *>(le);
} else {
le->Element::_next = at->Element::_next;
at->Element::_next = const_cast<LT *>(le);
}
} }
/** /**
* Remove element from list * Remove element from list
*/ */
void remove(LT *le) void remove(LT const *le)
{ {
if (!_first) return; if (!_first) return;
/* if specified element is the first of the list */ /* if specified element is the first of the list */
if (le == _first) if (le == _first) {
_first = le->Element::_next; _first = le->Element::_next;
else { } else {
/* search specified element in the list */ /* search specified element in the list */
Element *e = _first; Element *e = _first;
@ -119,7 +129,7 @@ namespace Genode {
List_element(T *object) : _object(object) { } List_element(T *object) : _object(object) { }
T *object() { return _object; } T *object() const { return _object; }
}; };
} }

View File

@ -141,7 +141,7 @@ namespace Genode {
Lock::Guard guard(_lock); Lock::Guard guard(_lock);
/* search list for context */ /* search list for context */
List_element<Signal_context> *le = _list.first(); List_element<Signal_context> const *le = _list.first();
for ( ; le; le = le->next()) { for ( ; le; le = le->next()) {
if (context == le->object()) { if (context == le->object()) {

View File

@ -32,7 +32,7 @@ void Genode::Volatile_object_base::debug_info() const
{ {
/* count number of weak pointers pointing to the object */ /* count number of weak pointers pointing to the object */
weak_ptr_cnt = 0; weak_ptr_cnt = 0;
for (Weak_ptr_base *curr = _list.first(); curr; curr = curr->next()) for (Weak_ptr_base const *curr = _list.first(); curr; curr = curr->next())
weak_ptr_cnt++; weak_ptr_cnt++;
} }

View File

@ -157,7 +157,7 @@ namespace Input {
* *
* \return total number of available input events * \return total number of available input events
*/ */
Genode::size_t flush_sources(Event *dst, Genode::size_t dst_max) const Genode::size_t flush_sources(Event *dst, Genode::size_t dst_max)
{ {
Genode::size_t dst_count = 0; Genode::size_t dst_count = 0;

View File

@ -48,10 +48,12 @@ class Libc::Mmap_registry
Genode::Lock mutable _lock; Genode::Lock mutable _lock;
Entry *_lookup_by_addr_unsynchronized(void * const start) const /*
* Common for both const and non-const lookup functions
*/
template <typename ENTRY>
static ENTRY *_lookup_by_addr_unsynchronized(ENTRY *curr, void * const start)
{ {
Entry *curr = _list.first();
for (; curr; curr = curr->next()) for (; curr; curr = curr->next())
if (curr->start == start) if (curr->start == start)
return curr; return curr;
@ -59,6 +61,16 @@ class Libc::Mmap_registry
return 0; return 0;
} }
Entry const *_lookup_by_addr_unsynchronized(void * const start) const
{
return _lookup_by_addr_unsynchronized(_list.first(), start);
}
Entry *_lookup_by_addr_unsynchronized(void * const start)
{
return _lookup_by_addr_unsynchronized(_list.first(), start);
}
public: public:
void insert(void *start, Genode::size_t len, Plugin *plugin) void insert(void *start, Genode::size_t len, Plugin *plugin)
@ -77,7 +89,7 @@ class Libc::Mmap_registry
{ {
Genode::Lock::Guard guard(_lock); Genode::Lock::Guard guard(_lock);
Entry * const e = _lookup_by_addr_unsynchronized(start); Entry const * const e = _lookup_by_addr_unsynchronized(start);
return e ? e->plugin : 0; return e ? e->plugin : 0;
} }

View File

@ -141,7 +141,7 @@ namespace Init {
bool is_unique(const char *name) const bool is_unique(const char *name) const
{ {
Genode::List_element<Child> *curr = first(); Genode::List_element<Child> const *curr = first();
for (; curr; curr = curr->next()) for (; curr; curr = curr->next())
if (curr->object()->has_name(name)) if (curr->object()->has_name(name))
return false; return false;
@ -151,7 +151,7 @@ namespace Init {
Genode::Server *lookup_server(const char *name) const Genode::Server *lookup_server(const char *name) const
{ {
Genode::List_element<Child> *curr = first(); Genode::List_element<Child> const *curr = first();
for (; curr; curr = curr->next()) for (; curr; curr = curr->next())
if (curr->object()->has_name(name)) if (curr->object()->has_name(name))
return curr->object()->server(); return curr->object()->server();

View File

@ -28,7 +28,7 @@ namespace File_system {
bool has_sub_node_unsynchronized(char const *name) const bool has_sub_node_unsynchronized(char const *name) const
{ {
Node *sub_node = _entries.first(); Node const *sub_node = _entries.first();
for (; sub_node; sub_node = sub_node->next()) for (; sub_node; sub_node = sub_node->next())
if (strcmp(sub_node->name(), name) == 0) if (strcmp(sub_node->name(), name) == 0)
return true; return true;