mirror of
https://github.com/git/git.git
synced 2026-01-10 01:56:42 +00:00
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
@@ -63,16 +63,10 @@ struct reftable_record_vtable {
|
||||
int (*equal)(const void *a, const void *b, int hash_size);
|
||||
};
|
||||
|
||||
/* record is a generic wrapper for different types of records. */
|
||||
struct reftable_record {
|
||||
void *data;
|
||||
struct reftable_record_vtable *ops;
|
||||
};
|
||||
|
||||
/* returns true for recognized block types. Block start with the block type. */
|
||||
int reftable_is_block_type(uint8_t typ);
|
||||
|
||||
/* creates a malloced record of the given type. Dispose with record_destroy */
|
||||
/* return an initialized record for the given type */
|
||||
struct reftable_record reftable_new_record(uint8_t typ);
|
||||
|
||||
/* Encode `key` into `dest`. Sets `is_restart` to indicate a restart. Returns
|
||||
@@ -100,6 +94,22 @@ struct reftable_obj_record {
|
||||
int offset_len;
|
||||
};
|
||||
|
||||
/* record is a generic wrapper for different types of records. It is normally
|
||||
* created on the stack, or embedded within another struct. If the type is
|
||||
* known, a fresh instance can be initialized explicitly. Otherwise, use
|
||||
* reftable_new_record() to initialize generically (as the index_record is not
|
||||
* valid as 0-initialized structure)
|
||||
*/
|
||||
struct reftable_record {
|
||||
uint8_t type;
|
||||
union {
|
||||
struct reftable_ref_record ref;
|
||||
struct reftable_log_record log;
|
||||
struct reftable_obj_record obj;
|
||||
struct reftable_index_record idx;
|
||||
} u;
|
||||
};
|
||||
|
||||
/* see struct record_vtable */
|
||||
int reftable_record_equal(struct reftable_record *a, struct reftable_record *b, int hash_size);
|
||||
void reftable_record_key(struct reftable_record *rec, struct strbuf *dest);
|
||||
@@ -114,25 +124,9 @@ int reftable_record_decode(struct reftable_record *rec, struct strbuf key,
|
||||
int hash_size);
|
||||
int reftable_record_is_deletion(struct reftable_record *rec);
|
||||
|
||||
/* zeroes out the embedded record */
|
||||
/* frees and zeroes out the embedded record */
|
||||
void reftable_record_release(struct reftable_record *rec);
|
||||
|
||||
/* clear and deallocate embedded record, and zero `rec`. */
|
||||
void reftable_record_destroy(struct reftable_record *rec);
|
||||
|
||||
/* initialize generic records from concrete records. The generic record should
|
||||
* be zeroed out. */
|
||||
void reftable_record_from_obj(struct reftable_record *rec,
|
||||
struct reftable_obj_record *objrec);
|
||||
void reftable_record_from_index(struct reftable_record *rec,
|
||||
struct reftable_index_record *idxrec);
|
||||
void reftable_record_from_ref(struct reftable_record *rec,
|
||||
struct reftable_ref_record *refrec);
|
||||
void reftable_record_from_log(struct reftable_record *rec,
|
||||
struct reftable_log_record *logrec);
|
||||
struct reftable_ref_record *reftable_record_as_ref(struct reftable_record *ref);
|
||||
struct reftable_log_record *reftable_record_as_log(struct reftable_record *ref);
|
||||
|
||||
/* for qsort. */
|
||||
int reftable_ref_record_compare_name(const void *a, const void *b);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user