Re: [PATCH v2 05/17] odb/source-inmemory: implement `read_object_stream()` callback
- From
Karthik Nayak <karthik.188@gmail.com>
- Date
- Apr 9, 2026, 09:49 UTC
- Message-ID
- <CAOLa=ZSHAF25zbJ=KHp=u0pFCpAHb-jd45A3dxTSn9pwKHkxFQ@mail.gmail.com>
- In-Reply-To
- <20260409-b4-pks-odb-source-inmemory-v2-5-f02b4f1c0f13@pks.im>
Patrick Steinhardt <ps@pks.im> writes:
Show 30 quoted lines
> Implement the `read_object_stream()` callback function for the in-memory
> source.
>
> Signed-off-by: Patrick Steinhardt <ps@pks.im>
> ---
> odb/source-inmemory.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 50 insertions(+)
>
> diff --git a/odb/source-inmemory.c b/odb/source-inmemory.c
> index 12c80f9b34..4a68169430 100644
> --- a/odb/source-inmemory.c
> +++ b/odb/source-inmemory.c
> @@ -1,6 +1,7 @@
> #include "git-compat-util.h"
> #include "odb.h"
> #include "odb/source-inmemory.h"
> +#include "odb/streaming.h"
> #include "repository.h"
>
> static const struct cached_object *find_cached_object(struct odb_source_inmemory *source,
> @@ -53,6 +54,54 @@ static int odb_source_inmemory_read_object_info(struct odb_source *source,
> return 0;
> }
>
> +struct odb_read_stream_inmemory {
> + struct odb_read_stream base;
> + const void *buf;
> + size_t offset;
> +};
> +To stream objects, we have a new structure which is used in the callback.
Show 6 quoted lines
> +static ssize_t odb_read_stream_inmemory_read(struct odb_read_stream *stream,
> + char *buf, size_t buf_len)
> +{
> + struct odb_read_stream_inmemory *inmemory =
> + container_of(stream, struct odb_read_stream_inmemory, base);
> + size_t bytes = buf_len;> + if (buf_len > inmemory->base.size - inmemory->offset) > + bytes = inmemory->base.size - inmemory->offset; > + memcpy(buf, inmemory->buf, bytes); > +
Shouldn't the offset also be set and we only memcpy offset onwards?
Show 27 quoted lines
> + return bytes;
> +}
> +
> +static int odb_read_stream_inmemory_close(struct odb_read_stream *stream UNUSED)
> +{
> + return 0;
> +}
> +
> +static int odb_source_inmemory_read_object_stream(struct odb_read_stream **out,
> + struct odb_source *source,
> + const struct object_id *oid)
> +{
> + struct odb_source_inmemory *inmemory = odb_source_inmemory_downcast(source);
> + struct odb_read_stream_inmemory *stream;
> + const struct cached_object *object;
> +
> + object = find_cached_object(inmemory, oid);
> + if (!object)
> + return -1;
> +
> + CALLOC_ARRAY(stream, 1);
> + stream->base.read = odb_read_stream_inmemory_read;
> + stream->base.close = odb_read_stream_inmemory_close;
> + stream->base.size = object->size;
> + stream->base.type = object->type;
> + stream->buf = object->buf;
> +So the object is simply mapped to the structure which is propagated in `read()`. Since we don't copy any new data over, `close()` has nothing to do.
[snip]