summaryrefslogtreecommitdiff
path: root/tempfile.h
diff options
context:
space:
mode:
authorJeff King <peff@peff.net>2017-09-05 12:15:04 (GMT)
committerJunio C Hamano <gitster@pobox.com>2017-09-06 08:19:54 (GMT)
commit422a21c6a086ec8c05c96b04bdccd960da141c04 (patch)
treefb616abdbb0e5af7b34ee5ac7b080a8a03257d87 /tempfile.h
parent24d82185d267daafc29ca61af1ed1dc746ba3463 (diff)
downloadgit-422a21c6a086ec8c05c96b04bdccd960da141c04.zip
git-422a21c6a086ec8c05c96b04bdccd960da141c04.tar.gz
git-422a21c6a086ec8c05c96b04bdccd960da141c04.tar.bz2
tempfile: remove deactivated list entries
Once a "struct tempfile" is added to the global cleanup list, it is never removed. This means that its storage must remain valid for the lifetime of the program. For single-use tempfiles and locks, this isn't a big deal: we just declare the struct static. But for library code which may take multiple simultaneous locks (like the ref code), they're forced to allocate a struct on the heap and leak it. This is mostly OK in practice. The size of the leak is bounded by the number of refs, and most programs exit after operating on a fixed number of refs (and allocate simultaneous memory proportional to the number of ref updates in the first place). But: 1. It isn't hard to imagine a real leak: a program which runs for a long time taking a series of ref update instructions and fulfilling them one by one. I don't think we have such a program now, but it's certainly plausible. 2. The leaked entries appear as false positives to tools like valgrind. Let's relax this rule by keeping only "active" tempfiles on the list. We can do this easily by moving the list-add operation from prepare_tempfile_object to activate_tempfile, and adding a deletion in deactivate_tempfile. Existing callers do not need to be updated immediately. They'll continue to leak any tempfile objects they may have allocated, but that's no different than the status quo. We can clean them up individually. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'tempfile.h')
-rw-r--r--tempfile.h15
1 files changed, 5 insertions, 10 deletions
diff --git a/tempfile.h b/tempfile.h
index 2ee24f4..e32b4df 100644
--- a/tempfile.h
+++ b/tempfile.h
@@ -17,12 +17,9 @@
*
* The caller:
*
- * * Allocates a `struct tempfile` either as a static variable or on
- * the heap, initialized to zeros. Once you use the structure to
- * call `create_tempfile()`, it belongs to the tempfile subsystem
- * and its storage must remain valid throughout the life of the
- * program (i.e. you cannot use an on-stack variable to hold this
- * structure).
+ * * Allocates a `struct tempfile`. Once the structure is passed to
+ * `create_tempfile()`, its storage must remain valid until
+ * `delete_tempfile()` or `rename_tempfile()` is called on it.
*
* * Attempts to create a temporary file by calling
* `create_tempfile()`.
@@ -52,9 +49,8 @@
* temporary file by calling `close_tempfile_gently()`, and later call
* `delete_tempfile()` or `rename_tempfile()`.
*
- * Even after the temporary file is renamed or deleted, the `tempfile`
- * object must not be freed or altered by the caller. However, it may
- * be reused; just pass it to another call of `create_tempfile()`.
+ * After the temporary file is renamed or deleted, the `tempfile`
+ * object may be reused or freed.
*
* If the program exits before `rename_tempfile()` or
* `delete_tempfile()` is called, an `atexit(3)` handler will close
@@ -88,7 +84,6 @@ struct tempfile {
volatile int fd;
FILE *volatile fp;
volatile pid_t owner;
- char on_list;
struct strbuf filename;
};