Make list_entry, hash_entry more type-safe.
authorBen Pfaff <blp@cs.stanford.edu>
Thu, 28 Sep 2006 12:36:41 +0000 (12:36 +0000)
committerBen Pfaff <blp@cs.stanford.edu>
Thu, 28 Sep 2006 12:36:41 +0000 (12:36 +0000)
Suggestion from Godmar Back.

src/lib/kernel/hash.h
src/lib/kernel/list.h

index 3f2e43cd595e31be32b524dd36d79204ab8990b4..db9f6746431a4aa24f60baf762b2c37fb965275e 100644 (file)
@@ -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. */
    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. */
 
 /* Computes and returns the hash value for hash element E, given
    auxiliary data AUX. */
index a041299f6701f02e9d840dc52dab9ab4077702d9..2388f9acdbf8e553d79ff6a27b93703020a3739e 100644 (file)
@@ -105,8 +105,9 @@ struct list
    name of the outer structure STRUCT and the member name MEMBER
    of the list element.  See the big comment at the top of the
    file for an example. */
    name of the outer structure STRUCT and the member name MEMBER
    of the list element.  See the big comment at the top of the
    file for an example. */
-#define list_entry(LIST_ELEM, STRUCT, MEMBER)                              \
-        ((STRUCT *) ((uint8_t *) (LIST_ELEM) - offsetof (STRUCT, MEMBER)))
+#define list_entry(LIST_ELEM, STRUCT, MEMBER)           \
+        ((STRUCT *) ((uint8_t *) &(LIST_ELEM)->next     \
+                     - offsetof (STRUCT, MEMBER.next)))
 
 void list_init (struct list *);
 
 
 void list_init (struct list *);