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 | */ |
| 22 | #define IS_ARRAY(array) \ |
| 23 | ZERO_OR_COMPILE_ERROR(!__builtin_types_compatible_p(__typeof__(array), \ |
| 24 | __typeof__(&(array)[0]))) |
| 25 | |
| 26 | #define ARRAY_SIZE(array) \ |
| 27 | ((unsigned long) (IS_ARRAY(array) + \ |
| 28 | (sizeof(array) / sizeof((array)[0])))) |
| 29 | |
| 30 | #define CONTAINER_OF(ptr, type, field) \ |
| 31 | ((type *)(((char *)(ptr)) - offsetof(type, field))) |
| 32 | |
| 33 | struct device { |
| 34 | int device_id; |
| 35 | }; |
| 36 | |
| 37 | #ifdef __cplusplus |
| 38 | } |
| 39 | #endif |
| 40 | |
| 41 | #endif /* __BL2_UTIL_H__ */ |
| 42 | |