Unify to use ARRAY_SIZE in TF-M codes

ARRAY_SIZE(foo) is preferred over sizeof(foo)/sizeof(foo[0]).

Signed-off-by: Xinyu Zhang <xinyu.zhang@arm.com>
Change-Id: I6d95ceece2edc6267491923e282e28710b68ab8d
diff --git a/interface/include/psa/client.h b/interface/include/psa/client.h
index 8fd2d13..d92de02 100644
--- a/interface/include/psa/client.h
+++ b/interface/include/psa/client.h
@@ -17,6 +17,10 @@
 extern "C" {
 #endif
 
+#ifndef IOVEC_LEN
+#define IOVEC_LEN(arr) ((uint32_t)(sizeof(arr)/sizeof(arr[0])))
+#endif
+
 /*********************** PSA Client Macros and Types *************************/
 
 /**
diff --git a/interface/src/tfm_audit_func_api.c b/interface/src/tfm_audit_func_api.c
index 3439635..4141970 100644
--- a/interface/src/tfm_audit_func_api.c
+++ b/interface/src/tfm_audit_func_api.c
@@ -1,29 +1,28 @@
 /*
- * Copyright (c) 2018-2019, Arm Limited. All rights reserved.
+ * Copyright (c) 2018-2021, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
  */
 
+#include "psa/client.h"
 #include "psa_audit_api.h"
 #include "tfm_veneers.h"
 #include "tfm_ns_interface.h"
 
-#define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
-
 #define API_DISPATCH(sfn_name)                                    \
     tfm_ns_interface_dispatch((veneer_fn)tfm_##sfn_name##_veneer, \
-        (uint32_t)in_vec, (uint32_t)ARRAY_SIZE(in_vec),           \
-        (uint32_t)out_vec, (uint32_t)ARRAY_SIZE(out_vec))
+        (uint32_t)in_vec, IOVEC_LEN(in_vec),                      \
+        (uint32_t)out_vec, IOVEC_LEN(out_vec))
 
 #define API_DISPATCH_NO_INVEC(sfn_name)                           \
     tfm_ns_interface_dispatch((veneer_fn)tfm_##sfn_name##_veneer, \
         (uint32_t)NULL, 0,                                        \
-        (uint32_t)out_vec, (uint32_t)ARRAY_SIZE(out_vec))
+        (uint32_t)out_vec, IOVEC_LEN(out_vec))
 
 #define API_DISPATCH_NO_OUTVEC(sfn_name)                          \
     tfm_ns_interface_dispatch((veneer_fn)tfm_##sfn_name##_veneer, \
-        (uint32_t)in_vec, (uint32_t)ARRAY_SIZE(in_vec),           \
+        (uint32_t)in_vec, IOVEC_LEN(in_vec),                      \
         (uint32_t)NULL, 0)
 
 psa_status_t psa_audit_retrieve_record(const uint32_t record_index,
diff --git a/interface/src/tfm_crypto_func_api.c b/interface/src/tfm_crypto_func_api.c
index dd6a90a..3286997 100644
--- a/interface/src/tfm_crypto_func_api.c
+++ b/interface/src/tfm_crypto_func_api.c
@@ -5,21 +5,20 @@
  *
  */
 
+#include "psa/client.h"
 #include "tfm_veneers.h"
 #include "tfm_crypto_defs.h"
 #include "psa/crypto.h"
 #include "tfm_ns_interface.h"
 
-#define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
-
 #define API_DISPATCH(sfn_name, sfn_id)                               \
         tfm_ns_interface_dispatch((veneer_fn)tfm_##sfn_name##_veneer,\
-        (uint32_t)in_vec, ARRAY_SIZE(in_vec),                        \
-        (uint32_t)out_vec, ARRAY_SIZE(out_vec))
+        (uint32_t)in_vec, IOVEC_LEN(in_vec),                         \
+        (uint32_t)out_vec, IOVEC_LEN(out_vec))
 
 #define API_DISPATCH_NO_OUTVEC(sfn_name, sfn_id)                     \
         tfm_ns_interface_dispatch((veneer_fn)tfm_##sfn_name##_veneer,\
-        (uint32_t)in_vec, ARRAY_SIZE(in_vec),                        \
+        (uint32_t)in_vec, IOVEC_LEN(in_vec),                         \
         (uint32_t)NULL, 0)
 
 psa_status_t psa_crypto_init(void)
diff --git a/interface/src/tfm_crypto_ipc_api.c b/interface/src/tfm_crypto_ipc_api.c
index a89dd2b..b5f59c2 100644
--- a/interface/src/tfm_crypto_ipc_api.c
+++ b/interface/src/tfm_crypto_ipc_api.c
@@ -11,8 +11,6 @@
 #include "psa_manifest/sid.h"
 #include "psa/client.h"
 
-#define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
-
 #define PSA_CONNECT(service)                                    \
     psa_handle_t ipc_handle;                                    \
     ipc_handle = psa_connect(service##_SID, service##_VERSION); \
@@ -24,12 +22,12 @@
 
 #define API_DISPATCH(sfn_name, sfn_id)                          \
     psa_call(ipc_handle, PSA_IPC_CALL,                          \
-        in_vec, ARRAY_SIZE(in_vec),                             \
-        out_vec, ARRAY_SIZE(out_vec))
+        in_vec, IOVEC_LEN(in_vec),                              \
+        out_vec, IOVEC_LEN(out_vec))
 
 #define API_DISPATCH_NO_OUTVEC(sfn_name, sfn_id)                \
     psa_call(ipc_handle, PSA_IPC_CALL,                          \
-        in_vec, ARRAY_SIZE(in_vec),                             \
+        in_vec, IOVEC_LEN(in_vec),                              \
         (psa_outvec *)NULL, 0)
 
 psa_status_t psa_crypto_init(void)
@@ -947,12 +945,12 @@
 
     PSA_CONNECT(TFM_CRYPTO);
 
-    size_t in_len = ARRAY_SIZE(in_vec);
+    size_t in_len = IOVEC_LEN(in_vec);
     if (additional_data == NULL) {
         in_len--;
     }
     status = psa_call(ipc_handle, PSA_IPC_CALL, in_vec, in_len,
-                      out_vec, ARRAY_SIZE(out_vec));
+                      out_vec, IOVEC_LEN(out_vec));
 
     *ciphertext_length = out_vec[0].len;
 
@@ -1008,12 +1006,12 @@
 
     PSA_CONNECT(TFM_CRYPTO);
 
-    size_t in_len = ARRAY_SIZE(in_vec);
+    size_t in_len = IOVEC_LEN(in_vec);
     if (additional_data == NULL) {
         in_len--;
     }
     status = psa_call(ipc_handle, PSA_IPC_CALL, in_vec, in_len,
-                      out_vec, ARRAY_SIZE(out_vec));
+                      out_vec, IOVEC_LEN(out_vec));
 
     *plaintext_length = out_vec[0].len;
 
@@ -1142,12 +1140,12 @@
 
     PSA_CONNECT(TFM_CRYPTO);
 
-    size_t in_len = ARRAY_SIZE(in_vec);
+    size_t in_len = IOVEC_LEN(in_vec);
     if (salt == NULL) {
         in_len--;
     }
     status = psa_call(ipc_handle, PSA_IPC_CALL, in_vec, in_len,
-                      out_vec, ARRAY_SIZE(out_vec));
+                      out_vec, IOVEC_LEN(out_vec));
 
     *output_length = out_vec[0].len;
 
@@ -1190,12 +1188,12 @@
 
     PSA_CONNECT(TFM_CRYPTO);
 
-    size_t in_len = ARRAY_SIZE(in_vec);
+    size_t in_len = IOVEC_LEN(in_vec);
     if (salt == NULL) {
         in_len--;
     }
     status = psa_call(ipc_handle, PSA_IPC_CALL, in_vec, in_len,
-                      out_vec, ARRAY_SIZE(out_vec));
+                      out_vec, IOVEC_LEN(out_vec));
 
     *output_length = out_vec[0].len;
 
diff --git a/interface/src/tfm_firmware_update_func_api.c b/interface/src/tfm_firmware_update_func_api.c
index b8d3541..1d34d03 100644
--- a/interface/src/tfm_firmware_update_func_api.c
+++ b/interface/src/tfm_firmware_update_func_api.c
@@ -5,14 +5,12 @@
  *
  */
 
+#include "psa/client.h"
 #include "psa/update.h"
 #include "tfm_api.h"
-
 #include "tfm_ns_interface.h"
 #include "tfm_veneers.h"
 
-#define IOVEC_LEN(x) (uint32_t)(sizeof(x)/sizeof(x[0]))
-
 psa_status_t psa_fwu_write(uint32_t image_id,
                            size_t block_offset,
                            const void *block,
diff --git a/interface/src/tfm_firmware_update_ipc_api.c b/interface/src/tfm_firmware_update_ipc_api.c
index 0118488..a21a333 100644
--- a/interface/src/tfm_firmware_update_ipc_api.c
+++ b/interface/src/tfm_firmware_update_ipc_api.c
@@ -5,13 +5,10 @@
  *
  */
 
-#include "psa/update.h"
-#include "tfm_api.h"
-
 #include "psa/client.h"
+#include "psa/update.h"
 #include "psa_manifest/sid.h"
-
-#define IOVEC_LEN(x) (uint32_t)(sizeof(x)/sizeof(x[0]))
+#include "tfm_api.h"
 
 psa_status_t psa_fwu_write(const psa_image_id_t image_id,
                            size_t block_offset,
diff --git a/interface/src/tfm_initial_attestation_func_api.c b/interface/src/tfm_initial_attestation_func_api.c
index 5efe904..48dbbe1 100644
--- a/interface/src/tfm_initial_attestation_func_api.c
+++ b/interface/src/tfm_initial_attestation_func_api.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018-2020, Arm Limited. All rights reserved.
+ * Copyright (c) 2018-2021, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
@@ -11,8 +11,6 @@
 #include "psa/client.h"
 #include "psa/crypto_types.h"
 
-#define IOVEC_LEN(x) (sizeof(x)/sizeof(x[0]))
-
 psa_status_t
 psa_initial_attest_get_token(const uint8_t *auth_challenge,
                              size_t         challenge_size,
diff --git a/interface/src/tfm_initial_attestation_ipc_api.c b/interface/src/tfm_initial_attestation_ipc_api.c
index b4a8f37..fa7a956 100644
--- a/interface/src/tfm_initial_attestation_ipc_api.c
+++ b/interface/src/tfm_initial_attestation_ipc_api.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018-2020, Arm Limited. All rights reserved.
+ * Copyright (c) 2018-2021, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
@@ -11,8 +11,6 @@
 #include "psa/crypto_types.h"
 #include "psa_manifest/sid.h"
 
-#define IOVEC_LEN(x) (sizeof(x)/sizeof(x[0]))
-
 psa_status_t
 psa_initial_attest_get_token(const uint8_t *auth_challenge,
                              size_t         challenge_size,
diff --git a/interface/src/tfm_its_func_api.c b/interface/src/tfm_its_func_api.c
index 483cbee..b554a34 100644
--- a/interface/src/tfm_its_func_api.c
+++ b/interface/src/tfm_its_func_api.c
@@ -1,18 +1,16 @@
 /*
- * Copyright (c) 2019, Arm Limited. All rights reserved.
+ * Copyright (c) 2019-2021, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
  */
 
+#include "psa/client.h"
 #include "psa/internal_trusted_storage.h"
 #include "tfm_api.h"
-
 #include "tfm_ns_interface.h"
 #include "tfm_veneers.h"
 
-#define IOVEC_LEN(x) (sizeof(x)/sizeof(x[0]))
-
 psa_status_t psa_its_set(psa_storage_uid_t uid,
                          size_t data_length,
                          const void *p_data,
diff --git a/interface/src/tfm_its_ipc_api.c b/interface/src/tfm_its_ipc_api.c
index 8ad4a3b..543b88f 100644
--- a/interface/src/tfm_its_ipc_api.c
+++ b/interface/src/tfm_its_ipc_api.c
@@ -1,17 +1,14 @@
 /*
- * Copyright (c) 2019, Arm Limited. All rights reserved.
+ * Copyright (c) 2019-2021, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
  */
 
-#include "psa/internal_trusted_storage.h"
-#include "tfm_api.h"
-
 #include "psa/client.h"
+#include "psa/internal_trusted_storage.h"
 #include "psa_manifest/sid.h"
-
-#define IOVEC_LEN(x) (sizeof(x)/sizeof(x[0]))
+#include "tfm_api.h"
 
 psa_status_t psa_its_set(psa_storage_uid_t uid,
                          size_t data_length,
diff --git a/interface/src/tfm_ps_func_api.c b/interface/src/tfm_ps_func_api.c
index d3c53a4..4674a35 100644
--- a/interface/src/tfm_ps_func_api.c
+++ b/interface/src/tfm_ps_func_api.c
@@ -1,17 +1,15 @@
 /*
- * Copyright (c) 2017-2020, Arm Limited. All rights reserved.
+ * Copyright (c) 2017-2021, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
  */
 
+#include "psa/client.h"
 #include "psa/protected_storage.h"
-
 #include "tfm_ns_interface.h"
 #include "tfm_veneers.h"
 
-#define IOVEC_LEN(x) (uint32_t)(sizeof(x)/sizeof(x[0]))
-
 psa_status_t psa_ps_set(psa_storage_uid_t uid,
                         size_t data_length,
                         const void *p_data,
diff --git a/interface/src/tfm_ps_ipc_api.c b/interface/src/tfm_ps_ipc_api.c
index 4f937db..106917e 100644
--- a/interface/src/tfm_ps_ipc_api.c
+++ b/interface/src/tfm_ps_ipc_api.c
@@ -1,16 +1,14 @@
 /*
- * Copyright (c) 2017-2020, Arm Limited. All rights reserved.
+ * Copyright (c) 2017-2021, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
  */
 
+#include "psa/client.h"
 #include "psa/protected_storage.h"
-
-#include "tfm_ns_interface.h"
 #include "psa_manifest/sid.h"
-
-#define IOVEC_LEN(x) (uint32_t)(sizeof(x)/sizeof(x[0]))
+#include "tfm_ns_interface.h"
 
 psa_status_t psa_ps_set(psa_storage_uid_t uid,
                         size_t data_length,