[PATCH 1/2] Core: added macro to calculate the length of an array.

Alejandro Colomar alx.manpages at gmail.com
Mon Nov 7 14:07:34 UTC 2022


This macro abstracts calculating how many items there are in an array.

Since recent versions of GCC and Clang provide a warning if we pass a
pointer to the construct in this macro, it is safe to use without any
further checks that the argument is an array.

Link: <https://stackoverflow.com/a/57537491>
Cc: Andrew Clayton <a.clayton at nginx.com>
Cc: Zhidao Hong <z.hong at f5.com>
Signed-off-by: Alejandro Colomar <alx at nginx.com>
---
 src/core/ngx_core.h | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/src/core/ngx_core.h b/src/core/ngx_core.h
index 7ecdca0c..bbca2df2 100644
--- a/src/core/ngx_core.h
+++ b/src/core/ngx_core.h
@@ -102,6 +102,9 @@ typedef void (*ngx_connection_handler_pt)(ngx_connection_t *c);
 #define ngx_max(val1, val2)  ((val1 < val2) ? (val2) : (val1))
 #define ngx_min(val1, val2)  ((val1 > val2) ? (val2) : (val1))
 
+#define ngx_nitems(arr)      (sizeof((arr)) / sizeof((arr)[0]))
+
+
 void ngx_cpuinfo(void);
 
 #if (NGX_HAVE_OPENAT)
-- 
2.37.2



More information about the nginx-devel mailing list