util: add SPDK_COUNTOF() array size macro

SPDK_COUNTOF works like sizeof, except it returns the number of elements
in an array instead of the number of bytes.

Change-Id: I38ff4dd3485ed9b630cc5660ff84851d0031911f
Signed-off-by: Daniel Verkamp <daniel.verkamp@intel.com>
This commit is contained in:
Daniel Verkamp 2017-03-03 13:44:04 -07:00 committed by Jim Harris
parent ba5081db23
commit b58a5d73ef
22 changed files with 60 additions and 35 deletions

View File

@ -40,6 +40,7 @@
#include "spdk/env.h"
#include "spdk/nvme.h"
#include "spdk/nvmf.h"
#include "spdk/util.h"
#include "nvmf_tgt.h"
@ -176,7 +177,7 @@ decode_rpc_listen_address(const struct spdk_json_val *val, void *out)
{
struct rpc_listen_address *req = (struct rpc_listen_address *)out;
if (spdk_json_decode_object(val, rpc_listen_address_decoders,
sizeof(rpc_listen_address_decoders) / sizeof(*rpc_listen_address_decoders),
SPDK_COUNTOF(rpc_listen_address_decoders),
req)) {
SPDK_ERRLOG("spdk_json_decode_object failed\n");
return -1;
@ -297,7 +298,7 @@ spdk_rpc_construct_nvmf_subsystem(struct spdk_jsonrpc_server_conn *conn,
req.core = -1; /* Explicitly set the core as the uninitialized value */
if (spdk_json_decode_object(params, rpc_subsystem_decoders,
sizeof(rpc_subsystem_decoders) / sizeof(*rpc_subsystem_decoders),
SPDK_COUNTOF(rpc_subsystem_decoders),
&req)) {
SPDK_ERRLOG("spdk_json_decode_object failed\n");
goto invalid;
@ -349,7 +350,7 @@ spdk_rpc_delete_nvmf_subsystem(struct spdk_jsonrpc_server_conn *conn,
struct spdk_json_write_ctx *w;
if (spdk_json_decode_object(params, rpc_delete_subsystem_decoders,
sizeof(rpc_delete_subsystem_decoders) / sizeof(*rpc_delete_subsystem_decoders),
SPDK_COUNTOF(rpc_delete_subsystem_decoders),
&req)) {
SPDK_ERRLOG("spdk_json_decode_object failed\n");
goto invalid;

View File

@ -47,6 +47,7 @@
#include "spdk/nvme_intel.h"
#include "spdk/nvmf_spec.h"
#include "spdk/pci_ids.h"
#include "spdk/util.h"
static int outstanding_commands;
@ -166,7 +167,7 @@ get_features(struct spdk_nvme_ctrlr *ctrlr)
/* Submit several GET FEATURES commands and wait for them to complete */
outstanding_commands = 0;
for (i = 0; i < sizeof(features_to_get) / sizeof(*features_to_get); i++) {
for (i = 0; i < SPDK_COUNTOF(features_to_get); i++) {
if (get_feature(ctrlr, features_to_get[i]) == 0) {
outstanding_commands++;
} else {
@ -714,7 +715,7 @@ print_controller(struct spdk_nvme_ctrlr *ctrlr, const struct spdk_nvme_transport
printf("Intel Health Information\n");
printf("==================\n");
for (i = 0;
i < sizeof(intel_smart_page.attributes) / sizeof(intel_smart_page.attributes[0]); i++) {
i < SPDK_COUNTOF(intel_smart_page.attributes); i++) {
if (intel_smart_page.attributes[i].code == SPDK_NVME_INTEL_SMART_PROGRAM_FAIL_COUNT) {
printf("Program Fail Count:\n");
printf(" Normalized Value : %d\n",

View File

@ -47,6 +47,7 @@
#include "spdk/nvme.h"
#include "spdk/env.h"
#include "spdk/util.h"
#define MAX_DEVS 64
@ -386,7 +387,7 @@ get_allocated_nsid(struct dev *dev)
}
printf("Allocated Namespace IDs:\n");
for (i = 0; i < sizeof(ns_list->ns_list) / sizeof(*ns_list->ns_list); i++) {
for (i = 0; i < SPDK_COUNTOF(ns_list->ns_list); i++) {
if (ns_list->ns_list[i] == 0) {
break;
}

View File

@ -48,6 +48,8 @@ extern "C" {
#define spdk_min(a,b) (((a)<(b))?(a):(b))
#define spdk_max(a,b) (((a)>(b))?(a):(b))
#define SPDK_COUNTOF(arr) (sizeof(arr) / sizeof((arr)[0]))
static inline uint32_t
spdk_u32log2(uint32_t x)
{

View File

@ -33,6 +33,7 @@
#include "blockdev_aio.h"
#include "spdk/rpc.h"
#include "spdk/util.h"
#include "spdk_internal/log.h"
@ -60,7 +61,7 @@ spdk_rpc_construct_aio_bdev(struct spdk_jsonrpc_server_conn *conn,
struct spdk_bdev *bdev;
if (spdk_json_decode_object(params, rpc_construct_aio_decoders,
sizeof(rpc_construct_aio_decoders) / sizeof(*rpc_construct_aio_decoders),
SPDK_COUNTOF(rpc_construct_aio_decoders),
&req)) {
SPDK_ERRLOG("spdk_json_decode_object failed\n");
goto invalid;

View File

@ -33,6 +33,7 @@
#include "blockdev_malloc.h"
#include "spdk/rpc.h"
#include "spdk/util.h"
#include "spdk_internal/log.h"
@ -56,7 +57,7 @@ spdk_rpc_construct_malloc_bdev(struct spdk_jsonrpc_server_conn *conn,
struct spdk_bdev *bdev;
if (spdk_json_decode_object(params, rpc_construct_malloc_decoders,
sizeof(rpc_construct_malloc_decoders) / sizeof(*rpc_construct_malloc_decoders),
SPDK_COUNTOF(rpc_construct_malloc_decoders),
&req)) {
SPDK_TRACELOG(SPDK_TRACE_DEBUG, "spdk_json_decode_object failed\n");
goto invalid;

View File

@ -35,6 +35,7 @@
#include "blockdev_nvme.h"
#include "spdk/rpc.h"
#include "spdk/util.h"
#include "spdk_internal/log.h"
@ -67,7 +68,7 @@ spdk_rpc_construct_nvme_bdev(struct spdk_jsonrpc_server_conn *conn,
size_t i;
if (spdk_json_decode_object(params, rpc_construct_nvme_decoders,
sizeof(rpc_construct_nvme_decoders) / sizeof(*rpc_construct_nvme_decoders),
SPDK_COUNTOF(rpc_construct_nvme_decoders),
&req)) {
SPDK_TRACELOG(SPDK_TRACE_DEBUG, "spdk_json_decode_object failed\n");
goto invalid;

View File

@ -33,6 +33,7 @@
#include "blockdev_rbd.h"
#include "spdk/rpc.h"
#include "spdk/util.h"
#include "spdk_internal/log.h"
@ -65,7 +66,7 @@ spdk_rpc_construct_rbd_bdev(struct spdk_jsonrpc_server_conn *conn,
struct spdk_bdev *bdev;
if (spdk_json_decode_object(params, rpc_construct_rbd_decoders,
sizeof(rpc_construct_rbd_decoders) / sizeof(*rpc_construct_rbd_decoders),
SPDK_COUNTOF(rpc_construct_rbd_decoders),
&req)) {
SPDK_TRACELOG(SPDK_TRACE_DEBUG, "spdk_json_decode_object failed\n");
goto invalid;

View File

@ -45,6 +45,7 @@
#include "spdk/assert.h"
#include "spdk/likely.h"
#include "spdk/util.h"
/* x86-64 userspace virtual addresses use only the low 47 bits [0..46],
* which is enough to cover 128 TB.
@ -138,7 +139,7 @@ spdk_mem_map_get_map_1gb(struct spdk_mem_map *map, uint64_t vfn_2mb)
map_1gb = malloc(sizeof(struct map_1gb));
if (map_1gb) {
/* initialize all entries to default translation */
for (i = 0; i < sizeof(map_1gb->map) / sizeof(map_1gb->map[0]); i++) {
for (i = 0; i < SPDK_COUNTOF(map_1gb->map); i++) {
map_1gb->map[i].translation_2mb = map->default_translation;
}
memset(map_1gb->ref_count, 0, sizeof(map_1gb->ref_count));

View File

@ -39,6 +39,7 @@
#include <unistd.h>
#include "spdk/rpc.h"
#include "spdk/util.h"
#include "spdk_internal/log.h"
@ -77,13 +78,13 @@ spdk_rpc_kill_instance(struct spdk_jsonrpc_server_conn *conn,
struct spdk_json_write_ctx *w;
if (spdk_json_decode_object(params, rpc_kill_instance_decoders,
sizeof(rpc_kill_instance_decoders) / sizeof(*rpc_kill_instance_decoders),
SPDK_COUNTOF(rpc_kill_instance_decoders),
&req)) {
SPDK_TRACELOG(SPDK_TRACE_DEBUG, "spdk_json_decode_object failed\n");
goto invalid;
}
sig_count = sizeof(signals) / sizeof(*signals);
sig_count = SPDK_COUNTOF(signals);
signal = atoi(req.sig_name);
for (i = 0 ; i < sig_count; i++) {
if (strcmp(req.sig_name, signals[i].signal_string) == 0 ||

View File

@ -39,6 +39,7 @@
#include "iscsi/init_grp.h"
#include "spdk/rpc.h"
#include "spdk/util.h"
#include "spdk_internal/log.h"
@ -171,7 +172,7 @@ spdk_rpc_add_initiator_group(struct spdk_jsonrpc_server_conn *conn,
struct spdk_json_write_ctx *w;
if (spdk_json_decode_object(params, rpc_initiator_group_decoders,
sizeof(rpc_initiator_group_decoders) / sizeof(*rpc_initiator_group_decoders), &req)) {
SPDK_COUNTOF(rpc_initiator_group_decoders), &req)) {
SPDK_ERRLOG("spdk_json_decode_object failed\n");
goto invalid;
}
@ -259,7 +260,7 @@ spdk_rpc_delete_initiator_group(struct spdk_jsonrpc_server_conn *conn,
struct spdk_iscsi_init_grp *ig;
if (spdk_json_decode_object(params, rpc_delete_initiator_group_decoders,
sizeof(rpc_delete_initiator_group_decoders) / sizeof(*rpc_delete_initiator_group_decoders),
SPDK_COUNTOF(rpc_delete_initiator_group_decoders),
&req)) {
SPDK_ERRLOG("spdk_json_decode_object failed\n");
goto invalid;
@ -500,7 +501,7 @@ spdk_rpc_construct_target_node(struct spdk_jsonrpc_server_conn *conn,
struct spdk_iscsi_tgt_node *target;
if (spdk_json_decode_object(params, rpc_target_node_decoders,
sizeof(rpc_target_node_decoders) / sizeof(*rpc_target_node_decoders),
SPDK_COUNTOF(rpc_target_node_decoders),
&req)) {
SPDK_ERRLOG("spdk_json_decode_object failed\n");
goto invalid;
@ -580,7 +581,7 @@ spdk_rpc_delete_target_node(struct spdk_jsonrpc_server_conn *conn,
struct spdk_json_write_ctx *w;
if (spdk_json_decode_object(params, rpc_delete_target_node_decoders,
sizeof(rpc_delete_target_node_decoders) / sizeof(*rpc_delete_target_node_decoders),
SPDK_COUNTOF(rpc_delete_target_node_decoders),
&req)) {
SPDK_ERRLOG("spdk_json_decode_object failed\n");
goto invalid;
@ -714,7 +715,7 @@ decode_rpc_portal(const struct spdk_json_val *val, void *out)
struct rpc_portal *portal = out;
return spdk_json_decode_object(val, rpc_portal_decoders,
sizeof(rpc_portal_decoders) / sizeof(*rpc_portal_decoders),
SPDK_COUNTOF(rpc_portal_decoders),
portal);
}
@ -743,7 +744,7 @@ spdk_rpc_add_portal_group(struct spdk_jsonrpc_server_conn *conn,
size_t i;
if (spdk_json_decode_object(params, rpc_portal_group_decoders,
sizeof(rpc_portal_group_decoders) / sizeof(*rpc_portal_group_decoders),
SPDK_COUNTOF(rpc_portal_group_decoders),
&req)) {
SPDK_ERRLOG("spdk_json_decode_object failed\n");
goto invalid;
@ -805,7 +806,7 @@ spdk_rpc_delete_portal_group(struct spdk_jsonrpc_server_conn *conn,
struct spdk_iscsi_portal_grp *pg;
if (spdk_json_decode_object(params, rpc_delete_portal_group_decoders,
sizeof(rpc_delete_portal_group_decoders) / sizeof(*rpc_delete_portal_group_decoders),
SPDK_COUNTOF(rpc_delete_portal_group_decoders),
&req)) {
SPDK_ERRLOG("spdk_json_decode_object failed\n");
goto invalid;

View File

@ -33,6 +33,8 @@
#include "jsonrpc_internal.h"
#include "spdk/util.h"
struct jsonrpc_request {
const struct spdk_json_val *version;
const struct spdk_json_val *method;
@ -63,7 +65,7 @@ parse_single_request(struct spdk_jsonrpc_server_conn *conn, struct spdk_json_val
struct jsonrpc_request req = {};
if (spdk_json_decode_object(values, jsonrpc_request_decoders,
sizeof(jsonrpc_request_decoders) / sizeof(*jsonrpc_request_decoders),
SPDK_COUNTOF(jsonrpc_request_decoders),
&req)) {
invalid = true;
goto done;

View File

@ -32,6 +32,7 @@
*/
#include "spdk/rpc.h"
#include "spdk/util.h"
#include "spdk_internal/log.h"
@ -58,7 +59,7 @@ spdk_rpc_set_trace_flag(struct spdk_jsonrpc_server_conn *conn,
struct spdk_json_write_ctx *w;
if (spdk_json_decode_object(params, rpc_trace_flag_decoders,
sizeof(rpc_trace_flag_decoders) / sizeof(*rpc_trace_flag_decoders), &req)) {
SPDK_COUNTOF(rpc_trace_flag_decoders), &req)) {
SPDK_TRACELOG(SPDK_TRACE_DEBUG, "spdk_json_decode_object failed\n");
goto invalid;
}
@ -95,7 +96,7 @@ spdk_rpc_clear_trace_flag(struct spdk_jsonrpc_server_conn *conn,
struct spdk_json_write_ctx *w;
if (spdk_json_decode_object(params, rpc_trace_flag_decoders,
sizeof(rpc_trace_flag_decoders) / sizeof(*rpc_trace_flag_decoders), &req)) {
SPDK_COUNTOF(rpc_trace_flag_decoders), &req)) {
SPDK_TRACELOG(SPDK_TRACE_DEBUG, "spdk_json_decode_object failed\n");
goto invalid;
}

View File

@ -38,6 +38,7 @@
#include "spdk/rpc.h"
#include "spdk/net.h"
#include "spdk/util.h"
#include "spdk_internal/log.h"
@ -66,7 +67,7 @@ spdk_rpc_add_ip_address(struct spdk_jsonrpc_server_conn *conn,
struct spdk_json_write_ctx *w;
if (spdk_json_decode_object(params, rpc_ip_address_decoders,
sizeof(rpc_ip_address_decoders) / sizeof(*rpc_ip_address_decoders),
SPDK_COUNTOF(rpc_ip_address_decoders),
&req)) {
SPDK_TRACELOG(SPDK_TRACE_DEBUG, "spdk_json_decode_object failed\n");
goto invalid;
@ -102,7 +103,7 @@ spdk_rpc_delete_ip_address(struct spdk_jsonrpc_server_conn *conn,
struct spdk_json_write_ctx *w;
if (spdk_json_decode_object(params, rpc_ip_address_decoders,
sizeof(rpc_ip_address_decoders) / sizeof(*rpc_ip_address_decoders),
SPDK_COUNTOF(rpc_ip_address_decoders),
&req)) {
SPDK_TRACELOG(SPDK_TRACE_DEBUG, "spdk_json_decode_object failed\n");
goto invalid;

View File

@ -38,6 +38,7 @@
#include "spdk/nvme.h"
#include "spdk/nvmf_spec.h"
#include "spdk/trace.h"
#include "spdk/util.h"
#include "spdk_internal/log.h"
@ -96,7 +97,7 @@ nvmf_direct_ctrlr_admin_identify_nslist(struct spdk_nvme_ctrlr *ctrlr,
}
ns_list->ns_list[count++] = i;
if (count == sizeof(*ns_list) / sizeof(uint32_t)) {
if (count == SPDK_COUNTOF(ns_list->ns_list)) {
break;
}
}

View File

@ -43,6 +43,7 @@
#include "spdk/trace.h"
#include "spdk/nvme_spec.h"
#include "spdk/util.h"
#include "spdk_internal/log.h"
@ -527,7 +528,7 @@ find_prop(uint32_t ofst)
{
size_t i;
for (i = 0; i < sizeof(nvmf_props) / sizeof(*nvmf_props); i++) {
for (i = 0; i < SPDK_COUNTOF(nvmf_props); i++) {
const struct nvmf_prop *prop = &nvmf_props[i];
if (prop->ofst == ofst) {

View File

@ -39,6 +39,7 @@
#include "spdk/log.h"
#include "spdk/nvmf.h"
#include "spdk/queue.h"
#include "spdk/util.h"
#include "nvmf_internal.h"
@ -48,7 +49,7 @@ static const struct spdk_nvmf_transport *const g_transports[] = {
#endif
};
#define NUM_TRANSPORTS (sizeof(g_transports) / sizeof(*g_transports))
#define NUM_TRANSPORTS (SPDK_COUNTOF(g_transports))
int
spdk_nvmf_transport_init(void)

View File

@ -46,6 +46,7 @@
#include "spdk/trace.h"
#include "spdk/scsi_spec.h"
#include "spdk/string.h"
#include "spdk/util.h"
#include "spdk_internal/log.h"
@ -230,7 +231,7 @@ identify_active_ns_list(struct spdk_nvmf_subsystem *subsystem,
continue;
}
ns_list->ns_list[count++] = i;
if (count == sizeof(*ns_list) / sizeof(uint32_t)) {
if (count == SPDK_COUNTOF(ns_list->ns_list)) {
break;
}
}

View File

@ -35,6 +35,7 @@
#include "scsi_internal.h"
#include "spdk/rpc.h"
#include "spdk/util.h"
static void
spdk_rpc_get_luns(struct spdk_jsonrpc_server_conn *conn,
@ -100,7 +101,7 @@ spdk_rpc_delete_lun(struct spdk_jsonrpc_server_conn *conn,
struct spdk_json_write_ctx *w;
if (spdk_json_decode_object(params, rpc_delete_lun_decoders,
sizeof(rpc_delete_lun_decoders) / sizeof(*rpc_delete_lun_decoders),
SPDK_COUNTOF(rpc_delete_lun_decoders),
&req)) {
SPDK_TRACELOG(SPDK_TRACE_DEBUG, "spdk_json_decode_object failed\n");
goto invalid;

View File

@ -35,6 +35,7 @@
#include "spdk_internal/log.h"
#include "spdk/rpc.h"
#include "spdk/util.h"
#include "spdk/vhost.h"
#include "task.h"
@ -144,7 +145,7 @@ spdk_rpc_construct_vhost_scsi_controller(struct spdk_jsonrpc_server_conn *conn,
uint64_t cpumask;
if (spdk_json_decode_object(params, rpc_construct_vhost_ctrlr,
sizeof(rpc_construct_vhost_ctrlr) / sizeof(*rpc_construct_vhost_ctrlr),
SPDK_COUNTOF(rpc_construct_vhost_ctrlr),
&req)) {
SPDK_TRACELOG(SPDK_TRACE_DEBUG, "spdk_json_decode_object failed\n");
rc = -EINVAL;
@ -193,7 +194,7 @@ spdk_rpc_add_vhost_scsi_lun(struct spdk_jsonrpc_server_conn *conn,
int rc;
if (spdk_json_decode_object(params, rpc_vhost_add_lun,
sizeof(rpc_vhost_add_lun) / sizeof(*rpc_vhost_add_lun),
SPDK_COUNTOF(rpc_vhost_add_lun),
&req)) {
SPDK_TRACELOG(SPDK_TRACE_DEBUG, "spdk_json_decode_object failed\n");
rc = -EINVAL;

View File

@ -38,6 +38,8 @@
#include <stdio.h>
#include <string.h>
#include "spdk/util.h"
static uint8_t g_buf[1000];
static uint8_t *g_write_pos;
@ -590,7 +592,7 @@ test_write_val(void)
struct spdk_json_val values[100];
char src[] = "{\"a\":[1,2,3],\"b\":{\"c\":\"d\"},\"e\":true,\"f\":false,\"g\":null}";
CU_ASSERT(spdk_json_parse(src, strlen(src), values, sizeof(values) / sizeof(*values), NULL,
CU_ASSERT(spdk_json_parse(src, strlen(src), values, SPDK_COUNTOF(values), NULL,
SPDK_JSON_PARSE_FLAG_DECODE_IN_PLACE) == 19);
BEGIN();

View File

@ -39,6 +39,7 @@
#include "spdk/nvme.h"
#include "spdk/env.h"
#include "spdk/util.h"
#define MAX_DEVS 64
@ -261,8 +262,8 @@ static void build_io_request_9(struct io_request *req)
const size_t req_off[] = { 0x800, 0x0, 0x0, 0x100, 0x800, 0x800 };
struct sgl_element *iovs = req->iovs;
uint32_t i;
req->nseg = sizeof(req_len) / sizeof(req_len[0]);
assert(sizeof(req_len) / sizeof(req_len[0]) == sizeof(req_off) / sizeof(req_off[0]));
req->nseg = SPDK_COUNTOF(req_len);
assert(SPDK_COUNTOF(req_len) == SPDK_COUNTOF(req_off));
for (i = 0; i < req->nseg; i++) {
iovs[i].base = spdk_zmalloc(req_off[i] + req_len[i], 0x4000, NULL);