+2
−2
+3
−0
+2
−1
Loading
We're often using structure packing throughout the code for the *_opts
structs to make sure their size will always change whenever a new field
is added. However, packing a structure has a couple of drawbacks:
__attribute__((packed)) is a gcc extension and isn't supported on some
compilers (e.g. msvc), and taking the address of a member field might
lead to Waddress-of-packed-member warnings.
Instead, pad the structure manually and use a new macro, SPDK_SIZEOF(),
to calculate the size of the structure. The macro takes two parameters:
pointer to a structure and the name of the last member in that
structure. It returns the size of the structure up to and including
that member. That way, even though a structure isn't packed, the size
calculated using SPDK_SIZEOF() won't include the padding at the end.
This method has one additional benefit. When a structure in version X
looks like this:
struct spdk_foo {
size_t size;
char bar;
};
and is used as:
struct spdk_foo foo = {};
foo.size = /* sizeof(foo) or SPDK_SIZEOF(&foo, bar) */;
foo.bar = 'b';
changes in version X+1 to:
struct spdk_foo {
size_t size;
char bar;
char baz;
};
and the code using it is recompiled without any changes. If it uses
sizeof() foo.baz will be implicitly zero-initialized, while if it uses
SPDK_SIZEOF(), the library that foo is passed to will see that foo.baz
is unassigned and will be able to pick any value as the default.
Signed-off-by:
Konrad Sztyber <konrad.sztyber@intel.com>
Change-Id: I049baf4db0cd08d7cc728e31eda2ee6bad47b264
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/18530
Community-CI: Mellanox Build Bot
Reviewed-by:
Ben Walker <benjamin.walker@intel.com>
Tested-by:
SPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by:
Tomasz Zawadzki <tomasz.zawadzki@intel.com>