Skip to content

Commit d4e2d15

Browse files
avargitster
authored andcommitted
streaming.c: move {open,close,read} from vtable to "struct git_istream"
Move the definition of the structure around the open/close/read functions introduced in 46bf043 (streaming: a new API to read from the object store, 2011-05-11) to instead populate "close" and "read" members in the "struct git_istream". This gets us rid of an extra pointer deference, and I think makes more sense. The "close" and "read" functions are the primary interface to the stream itself. Let's also populate a "open" callback in the same struct. That's now used by open_istream() after istream_source() decides what "open" function should be used. This isn't needed to get rid of the "stream_vtbl" variables, but makes sense for consistency. Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent de94c0e commit d4e2d15

File tree

1 file changed

+29
-43
lines changed

1 file changed

+29
-43
lines changed

streaming.c

Lines changed: 29 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ typedef int (*open_istream_fn)(struct git_istream *,
1515
typedef int (*close_istream_fn)(struct git_istream *);
1616
typedef ssize_t (*read_istream_fn)(struct git_istream *, char *, size_t);
1717

18-
struct stream_vtbl {
19-
close_istream_fn close;
20-
read_istream_fn read;
21-
};
22-
2318
#define FILTER_BUFFER (1024*16)
2419

2520
struct filtered_istream {
@@ -33,7 +28,10 @@ struct filtered_istream {
3328
};
3429

3530
struct git_istream {
36-
const struct stream_vtbl *vtbl;
31+
open_istream_fn open;
32+
close_istream_fn close;
33+
read_istream_fn read;
34+
3735
unsigned long size; /* inflated size of full object */
3836
git_zstream z;
3937
enum { z_unused, z_used, z_done, z_error } z_state;
@@ -146,18 +144,14 @@ static ssize_t read_istream_filtered(struct git_istream *st, char *buf,
146144
return filled;
147145
}
148146

149-
static struct stream_vtbl filtered_vtbl = {
150-
close_istream_filtered,
151-
read_istream_filtered,
152-
};
153-
154147
static struct git_istream *attach_stream_filter(struct git_istream *st,
155148
struct stream_filter *filter)
156149
{
157150
struct git_istream *ifs = xmalloc(sizeof(*ifs));
158151
struct filtered_istream *fs = &(ifs->u.filtered);
159152

160-
ifs->vtbl = &filtered_vtbl;
153+
ifs->close = close_istream_filtered;
154+
ifs->read = read_istream_filtered;
161155
fs->upstream = st;
162156
fs->filter = filter;
163157
fs->i_end = fs->i_ptr = 0;
@@ -225,11 +219,6 @@ static int close_istream_loose(struct git_istream *st)
225219
return 0;
226220
}
227221

228-
static struct stream_vtbl loose_vtbl = {
229-
close_istream_loose,
230-
read_istream_loose,
231-
};
232-
233222
static int open_istream_loose(struct git_istream *st, struct repository *r,
234223
const struct object_id *oid,
235224
enum object_type *type)
@@ -251,8 +240,9 @@ static int open_istream_loose(struct git_istream *st, struct repository *r,
251240
st->u.loose.hdr_used = strlen(st->u.loose.hdr) + 1;
252241
st->u.loose.hdr_avail = st->z.total_out;
253242
st->z_state = z_used;
243+
st->close = close_istream_loose;
244+
st->read = read_istream_loose;
254245

255-
st->vtbl = &loose_vtbl;
256246
return 0;
257247
}
258248

@@ -328,11 +318,6 @@ static int close_istream_pack_non_delta(struct git_istream *st)
328318
return 0;
329319
}
330320

331-
static struct stream_vtbl pack_non_delta_vtbl = {
332-
close_istream_pack_non_delta,
333-
read_istream_pack_non_delta,
334-
};
335-
336321
static int open_istream_pack_non_delta(struct git_istream *st,
337322
struct repository *r,
338323
const struct object_id *oid,
@@ -358,7 +343,9 @@ static int open_istream_pack_non_delta(struct git_istream *st,
358343
break;
359344
}
360345
st->z_state = z_unused;
361-
st->vtbl = &pack_non_delta_vtbl;
346+
st->close = close_istream_pack_non_delta;
347+
st->read = read_istream_pack_non_delta;
348+
362349
return 0;
363350
}
364351

@@ -389,17 +376,13 @@ static ssize_t read_istream_incore(struct git_istream *st, char *buf, size_t sz)
389376
return read_size;
390377
}
391378

392-
static struct stream_vtbl incore_vtbl = {
393-
close_istream_incore,
394-
read_istream_incore,
395-
};
396-
397379
static int open_istream_incore(struct git_istream *st, struct repository *r,
398380
const struct object_id *oid, enum object_type *type)
399381
{
400382
st->u.incore.buf = read_object_file_extended(r, oid, type, &st->size, 0);
401383
st->u.incore.read_ptr = 0;
402-
st->vtbl = &incore_vtbl;
384+
st->close = close_istream_incore;
385+
st->read = read_istream_incore;
403386

404387
return st->u.incore.buf ? 0 : -1;
405388
}
@@ -408,10 +391,10 @@ static int open_istream_incore(struct git_istream *st, struct repository *r,
408391
* static helpers variables and functions for users of streaming interface
409392
*****************************************************************************/
410393

411-
static open_istream_fn istream_source(struct git_istream *st,
412-
struct repository *r,
413-
const struct object_id *oid,
414-
enum object_type *type)
394+
static int istream_source(struct git_istream *st,
395+
struct repository *r,
396+
const struct object_id *oid,
397+
enum object_type *type)
415398
{
416399
unsigned long size;
417400
int status;
@@ -421,20 +404,23 @@ static open_istream_fn istream_source(struct git_istream *st,
421404
oi.sizep = &size;
422405
status = oid_object_info_extended(r, oid, &oi, 0);
423406
if (status < 0)
424-
return NULL;
407+
return status;
425408

426409
switch (oi.whence) {
427410
case OI_LOOSE:
428-
return open_istream_loose;
411+
st->open = open_istream_loose;
412+
return 0;
429413
case OI_PACKED:
430414
if (!oi.u.packed.is_delta && big_file_threshold < size) {
431415
st->u.in_pack.pack = oi.u.packed.pack;
432416
st->u.in_pack.pos = oi.u.packed.offset;
433-
return open_istream_pack_non_delta;
417+
st->open = open_istream_pack_non_delta;
418+
return 0;
434419
}
435420
/* fallthru */
436421
default:
437-
return open_istream_incore;
422+
st->open = open_istream_incore;
423+
return 0;
438424
}
439425
}
440426

@@ -444,14 +430,14 @@ static open_istream_fn istream_source(struct git_istream *st,
444430

445431
int close_istream(struct git_istream *st)
446432
{
447-
int r = st->vtbl->close(st);
433+
int r = st->close(st);
448434
free(st);
449435
return r;
450436
}
451437

452438
ssize_t read_istream(struct git_istream *st, void *buf, size_t sz)
453439
{
454-
return st->vtbl->read(st, buf, sz);
440+
return st->read(st, buf, sz);
455441
}
456442

457443
struct git_istream *open_istream(struct repository *r,
@@ -462,14 +448,14 @@ struct git_istream *open_istream(struct repository *r,
462448
{
463449
struct git_istream *st = xmalloc(sizeof(*st));
464450
const struct object_id *real = lookup_replace_object(r, oid);
465-
open_istream_fn open_fn = istream_source(st, r, real, type);
451+
int ret = istream_source(st, r, real, type);
466452

467-
if (!open_fn) {
453+
if (ret) {
468454
free(st);
469455
return NULL;
470456
}
471457

472-
if (open_fn(st, r, real, type)) {
458+
if (st->open(st, r, real, type)) {
473459
if (open_istream_incore(st, r, real, type)) {
474460
free(st);
475461
return NULL;

0 commit comments

Comments
 (0)