Make list_entry, hash_entry more type-safe.
[pintos-anon] / src / lib / kernel / hash.h
index 7f25c1025769327a88eeee4e202dffa20f28cedd..db9f6746431a4aa24f60baf762b2c37fb965275e 100644 (file)
@@ -3,6 +3,9 @@
 
 /* Hash table.
 
+   This data structure is thoroughly documented in the Tour of
+   Pintos for Project 3.
+
    This is a standard hash table with chaining.  To locate an
    element in the table, we compute a hash function over the
    element's data and use that as an index into an array of
    conversion from a struct hash_elem back to a structure object
    that contains it.  This is the same technique used in the
    linked list implementation.  Refer to lib/kernel/list.h for a
-   detailed explanation.
-
-   The FAQ for the VM project contains a detailed example of how
-   to use the hash table. */
+   detailed explanation. */
 
 #include <stdbool.h>
 #include <stddef.h>
@@ -36,8 +36,9 @@ struct hash_elem
    name of the outer structure STRUCT and the member name MEMBER
    of the hash element.  See the big comment at the top of the
    file for an example. */
-#define hash_entry(HASH_ELEM, STRUCT, MEMBER)                              \
-        ((STRUCT *) ((uint8_t *) (HASH_ELEM) - offsetof (STRUCT, MEMBER)))
+#define hash_entry(HASH_ELEM, STRUCT, MEMBER)                   \
+        ((STRUCT *) ((uint8_t *) &(HASH_ELEM)->list_elem        \
+                     - offsetof (STRUCT, MEMBER.list_elem)))
 
 /* Computes and returns the hash value for hash element E, given
    auxiliary data AUX. */