From f4b122ad83e037c09f8dd359dcbedb3d9577fa98 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Wed, 7 Nov 2012 13:46:37 -0800 Subject: [PATCH] Remove manual pointer casts from map-gen and pmap-gen There were a lot of manual conversions from generic map_node* to the KEYSYM variants (which had a duplicate of map_node at their heads). Now there's a genuine map_node in each KEYSYM node, and we use the trustworthy container_of and hlist_for_each_entry to go back and forth. --- runtime/dyninst/linux_defs.h | 8 ++++++ runtime/map-gen.c | 38 ++++++++++++------------- runtime/pmap-gen.c | 55 +++++++++++++++++------------------- 3 files changed, 52 insertions(+), 49 deletions(-) diff --git a/runtime/dyninst/linux_defs.h b/runtime/dyninst/linux_defs.h index d2348eb6c..a7b285e08 100644 --- a/runtime/dyninst/linux_defs.h +++ b/runtime/dyninst/linux_defs.h @@ -334,9 +334,17 @@ static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h) n->pprev = &h->first; } +#define hlist_entry(ptr, type, member) container_of(ptr,type,member) + #define hlist_for_each(pos, head) \ for (pos = (head)->first; pos ; pos = pos->next) +#define hlist_for_each_entry(tpos, pos, head, member) \ + for (pos = (head)->first; \ + pos && \ + ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ + pos = pos->next) + #endif /* _STAPDYN_LINUX_DEFS_H_ */ diff --git a/runtime/map-gen.c b/runtime/map-gen.c index 56639e63a..4a82248ae 100644 --- a/runtime/map-gen.c +++ b/runtime/map-gen.c @@ -327,12 +327,8 @@ /* */ struct KEYSYM(map_node) { - /* list of other nodes in the map */ - struct list_head lnode; - /* list of nodes with the same hash value */ - struct hlist_node hnode; - /* pointer back to the map struct */ - struct map_root *map; + /* common node bits */ + struct map_node node; KEY1STOR; #if KEY_ARITY > 1 @@ -361,6 +357,12 @@ struct KEYSYM(map_node) { #endif }; +static inline struct KEYSYM(map_node)* +KEYSYM(get_map_node) (struct map_node* m) +{ + return container_of(m, struct KEYSYM(map_node), node); +} + #define type_to_enum(type) \ ({ \ int ret; \ @@ -374,7 +376,7 @@ struct KEYSYM(map_node) { static key_data KEYSYM(map_get_key) (struct map_node *mn, int n, int *type) { key_data ptr; - struct KEYSYM(map_node) *m = (struct KEYSYM(map_node) *)mn; + struct KEYSYM(map_node) *m = KEYSYM(get_map_node)(mn); if (n > KEY_ARITY || n < 1) { if (type) @@ -621,8 +623,7 @@ static int KEYSYM(__stp_map_set) (MAP map, ALLKEYSD(key), VSTYPE val, int add) hv = KEYSYM(hash) (ALLKEYS(key)); head = &map->hashes[hv]; - hlist_for_each(e, head) { - n = (struct KEYSYM(map_node) *)((long)e - sizeof(struct list_head)); + hlist_for_each_entry(n, e, head, node.hnode) { if (KEY1_EQ_P(n->key1, key1) #if KEY_ARITY > 1 && KEY2_EQ_P(n->key2, key2) @@ -649,15 +650,15 @@ static int KEYSYM(__stp_map_set) (MAP map, ALLKEYSD(key), VSTYPE val, int add) #endif #endif ) { - return MAP_SET_VAL(map,(struct map_node *)n, val, add); + return MAP_SET_VAL(map, &n->node, val, add); } } /* key not found */ - n = (struct KEYSYM(map_node)*)_new_map_create (map, head); + n = KEYSYM(get_map_node)(_new_map_create (map, head)); if (n == NULL) return -1; KEYCPY(n); - return MAP_SET_VAL(map,(struct map_node *)n, val, 0); + return MAP_SET_VAL(map, &n->node, val, 0); } static int KEYSYM(_stp_map_set) (MAP map, ALLKEYSD(key), VSTYPE val) @@ -684,8 +685,7 @@ static VALTYPE KEYSYM(_stp_map_get) (MAP map, ALLKEYSD(key)) hv = KEYSYM(hash) (ALLKEYS(key)); head = &map->hashes[hv]; - hlist_for_each(e, head) { - n = (struct KEYSYM(map_node) *)((long)e - sizeof(struct list_head)); + hlist_for_each_entry(n, e, head, node.hnode) { if (KEY1_EQ_P(n->key1, key1) #if KEY_ARITY > 1 && KEY2_EQ_P(n->key2, key2) @@ -712,7 +712,7 @@ static VALTYPE KEYSYM(_stp_map_get) (MAP map, ALLKEYSD(key)) #endif #endif ) { - return MAP_GET_VAL((struct map_node *)n); + return MAP_GET_VAL(&n->node); } } /* key not found */ @@ -732,8 +732,7 @@ static int KEYSYM(_stp_map_del) (MAP map, ALLKEYSD(key)) hv = KEYSYM(hash) (ALLKEYS(key)); head = &map->hashes[hv]; - hlist_for_each(e, head) { - n = (struct KEYSYM(map_node) *)((long)e - sizeof(struct list_head)); + hlist_for_each_entry(n, e, head, node.hnode) { if (KEY1_EQ_P(n->key1, key1) #if KEY_ARITY > 1 && KEY2_EQ_P(n->key2, key2) @@ -760,7 +759,7 @@ static int KEYSYM(_stp_map_del) (MAP map, ALLKEYSD(key)) #endif #endif ) { - _new_map_del_node(map,(struct map_node *)n); + _new_map_del_node(map, &n->node); return 0; } } @@ -781,8 +780,7 @@ static int KEYSYM(_stp_map_exists) (MAP map, ALLKEYSD(key)) hv = KEYSYM(hash) (ALLKEYS(key)); head = &map->hashes[hv]; - hlist_for_each(e, head) { - n = (struct KEYSYM(map_node) *)((long)e - sizeof(struct list_head)); + hlist_for_each_entry(n, e, head, node.hnode) { if (KEY1_EQ_P(n->key1, key1) #if KEY_ARITY > 1 && KEY2_EQ_P(n->key2, key2) diff --git a/runtime/pmap-gen.c b/runtime/pmap-gen.c index 4ec840bf3..d3e452cc4 100644 --- a/runtime/pmap-gen.c +++ b/runtime/pmap-gen.c @@ -327,12 +327,8 @@ /* */ struct KEYSYM(pmap_node) { - /* list of other nodes in the map */ - struct list_head lnode; - /* list of nodes with the same hash value */ - struct hlist_node hnode; - /* pointer back to the map struct */ - struct map_root *map; + /* common node bits */ + struct map_node node; KEY1STOR; #if KEY_ARITY > 1 @@ -361,6 +357,12 @@ struct KEYSYM(pmap_node) { #endif }; +static inline struct KEYSYM(pmap_node)* +KEYSYM(get_pmap_node) (struct map_node* m) +{ + return container_of(m, struct KEYSYM(pmap_node), node); +} + #define type_to_enum(type) \ ({ \ int ret; \ @@ -374,8 +376,8 @@ struct KEYSYM(pmap_node) { /* returns 1 on match, 0 otherwise */ static int KEYSYM(pmap_key_cmp) (struct map_node *m1, struct map_node *m2) { - struct KEYSYM(pmap_node) *n1 = (struct KEYSYM(pmap_node) *)m1; - struct KEYSYM(pmap_node) *n2 = (struct KEYSYM(pmap_node) *)m2; + struct KEYSYM(pmap_node) *n1 = KEYSYM(get_pmap_node)(m1); + struct KEYSYM(pmap_node) *n2 = KEYSYM(get_pmap_node)(m2); if (KEY1_EQ_P(n1->key1, n2->key1) #if KEY_ARITY > 1 && KEY2_EQ_P(n1->key2, n2->key2) @@ -410,8 +412,8 @@ static int KEYSYM(pmap_key_cmp) (struct map_node *m1, struct map_node *m2) /* copy keys for m2 -> m1 */ static void KEYSYM(pmap_copy_keys) (struct map_node *m1, struct map_node *m2) { - struct KEYSYM(pmap_node) *dst = (struct KEYSYM(pmap_node) *)m1; - struct KEYSYM(pmap_node) *src = (struct KEYSYM(pmap_node) *)m2; + struct KEYSYM(pmap_node) *dst = KEYSYM(get_pmap_node)(m1); + struct KEYSYM(pmap_node) *src = KEYSYM(get_pmap_node)(m2); #if KEY1_TYPE == STRING str_copy (dst->key1, src->key1); #else @@ -478,7 +480,7 @@ static void KEYSYM(pmap_copy_keys) (struct map_node *m1, struct map_node *m2) static key_data KEYSYM(pmap_get_key) (struct map_node *mn, int n, int *type) { key_data ptr; - struct KEYSYM(pmap_node) *m = (struct KEYSYM(pmap_node) *)mn; + struct KEYSYM(pmap_node) *m = KEYSYM(get_pmap_node)(mn); if (n > KEY_ARITY || n < 1) { if (type) @@ -755,8 +757,7 @@ static int KEYSYM(__stp_pmap_set) (MAP map, ALLKEYSD(key), VSTYPE val, int add) hv = KEYSYM(phash) (ALLKEYS(key)); head = &map->hashes[hv]; - hlist_for_each(e, head) { - n = (struct KEYSYM(pmap_node) *)((long)e - sizeof(struct list_head)); + hlist_for_each_entry(n, e, head, node.hnode) { if (KEY1_EQ_P(n->key1, key1) #if KEY_ARITY > 1 && KEY2_EQ_P(n->key2, key2) @@ -783,17 +784,17 @@ static int KEYSYM(__stp_pmap_set) (MAP map, ALLKEYSD(key), VSTYPE val, int add) #endif #endif ) { - return MAP_SET_VAL(map,(struct map_node *)n, val, add); + return MAP_SET_VAL(map, &n->node, val, add); } } /* key not found */ - n = (struct KEYSYM(pmap_node)*)_new_map_create (map, head); + n = KEYSYM(get_pmap_node)(_new_map_create (map, head)); if (n == NULL) return -1; KEYCPY(n); - return MAP_SET_VAL(map,(struct map_node *)n, val, 0); + return MAP_SET_VAL(map, &n->node, val, 0); } static int KEYSYM(_stp_pmap_set) (PMAP pmap, ALLKEYSD(key), VSTYPE val) @@ -851,8 +852,7 @@ static VALTYPE KEYSYM(_stp_pmap_get_cpu) (PMAP pmap, ALLKEYSD(key)) hv = KEYSYM(phash) (ALLKEYS(key)); head = &map->hashes[hv]; - hlist_for_each(e, head) { - n = (struct KEYSYM(pmap_node) *)((long)e - sizeof(struct list_head)); + hlist_for_each_entry(n, e, head, node.hnode) { if (KEY1_EQ_P(n->key1, key1) #if KEY_ARITY > 1 && KEY2_EQ_P(n->key2, key2) @@ -879,7 +879,7 @@ static VALTYPE KEYSYM(_stp_pmap_get_cpu) (PMAP pmap, ALLKEYSD(key)) #endif #endif ) { - res = MAP_GET_VAL((struct map_node *)n); + res = MAP_GET_VAL(&n->node); MAP_UNLOCK(map); MAP_PUT_CPU(); return res; @@ -909,8 +909,7 @@ static VALTYPE KEYSYM(_stp_pmap_get) (PMAP pmap, ALLKEYSD(key)) /* first look it up in the aggregation map */ agg = &pmap->agg; ahead = &agg->hashes[hv]; - hlist_for_each(e, ahead) { - n = (struct KEYSYM(pmap_node) *)((long)e - sizeof(struct list_head)); + hlist_for_each_entry(n, e, ahead, node.hnode) { if (KEY1_EQ_P(n->key1, key1) #if KEY_ARITY > 1 && KEY2_EQ_P(n->key2, key2) @@ -937,7 +936,7 @@ static VALTYPE KEYSYM(_stp_pmap_get) (PMAP pmap, ALLKEYSD(key)) #endif #endif ) { - anode = (struct map_node *)n; + anode = &n->node; clear_agg = 1; break; } @@ -952,8 +951,7 @@ static VALTYPE KEYSYM(_stp_pmap_get) (PMAP pmap, ALLKEYSD(key)) #endif head = &map->hashes[hv]; - hlist_for_each(e, head) { - n = (struct KEYSYM(pmap_node) *)((long)e - sizeof(struct list_head)); + hlist_for_each_entry(n, e, head, node.hnode) { if (KEY1_EQ_P(n->key1, key1) #if KEY_ARITY > 1 && KEY2_EQ_P(n->key2, key2) @@ -981,13 +979,13 @@ static VALTYPE KEYSYM(_stp_pmap_get) (PMAP pmap, ALLKEYSD(key)) #endif ) { if (anode == NULL) { - anode = _stp_new_agg(agg, ahead, (struct map_node *)n); + anode = _stp_new_agg(agg, ahead, &n->node); } else { if (clear_agg) { _new_map_clear_node (anode); clear_agg = 0; } - _stp_add_agg(anode, (struct map_node *)n); + _stp_add_agg(anode, &n->node); } } } @@ -1016,8 +1014,7 @@ static int KEYSYM(__stp_pmap_del) (MAP map, ALLKEYSD(key)) hv = KEYSYM(phash) (ALLKEYS(key)); head = &map->hashes[hv]; - hlist_for_each(e, head) { - n = (struct KEYSYM(pmap_node) *)((long)e - sizeof(struct list_head)); + hlist_for_each_entry(n, e, head, node.hnode) { if (KEY1_EQ_P(n->key1, key1) #if KEY_ARITY > 1 && KEY2_EQ_P(n->key2, key2) @@ -1044,7 +1041,7 @@ static int KEYSYM(__stp_pmap_del) (MAP map, ALLKEYSD(key)) #endif #endif ) { - _new_map_del_node(map,(struct map_node *)n); + _new_map_del_node(map, &n->node); return 0; } } -- 2.43.5