[02/16] hash: remove use of VLAs for Windows built code

Message ID 1713397319-26135-3-git-send-email-roretzla@linux.microsoft.com (mailing list archive)
State Superseded
Delegated to: Thomas Monjalon
Headers
Series remove use of VLAs for Windows built code |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Tyler Retzlaff April 17, 2024, 11:41 p.m. UTC
  MSVC does not support VLAs, replace VLAs with standard C arrays
or alloca(). alloca() is available for all toolchain/platform
combinations officially supported by DPDK.

Signed-off-by: Tyler Retzlaff <roretzla@linux.microsoft.com>
---
 lib/hash/rte_cuckoo_hash.c | 4 ++--
 lib/hash/rte_thash.c       | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
  

Comments

Morten Brørup April 18, 2024, 6:45 a.m. UTC | #1
> -	int32_t positions[num_keys];
> +	int32_t *positions = alloca(sizeof(int32_t) * num_keys);

A general comment, using this patch as an example...

I wonder if adding const has any effect on the compilers' optimizers:

int32_t * const positions = alloca(sizeof(int32_t) * num_keys);

Perhaps not in this specific case, but maybe elsewhere.
Especially if passing the array by reference.
We're not good at adding const and restrict to function parameters, so declaring the alloca()'d arrays const might compensate for that.
  

Patch

diff --git a/lib/hash/rte_cuckoo_hash.c b/lib/hash/rte_cuckoo_hash.c
index 9cf9464..bd2ff08 100644
--- a/lib/hash/rte_cuckoo_hash.c
+++ b/lib/hash/rte_cuckoo_hash.c
@@ -2359,7 +2359,7 @@  struct rte_hash *
 			(num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
 			(hit_mask == NULL)), -EINVAL);
 
-	int32_t positions[num_keys];
+	int32_t *positions = alloca(sizeof(int32_t) * num_keys);
 
 	__rte_hash_lookup_bulk(h, keys, num_keys, positions, hit_mask, data);
 
@@ -2475,7 +2475,7 @@  struct rte_hash *
 			(num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
 			(hit_mask == NULL)), -EINVAL);
 
-	int32_t positions[num_keys];
+	int32_t *positions = alloca(sizeof(int32_t) * num_keys);
 
 	__rte_hash_lookup_with_hash_bulk(h, keys, sig, num_keys,
 			positions, hit_mask, data);
diff --git a/lib/hash/rte_thash.c b/lib/hash/rte_thash.c
index 68f653f..633e211 100644
--- a/lib/hash/rte_thash.c
+++ b/lib/hash/rte_thash.c
@@ -771,7 +771,7 @@  struct rte_thash_subtuple_helper *
 	uint32_t desired_value,	unsigned int attempts,
 	rte_thash_check_tuple_t fn, void *userdata)
 {
-	uint32_t tmp_tuple[tuple_len / sizeof(uint32_t)];
+	uint32_t *tmp_tuple = alloca(tuple_len);
 	unsigned int i, j, ret = 0;
 	uint32_t hash, adj_bits;
 	const uint8_t *hash_key;