Skip to content

Commit 5f0d8d2

Browse files
pks-tgitster
authored andcommitted
streaming: create structure for packed object streams
As explained in a preceding commit, we want to get rid of the union of stream-type specific data in `struct odb_read_stream`. Create a new structure for packed object streams to move towards this design. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent b7774c0 commit 5f0d8d2

1 file changed

Lines changed: 40 additions & 35 deletions

File tree

streaming.c

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@ struct odb_read_stream {
3939
enum { z_unused, z_used, z_done, z_error } z_state;
4040

4141
union {
42-
struct {
43-
struct packed_git *pack;
44-
off_t pos;
45-
} in_pack;
46-
4742
struct filtered_istream filtered;
4843
} u;
4944
};
@@ -287,16 +282,23 @@ static int open_istream_loose(struct odb_read_stream **out,
287282
*
288283
*****************************************************************/
289284

290-
static ssize_t read_istream_pack_non_delta(struct odb_read_stream *st, char *buf,
285+
struct odb_packed_read_stream {
286+
struct odb_read_stream base;
287+
struct packed_git *pack;
288+
off_t pos;
289+
};
290+
291+
static ssize_t read_istream_pack_non_delta(struct odb_read_stream *_st, char *buf,
291292
size_t sz)
292293
{
294+
struct odb_packed_read_stream *st = (struct odb_packed_read_stream *)_st;
293295
size_t total_read = 0;
294296

295-
switch (st->z_state) {
297+
switch (st->base.z_state) {
296298
case z_unused:
297-
memset(&st->z, 0, sizeof(st->z));
298-
git_inflate_init(&st->z);
299-
st->z_state = z_used;
299+
memset(&st->base.z, 0, sizeof(st->base.z));
300+
git_inflate_init(&st->base.z);
301+
st->base.z_state = z_used;
300302
break;
301303
case z_done:
302304
return 0;
@@ -311,21 +313,21 @@ static ssize_t read_istream_pack_non_delta(struct odb_read_stream *st, char *buf
311313
struct pack_window *window = NULL;
312314
unsigned char *mapped;
313315

314-
mapped = use_pack(st->u.in_pack.pack, &window,
315-
st->u.in_pack.pos, &st->z.avail_in);
316+
mapped = use_pack(st->pack, &window,
317+
st->pos, &st->base.z.avail_in);
316318

317-
st->z.next_out = (unsigned char *)buf + total_read;
318-
st->z.avail_out = sz - total_read;
319-
st->z.next_in = mapped;
320-
status = git_inflate(&st->z, Z_FINISH);
319+
st->base.z.next_out = (unsigned char *)buf + total_read;
320+
st->base.z.avail_out = sz - total_read;
321+
st->base.z.next_in = mapped;
322+
status = git_inflate(&st->base.z, Z_FINISH);
321323

322-
st->u.in_pack.pos += st->z.next_in - mapped;
323-
total_read = st->z.next_out - (unsigned char *)buf;
324+
st->pos += st->base.z.next_in - mapped;
325+
total_read = st->base.z.next_out - (unsigned char *)buf;
324326
unuse_pack(&window);
325327

326328
if (status == Z_STREAM_END) {
327-
git_inflate_end(&st->z);
328-
st->z_state = z_done;
329+
git_inflate_end(&st->base.z);
330+
st->base.z_state = z_done;
329331
break;
330332
}
331333

@@ -338,17 +340,18 @@ static ssize_t read_istream_pack_non_delta(struct odb_read_stream *st, char *buf
338340
* or truncated), then use_pack() catches that and will die().
339341
*/
340342
if (status != Z_OK && status != Z_BUF_ERROR) {
341-
git_inflate_end(&st->z);
342-
st->z_state = z_error;
343+
git_inflate_end(&st->base.z);
344+
st->base.z_state = z_error;
343345
return -1;
344346
}
345347
}
346348
return total_read;
347349
}
348350

349-
static int close_istream_pack_non_delta(struct odb_read_stream *st)
351+
static int close_istream_pack_non_delta(struct odb_read_stream *_st)
350352
{
351-
close_deflated_stream(st);
353+
struct odb_packed_read_stream *st = (struct odb_packed_read_stream *)_st;
354+
close_deflated_stream(&st->base);
352355
return 0;
353356
}
354357

@@ -358,19 +361,17 @@ static int open_istream_pack_non_delta(struct odb_read_stream **out,
358361
struct packed_git *pack,
359362
off_t offset)
360363
{
361-
struct odb_read_stream stream = {
362-
.close = close_istream_pack_non_delta,
363-
.read = read_istream_pack_non_delta,
364-
};
364+
struct odb_packed_read_stream *stream;
365365
struct pack_window *window;
366366
enum object_type in_pack_type;
367+
size_t size;
367368

368369
window = NULL;
369370

370371
in_pack_type = unpack_object_header(pack,
371372
&window,
372373
&offset,
373-
&stream.size);
374+
&size);
374375
unuse_pack(&window);
375376
switch (in_pack_type) {
376377
default:
@@ -381,13 +382,17 @@ static int open_istream_pack_non_delta(struct odb_read_stream **out,
381382
case OBJ_TAG:
382383
break;
383384
}
384-
stream.type = in_pack_type;
385-
stream.z_state = z_unused;
386-
stream.u.in_pack.pack = pack;
387-
stream.u.in_pack.pos = offset;
388385

389-
CALLOC_ARRAY(*out, 1);
390-
**out = stream;
386+
CALLOC_ARRAY(stream, 1);
387+
stream->base.close = close_istream_pack_non_delta;
388+
stream->base.read = read_istream_pack_non_delta;
389+
stream->base.type = in_pack_type;
390+
stream->base.size = size;
391+
stream->base.z_state = z_unused;
392+
stream->pack = pack;
393+
stream->pos = offset;
394+
395+
*out = &stream->base;
391396

392397
return 0;
393398
}

0 commit comments

Comments
 (0)