Skip to content

Commit 3ddef47

Browse files
pks-tgitster
authored andcommitted
reftable/record: improve semantics when initializing records
According to our usual coding style, the `reftable_new_record()` function would indicate that it is allocating a new record. This is not the case though as the function merely initializes records without allocating any memory. Replace `reftable_new_record()` with a new `reftable_record_init()` function that takes a record pointer as input and initializes it accordingly. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 62d3c8e commit 3ddef47

File tree

6 files changed

+33
-54
lines changed

6 files changed

+33
-54
lines changed

reftable/block.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -382,23 +382,23 @@ int block_reader_seek(struct block_reader *br, struct block_iter *it,
382382
.key = *want,
383383
.r = br,
384384
};
385-
struct reftable_record rec = reftable_new_record(block_reader_type(br));
386-
int err = 0;
387385
struct block_iter next = BLOCK_ITER_INIT;
386+
struct reftable_record rec;
387+
int err = 0, i;
388388

389-
int i = binsearch(br->restart_count, &restart_key_less, &args);
390389
if (args.error) {
391390
err = REFTABLE_FORMAT_ERROR;
392391
goto done;
393392
}
394393

395-
it->br = br;
396-
if (i > 0) {
397-
i--;
398-
it->next_off = block_reader_restart_offset(br, i);
399-
} else {
394+
i = binsearch(br->restart_count, &restart_key_less, &args);
395+
if (i > 0)
396+
it->next_off = block_reader_restart_offset(br, i - 1);
397+
else
400398
it->next_off = br->header_off + 4;
401-
}
399+
it->br = br;
400+
401+
reftable_record_init(&rec, block_reader_type(br));
402402

403403
/* We're looking for the last entry less/equal than the wanted key, so
404404
we have to go one entry too far and then back up.

reftable/merged.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ static int merged_iter_init(struct merged_iter *mi)
2121
{
2222
for (size_t i = 0; i < mi->stack_len; i++) {
2323
struct pq_entry e = {
24-
.rec = reftable_new_record(mi->typ),
2524
.index = i,
2625
};
2726
int err;
2827

28+
reftable_record_init(&e.rec, mi->typ);
2929
err = iterator_next(&mi->stack[i], &e.rec);
3030
if (err < 0)
3131
return err;
@@ -57,10 +57,12 @@ static int merged_iter_advance_nonnull_subiter(struct merged_iter *mi,
5757
size_t idx)
5858
{
5959
struct pq_entry e = {
60-
.rec = reftable_new_record(mi->typ),
6160
.index = idx,
6261
};
63-
int err = iterator_next(&mi->stack[idx], &e.rec);
62+
int err;
63+
64+
reftable_record_init(&e.rec, mi->typ);
65+
err = iterator_next(&mi->stack[idx], &e.rec);
6466
if (err < 0)
6567
return err;
6668

reftable/reader.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,13 +444,13 @@ static int reader_start(struct reftable_reader *r, struct table_iter *ti,
444444
static int reader_seek_linear(struct table_iter *ti,
445445
struct reftable_record *want)
446446
{
447-
struct reftable_record rec =
448-
reftable_new_record(reftable_record_type(want));
449447
struct strbuf want_key = STRBUF_INIT;
450448
struct strbuf got_key = STRBUF_INIT;
451449
struct table_iter next = TABLE_ITER_INIT;
450+
struct reftable_record rec;
452451
int err = -1;
453452

453+
reftable_record_init(&rec, reftable_record_type(want));
454454
reftable_record_key(want, &want_key);
455455

456456
while (1) {

reftable/record.c

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,45 +1259,22 @@ reftable_record_vtable(struct reftable_record *rec)
12591259
abort();
12601260
}
12611261

1262-
struct reftable_record reftable_new_record(uint8_t typ)
1262+
void reftable_record_init(struct reftable_record *rec, uint8_t typ)
12631263
{
1264-
struct reftable_record clean = {
1265-
.type = typ,
1266-
};
1264+
memset(rec, 0, sizeof(*rec));
1265+
rec->type = typ;
12671266

1268-
/* the following is involved, but the naive solution (just return
1269-
* `clean` as is, except for BLOCK_TYPE_INDEX), returns a garbage
1270-
* clean.u.obj.offsets pointer on Windows VS CI. Go figure.
1271-
*/
12721267
switch (typ) {
1273-
case BLOCK_TYPE_OBJ:
1274-
{
1275-
struct reftable_obj_record obj = { 0 };
1276-
clean.u.obj = obj;
1277-
break;
1278-
}
1279-
case BLOCK_TYPE_INDEX:
1280-
{
1281-
struct reftable_index_record idx = {
1282-
.last_key = STRBUF_INIT,
1283-
};
1284-
clean.u.idx = idx;
1285-
break;
1286-
}
12871268
case BLOCK_TYPE_REF:
1288-
{
1289-
struct reftable_ref_record ref = { 0 };
1290-
clean.u.ref = ref;
1291-
break;
1292-
}
12931269
case BLOCK_TYPE_LOG:
1294-
{
1295-
struct reftable_log_record log = { 0 };
1296-
clean.u.log = log;
1297-
break;
1298-
}
1270+
case BLOCK_TYPE_OBJ:
1271+
return;
1272+
case BLOCK_TYPE_INDEX:
1273+
strbuf_init(&rec->u.idx.last_key, 0);
1274+
return;
1275+
default:
1276+
BUG("unhandled record type");
12991277
}
1300-
return clean;
13011278
}
13021279

13031280
void reftable_record_print(struct reftable_record *rec, int hash_size)

reftable/record.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,6 @@ struct reftable_record_vtable {
6969
/* returns true for recognized block types. Block start with the block type. */
7070
int reftable_is_block_type(uint8_t typ);
7171

72-
/* return an initialized record for the given type */
73-
struct reftable_record reftable_new_record(uint8_t typ);
74-
7572
/* Encode `key` into `dest`. Sets `is_restart` to indicate a restart. Returns
7673
* number of bytes written. */
7774
int reftable_encode_key(int *is_restart, struct string_view dest,
@@ -100,8 +97,8 @@ struct reftable_obj_record {
10097
/* record is a generic wrapper for different types of records. It is normally
10198
* created on the stack, or embedded within another struct. If the type is
10299
* known, a fresh instance can be initialized explicitly. Otherwise, use
103-
* reftable_new_record() to initialize generically (as the index_record is not
104-
* valid as 0-initialized structure)
100+
* `reftable_record_init()` to initialize generically (as the index_record is
101+
* not valid as 0-initialized structure)
105102
*/
106103
struct reftable_record {
107104
uint8_t type;
@@ -113,6 +110,9 @@ struct reftable_record {
113110
} u;
114111
};
115112

113+
/* Initialize the reftable record for the given type */
114+
void reftable_record_init(struct reftable_record *rec, uint8_t typ);
115+
116116
/* see struct record_vtable */
117117
int reftable_record_equal(struct reftable_record *a, struct reftable_record *b, int hash_size);
118118
void reftable_record_print(struct reftable_record *rec, int hash_size);

reftable/record_test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616

1717
static void test_copy(struct reftable_record *rec)
1818
{
19-
struct reftable_record copy = { 0 };
19+
struct reftable_record copy;
2020
uint8_t typ;
2121

2222
typ = reftable_record_type(rec);
23-
copy = reftable_new_record(typ);
23+
reftable_record_init(&copy, typ);
2424
reftable_record_copy_from(&copy, rec, GIT_SHA1_RAWSZ);
2525
/* do it twice to catch memory leaks */
2626
reftable_record_copy_from(&copy, rec, GIT_SHA1_RAWSZ);

0 commit comments

Comments
 (0)