1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * Support for Intel Camera Imaging ISP subsystem.
4 * Copyright (c) 2015, Intel Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 */
15
16 #ifndef __ASSERT_SUPPORT_H_INCLUDED__
17 #define __ASSERT_SUPPORT_H_INCLUDED__
18
19 /**
20 * The following macro can help to test the size of a struct at compile
21 * time rather than at run-time. It does not work for all compilers; see
22 * below.
23 *
24 * Depending on the value of 'condition', the following macro is expanded to:
25 * - condition==true:
26 * an expression containing an array declaration with negative size,
27 * usually resulting in a compilation error
28 * - condition==false:
29 * (void) 1; // C statement with no effect
30 *
31 * example:
32 * COMPILATION_ERROR_IF( sizeof(struct host_sp_queues) != SIZE_OF_HOST_SP_QUEUES_STRUCT);
33 *
34 * verify that the macro indeed triggers a compilation error with your compiler:
35 * COMPILATION_ERROR_IF( sizeof(struct host_sp_queues) != (sizeof(struct host_sp_queues)+1) );
36 *
37 * Not all compilers will trigger an error with this macro; use a search engine to search for
38 * BUILD_BUG_ON to find other methods.
39 */
40 #define COMPILATION_ERROR_IF(condition) ((void)sizeof(char[1 - 2 * !!(condition)]))
41
42 /* Compile time assertion */
43 #ifndef CT_ASSERT
44 #define CT_ASSERT(cnd) ((void)sizeof(char[(cnd) ? 1 : -1]))
45 #endif /* CT_ASSERT */
46
47 #include <linux/bug.h>
48
49 /* TODO: it would be cleaner to use this:
50 * #define assert(cnd) BUG_ON(cnd)
51 * but that causes many compiler warnings (==errors) under Android
52 * because it seems that the BUG_ON() macro is not seen as a check by
53 * gcc like the BUG() macro is. */
54 #define assert(cnd) \
55 do { \
56 if (!(cnd)) \
57 BUG(); \
58 } while (0)
59
60 #ifndef PIPE_GENERATION
61 /* Deprecated OP___assert, this is still used in ~1000 places
62 * in the code. This will be removed over time.
63 * The implementation for the pipe generation tool is in see support.isp.h */
64 #define OP___assert(cnd) assert(cnd)
65
compile_time_assert(unsigned int cond)66 static inline void compile_time_assert(unsigned int cond)
67 {
68 /* Call undefined function if cond is false */
69 void _compile_time_assert(void);
70 if (!cond) _compile_time_assert();
71 }
72 #endif /* PIPE_GENERATION */
73
74 #endif /* __ASSERT_SUPPORT_H_INCLUDED__ */
75