hashmap: remove type arg from hashmap_{get,put,remove}_entry
Since these macros already take a `keyvar' pointer of a known type, we can rely on OFFSETOF_VAR to get the correct offset without relying on non-portable `__typeof__' and `offsetof'. Argument order is also rearranged, so `keyvar' and `member' are sequential as they are used as: `keyvar->member' Signed-off-by: Eric Wong <e@80x24.org> Reviewed-by: Derrick Stolee <stolee@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>maint
							parent
							
								
									23dee69f53
								
							
						
					
					
						commit
						404ab78e39
					
				
							
								
								
									
										2
									
								
								attr.c
								
								
								
								
							
							
						
						
									
										2
									
								
								attr.c
								
								
								
								
							|  | @ -103,7 +103,7 @@ static void *attr_hashmap_get(struct attr_hashmap *map, | ||||||
| 	hashmap_entry_init(&k.ent, memhash(key, keylen)); | 	hashmap_entry_init(&k.ent, memhash(key, keylen)); | ||||||
| 	k.key = key; | 	k.key = key; | ||||||
| 	k.keylen = keylen; | 	k.keylen = keylen; | ||||||
| 	e = hashmap_get_entry(&map->map, &k, NULL, struct attr_hash_entry, ent); | 	e = hashmap_get_entry(&map->map, &k, ent, NULL); | ||||||
|  |  | ||||||
| 	return e ? e->value : NULL; | 	return e ? e->value : NULL; | ||||||
| } | } | ||||||
|  |  | ||||||
							
								
								
									
										10
									
								
								blame.c
								
								
								
								
							
							
						
						
									
										10
									
								
								blame.c
								
								
								
								
							|  | @ -419,8 +419,8 @@ static void get_fingerprint(struct fingerprint *result, | ||||||
| 			continue; | 			continue; | ||||||
| 		hashmap_entry_init(&entry->entry, hash); | 		hashmap_entry_init(&entry->entry, hash); | ||||||
|  |  | ||||||
| 		found_entry = hashmap_get_entry(&result->map, entry, NULL, | 		found_entry = hashmap_get_entry(&result->map, entry, | ||||||
| 					struct fingerprint_entry, entry); | 						/* member name */ entry, NULL); | ||||||
| 		if (found_entry) { | 		if (found_entry) { | ||||||
| 			found_entry->count += 1; | 			found_entry->count += 1; | ||||||
| 		} else { | 		} else { | ||||||
|  | @ -452,8 +452,7 @@ static int fingerprint_similarity(struct fingerprint *a, struct fingerprint *b) | ||||||
|  |  | ||||||
| 	hashmap_for_each_entry(&b->map, &iter, entry_b, | 	hashmap_for_each_entry(&b->map, &iter, entry_b, | ||||||
| 				entry /* member name */) { | 				entry /* member name */) { | ||||||
| 		entry_a = hashmap_get_entry(&a->map, entry_b, NULL, | 		entry_a = hashmap_get_entry(&a->map, entry_b, entry, NULL); | ||||||
| 					struct fingerprint_entry, entry); |  | ||||||
| 		if (entry_a) { | 		if (entry_a) { | ||||||
| 			intersection += entry_a->count < entry_b->count ? | 			intersection += entry_a->count < entry_b->count ? | ||||||
| 					entry_a->count : entry_b->count; | 					entry_a->count : entry_b->count; | ||||||
|  | @ -474,8 +473,7 @@ static void fingerprint_subtract(struct fingerprint *a, struct fingerprint *b) | ||||||
|  |  | ||||||
| 	hashmap_for_each_entry(&b->map, &iter, entry_b, | 	hashmap_for_each_entry(&b->map, &iter, entry_b, | ||||||
| 				entry /* member name */) { | 				entry /* member name */) { | ||||||
| 		entry_a = hashmap_get_entry(&a->map, entry_b, NULL, | 		entry_a = hashmap_get_entry(&a->map, entry_b, entry, NULL); | ||||||
| 					struct fingerprint_entry, entry); |  | ||||||
| 		if (entry_a) { | 		if (entry_a) { | ||||||
| 			if (entry_a->count <= entry_b->count) | 			if (entry_a->count <= entry_b->count) | ||||||
| 				hashmap_remove(&a->map, &entry_b->entry, NULL); | 				hashmap_remove(&a->map, &entry_b->entry, NULL); | ||||||
|  |  | ||||||
|  | @ -167,7 +167,7 @@ static void add_left_or_right(struct hashmap *map, const char *path, | ||||||
|  |  | ||||||
| 	FLEX_ALLOC_STR(e, path, path); | 	FLEX_ALLOC_STR(e, path, path); | ||||||
| 	hashmap_entry_init(&e->entry, strhash(path)); | 	hashmap_entry_init(&e->entry, strhash(path)); | ||||||
| 	existing = hashmap_get_entry(map, e, NULL, struct pair_entry, entry); | 	existing = hashmap_get_entry(map, e, entry, NULL); | ||||||
| 	if (existing) { | 	if (existing) { | ||||||
| 		free(e); | 		free(e); | ||||||
| 		e = existing; | 		e = existing; | ||||||
|  |  | ||||||
|  | @ -156,7 +156,7 @@ static const void *anonymize_mem(struct hashmap *map, | ||||||
| 	hashmap_entry_init(&key.hash, memhash(orig, *len)); | 	hashmap_entry_init(&key.hash, memhash(orig, *len)); | ||||||
| 	key.orig = orig; | 	key.orig = orig; | ||||||
| 	key.orig_len = *len; | 	key.orig_len = *len; | ||||||
| 	ret = hashmap_get_entry(map, &key, NULL, struct anonymized_entry, hash); | 	ret = hashmap_get_entry(map, &key, hash, NULL); | ||||||
|  |  | ||||||
| 	if (!ret) { | 	if (!ret) { | ||||||
| 		ret = xmalloc(sizeof(*ret)); | 		ret = xmalloc(sizeof(*ret)); | ||||||
|  |  | ||||||
							
								
								
									
										3
									
								
								config.c
								
								
								
								
							
							
						
						
									
										3
									
								
								config.c
								
								
								
								
							|  | @ -1863,8 +1863,7 @@ static struct config_set_element *configset_find_element(struct config_set *cs, | ||||||
|  |  | ||||||
| 	hashmap_entry_init(&k.ent, strhash(normalized_key)); | 	hashmap_entry_init(&k.ent, strhash(normalized_key)); | ||||||
| 	k.key = normalized_key; | 	k.key = normalized_key; | ||||||
| 	found_entry = hashmap_get_entry(&cs->config_hash, &k, NULL, | 	found_entry = hashmap_get_entry(&cs->config_hash, &k, ent, NULL); | ||||||
| 				struct config_set_element, ent); |  | ||||||
| 	free(normalized_key); | 	free(normalized_key); | ||||||
| 	return found_entry; | 	return found_entry; | ||||||
| } | } | ||||||
|  |  | ||||||
							
								
								
									
										6
									
								
								diff.c
								
								
								
								
							
							
						
						
									
										6
									
								
								diff.c
								
								
								
								
							|  | @ -1146,15 +1146,13 @@ static void mark_color_as_moved(struct diff_options *o, | ||||||
| 		case DIFF_SYMBOL_PLUS: | 		case DIFF_SYMBOL_PLUS: | ||||||
| 			hm = del_lines; | 			hm = del_lines; | ||||||
| 			key = prepare_entry(o, n); | 			key = prepare_entry(o, n); | ||||||
| 			match = hashmap_get_entry(hm, key, NULL, | 			match = hashmap_get_entry(hm, key, ent, NULL); | ||||||
| 						struct moved_entry, ent); |  | ||||||
| 			free(key); | 			free(key); | ||||||
| 			break; | 			break; | ||||||
| 		case DIFF_SYMBOL_MINUS: | 		case DIFF_SYMBOL_MINUS: | ||||||
| 			hm = add_lines; | 			hm = add_lines; | ||||||
| 			key = prepare_entry(o, n); | 			key = prepare_entry(o, n); | ||||||
| 			match = hashmap_get_entry(hm, key, NULL, | 			match = hashmap_get_entry(hm, key, ent, NULL); | ||||||
| 						struct moved_entry, ent); |  | ||||||
| 			free(key); | 			free(key); | ||||||
| 			break; | 			break; | ||||||
| 		default: | 		default: | ||||||
|  |  | ||||||
|  | @ -311,7 +311,7 @@ const void *memintern(const void *data, size_t len) | ||||||
| 	/* lookup interned string in pool */ | 	/* lookup interned string in pool */ | ||||||
| 	hashmap_entry_init(&key.ent, memhash(data, len)); | 	hashmap_entry_init(&key.ent, memhash(data, len)); | ||||||
| 	key.len = len; | 	key.len = len; | ||||||
| 	e = hashmap_get_entry(&map, &key, data, struct pool_entry, ent); | 	e = hashmap_get_entry(&map, &key, ent, data); | ||||||
| 	if (!e) { | 	if (!e) { | ||||||
| 		/* not found: create it */ | 		/* not found: create it */ | ||||||
| 		FLEX_ALLOC_MEM(e, data, data, len); | 		FLEX_ALLOC_MEM(e, data, data, len); | ||||||
|  |  | ||||||
							
								
								
									
										45
									
								
								hashmap.h
								
								
								
								
							
							
						
						
									
										45
									
								
								hashmap.h
								
								
								
								
							|  | @ -63,7 +63,7 @@ | ||||||
|  *             k.key = key; |  *             k.key = key; | ||||||
|  * |  * | ||||||
|  *             flags &= ~COMPARE_VALUE; |  *             flags &= ~COMPARE_VALUE; | ||||||
|  *             e = hashmap_get_entry(&map, &k, NULL, struct long2string, ent); |  *             e = hashmap_get_entry(&map, &k, ent, NULL); | ||||||
|  *             if (e) { |  *             if (e) { | ||||||
|  *                 printf("first: %ld %s\n", e->key, e->value); |  *                 printf("first: %ld %s\n", e->key, e->value); | ||||||
|  *                 while ((e = hashmap_get_next_entry(&map, e, |  *                 while ((e = hashmap_get_next_entry(&map, e, | ||||||
|  | @ -359,8 +359,17 @@ void hashmap_add(struct hashmap *map, struct hashmap_entry *entry); | ||||||
| struct hashmap_entry *hashmap_put(struct hashmap *map, | struct hashmap_entry *hashmap_put(struct hashmap *map, | ||||||
| 				struct hashmap_entry *entry); | 				struct hashmap_entry *entry); | ||||||
|  |  | ||||||
| #define hashmap_put_entry(map, keyvar, type, member) \ | /* | ||||||
| 	container_of_or_null(hashmap_put(map, &(keyvar)->member), type, member) |  * Adds or replaces a hashmap entry contained within @keyvar, | ||||||
|  |  * where @keyvar is a pointer to a struct containing a | ||||||
|  |  * "struct hashmap_entry" @member. | ||||||
|  |  * | ||||||
|  |  * Returns the replaced pointer which is of the same type as @keyvar, | ||||||
|  |  * or NULL if not found. | ||||||
|  |  */ | ||||||
|  | #define hashmap_put_entry(map, keyvar, member) \ | ||||||
|  | 	container_of_or_null_offset(hashmap_put(map, &(keyvar)->member), \ | ||||||
|  | 				OFFSETOF_VAR(keyvar, member)) | ||||||
|  |  | ||||||
| /* | /* | ||||||
|  * Removes a hashmap entry matching the specified key. If the hashmap contains |  * Removes a hashmap entry matching the specified key. If the hashmap contains | ||||||
|  | @ -373,9 +382,20 @@ struct hashmap_entry *hashmap_remove(struct hashmap *map, | ||||||
| 					const struct hashmap_entry *key, | 					const struct hashmap_entry *key, | ||||||
| 					const void *keydata); | 					const void *keydata); | ||||||
|  |  | ||||||
| #define hashmap_remove_entry(map, keyvar, keydata, type, member) \ | /* | ||||||
| 	container_of_or_null(hashmap_remove(map, &(keyvar)->member, keydata), \ |  * Removes a hashmap entry contained within @keyvar, | ||||||
| 				type, member) |  * where @keyvar is a pointer to a struct containing a | ||||||
|  |  * "struct hashmap_entry" @member. | ||||||
|  |  * | ||||||
|  |  * See `hashmap_get` for an explanation of @keydata | ||||||
|  |  * | ||||||
|  |  * Returns the replaced pointer which is of the same type as @keyvar, | ||||||
|  |  * or NULL if not found. | ||||||
|  |  */ | ||||||
|  | #define hashmap_remove_entry(map, keyvar, member, keydata) \ | ||||||
|  | 	container_of_or_null_offset( \ | ||||||
|  | 			hashmap_remove(map, &(keyvar)->member, keydata), \ | ||||||
|  | 			OFFSETOF_VAR(keyvar, member)) | ||||||
|  |  | ||||||
| /* | /* | ||||||
|  * Returns the `bucket` an entry is stored in. |  * Returns the `bucket` an entry is stored in. | ||||||
|  | @ -436,13 +456,14 @@ static inline struct hashmap_entry *hashmap_iter_first(struct hashmap *map, | ||||||
| 						OFFSETOF_VAR(var, member))) | 						OFFSETOF_VAR(var, member))) | ||||||
|  |  | ||||||
| /* | /* | ||||||
|  * returns a @pointer of @type matching @keyvar, or NULL if nothing found. |  * returns a pointer of type matching @keyvar, or NULL if nothing found. | ||||||
|  * @keyvar is a pointer of @type |  * @keyvar is a pointer to a struct containing a | ||||||
|  * @member is the name of the "struct hashmap_entry" field in @type |  * "struct hashmap_entry" @member. | ||||||
|  */ |  */ | ||||||
| #define hashmap_get_entry(map, keyvar, keydata, type, member) \ | #define hashmap_get_entry(map, keyvar, member, keydata) \ | ||||||
| 	container_of_or_null(hashmap_get(map, &(keyvar)->member, keydata), \ | 	container_of_or_null_offset( \ | ||||||
| 				type, member) | 				hashmap_get(map, &(keyvar)->member, keydata), \ | ||||||
|  | 				OFFSETOF_VAR(keyvar, member)) | ||||||
|  |  | ||||||
| #define hashmap_get_entry_from_hash(map, hash, keydata, type, member) \ | #define hashmap_get_entry_from_hash(map, hash, keydata, type, member) \ | ||||||
| 	container_of_or_null(hashmap_get_from_hash(map, hash, keydata), \ | 	container_of_or_null(hashmap_get_from_hash(map, hash, keydata), \ | ||||||
|  |  | ||||||
|  | @ -65,8 +65,7 @@ static struct dir_rename_entry *dir_rename_find_entry(struct hashmap *hashmap, | ||||||
| 		return NULL; | 		return NULL; | ||||||
| 	hashmap_entry_init(&key.ent, strhash(dir)); | 	hashmap_entry_init(&key.ent, strhash(dir)); | ||||||
| 	key.dir = dir; | 	key.dir = dir; | ||||||
| 	return hashmap_get_entry(hashmap, &key, NULL, | 	return hashmap_get_entry(hashmap, &key, ent, NULL); | ||||||
| 				struct dir_rename_entry, ent); |  | ||||||
| } | } | ||||||
|  |  | ||||||
| static int dir_rename_cmp(const void *unused_cmp_data, | static int dir_rename_cmp(const void *unused_cmp_data, | ||||||
|  | @ -104,8 +103,7 @@ static struct collision_entry *collision_find_entry(struct hashmap *hashmap, | ||||||
|  |  | ||||||
| 	hashmap_entry_init(&key.ent, strhash(target_file)); | 	hashmap_entry_init(&key.ent, strhash(target_file)); | ||||||
| 	key.target_file = target_file; | 	key.target_file = target_file; | ||||||
| 	return hashmap_get_entry(hashmap, &key, NULL, | 	return hashmap_get_entry(hashmap, &key, ent, NULL); | ||||||
| 				struct collision_entry, ent); |  | ||||||
| } | } | ||||||
|  |  | ||||||
| static int collision_cmp(const void *unused_cmp_data, | static int collision_cmp(const void *unused_cmp_data, | ||||||
|  |  | ||||||
|  | @ -37,8 +37,7 @@ static struct dir_entry *find_dir_entry__hash(struct index_state *istate, | ||||||
| 	struct dir_entry key; | 	struct dir_entry key; | ||||||
| 	hashmap_entry_init(&key.ent, hash); | 	hashmap_entry_init(&key.ent, hash); | ||||||
| 	key.namelen = namelen; | 	key.namelen = namelen; | ||||||
| 	return hashmap_get_entry(&istate->dir_hash, &key, name, | 	return hashmap_get_entry(&istate->dir_hash, &key, ent, name); | ||||||
| 					struct dir_entry, ent); |  | ||||||
| } | } | ||||||
|  |  | ||||||
| static struct dir_entry *find_dir_entry(struct index_state *istate, | static struct dir_entry *find_dir_entry(struct index_state *istate, | ||||||
|  |  | ||||||
|  | @ -101,8 +101,7 @@ struct patch_id *has_commit_patch_id(struct commit *commit, | ||||||
| 	if (init_patch_id_entry(&patch, commit, ids)) | 	if (init_patch_id_entry(&patch, commit, ids)) | ||||||
| 		return NULL; | 		return NULL; | ||||||
|  |  | ||||||
| 	return hashmap_get_entry(&ids->patches, &patch, NULL, | 	return hashmap_get_entry(&ids->patches, &patch, ent, NULL); | ||||||
| 					struct patch_id, ent); |  | ||||||
| } | } | ||||||
|  |  | ||||||
| struct patch_id *add_commit_patch_id(struct commit *commit, | struct patch_id *add_commit_patch_id(struct commit *commit, | ||||||
|  |  | ||||||
|  | @ -229,9 +229,7 @@ static void find_exact_matches(struct string_list *a, struct string_list *b) | ||||||
| 		util->patch = b->items[i].string; | 		util->patch = b->items[i].string; | ||||||
| 		util->diff = util->patch + util->diff_offset; | 		util->diff = util->patch + util->diff_offset; | ||||||
| 		hashmap_entry_init(&util->e, strhash(util->diff)); | 		hashmap_entry_init(&util->e, strhash(util->diff)); | ||||||
| 		other = hashmap_remove_entry(&map, util, NULL, | 		other = hashmap_remove_entry(&map, util, e, NULL); | ||||||
| 					struct patch_util, |  | ||||||
| 					e /* member name */); |  | ||||||
| 		if (other) { | 		if (other) { | ||||||
| 			if (other->matching >= 0) | 			if (other->matching >= 0) | ||||||
| 				BUG("already assigned!"); | 				BUG("already assigned!"); | ||||||
|  |  | ||||||
							
								
								
									
										3
									
								
								remote.c
								
								
								
								
							
							
						
						
									
										3
									
								
								remote.c
								
								
								
								
							|  | @ -162,8 +162,7 @@ static struct remote *make_remote(const char *name, int len) | ||||||
| 	remotes[remotes_nr++] = ret; | 	remotes[remotes_nr++] = ret; | ||||||
|  |  | ||||||
| 	hashmap_entry_init(&ret->ent, lookup_entry.hash); | 	hashmap_entry_init(&ret->ent, lookup_entry.hash); | ||||||
| 	replaced = hashmap_put_entry(&remotes_hash, ret, struct remote, | 	replaced = hashmap_put_entry(&remotes_hash, ret, ent); | ||||||
| 					ent /* member name */); |  | ||||||
| 	assert(replaced == NULL);  /* no previous entry overwritten */ | 	assert(replaced == NULL);  /* no previous entry overwritten */ | ||||||
| 	return ret; | 	return ret; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -151,8 +151,7 @@ static void paths_and_oids_insert(struct hashmap *map, | ||||||
| 	key.path = (char *)path; | 	key.path = (char *)path; | ||||||
| 	oidset_init(&key.trees, 0); | 	oidset_init(&key.trees, 0); | ||||||
|  |  | ||||||
| 	entry = hashmap_get_entry(map, &key, NULL, | 	entry = hashmap_get_entry(map, &key, ent, NULL); | ||||||
| 				struct path_and_oids_entry, ent); |  | ||||||
| 	if (!entry) { | 	if (!entry) { | ||||||
| 		entry = xcalloc(1, sizeof(struct path_and_oids_entry)); | 		entry = xcalloc(1, sizeof(struct path_and_oids_entry)); | ||||||
| 		hashmap_entry_init(&entry->ent, hash); | 		hashmap_entry_init(&entry->ent, hash); | ||||||
|  |  | ||||||
|  | @ -24,8 +24,7 @@ struct subprocess_entry *subprocess_find_entry(struct hashmap *hashmap, const ch | ||||||
|  |  | ||||||
| 	hashmap_entry_init(&key.ent, strhash(cmd)); | 	hashmap_entry_init(&key.ent, strhash(cmd)); | ||||||
| 	key.cmd = cmd; | 	key.cmd = cmd; | ||||||
| 	return hashmap_get_entry(hashmap, &key, NULL, | 	return hashmap_get_entry(hashmap, &key, ent, NULL); | ||||||
| 				struct subprocess_entry, ent); |  | ||||||
| } | } | ||||||
|  |  | ||||||
| int subprocess_read_status(int fd, struct strbuf *status) | int subprocess_read_status(int fd, struct strbuf *status) | ||||||
|  |  | ||||||
|  | @ -141,9 +141,7 @@ static void cache_remove_path(struct submodule_cache *cache, | ||||||
| 	struct submodule_entry *removed; | 	struct submodule_entry *removed; | ||||||
| 	hashmap_entry_init(&e.ent, hash); | 	hashmap_entry_init(&e.ent, hash); | ||||||
| 	e.config = submodule; | 	e.config = submodule; | ||||||
| 	removed = hashmap_remove_entry(&cache->for_path, &e, NULL, | 	removed = hashmap_remove_entry(&cache->for_path, &e, ent, NULL); | ||||||
| 					struct submodule_entry, |  | ||||||
| 					ent /* member name */); |  | ||||||
| 	free(removed); | 	free(removed); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -172,8 +170,7 @@ static const struct submodule *cache_lookup_path(struct submodule_cache *cache, | ||||||
| 	hashmap_entry_init(&key.ent, hash); | 	hashmap_entry_init(&key.ent, hash); | ||||||
| 	key.config = &key_config; | 	key.config = &key_config; | ||||||
|  |  | ||||||
| 	entry = hashmap_get_entry(&cache->for_path, &key, NULL, | 	entry = hashmap_get_entry(&cache->for_path, &key, ent, NULL); | ||||||
| 				struct submodule_entry, ent); |  | ||||||
| 	if (entry) | 	if (entry) | ||||||
| 		return entry->config; | 		return entry->config; | ||||||
| 	return NULL; | 	return NULL; | ||||||
|  | @ -193,8 +190,7 @@ static struct submodule *cache_lookup_name(struct submodule_cache *cache, | ||||||
| 	hashmap_entry_init(&key.ent, hash); | 	hashmap_entry_init(&key.ent, hash); | ||||||
| 	key.config = &key_config; | 	key.config = &key_config; | ||||||
|  |  | ||||||
| 	entry = hashmap_get_entry(&cache->for_name, &key, NULL, | 	entry = hashmap_get_entry(&cache->for_name, &key, ent, NULL); | ||||||
| 				struct submodule_entry, ent); |  | ||||||
| 	if (entry) | 	if (entry) | ||||||
| 		return entry->config; | 		return entry->config; | ||||||
| 	return NULL; | 	return NULL; | ||||||
|  |  | ||||||
|  | @ -189,9 +189,7 @@ int cmd__hashmap(int argc, const char **argv) | ||||||
| 			entry = alloc_test_entry(hash, p1, p2); | 			entry = alloc_test_entry(hash, p1, p2); | ||||||
|  |  | ||||||
| 			/* add / replace entry */ | 			/* add / replace entry */ | ||||||
| 			entry = hashmap_put_entry(&map, entry, | 			entry = hashmap_put_entry(&map, entry, ent); | ||||||
| 						struct test_entry, |  | ||||||
| 						ent /* member name */); |  | ||||||
|  |  | ||||||
| 			/* print and free replaced entry, if any */ | 			/* print and free replaced entry, if any */ | ||||||
| 			puts(entry ? get_value(entry) : "NULL"); | 			puts(entry ? get_value(entry) : "NULL"); | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	 Eric Wong
						Eric Wong