refs/iterator: separate lifecycle from iteration
The ref and reflog iterators have their lifecycle attached to iteration:
once the iterator reaches its end, it is automatically released and the
caller doesn't have to care about that anymore. When the iterator should
be released before it has been exhausted, callers must explicitly abort
the iterator via `ref_iterator_abort()`.
This lifecycle is somewhat unusual in the Git codebase and creates two
problems:
- Callsites need to be very careful about when exactly they call
`ref_iterator_abort()`, as calling the function is only valid when
the iterator itself still is. This leads to somewhat awkward calling
patterns in some situations.
- It is impossible to reuse iterators and re-seek them to a different
prefix. This feature isn't supported by any iterator implementation
except for the reftable iterators anyway, but if it was implemented
it would allow us to optimize cases where we need to search for
specific references repeatedly by reusing internal state.
Detangle the lifecycle from iteration so that we don't deallocate the
iterator anymore once it is exhausted. Instead, callers are now expected
to always call a newly introduce `ref_iterator_free()` function that
deallocates the iterator and its internal state.
Note that the `dir_iterator` is somewhat special because it does not
implement the `ref_iterator` interface, but is only used to implement
other iterators. Consequently, we have to provide `dir_iterator_free()`
instead of `dir_iterator_release()` as the allocated structure itself is
managed by the `dir_iterator` interfaces, as well, and not freed by
`ref_iterator_free()` like in all the other cases.
While at it, drop the return value of `ref_iterator_abort()`, which
wasn't really required by any of the iterator implementations anyway.
Furthermore, stop calling `base_ref_iterator_free()` in any of the
backends, but instead call it in `ref_iterator_free()`.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
committed by
Junio C Hamano
parent
9e39a966ec
commit
cec2b6f55a
@@ -28,7 +28,7 @@
|
||||
*
|
||||
* while ((ok = dir_iterator_advance(iter)) == ITER_OK) {
|
||||
* if (want_to_stop_iteration()) {
|
||||
* ok = dir_iterator_abort(iter);
|
||||
* ok = ITER_DONE;
|
||||
* break;
|
||||
* }
|
||||
*
|
||||
@@ -39,6 +39,7 @@
|
||||
*
|
||||
* if (ok != ITER_DONE)
|
||||
* handle_error();
|
||||
* dir_iterator_free(iter);
|
||||
*
|
||||
* Callers are allowed to modify iter->path while they are working,
|
||||
* but they must restore it to its original contents before calling
|
||||
@@ -107,11 +108,7 @@ struct dir_iterator *dir_iterator_begin(const char *path, unsigned int flags);
|
||||
*/
|
||||
int dir_iterator_advance(struct dir_iterator *iterator);
|
||||
|
||||
/*
|
||||
* End the iteration before it has been exhausted. Free the
|
||||
* dir_iterator and any associated resources and return ITER_DONE. On
|
||||
* error, free the dir_iterator and return ITER_ERROR.
|
||||
*/
|
||||
int dir_iterator_abort(struct dir_iterator *iterator);
|
||||
/* Free the dir_iterator and any associated resources. */
|
||||
void dir_iterator_free(struct dir_iterator *iterator);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user