util: Add macro SPDK_SIZEOF_MEMBER to get size of a member of a struct

We find a few files to get the size of a member of a struct. How to
do it is a little complex. So add a macro to do it will be helpful
to read the current code and develop new features.

lib/dif had used member_size() internally but Linux use sizeof_member()
as the macro. Besides, SPDK have used upper case letters for similar
macros, SPDK_CONTAINEROF() and SPDK_COUNTOF(). Hence spdk_member_size()
may be good but propose SPDK_SIZEOF_MEMBER() as the macro.

Signed-off-by: Shuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
Change-Id: I2179c845a3b75fb71aa039075cc4dfd30617b898
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/8738
Reviewed-by: Konrad Sztyber <konrad.sztyber@intel.com>
Reviewed-by: Changpeng Liu <changpeng.liu@intel.com>
Reviewed-by: Tomasz Zawadzki <tomasz.zawadzki@intel.com>
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Community-CI: Broadcom CI <spdk-ci.pdl@broadcom.com>
This commit is contained in:
Shuhei Matsumoto 2021-07-12 09:09:19 +09:00 committed by Tomasz Zawadzki
parent e8cd34ae9f
commit 320ab72fb5
8 changed files with 19 additions and 14 deletions

View File

@ -124,6 +124,8 @@ and for io_devices to provide faster lookup.
Red-black tree macros has been added by using the macros provided by the FreeBSD operating system
under the same BSD license.
Add an new macro `SPDK_SIZEOF_MEMBER` to get the size of a member of a struct.
## v21.04:
### accel

View File

@ -53,6 +53,11 @@ extern "C" {
#define SPDK_CONTAINEROF(ptr, type, member) ((type *)((uintptr_t)ptr - offsetof(type, member)))
/**
* Get the size of a member of a struct.
*/
#define SPDK_SIZEOF_MEMBER(type, member) (sizeof(((type *)0)->member))
#define SPDK_SEC_TO_USEC 1000000ULL
#define SPDK_SEC_TO_NSEC 1000000000ULL

View File

@ -392,7 +392,7 @@ struct spdk_blob_md_page {
#define SPDK_BS_PAGE_SIZE 0x1000
SPDK_STATIC_ASSERT(SPDK_BS_PAGE_SIZE == sizeof(struct spdk_blob_md_page), "Invalid md page size");
#define SPDK_BS_MAX_DESC_SIZE sizeof(((struct spdk_blob_md_page*)0)->descriptors)
#define SPDK_BS_MAX_DESC_SIZE SPDK_SIZEOF_MEMBER(struct spdk_blob_md_page, descriptors)
/* Maximum number of extents a single Extent Page can fit.
* For an SPDK_BS_PAGE_SIZE of 4K SPDK_EXTENTS_PER_EP would be 512. */

View File

@ -72,7 +72,7 @@ struct load_json_config_ctx;
typedef void (*client_resp_handler)(struct load_json_config_ctx *,
struct spdk_jsonrpc_client_response *);
#define RPC_SOCKET_PATH_MAX sizeof(((struct sockaddr_un *)0)->sun_path)
#define RPC_SOCKET_PATH_MAX SPDK_SIZEOF_MEMBER(struct sockaddr_un, sun_path)
/* 1s connections timeout */
#define RPC_CLIENT_CONNECT_TIMEOUT_US (1U * 1000U * 1000U)

View File

@ -413,9 +413,9 @@ spdk_nvme_ns_get_nguid(const struct spdk_nvme_ns *ns)
size_t size;
nguid = nvme_ns_find_id_desc(ns, SPDK_NVME_NIDT_NGUID, &size);
if (nguid && size != sizeof(((struct spdk_nvme_ns_data *)0)->nguid)) {
if (nguid && size != SPDK_SIZEOF_MEMBER(struct spdk_nvme_ns_data, nguid)) {
SPDK_WARNLOG("Invalid NIDT_NGUID descriptor length reported: %zu (expected: %zu)\n",
size, sizeof(((struct spdk_nvme_ns_data *)0)->nguid));
size, SPDK_SIZEOF_MEMBER(struct spdk_nvme_ns_data, nguid));
return NULL;
}

View File

@ -63,7 +63,7 @@ SPDK_STATIC_ASSERT(sizeof(struct spdk_reduce_vol_superblock) == 4096, "size inco
#define SPDK_REDUCE_SIGNATURE "SPDKREDU"
/* null terminator counts one */
SPDK_STATIC_ASSERT(sizeof(SPDK_REDUCE_SIGNATURE) - 1 ==
sizeof(((struct spdk_reduce_vol_superblock *)0)->signature), "size incorrect");
SPDK_SIZEOF_MEMBER(struct spdk_reduce_vol_superblock, signature), "size incorrect");
#define REDUCE_PATH_MAX 4096

View File

@ -52,7 +52,7 @@
#define DEFAULT_MAX_UNMAP_BLOCK_DESCRIPTOR_COUNT 256
#define INQUIRY_OFFSET(field) offsetof(struct spdk_scsi_cdb_inquiry_data, field) + \
sizeof(((struct spdk_scsi_cdb_inquiry_data *)0x0)->field)
SPDK_SIZEOF_MEMBER(struct spdk_scsi_cdb_inquiry_data, field)
static void bdev_scsi_process_block_resubmit(void *arg);

View File

@ -1115,8 +1115,6 @@ dif_inject_error(struct _dif_sgl *sgl, uint32_t block_size, uint32_t num_blocks,
return -1;
}
#define _member_size(type, member) sizeof(((type *)0)->member)
int
spdk_dif_inject_error(struct iovec *iovs, int iovcnt, uint32_t num_blocks,
const struct spdk_dif_ctx *ctx, uint32_t inject_flags,
@ -1135,7 +1133,7 @@ spdk_dif_inject_error(struct iovec *iovs, int iovcnt, uint32_t num_blocks,
if (inject_flags & SPDK_DIF_REFTAG_ERROR) {
rc = dif_inject_error(&sgl, ctx->block_size, num_blocks,
ctx->guard_interval + offsetof(struct spdk_dif, ref_tag),
_member_size(struct spdk_dif, ref_tag),
SPDK_SIZEOF_MEMBER(struct spdk_dif, ref_tag),
inject_offset);
if (rc != 0) {
SPDK_ERRLOG("Failed to inject error to Reference Tag.\n");
@ -1146,7 +1144,7 @@ spdk_dif_inject_error(struct iovec *iovs, int iovcnt, uint32_t num_blocks,
if (inject_flags & SPDK_DIF_APPTAG_ERROR) {
rc = dif_inject_error(&sgl, ctx->block_size, num_blocks,
ctx->guard_interval + offsetof(struct spdk_dif, app_tag),
_member_size(struct spdk_dif, app_tag),
SPDK_SIZEOF_MEMBER(struct spdk_dif, app_tag),
inject_offset);
if (rc != 0) {
SPDK_ERRLOG("Failed to inject error to Application Tag.\n");
@ -1156,7 +1154,7 @@ spdk_dif_inject_error(struct iovec *iovs, int iovcnt, uint32_t num_blocks,
if (inject_flags & SPDK_DIF_GUARD_ERROR) {
rc = dif_inject_error(&sgl, ctx->block_size, num_blocks,
ctx->guard_interval,
_member_size(struct spdk_dif, guard),
SPDK_SIZEOF_MEMBER(struct spdk_dif, guard),
inject_offset);
if (rc != 0) {
SPDK_ERRLOG("Failed to inject error to Guard.\n");
@ -1422,7 +1420,7 @@ spdk_dix_inject_error(struct iovec *iovs, int iovcnt, struct iovec *md_iov,
if (inject_flags & SPDK_DIF_REFTAG_ERROR) {
rc = dif_inject_error(&md_sgl, ctx->md_size, num_blocks,
ctx->guard_interval + offsetof(struct spdk_dif, ref_tag),
_member_size(struct spdk_dif, ref_tag),
SPDK_SIZEOF_MEMBER(struct spdk_dif, ref_tag),
inject_offset);
if (rc != 0) {
SPDK_ERRLOG("Failed to inject error to Reference Tag.\n");
@ -1433,7 +1431,7 @@ spdk_dix_inject_error(struct iovec *iovs, int iovcnt, struct iovec *md_iov,
if (inject_flags & SPDK_DIF_APPTAG_ERROR) {
rc = dif_inject_error(&md_sgl, ctx->md_size, num_blocks,
ctx->guard_interval + offsetof(struct spdk_dif, app_tag),
_member_size(struct spdk_dif, app_tag),
SPDK_SIZEOF_MEMBER(struct spdk_dif, app_tag),
inject_offset);
if (rc != 0) {
SPDK_ERRLOG("Failed to inject error to Application Tag.\n");
@ -1444,7 +1442,7 @@ spdk_dix_inject_error(struct iovec *iovs, int iovcnt, struct iovec *md_iov,
if (inject_flags & SPDK_DIF_GUARD_ERROR) {
rc = dif_inject_error(&md_sgl, ctx->md_size, num_blocks,
ctx->guard_interval,
_member_size(struct spdk_dif, guard),
SPDK_SIZEOF_MEMBER(struct spdk_dif, guard),
inject_offset);
if (rc != 0) {
SPDK_ERRLOG("Failed to inject error to Guard.\n");