Hash for each helpers
authorBarret Rhoden <brho@cs.berkeley.edu>
Wed, 23 Mar 2011 20:52:00 +0000 (13:52 -0700)
committerKevin Klues <klueska@cs.berkeley.edu>
Thu, 3 Nov 2011 00:36:00 +0000 (17:36 -0700)
In case you want a for each over each value of the hash.  Not a big
deal.

kern/include/hashtable.h
kern/src/hashtable.c
kern/src/process.c

index 00c1d15..20804fb 100644 (file)
@@ -6,7 +6,8 @@
  *   - Provides common hash and equality functions (meant for longs)
  *   - Uses the slab allocator for hash entry allocation.
  *   - Merges the iterator code with the main hash table code, mostly to avoid
- *   externing the hentry cache. */
+ *   externing the hentry cache.
+ *   - hash for each */
 
 #ifndef __ROS_KERN_HASHTABLE_H__
 #define __ROS_KERN_HASHTABLE_H__
@@ -294,6 +295,10 @@ ssize_t fnname (hashtable_itr_t *i, hashtable_t *h, keytype *k) \
     return (hashtable_iterator_search(i,h,k)); \
 }
 
+/* Runs func on each member of the hash table */
+void hash_for_each(struct hashtable *hash, void func(void*));
+/* Same, but removes the item too */
+void hash_for_each_remove(struct hashtable *hash, void func(void*));
 
 #endif /* __ROS_KERN_HASHTABLE_H__ */
 
index 5d1d25a..d8fc1d8 100644 (file)
@@ -424,6 +424,31 @@ hashtable_iterator_search(hashtable_itr_t *itr,
     return 0;
 }
 
+/* Runs func on each member of the hash table */
+void hash_for_each(struct hashtable *hash, void func(void*))
+{
+       if (hashtable_count(hash)) {
+               hashtable_itr_t *iter = hashtable_iterator(hash);
+               do {
+                       void *item = hashtable_iterator_value(iter);
+                       func(item);
+               } while (hashtable_iterator_advance(iter));
+       }
+}
+
+/* Runs func on each member of the hash table, removing the item after
+ * processing it.  Make sure func frees the item, o/w you'll leak. */
+void hash_for_each_remove(struct hashtable *hash, void func(void*))
+{
+       if (hashtable_count(hash)) {
+               hashtable_itr_t *iter = hashtable_iterator(hash);
+               do {
+                       void *item = hashtable_iterator_value(iter);
+                       func(item);
+               } while (hashtable_iterator_remove(iter));
+       }
+}
+
 /*
  * Copyright (c) 2002, 2004, Christopher Clark
  * All rights reserved.
index 77abbbc..07afab8 100644 (file)
@@ -1513,17 +1513,16 @@ void print_idlecoremap(void)
 
 void print_allpids(void)
 {
-       spin_lock(&pid_hash_lock);
-       if (hashtable_count(pid_hash)) {
-               hashtable_itr_t *phtable_i = hashtable_iterator(pid_hash);
-               printk("PID      STATE    \n");
-               printk("------------------\n");
-               do {
-                       struct proc *p = hashtable_iterator_value(phtable_i);
-                       printk("%8d %s\n", hashtable_iterator_key(phtable_i),
-                              p ? procstate2str(p->state) : "(null)");
-               } while (hashtable_iterator_advance(phtable_i));
+       void print_proc_state(void *item)
+       {
+               struct proc *p = (struct proc*)item;
+               assert(p);
+               printk("%8d %s\n", p->pid, procstate2str(p->state));
        }
+       printk("PID      STATE    \n");
+       printk("------------------\n");
+       spin_lock(&pid_hash_lock);
+       hash_for_each(pid_hash, print_proc_state);
        spin_unlock(&pid_hash_lock);
 }