Skip to content

Commit 20daad2

Browse files
jltoblergitster
authored andcommitted
object-file: use container_of() to convert from base types
To improve code hygiene, replace direct casts from `struct odb_transaction` and `struct odb_read_stream` to their concrete implementations with `container_of()`. Signed-off-by: Justin Tobler <jltobler@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 73fd778 commit 20daad2

1 file changed

Lines changed: 16 additions & 7 deletions

File tree

object-file.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,8 @@ struct odb_transaction_files {
719719

720720
static void prepare_loose_object_transaction(struct odb_transaction *base)
721721
{
722-
struct odb_transaction_files *transaction = (struct odb_transaction_files *)base;
722+
struct odb_transaction_files *transaction =
723+
container_of(base, struct odb_transaction_files, base);
723724

724725
/*
725726
* We lazily create the temporary object directory
@@ -738,7 +739,8 @@ static void prepare_loose_object_transaction(struct odb_transaction *base)
738739
static void fsync_loose_object_transaction(struct odb_transaction *base,
739740
int fd, const char *filename)
740741
{
741-
struct odb_transaction_files *transaction = (struct odb_transaction_files *)base;
742+
struct odb_transaction_files *transaction =
743+
container_of(base, struct odb_transaction_files, base);
742744

743745
/*
744746
* If we have an active ODB transaction, we issue a call that
@@ -1634,11 +1636,14 @@ int index_fd(struct index_state *istate, struct object_id *oid,
16341636
type, path, flags);
16351637
} else {
16361638
struct object_database *odb = the_repository->objects;
1639+
struct odb_transaction_files *files_transaction;
16371640
struct odb_transaction *transaction;
16381641

16391642
transaction = odb_transaction_begin(odb);
1640-
ret = index_blob_packfile_transaction((struct odb_transaction_files *)odb->transaction,
1641-
oid, fd,
1643+
files_transaction = container_of(odb->transaction,
1644+
struct odb_transaction_files,
1645+
base);
1646+
ret = index_blob_packfile_transaction(files_transaction, oid, fd,
16421647
xsize_t(st->st_size),
16431648
path, flags);
16441649
odb_transaction_commit(transaction);
@@ -1992,7 +1997,8 @@ int read_loose_object(struct repository *repo,
19921997

19931998
static void odb_transaction_files_commit(struct odb_transaction *base)
19941999
{
1995-
struct odb_transaction_files *transaction = (struct odb_transaction_files *)base;
2000+
struct odb_transaction_files *transaction =
2001+
container_of(base, struct odb_transaction_files, base);
19962002

19972003
flush_loose_object_transaction(transaction);
19982004
flush_packfile_transaction(transaction);
@@ -2047,7 +2053,8 @@ struct odb_loose_read_stream {
20472053

20482054
static ssize_t read_istream_loose(struct odb_read_stream *_st, char *buf, size_t sz)
20492055
{
2050-
struct odb_loose_read_stream *st = (struct odb_loose_read_stream *)_st;
2056+
struct odb_loose_read_stream *st =
2057+
container_of(_st, struct odb_loose_read_stream, base);
20512058
size_t total_read = 0;
20522059

20532060
switch (st->z_state) {
@@ -2093,7 +2100,9 @@ static ssize_t read_istream_loose(struct odb_read_stream *_st, char *buf, size_t
20932100

20942101
static int close_istream_loose(struct odb_read_stream *_st)
20952102
{
2096-
struct odb_loose_read_stream *st = (struct odb_loose_read_stream *)_st;
2103+
struct odb_loose_read_stream *st =
2104+
container_of(_st, struct odb_loose_read_stream, base);
2105+
20972106
if (st->z_state == ODB_LOOSE_READ_STREAM_INUSE)
20982107
git_inflate_end(&st->z);
20992108
munmap(st->mapped, st->mapsize);

0 commit comments

Comments
 (0)