struct snapshot: store `start` rather than `header_len`

Store a pointer to the start of the actual references within the `packed-refs` contents rather than storing the length of the header. This is more convenient for most users of this field. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Jan 24, 2018 at 12:14 UTC 4a2854f77cc913fafc7f3191a61b343280979643
1 file changed +33 -31
refs/packed-backend.c
+33 -31
@@ -68,17 +68,21 @@ struct snapshot {
68 int mmapped;
69
70 /*
71 - * The contents of the `packed-refs` file. If the file was
72 - * already sorted, this points at the mmapped contents of the
73 - * file. If not, this points at heap-allocated memory
74 - * containing the contents, sorted. If there were no contents
75 - * (e.g., because the file didn't exist), `buf` and `eof` are
76 - * both NULL.
71 + * The contents of the `packed-refs` file:
72 + *
73 + * - buf -- a pointer to the start of the memory
74 + * - start -- a pointer to the first byte of actual references
75 + * (i.e., after the header line, if one is present)
76 + * - eof -- a pointer just past the end of the reference
77 + * contents
78 + *
79 + * If the `packed-refs` file was already sorted, `buf` points
80 + * at the mmapped contents of the file. If not, it points at
81 + * heap-allocated memory containing the contents, sorted. If
82 + * there were no contents (e.g., because the file didn't
83 + * exist), `buf`, `start`, and `eof` are all NULL.
84 */
78 - char *buf, *eof;
79 -
80 - /* The size of the header line, if any; otherwise, 0: */
81 - size_t header_len;
85 + char *buf, *start, *eof;
86
87 /*
88 * What is the peeled state of the `packed-refs` file that
@@ -169,8 +173,7 @@ static void clear_snapshot_buffer(struct snapshot *snapshot)
173 } else {
174 free(snapshot->buf);
175 }
172 - snapshot->buf = snapshot->eof = NULL;
173 - snapshot->header_len = 0;
176 + snapshot->buf = snapshot->start = snapshot->eof = NULL;
177 }
178
179 /*
@@ -319,13 +322,14 @@ static void sort_snapshot(struct snapshot *snapshot)
322 size_t len, i;
323 char *new_buffer, *dst;
324
322 - pos = snapshot->buf + snapshot->header_len;
325 + pos = snapshot->start;
326 eof = snapshot->eof;
324 - len = eof - pos;
327
326 - if (!len)
328 + if (pos == eof)
329 return;
330
331 + len = eof - pos;
332 +
333 /*
334 * Initialize records based on a crude estimate of the number
335 * of references in the file (we'll grow it below if needed):
@@ -391,9 +395,8 @@ static void sort_snapshot(struct snapshot *snapshot)
395 * place:
396 */
397 clear_snapshot_buffer(snapshot);
394 - snapshot->buf = new_buffer;
398 + snapshot->buf = snapshot->start = new_buffer;
399 snapshot->eof = new_buffer + len;
396 - snapshot->header_len = 0;
400
401 cleanup:
402 free(records);
@@ -442,14 +445,14 @@ static const char *find_end_of_record(const char *p, const char *end)
445 */
446 static void verify_buffer_safe(struct snapshot *snapshot)
447 {
445 - const char *buf = snapshot->buf + snapshot->header_len;
448 + const char *start = snapshot->start;
449 const char *eof = snapshot->eof;
450 const char *last_line;
451
449 - if (buf == eof)
452 + if (start == eof)
453 return;
454
452 - last_line = find_start_of_record(buf, eof - 1);
455 + last_line = find_start_of_record(start, eof - 1);
456 if (*(eof - 1) != '\n' || eof - last_line < GIT_SHA1_HEXSZ + 2)
457 die_invalid_line(snapshot->refs->path,
458 last_line, eof - last_line);
@@ -495,18 +498,19 @@ static int load_contents(struct snapshot *snapshot)
498 bytes_read = read_in_full(fd, snapshot->buf, size);
499 if (bytes_read < 0 || bytes_read != size)
500 die_errno("couldn't read %s", snapshot->refs->path);
498 - snapshot->eof = snapshot->buf + size;
501 snapshot->mmapped = 0;
502 break;
503 case MMAP_TEMPORARY:
504 case MMAP_OK:
505 snapshot->buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
504 - snapshot->eof = snapshot->buf + size;
506 snapshot->mmapped = 1;
507 break;
508 }
509 close(fd);
510
511 + snapshot->start = snapshot->buf;
512 + snapshot->eof = snapshot->buf + size;
513 +
514 return 1;
515 }
516
@@ -539,7 +543,7 @@ static const char *find_reference_location(struct snapshot *snapshot,
543 * preceding records all have reference names that come
544 * *before* `refname`.
545 */
542 - const char *lo = snapshot->buf + snapshot->header_len;
546 + const char *lo = snapshot->start;
547
548 /*
549 * A pointer to a the first character of a record whose
@@ -617,8 +621,7 @@ static struct snapshot *create_snapshot(struct packed_ref_store *refs)
621 /* If the file has a header line, process it: */
622 if (snapshot->buf < snapshot->eof && *snapshot->buf == '#') {
623 struct strbuf tmp = STRBUF_INIT;
620 - char *p;
621 - const char *eol;
624 + char *p, *eol;
625 struct string_list traits = STRING_LIST_INIT_NODUP;
626
627 eol = memchr(snapshot->buf, '\n',
@@ -647,7 +650,7 @@ static struct snapshot *create_snapshot(struct packed_ref_store *refs)
650 /* perhaps other traits later as well */
651
652 /* The "+ 1" is for the LF character. */
650 - snapshot->header_len = eol + 1 - snapshot->buf;
653 + snapshot->start = eol + 1;
654
655 string_list_clear(&traits, 0);
656 strbuf_release(&tmp);
@@ -671,13 +674,12 @@ static struct snapshot *create_snapshot(struct packed_ref_store *refs)
674 * We don't want to leave the file mmapped, so we are
675 * forced to make a copy now:
676 */
674 - size_t size = snapshot->eof -
675 - (snapshot->buf + snapshot->header_len);
677 + size_t size = snapshot->eof - snapshot->start;
678 char *buf_copy = xmalloc(size);
679
678 - memcpy(buf_copy, snapshot->buf + snapshot->header_len, size);
680 + memcpy(buf_copy, snapshot->start, size);
681 clear_snapshot_buffer(snapshot);
680 - snapshot->buf = buf_copy;
682 + snapshot->buf = snapshot->start = buf_copy;
683 snapshot->eof = buf_copy + size;
684 }
685
@@ -937,7 +939,7 @@ static struct ref_iterator *packed_ref_iterator_begin(
939 if (prefix && *prefix)
940 start = find_reference_location(snapshot, prefix, 0);
941 else
940 - start = snapshot->buf + snapshot->header_len;
942 + start = snapshot->start;
943
944 iter->pos = start;
945 iter->eof = snapshot->eof;