Welcome! Log In Create A New Profile

Advanced

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

Alejandro Colomar
November 07, 2022 09:10AM
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@nginx.com>
Cc: Zhidao Hong <z.hong@f5.com>
Signed-off-by: Alejandro Colomar <alx@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

_______________________________________________
nginx-devel mailing list -- nginx-devel@nginx.org
To unsubscribe send an email to nginx-devel-leave@nginx.org
Subject Author Views Posted

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

Alejandro Colomar 320 November 07, 2022 09:10AM

[PATCH 2/2] Core: replaced unsafe uses of sizeof() by ngx_nitems().

Alejandro Colomar 96 November 07, 2022 09:10AM

Re: [PATCH 2/2] Core: replaced unsafe uses of sizeof() by ngx_nitems().

Maxim Dounin 138 November 08, 2022 06:14AM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 292
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready