From f43cff3728e58c377d1e03b13db62514217abfe1 Mon Sep 17 00:00:00 2001
From: Faith Ekstrand <faith.ekstrand@collabora.com>
Date: Tue, 29 Jul 2025 03:10:41 -0400
Subject: [PATCH] util: Move STACK_ARRAY into util

It's useful for more than just Vulkan.

Reviewed-by: Boris Brezillon <boris.brezillon@collabora.com>
Reviewed-by: Christoph Pillmayer <christoph.pillmayer@arm.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/36385>
---
 src/util/meson.build      |  1 +
 src/util/stack_array.h    | 45 +++++++++++++++++++++++++++++++++++++++
 src/vulkan/util/vk_util.h | 17 +--------------
 3 files changed, 47 insertions(+), 16 deletions(-)
 create mode 100644 src/util/stack_array.h

diff --git a/src/util/meson.build b/src/util/meson.build
index a60fbc7b6604..ed746734b877 100644
--- a/src/util/meson.build
+++ b/src/util/meson.build
@@ -120,6 +120,7 @@ files_mesa_util = files(
   'softfloat.h',
   'sparse_array.c',
   'sparse_array.h',
+  'stack_array.h',
   'string_buffer.c',
   'string_buffer.h',
   'strndup.h',
diff --git a/src/util/stack_array.h b/src/util/stack_array.h
new file mode 100644
index 000000000000..e2133bdc2f4d
--- /dev/null
+++ b/src/util/stack_array.h
@@ -0,0 +1,45 @@
+/*
+ * Copyright © 2025 Collabora, Ltd.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include <stdlib.h>
+
+#ifndef UTIL_STACK_ARRAY_H
+#define UTIL_STACK_ARRAY_H
+
+#define STACK_ARRAY_SIZE 8
+
+/* Sometimes gcc may claim -Wmaybe-uninitialized for the stack array in some
+ * places it can't verify that when size is 0 nobody down the call chain reads
+ * the array. Please don't try to fix it by zero-initializing the array here
+ * since it's used in a lot of different places. An "if (size == 0) return;"
+ * may work for you.
+ */
+#define STACK_ARRAY(type, name, size) \
+   type _stack_##name[STACK_ARRAY_SIZE]; \
+   type *const name = \
+     ((size) <= STACK_ARRAY_SIZE ? _stack_##name : (type *)malloc((size) * sizeof(type)))
+
+#define STACK_ARRAY_FINISH(name) \
+   if (name != _stack_##name) free(name)
+
+#endif /* UTIL_STACK_ARRAY_H */
diff --git a/src/vulkan/util/vk_util.h b/src/vulkan/util/vk_util.h
index aea4c569a188..4f5972b291a7 100644
--- a/src/vulkan/util/vk_util.h
+++ b/src/vulkan/util/vk_util.h
@@ -26,6 +26,7 @@
 #include "compiler/shader_enums.h"
 #include "util/bitscan.h"
 #include "util/macros.h"
+#include "util/stack_array.h"
 #include "c99_compat.h"
 
 #include <stdlib.h>
@@ -340,22 +341,6 @@ struct nir_spirv_specialization*
 vk_spec_info_to_nir_spirv(const VkSpecializationInfo *spec_info,
                           uint32_t *out_num_spec_entries);
 
-#define STACK_ARRAY_SIZE 8
-
-/* Sometimes gcc may claim -Wmaybe-uninitialized for the stack array in some
- * places it can't verify that when size is 0 nobody down the call chain reads
- * the array. Please don't try to fix it by zero-initializing the array here
- * since it's used in a lot of different places. An "if (size == 0) return;"
- * may work for you.
- */
-#define STACK_ARRAY(type, name, size) \
-   type _stack_##name[STACK_ARRAY_SIZE]; \
-   type *const name = \
-     ((size) <= STACK_ARRAY_SIZE ? _stack_##name : (type *)malloc((size) * sizeof(type)))
-
-#define STACK_ARRAY_FINISH(name) \
-   if (name != _stack_##name) free(name)
-
 static inline uint8_t
 vk_index_type_to_bytes(VkIndexType type)
 {
-- 
GitLab