Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
TTornblom | c640e07 | 2019-06-14 14:33:51 +0200 | [diff] [blame^] | 22 | #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 Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 29 | #define IS_ARRAY(array) \ |
| 30 | ZERO_OR_COMPILE_ERROR(!__builtin_types_compatible_p(__typeof__(array), \ |
| 31 | __typeof__(&(array)[0]))) |
TTornblom | c640e07 | 2019-06-14 14:33:51 +0200 | [diff] [blame^] | 32 | #endif |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 33 | |
| 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 Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 41 | #ifdef __cplusplus |
| 42 | } |
| 43 | #endif |
| 44 | |
| 45 | #endif /* __BL2_UTIL_H__ */ |
| 46 | |