streaming: create structure for in-core 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 in-core object streams to move towards this design. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Nov 23, 2025 at 19:59 UTC e030d0aeb5ebf79cdc4910e79d59e33998de78cd
1 file changed +25 -19
streaming.c
+25 -19
@@ -39,11 +39,6 @@ struct odb_read_stream {
39 enum { z_unused, z_used, z_done, z_error } z_state;
40
41 union {
42 - struct {
43 - char *buf; /* from odb_read_object_info_extended() */
44 - unsigned long read_ptr;
45 - } incore;
46 -
42 struct {
43 void *mapped;
44 unsigned long mapsize;
@@ -401,22 +396,30 @@ static int open_istream_pack_non_delta(struct odb_read_stream **out,
396 *
397 *****************************************************************/
398
404 -static int close_istream_incore(struct odb_read_stream *st)
399 +struct odb_incore_read_stream {
400 + struct odb_read_stream base;
401 + char *buf; /* from odb_read_object_info_extended() */
402 + unsigned long read_ptr;
403 +};
404 +
405 +static int close_istream_incore(struct odb_read_stream *_st)
406 {
406 - free(st->u.incore.buf);
407 + struct odb_incore_read_stream *st = (struct odb_incore_read_stream *)_st;
408 + free(st->buf);
409 return 0;
410 }
411
410 -static ssize_t read_istream_incore(struct odb_read_stream *st, char *buf, size_t sz)
412 +static ssize_t read_istream_incore(struct odb_read_stream *_st, char *buf, size_t sz)
413 {
414 + struct odb_incore_read_stream *st = (struct odb_incore_read_stream *)_st;
415 size_t read_size = sz;
413 - size_t remainder = st->size - st->u.incore.read_ptr;
416 + size_t remainder = st->base.size - st->read_ptr;
417
418 if (remainder <= read_size)
419 read_size = remainder;
420 if (read_size) {
418 - memcpy(buf, st->u.incore.buf + st->u.incore.read_ptr, read_size);
419 - st->u.incore.read_ptr += read_size;
421 + memcpy(buf, st->buf + st->read_ptr, read_size);
422 + st->read_ptr += read_size;
423 }
424 return read_size;
425 }
@@ -426,22 +429,25 @@ static int open_istream_incore(struct odb_read_stream **out,
429 const struct object_id *oid)
430 {
431 struct object_info oi = OBJECT_INFO_INIT;
429 - struct odb_read_stream stream = {
430 - .close = close_istream_incore,
431 - .read = read_istream_incore,
432 + struct odb_incore_read_stream stream = {
433 + .base.close = close_istream_incore,
434 + .base.read = read_istream_incore,
435 };
436 + struct odb_incore_read_stream *st;
437 int ret;
438
435 - oi.typep = &stream.type;
436 - oi.sizep = &stream.size;
437 - oi.contentp = (void **)&stream.u.incore.buf;
439 + oi.typep = &stream.base.type;
440 + oi.sizep = &stream.base.size;
441 + oi.contentp = (void **)&stream.buf;
442 ret = odb_read_object_info_extended(r->objects, oid, &oi,
443 OBJECT_INFO_DIE_IF_CORRUPT);
444 if (ret)
445 return ret;
446
443 - CALLOC_ARRAY(*out, 1);
444 - **out = stream;
447 + CALLOC_ARRAY(st, 1);
448 + *st = stream;
449 + *out = &st->base;
450 +
451 return 0;
452 }
453