shadow/lib/sizeof.h
Alejandro Colomar 9214a8e44d lib/: Use non-empty compound literals
While the empty one is more correct, {0} will also work, and will
likely silence diagnostics in old compiler versions.

Empty compound literals are only supported in GCC since commit
gcc.git 14cfa01755a6 (2022-08-25; "c: Support C2x empty initializer braces")

Reported-by: Serge Hallyn <serge@hallyn.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-12-30 10:07:45 -06:00

34 lines
782 B
C

// SPDX-FileCopyrightText: 2022-2025, 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>
#if __has_include(<stdcountof.h>)
# include <stdcountof.h>
#endif
#include <sys/types.h>
#define typeas(T) typeof((T){0})
#define ssizeof(x) ({(ssize_t){sizeof(x)};})
#define memberof(T, member) ((T){}.member)
#define WIDTHOF(x) (sizeof(x) * CHAR_BIT)
#if !defined(countof)
# define countof(a) (sizeof(a) / sizeof((a)[0]))
#endif
// sizeof_a - sizeof array
#define sizeof_a(a) (countof(a) * sizeof((a)[0]))
#define STRLEN(s) (countof("" s "") - 1)
#endif // include guard