Skip to content

Commit 9db3644

Browse files
committed
Merge branch 'jt/object-file-use-container-of'
Code clean-up. * jt/object-file-use-container-of: object-file.c: avoid container_of() of a NULL container object-file: use `container_of()` to convert from base types
2 parents 13763ec + 8e06f96 commit 9db3644

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
@@ -734,7 +734,8 @@ struct odb_transaction_files {
734734

735735
static void prepare_loose_object_transaction(struct odb_transaction *base)
736736
{
737-
struct odb_transaction_files *transaction = (struct odb_transaction_files *)base;
737+
struct odb_transaction_files *transaction =
738+
container_of_or_null(base, struct odb_transaction_files, base);
738739

739740
/*
740741
* We lazily create the temporary object directory
@@ -753,7 +754,8 @@ static void prepare_loose_object_transaction(struct odb_transaction *base)
753754
static void fsync_loose_object_transaction(struct odb_transaction *base,
754755
int fd, const char *filename)
755756
{
756-
struct odb_transaction_files *transaction = (struct odb_transaction_files *)base;
757+
struct odb_transaction_files *transaction =
758+
container_of_or_null(base, struct odb_transaction_files, base);
757759

758760
/*
759761
* If we have an active ODB transaction, we issue a call that
@@ -1649,11 +1651,14 @@ int index_fd(struct index_state *istate, struct object_id *oid,
16491651
type, path, flags);
16501652
} else {
16511653
struct object_database *odb = the_repository->objects;
1654+
struct odb_transaction_files *files_transaction;
16521655
struct odb_transaction *transaction;
16531656

16541657
transaction = odb_transaction_begin(odb);
1655-
ret = index_blob_packfile_transaction((struct odb_transaction_files *)odb->transaction,
1656-
oid, fd,
1658+
files_transaction = container_of(odb->transaction,
1659+
struct odb_transaction_files,
1660+
base);
1661+
ret = index_blob_packfile_transaction(files_transaction, oid, fd,
16571662
xsize_t(st->st_size),
16581663
path, flags);
16591664
odb_transaction_commit(transaction);
@@ -2035,7 +2040,8 @@ int read_loose_object(struct repository *repo,
20352040

20362041
static void odb_transaction_files_commit(struct odb_transaction *base)
20372042
{
2038-
struct odb_transaction_files *transaction = (struct odb_transaction_files *)base;
2043+
struct odb_transaction_files *transaction =
2044+
container_of(base, struct odb_transaction_files, base);
20392045

20402046
flush_loose_object_transaction(transaction);
20412047
flush_packfile_transaction(transaction);
@@ -2090,7 +2096,8 @@ struct odb_loose_read_stream {
20902096

20912097
static ssize_t read_istream_loose(struct odb_read_stream *_st, char *buf, size_t sz)
20922098
{
2093-
struct odb_loose_read_stream *st = (struct odb_loose_read_stream *)_st;
2099+
struct odb_loose_read_stream *st =
2100+
container_of(_st, struct odb_loose_read_stream, base);
20942101
size_t total_read = 0;
20952102

20962103
switch (st->z_state) {
@@ -2136,7 +2143,9 @@ static ssize_t read_istream_loose(struct odb_read_stream *_st, char *buf, size_t
21362143

21372144
static int close_istream_loose(struct odb_read_stream *_st)
21382145
{
2139-
struct odb_loose_read_stream *st = (struct odb_loose_read_stream *)_st;
2146+
struct odb_loose_read_stream *st =
2147+
container_of(_st, struct odb_loose_read_stream, base);
2148+
21402149
if (st->z_state == ODB_LOOSE_READ_STREAM_INUSE)
21412150
git_inflate_end(&st->z);
21422151
munmap(st->mapped, st->mapsize);

0 commit comments

Comments
 (0)