Commit 1afe03c4 authored by Konrad Sztyber's avatar Konrad Sztyber Committed by Tomasz Zawadzki
Browse files

util: add SPDK_GET_FIELD()



The sizes of many public structures are tracked in order to ensure ABI
compatibility.  This requires checking the size of a structure before
accessing any of its fields.

This patch introduces a macro that makes this a bit more convenient and
uses it to get fields from spdk_bdev_ext_io_opts.

Signed-off-by: default avatarKonrad Sztyber <konrad.sztyber@intel.com>
Change-Id: Id0e92d2df02cfa24fda148353ae049d7fc7183ea
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/22591


Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: default avatarBen Walker <ben@nvidia.com>
Reviewed-by: default avatarJim Harris <jim.harris@samsung.com>
Community-CI: Mellanox Build Bot
parent c3a650d5
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -61,6 +61,18 @@ extern "C" {

#define SPDK_BIT(n) (1ul << (n))

/**
 * Get a field from a structure with size tracking.  The fourth parameter is
 * optional and can be used to specify the size of the object.  If unset,
 * (obj)->size will be used by default.
 */
#define SPDK_GET_FIELD(obj, field, defval, ...) \
	_SPDK_GET_FIELD(obj, field, defval, ## __VA_ARGS__, (obj)->size)

#define _SPDK_GET_FIELD(obj, field, defval, size, ...) \
	((size) >= (offsetof(__typeof__(*(obj)), field) + sizeof((obj)->field)) ? \
	 (obj)->field : (defval))

uint32_t spdk_u32log2(uint32_t x);

static inline uint32_t
+1 −2
Original line number Diff line number Diff line
@@ -436,8 +436,7 @@ static void claim_reset(struct spdk_bdev *bdev);
static void bdev_ch_retry_io(struct spdk_bdev_channel *bdev_ch);

#define bdev_get_ext_io_opt(opts, field, defval) \
	(((opts) != NULL && offsetof(struct spdk_bdev_ext_io_opts, field) + \
	 sizeof((opts)->field) <= (opts)->size) ? (opts)->field : (defval))
	((opts) != NULL ? SPDK_GET_FIELD(opts, field, defval) : (defval))

void
spdk_bdev_get_opts(struct spdk_bdev_opts *opts, size_t opts_size)