This makes it safe to call sizeof() on an array. Calling sizeof() directly on an array is dangerous, because if the array changes to be a pointer, the behavior will unexpectedly change. It's the same problem as with NITEMS(). Link: <https://stackoverflow.com/a/57537491> Cc: Christian Göttsche <cgzones@googlemail.com> Cc: Serge Hallyn <serge@hallyn.com> Cc: Iker Pedrosa <ipedrosa@redhat.com> Signed-off-by: Alejandro Colomar <alx@kernel.org>
25 lines
506 B
C
25 lines
506 B
C
/*
|
|
* SPDX-FileCopyrightText: 2022-2023, Alejandro Colomar <alx@kernel.org>
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*/
|
|
|
|
|
|
#ifndef SHADOW_INCLUDE_LIBMISC_SIZEOF_H_
|
|
#define SHADOW_INCLUDE_LIBMISC_SIZEOF_H_
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include "must_be.h"
|
|
|
|
|
|
#define WIDTHOF(x) (sizeof(x) * CHAR_BIT)
|
|
#define SIZEOF_ARRAY(a) (sizeof(a) + must_be_array(a))
|
|
#define NITEMS(a) (SIZEOF_ARRAY((a)) / sizeof((a)[0]))
|
|
#define STRLEN(s) (NITEMS(s) - 1)
|
|
|
|
|
|
#endif // include guard
|