Skip to content

Commit 4f78c24

Browse files
mhaggergitster
authored andcommitted
refs: document the lifetime of the args passed to each_ref_fn
The lifetime of the memory pointed to by the refname and sha1 arguments to each_ref_fn was never documented, but some callers used to assume that it was essentially permanent. In fact the API does *not* guarantee that these objects live beyond a single callback invocation. In the current code, the lifetimes are bound together with the lifetimes of the ref_caches. Since these are usually long, the callers usually got away with their sloppiness. But even today, if a ref_cache is invalidated the memory can be freed. And planned changes to reference caching, needed to eliminate race conditions, will probably need to shorten the lifetimes of these objects. The commits leading up to this have (hopefully) fixed all of the callers of the for_each_ref()-like functions. This commit does the last step: documents what each_ref_fn callbacks can assume about object lifetimes. Signed-off-by: Michael Haggerty <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bf42772 commit 4f78c24

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

refs.h

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,23 @@ struct ref_lock {
1515
#define REF_ISBROKEN 0x04
1616

1717
/*
18-
* Calls the specified function for each ref file until it returns
19-
* nonzero, and returns the value. Please note that it is not safe to
20-
* modify references while an iteration is in progress, unless the
21-
* same callback function invocation that modifies the reference also
22-
* returns a nonzero value to immediately stop the iteration.
18+
* The signature for the callback function for the for_each_*()
19+
* functions below. The memory pointed to by the refname and sha1
20+
* arguments is only guaranteed to be valid for the duration of a
21+
* single callback invocation.
22+
*/
23+
typedef int each_ref_fn(const char *refname,
24+
const unsigned char *sha1, int flags, void *cb_data);
25+
26+
/*
27+
* The following functions invoke the specified callback function for
28+
* each reference indicated. If the function ever returns a nonzero
29+
* value, stop the iteration and return that value. Please note that
30+
* it is not safe to modify references while an iteration is in
31+
* progress, unless the same callback function invocation that
32+
* modifies the reference also returns a nonzero value to immediately
33+
* stop the iteration.
2334
*/
24-
typedef int each_ref_fn(const char *refname, const unsigned char *sha1, int flags, void *cb_data);
2535
extern int head_ref(each_ref_fn, void *);
2636
extern int for_each_ref(each_ref_fn, void *);
2737
extern int for_each_ref_in(const char *, each_ref_fn, void *);

0 commit comments

Comments
 (0)