blob: ea8df988806b48574bfc66558b355f8eba324ffd [file] [log] [blame]
Tamas Ban581034a2017-12-19 19:54:37 +00001/*
2 * Copyright (c) 2011-2014, Wind River Systems, Inc.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef __BL2_UTIL_H__
8#define __BL2_UTIL_H__
9
10#ifdef __cplusplus
11 extern "C" {
12#endif
13
14#include <stddef.h>
15
16 /* Evaluates to 0 if cond is true-ish; compile error otherwise */
17#define ZERO_OR_COMPILE_ERROR(cond) ((int) sizeof(char[1 - 2 * !(cond)]) - 1)
18
19 /* Evaluates to 0 if array is an array; compile error if not array (e.g.
20 * pointer)
21 */
TTornblomc640e072019-06-14 14:33:51 +020022#if defined(NO_TYPEOF)
23 /* __typeof__ is a non-standard gcc extension, not universally available.
24 * As this is just compile time data type test, assume things are ok for
25 * tool chains missing this feature.
26 */
27#define IS_ARRAY(array) 0
28#else
Tamas Ban581034a2017-12-19 19:54:37 +000029#define IS_ARRAY(array) \
30 ZERO_OR_COMPILE_ERROR(!__builtin_types_compatible_p(__typeof__(array), \
31 __typeof__(&(array)[0])))
TTornblomc640e072019-06-14 14:33:51 +020032#endif
Tamas Ban581034a2017-12-19 19:54:37 +000033
34#define ARRAY_SIZE(array) \
35 ((unsigned long) (IS_ARRAY(array) + \
36 (sizeof(array) / sizeof((array)[0]))))
37
38#define CONTAINER_OF(ptr, type, field) \
39 ((type *)(((char *)(ptr)) - offsetof(type, field)))
40
Tamas Ban581034a2017-12-19 19:54:37 +000041#ifdef __cplusplus
42}
43#endif
44
45#endif /* __BL2_UTIL_H__ */
46