summaryrefslogtreecommitdiff
path: root/quantum/util.h
diff options
context:
space:
mode:
authorJeff Epler <jepler@gmail.com>2022-08-30 03:20:04 -0500
committerGitHub <noreply@github.com>2022-08-30 10:20:04 +0200
commit9632360caa5e6511b0ec13cb4c55eb64408232b5 (patch)
treedb4e0d47fb8d4918a3f7361647a4afca13da23e0 /quantum/util.h
parent2c5aa98143fc1bb76299033042b374b2ce148d93 (diff)
Use a macro to compute the size of arrays at compile time (#18044)
* Add ARRAY_SIZE and CEILING utility macros * Apply a coccinelle patch to use ARRAY_SIZE * fix up some straggling items * Fix 'make test:secure' * Enhance ARRAY_SIZE macro to reject acting on pointers The previous definition would not produce a diagnostic for ``` int *p; size_t num_elem = ARRAY_SIZE(p) ``` but the new one will. * explicitly get definition of ARRAY_SIZE * Convert to ARRAY_SIZE when const is involved The following spatch finds additional instances where the array is const and the division is by the size of the type, not the size of the first element: ``` @ rule5a using "empty.iso" @ type T; const T[] E; @@ - (sizeof(E)/sizeof(T)) + ARRAY_SIZE(E) @ rule6a using "empty.iso" @ type T; const T[] E; @@ - sizeof(E)/sizeof(T) + ARRAY_SIZE(E) ``` * New instances of ARRAY_SIZE added since initial spatch run * Use `ARRAY_SIZE` in docs (found by grep) * Manually use ARRAY_SIZE hs_set is expected to be the same size as uint16_t, though it's made of two 8-bit integers * Just like char, sizeof(uint8_t) is guaranteed to be 1 This is at least true on any plausible system where qmk is actually used. Per my understanding it's universally true, assuming that uint8_t exists: https://stackoverflow.com/questions/48655310/can-i-assume-that-sizeofuint8-t-1 * Run qmk-format on core C files touched in this branch Co-authored-by: Stefan Kerkmann <karlk90@pm.me>
Diffstat (limited to 'quantum/util.h')
-rw-r--r--quantum/util.h49
1 files changed, 33 insertions, 16 deletions
diff --git a/quantum/util.h b/quantum/util.h
index 21c290013c..9c034cc404 100644
--- a/quantum/util.h
+++ b/quantum/util.h
@@ -1,19 +1,7 @@
-/*
-Copyright 2011 Jun Wako <wakojun@gmail.com>
-
-This program is free software: you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation, either version 2 of the License, or
-(at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program. If not, see <http://www.gnu.org/licenses/>.
-*/
+// Copyright 2022 Stefan Kerkmann (KarlK90)
+// Copyright 2011 Jun Wako <wakojun@gmail.com>
+// SPDX-License-Identifier: GPL-2.0-or-later
+
#pragma once
#include "bitwise.h"
@@ -29,3 +17,32 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#if !defined(MAX)
# define MAX(x, y) (((x) > (y)) ? (x) : (y))
#endif
+
+#if !defined(CEILING)
+/**
+ * @brief Computes the rounded up result of a division of two integers at
+ * compile time.
+ */
+# define CEILING(dividend, divisor) (((dividend) + (divisor)-1) / (divisor))
+#endif
+
+#if !defined(IS_ARRAY)
+/**
+ * @brief Returns true if the value is an array, false if it's a pointer.
+ *
+ * This macro is ill-formed for scalars, which is OK for its intended use in
+ * ARRAY_SIZE.
+ */
+# define IS_ARRAY(value) (!__builtin_types_compatible_p(typeof((value)), typeof(&(value)[0])))
+#endif
+
+#if !defined(ARRAY_SIZE)
+/**
+ * @brief Computes the number of elements of the given array at compile time.
+ *
+ * This Macro can only be used for statically allocated arrays that have not
+ * been decayed into a pointer. This is detected at compile time, though the
+ * error message for scalar values is poor.
+ */
+# define ARRAY_SIZE(array) (__builtin_choose_expr(IS_ARRAY((array)), sizeof((array)) / sizeof((array)[0]), (void)0))
+#endif