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>
This commit is contained in:
Faith Ekstrand 2025-07-29 03:10:41 -04:00 committed by Boris Brezillon
parent 61f7990b9a
commit f43cff3728
3 changed files with 47 additions and 16 deletions

View file

@ -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.c',

45
src/util/stack_array.h Normal file
View file

@ -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 */

View file

@ -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)
{