streaming.c: remove enum/function/vtbl indirection

Remove the indirection of discovering a function pointer to use via an enum and virtual table. This refactors code added in 46bf043807c (streaming: a new API to read from the object store, 2011-05-11). We can instead simply return an "open_istream_fn" for use from the "istream_source()" selector function directly. This allows us to get rid of the "incore", "loose" and "pack_non_delta" enum variables. We'll return the functions instead. The "stream_error" variable in that enum can likewise go in favor of returning NULL, which is what the open_istream() was doing when it got that value anyway. We can thus remove the entire enum, and the "open_istream_tbl" virtual table that (indirectly) referenced it. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed May 5, 2021 at 14:33 UTC 0d9af06e36d2e88e5e370c23e876a835fcf8b7ce
1 file changed +11 -25
streaming.c
+11 -25
@@ -8,13 +8,6 @@
8 #include "replace-object.h"
9 #include "packfile.h"
10
11 -enum input_source {
12 - stream_error = -1,
13 - incore = 0,
14 - loose = 1,
15 - pack_non_delta = 2
16 -};
17 -
11 typedef int (*open_istream_fn)(struct git_istream *,
12 struct repository *,
13 struct object_info *,
@@ -424,16 +417,10 @@ static open_method_decl(incore)
417 * static helpers variables and functions for users of streaming interface
418 *****************************************************************************/
419
427 -static open_istream_fn open_istream_tbl[] = {
428 - open_istream_incore,
429 - open_istream_loose,
430 - open_istream_pack_non_delta,
431 -};
432 -
433 -static enum input_source istream_source(struct repository *r,
434 - const struct object_id *oid,
435 - enum object_type *type,
436 - struct object_info *oi)
420 +static open_istream_fn istream_source(struct repository *r,
421 + const struct object_id *oid,
422 + enum object_type *type,
423 + struct object_info *oi)
424 {
425 unsigned long size;
426 int status;
@@ -442,21 +429,20 @@ static enum input_source istream_source(struct repository *r,
429 oi->sizep = &size;
430 status = oid_object_info_extended(r, oid, oi, 0);
431 if (status < 0)
445 - return stream_error;
432 + return NULL;
433
434 switch (oi->whence) {
435 case OI_LOOSE:
449 - return loose;
436 + return open_istream_loose;
437 case OI_PACKED:
438 if (!oi->u.packed.is_delta && big_file_threshold < size)
452 - return pack_non_delta;
439 + return open_istream_pack_non_delta;
440 /* fallthru */
441 default:
455 - return incore;
442 + return open_istream_incore;
443 }
444 }
445
459 -
446 /****************************************************************
447 * Users of streaming interface
448 ****************************************************************/
@@ -482,13 +468,13 @@ struct git_istream *open_istream(struct repository *r,
468 struct git_istream *st;
469 struct object_info oi = OBJECT_INFO_INIT;
470 const struct object_id *real = lookup_replace_object(r, oid);
485 - enum input_source src = istream_source(r, real, type, &oi);
471 + open_istream_fn open_fn = istream_source(r, real, type, &oi);
472
487 - if (src < 0)
473 + if (!open_fn)
474 return NULL;
475
476 st = xmalloc(sizeof(*st));
491 - if (open_istream_tbl[src](st, r, &oi, real, type)) {
477 + if (open_fn(st, r, &oi, real, type)) {
478 if (open_istream_incore(st, r, &oi, real, type)) {
479 free(st);
480 return NULL;