blob: 1688b8ea766e23bdbe04d5e11a5e7094e0dce248 [file] [log] [blame]
Jon Cruz35b2eaa2015-06-15 15:37:08 -07001/*
2 * Copyright © 2015 Samsung Electronics Co., Ltd
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
16 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
17 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 * SOFTWARE.
20 */
21
22#ifndef WESTON_HELPERS_H
23#define WESTON_HELPERS_H
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
29/**
30 * @file
31 * Simple misc helper macros.
32 */
33
34/**
35 * Compile-time computation of number of items in a hardcoded array.
36 *
37 * @param a the array being measured.
38 * @return the number of items hardcoded into the array.
39 */
40#ifndef ARRAY_LENGTH
41#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
42#endif
43
Leandro Ribeiro3193ab52021-04-27 19:00:34 -030044/**
45 * Compile-time copy of hardcoded arrays.
46 *
47 * @param dst the array to copy to.
48 * @param src the source array.
49 */
50#ifndef ARRAY_COPY
51#define ARRAY_COPY(dst, src) \
52do { \
53 static_assert(ARRAY_LENGTH(src) == ARRAY_LENGTH(dst), \
54 "src and dst sizes must match"); \
55 static_assert(sizeof(src) == sizeof(dst), \
56 "src and dst sizes must match"); \
Leandro Ribeiro58393ca2021-04-28 10:43:27 -030057 memcpy((dst), (src), sizeof(src)); \
Leandro Ribeiro3193ab52021-04-27 19:00:34 -030058} while (0)
59#endif
60
Jon Cruzd618f682015-06-15 15:37:09 -070061/**
62 * Returns the smaller of two values.
63 *
64 * @param x the first item to compare.
65 * @param y the second item to compare.
66 * @return the value that evaluates to lesser than the other.
67 */
68#ifndef MIN
69#define MIN(x,y) (((x) < (y)) ? (x) : (y))
70#endif
71
Jon Cruz867d50e2015-06-15 15:37:10 -070072/**
Quentin Glidic8f9d90a2016-08-12 10:41:36 +020073 * Returns the bigger of two values.
74 *
75 * @param x the first item to compare.
76 * @param y the second item to compare.
77 * @return the value that evaluates to more than the other.
78 */
79#ifndef MAX
80#define MAX(x,y) (((x) > (y)) ? (x) : (y))
81#endif
82
83/**
Abdur Rehman5735eed2017-01-01 19:46:41 +050084 * Returns a pointer to the containing struct of a given member item.
Jon Cruz867d50e2015-06-15 15:37:10 -070085 *
86 * To demonstrate, the following example retrieves a pointer to
87 * `example_container` given only its `destroy_listener` member:
88 *
89 * @code
90 * struct example_container {
91 * struct wl_listener destroy_listener;
92 * // other members...
93 * };
94 *
95 * void example_container_destroy(struct wl_listener *listener, void *data)
96 * {
97 * struct example_container *ctr;
98 *
99 * ctr = wl_container_of(listener, ctr, destroy_listener);
100 * // destroy ctr...
101 * }
102 * @endcode
103 *
104 * @param ptr A valid pointer to the contained item.
105 *
106 * @param type A pointer to the type of content that the list item
107 * stores. Type does not need be a valid pointer; a null or
108 * an uninitialised pointer will suffice.
109 *
110 * @param member The named location of ptr within the sample type.
111 *
112 * @return The container for the specified pointer.
113 */
114#ifndef container_of
115#define container_of(ptr, type, member) ({ \
116 const __typeof__( ((type *)0)->member ) *__mptr = (ptr); \
117 (type *)( (char *)__mptr - offsetof(type,member) );})
118#endif
119
Daniel Stone11f91bb2018-07-09 13:05:59 +0100120/**
121 * Build-time static assertion support
122 *
123 * A build-time equivalent to assert(), will generate a compilation error
124 * if the supplied condition does not evaluate true.
125 *
126 * The following example demonstrates use of static_assert to ensure that
127 * arrays which are supposed to mirror each other have a consistent
128 * size.
129 *
130 * This is only a fallback definition; support must be provided by the
131 * compiler itself.
132 *
133 * @code
134 * int small[4];
135 * long expanded[4];
136 *
137 * static_assert(ARRAY_LENGTH(small) == ARRAY_LENGTH(expanded),
138 * "size mismatch between small and expanded arrays");
139 * for (i = 0; i < ARRAY_LENGTH(small); i++)
140 * expanded[i] = small[4];
141 * @endcode
142 *
Marius Vlad48392642019-06-14 13:04:52 +0300143 * @param cond Expression to check for truth
Daniel Stone11f91bb2018-07-09 13:05:59 +0100144 * @param msg Message to print on failure
145 */
146#ifndef static_assert
147# ifdef _Static_assert
148# define static_assert(cond, msg) _Static_assert(cond, msg)
149# else
150# define static_assert(cond, msg)
151# endif
152#endif
153
Pekka Paalaneneb5a95b2021-02-15 13:34:01 +0200154/** Ensure argument is of given type */
155#ifndef TYPEVERIFY
156#define TYPEVERIFY(type, arg) ({ \
157 typeof(arg) tmp___ = (arg); \
158 (void)((type)0 == tmp___); \
159 tmp___; })
160#endif
161
Jon Cruz35b2eaa2015-06-15 15:37:08 -0700162#ifdef __cplusplus
163}
164#endif
165
166#endif /* WESTON_HELPERS_H */