reftable: make reftable_record a tagged union
This reduces the amount of glue code, because we don't need a void pointer or vtable within the structure. The only snag is that reftable_index_record contain a strbuf, so it cannot be zero-initialized. To address this, use reftable_new_record() to return fresh instance, given a record type. Since reftable_new_record() doesn't cause heap allocation anymore, it should be balanced with reftable_record_release() rather than reftable_record_destroy(). Thanks to Peff for the suggestion. Helped-by: Jeff King <peff@peff.net> Signed-off-by: Han-Wen Nienhuys <hanwen@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
9391b88dab
commit
66c0dabab5
@ -7,6 +7,7 @@ https://developers.google.com/open-source/licenses/bsd
|
||||
*/
|
||||
|
||||
#include "basics.h"
|
||||
#include "constants.h"
|
||||
#include "record.h"
|
||||
#include "generic.h"
|
||||
#include "reftable-iterator.h"
|
||||
@ -15,23 +16,21 @@ https://developers.google.com/open-source/licenses/bsd
|
||||
int reftable_table_seek_ref(struct reftable_table *tab,
|
||||
struct reftable_iterator *it, const char *name)
|
||||
{
|
||||
struct reftable_ref_record ref = {
|
||||
.refname = (char *)name,
|
||||
};
|
||||
struct reftable_record rec = { NULL };
|
||||
reftable_record_from_ref(&rec, &ref);
|
||||
struct reftable_record rec = { .type = BLOCK_TYPE_REF,
|
||||
.u.ref = {
|
||||
.refname = (char *)name,
|
||||
} };
|
||||
return tab->ops->seek_record(tab->table_arg, it, &rec);
|
||||
}
|
||||
|
||||
int reftable_table_seek_log(struct reftable_table *tab,
|
||||
struct reftable_iterator *it, const char *name)
|
||||
{
|
||||
struct reftable_log_record log = {
|
||||
.refname = (char *)name,
|
||||
.update_index = ~((uint64_t)0),
|
||||
};
|
||||
struct reftable_record rec = { NULL };
|
||||
reftable_record_from_log(&rec, &log);
|
||||
struct reftable_record rec = { .type = BLOCK_TYPE_LOG,
|
||||
.u.log = {
|
||||
.refname = (char *)name,
|
||||
.update_index = ~((uint64_t)0),
|
||||
} };
|
||||
return tab->ops->seek_record(tab->table_arg, it, &rec);
|
||||
}
|
||||
|
||||
@ -129,17 +128,25 @@ void reftable_iterator_destroy(struct reftable_iterator *it)
|
||||
int reftable_iterator_next_ref(struct reftable_iterator *it,
|
||||
struct reftable_ref_record *ref)
|
||||
{
|
||||
struct reftable_record rec = { NULL };
|
||||
reftable_record_from_ref(&rec, ref);
|
||||
return iterator_next(it, &rec);
|
||||
struct reftable_record rec = {
|
||||
.type = BLOCK_TYPE_REF,
|
||||
.u.ref = *ref,
|
||||
};
|
||||
int err = iterator_next(it, &rec);
|
||||
*ref = rec.u.ref;
|
||||
return err;
|
||||
}
|
||||
|
||||
int reftable_iterator_next_log(struct reftable_iterator *it,
|
||||
struct reftable_log_record *log)
|
||||
{
|
||||
struct reftable_record rec = { NULL };
|
||||
reftable_record_from_log(&rec, log);
|
||||
return iterator_next(it, &rec);
|
||||
struct reftable_record rec = {
|
||||
.type = BLOCK_TYPE_LOG,
|
||||
.u.log = *log,
|
||||
};
|
||||
int err = iterator_next(it, &rec);
|
||||
*log = rec.u.log;
|
||||
return err;
|
||||
}
|
||||
|
||||
int iterator_next(struct reftable_iterator *it, struct reftable_record *rec)
|
||||
|
Reference in New Issue
Block a user