Skip to content

Commit 84a089d

Browse files
committed
maps: provide return value when deleting entries
Currently, the delete functions of maps do not provide a return value. Like this, it is impossible to tell whether the entry has really been deleted or not. Change the implementation to provide either a return value of zero if the entry has been successfully deleted or `GIT_ENOTFOUND` if the key could not be found. Convert callers to the `delete_at` functions to instead use this higher-level interface.
1 parent 8da9394 commit 84a089d

File tree

10 files changed

+119
-48
lines changed

10 files changed

+119
-48
lines changed

src/idxmap.c

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,24 @@ void git_idxmap_icase_insert(git_idxmap_icase *map, const git_index_entry *key,
160160
}
161161
}
162162

163+
int git_idxmap_delete(git_idxmap *map, const git_index_entry *key)
164+
{
165+
khiter_t idx = git_idxmap_lookup_index(map, key);
166+
if (!git_idxmap_valid_index(map, idx))
167+
return GIT_ENOTFOUND;
168+
git_idxmap_delete_at(map, idx);
169+
return 0;
170+
}
171+
172+
int git_idxmap_icase_delete(git_idxmap_icase *map, const git_index_entry *key)
173+
{
174+
khiter_t idx = git_idxmap_icase_lookup_index(map, key);
175+
if (!git_idxmap_valid_index((git_idxmap *)map, idx))
176+
return GIT_ENOTFOUND;
177+
git_idxmap_icase_delete_at(map, idx);
178+
return 0;
179+
}
180+
163181
size_t git_idxmap_lookup_index(git_idxmap *map, const git_index_entry *key)
164182
{
165183
return kh_get(idx, map, key);
@@ -204,16 +222,3 @@ void git_idxmap_icase_delete_at(git_idxmap_icase *map, size_t idx)
204222
{
205223
kh_del(idxicase, map, idx);
206224
}
207-
208-
void git_idxmap_delete(git_idxmap *map, const git_index_entry *key)
209-
{
210-
khiter_t idx = git_idxmap_lookup_index(map, key);
211-
if (git_idxmap_valid_index(map, idx))
212-
git_idxmap_delete_at(map, idx);
213-
}
214-
void git_idxmap_icase_delete(git_idxmap_icase *map, const git_index_entry *key)
215-
{
216-
khiter_t idx = git_idxmap_icase_lookup_index(map, key);
217-
if (git_idxmap_valid_index((git_idxmap *)map, idx))
218-
git_idxmap_icase_delete_at(map, idx);
219-
}

src/idxmap.h

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,34 @@ int git_idxmap_set(git_idxmap *map, const git_index_entry *key, void *value);
146146
*/
147147
int git_idxmap_icase_set(git_idxmap_icase *map, const git_index_entry *key, void *value);
148148

149+
/**
150+
* Delete an entry from the map.
151+
*
152+
* Delete the given key and its value from the map. If no such
153+
* key exists, this will do nothing.
154+
*
155+
* @param map map to delete key in
156+
* @param key key to delete
157+
* @return `0` if the key has been deleted, GIT_ENOTFOUND if no
158+
* such key was found, a negative code in case of an
159+
* error
160+
*/
161+
int git_idxmap_delete(git_idxmap *map, const git_index_entry *key);
162+
163+
/**
164+
* Delete an entry from the map.
165+
*
166+
* Delete the given key and its value from the map. If no such
167+
* key exists, this will do nothing.
168+
*
169+
* @param map map to delete key in
170+
* @param key key to delete
171+
* @return `0` if the key has been deleted, GIT_ENOTFOUND if no
172+
* such key was found, a negative code in case of an
173+
* error
174+
*/
175+
int git_idxmap_icase_delete(git_idxmap_icase *map, const git_index_entry *key);
176+
149177
void git_idxmap_insert(git_idxmap *map, const git_index_entry *key, void *value, int *rval);
150178
void git_idxmap_icase_insert(git_idxmap_icase *map, const git_index_entry *key, void *value, int *rval);
151179

@@ -160,7 +188,4 @@ int git_idxmap_icase_has_data(git_idxmap_icase *map, size_t idx);
160188
void git_idxmap_delete_at(git_idxmap *map, size_t idx);
161189
void git_idxmap_icase_delete_at(git_idxmap_icase *map, size_t idx);
162190

163-
void git_idxmap_delete(git_idxmap *map, const git_index_entry *key);
164-
void git_idxmap_icase_delete(git_idxmap_icase *map, const git_index_entry *key);
165-
166191
#endif

src/mwindow.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,21 +94,19 @@ int git_mwindow_get_pack(struct git_pack_file **out, const char *path)
9494
void git_mwindow_put_pack(struct git_pack_file *pack)
9595
{
9696
int count;
97-
size_t pos;
9897

9998
if (git_mutex_lock(&git__mwindow_mutex) < 0)
10099
return;
101100

102101
/* put before get would be a corrupted state */
103102
assert(git__pack_cache);
104103

105-
pos = git_strmap_lookup_index(git__pack_cache, pack->pack_name);
106104
/* if we cannot find it, the state is corrupted */
107-
assert(git_strmap_valid_index(git__pack_cache, pos));
105+
assert(git_strmap_exists(git__pack_cache, pack->pack_name));
108106

109107
count = git_atomic_dec(&pack->refcount);
110108
if (count == 0) {
111-
git_strmap_delete_at(git__pack_cache, pos);
109+
git_strmap_delete(git__pack_cache, pack->pack_name);
112110
git_packfile_free(pack);
113111
}
114112

src/offmap.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ int git_offmap_set(git_offmap *map, const git_off_t key, void *value)
6868
return 0;
6969
}
7070

71+
int git_offmap_delete(git_offmap *map, const git_off_t key)
72+
{
73+
khiter_t idx = git_offmap_lookup_index(map, key);
74+
if (!git_offmap_valid_index(map, idx))
75+
return GIT_ENOTFOUND;
76+
git_offmap_delete_at(map, idx);
77+
return 0;
78+
}
7179

7280
size_t git_offmap_lookup_index(git_offmap *map, const git_off_t key)
7381
{
@@ -125,13 +133,6 @@ void git_offmap_insert(git_offmap *map, const git_off_t key, void *value, int *r
125133
}
126134
}
127135

128-
void git_offmap_delete(git_offmap *map, const git_off_t key)
129-
{
130-
khiter_t idx = git_offmap_lookup_index(map, key);
131-
if (git_offmap_valid_index(map, idx))
132-
git_offmap_delete_at(map, idx);
133-
}
134-
135136
size_t git_offmap_begin(git_offmap *map)
136137
{
137138
GIT_UNUSED(map);

src/offmap.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,20 @@ void *git_offmap_get(git_offmap *map, const git_off_t key);
7676
*/
7777
int git_offmap_set(git_offmap *map, const git_off_t key, void *value);
7878

79+
/**
80+
* Delete an entry from the map.
81+
*
82+
* Delete the given key and its value from the map. If no such
83+
* key exists, this will do nothing.
84+
*
85+
* @param map map to delete key in
86+
* @param key key to delete
87+
* @return `0` if the key has been deleted, GIT_ENOTFOUND if no
88+
* such key was found, a negative code in case of an
89+
* error
90+
*/
91+
int git_offmap_delete(git_offmap *map, const git_off_t key);
92+
7993
size_t git_offmap_lookup_index(git_offmap *map, const git_off_t key);
8094
int git_offmap_valid_index(git_offmap *map, size_t idx);
8195

@@ -89,7 +103,6 @@ void git_offmap_delete_at(git_offmap *map, size_t idx);
89103

90104
int git_offmap_put(git_offmap *map, const git_off_t key, int *err);
91105
void git_offmap_insert(git_offmap *map, const git_off_t key, void *value, int *rval);
92-
void git_offmap_delete(git_offmap *map, const git_off_t key);
93106

94107
size_t git_offmap_begin(git_offmap *map);
95108
size_t git_offmap_end(git_offmap *map);

src/oidmap.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ int git_oidmap_set(git_oidmap *map, const git_oid *key, void *value)
7474
return 0;
7575
}
7676

77+
int git_oidmap_delete(git_oidmap *map, const git_oid *key)
78+
{
79+
khiter_t idx = git_oidmap_lookup_index(map, key);
80+
if (!git_oidmap_valid_index(map, idx))
81+
return GIT_ENOTFOUND;
82+
git_oidmap_delete_at(map, idx);
83+
return 0;
84+
}
85+
7786
size_t git_oidmap_lookup_index(git_oidmap *map, const git_oid *key)
7887
{
7988
return kh_get(oid, map, key);
@@ -135,13 +144,6 @@ void git_oidmap_insert(git_oidmap *map, const git_oid *key, void *value, int *rv
135144
}
136145
}
137146

138-
void git_oidmap_delete(git_oidmap *map, const git_oid *key)
139-
{
140-
khiter_t idx = git_oidmap_lookup_index(map, key);
141-
if (git_oidmap_valid_index(map, idx))
142-
git_oidmap_delete_at(map, idx);
143-
}
144-
145147
size_t git_oidmap_begin(git_oidmap *map)
146148
{
147149
GIT_UNUSED(map);

src/oidmap.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,20 @@ void *git_oidmap_get(git_oidmap *map, const git_oid *key);
7676
*/
7777
int git_oidmap_set(git_oidmap *map, const git_oid *key, void *value);
7878

79+
/**
80+
* Delete an entry from the map.
81+
*
82+
* Delete the given key and its value from the map. If no such
83+
* key exists, this will do nothing.
84+
*
85+
* @param map map to delete key in
86+
* @param key key to delete
87+
* @return `0` if the key has been deleted, GIT_ENOTFOUND if no
88+
* such key was found, a negative code in case of an
89+
* error
90+
*/
91+
int git_oidmap_delete(git_oidmap *map, const git_oid *key);
92+
7993
size_t git_oidmap_lookup_index(git_oidmap *map, const git_oid *key);
8094
int git_oidmap_valid_index(git_oidmap *map, size_t idx);
8195

@@ -90,7 +104,6 @@ void git_oidmap_delete_at(git_oidmap *map, size_t idx);
90104

91105
int git_oidmap_put(git_oidmap *map, const git_oid *key, int *err);
92106
void git_oidmap_insert(git_oidmap *map, const git_oid *key, void *value, int *rval);
93-
void git_oidmap_delete(git_oidmap *map, const git_oid *key);
94107

95108
size_t git_oidmap_begin(git_oidmap *map);
96109
size_t git_oidmap_end(git_oidmap *map);

src/sortedcache.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,9 +358,9 @@ int git_sortedcache_lookup_index(
358358
int git_sortedcache_remove(git_sortedcache *sc, size_t pos)
359359
{
360360
char *item;
361-
size_t mappos;
362361

363-
/* because of pool allocation, this can't actually remove the item,
362+
/*
363+
* Because of pool allocation, this can't actually remove the item,
364364
* but we can remove it from the items vector and the hash table.
365365
*/
366366

@@ -371,8 +371,7 @@ int git_sortedcache_remove(git_sortedcache *sc, size_t pos)
371371

372372
(void)git_vector_remove(&sc->items, pos);
373373

374-
mappos = git_strmap_lookup_index(sc->map, item + sc->item_path_offset);
375-
git_strmap_delete_at(sc->map, mappos);
374+
git_strmap_delete(sc->map, item + sc->item_path_offset);
376375

377376
if (sc->free_item)
378377
sc->free_item(sc->free_item_payload, item);

src/strmap.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ int git_strmap_set(git_strmap *map, const char *key, void *value)
6767
return 0;
6868
}
6969

70+
int git_strmap_delete(git_strmap *map, const char *key)
71+
{
72+
khiter_t idx = git_strmap_lookup_index(map, key);
73+
if (!git_strmap_valid_index(map, idx))
74+
return GIT_ENOTFOUND;
75+
git_strmap_delete_at(map, idx);
76+
return 0;
77+
}
78+
7079
size_t git_strmap_lookup_index(git_strmap *map, const char *key)
7180
{
7281
return kh_get(str, map, key);
@@ -128,13 +137,6 @@ void git_strmap_insert(git_strmap *map, const char *key, void *value, int *rval)
128137
}
129138
}
130139

131-
void git_strmap_delete(git_strmap *map, const char *key)
132-
{
133-
khiter_t idx = git_strmap_lookup_index(map, key);
134-
if (git_strmap_valid_index(map, idx))
135-
git_strmap_delete_at(map, idx);
136-
}
137-
138140
size_t git_strmap_begin(git_strmap *map)
139141
{
140142
GIT_UNUSED(map);

src/strmap.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,20 @@ void *git_strmap_get(git_strmap *map, const char *key);
7474
*/
7575
int git_strmap_set(git_strmap *map, const char *key, void *value);
7676

77+
/**
78+
* Delete an entry from the map.
79+
*
80+
* Delete the given key and its value from the map. If no such
81+
* key exists, this will do nothing.
82+
*
83+
* @param map map to delete key in
84+
* @param key key to delete
85+
* @return `0` if the key has been deleted, GIT_ENOTFOUND if no
86+
* such key was found, a negative code in case of an
87+
* error
88+
*/
89+
int git_strmap_delete(git_strmap *map, const char *key);
90+
7791
size_t git_strmap_lookup_index(git_strmap *map, const char *key);
7892
int git_strmap_valid_index(git_strmap *map, size_t idx);
7993

@@ -88,7 +102,6 @@ void git_strmap_delete_at(git_strmap *map, size_t idx);
88102

89103
int git_strmap_put(git_strmap *map, const char *key, int *err);
90104
void git_strmap_insert(git_strmap *map, const char *key, void *value, int *rval);
91-
void git_strmap_delete(git_strmap *map, const char *key);
92105

93106
#define git_strmap_foreach(h, kvar, vvar, code) { size_t __i; \
94107
for (__i = git_strmap_begin(h); __i != git_strmap_end(h); ++__i) { \

0 commit comments

Comments
 (0)