blob: eb0a8f789af555291467e920848e2e3ad122a6ca [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#define STRING(s) #s
45
46/**
47 * Compile-time copy of hardcoded arrays.
48 *
49 * @param dst the array to copy to.
50 * @param src the source array.
51 */
52#ifndef ARRAY_COPY
53#define ARRAY_COPY(dst, src) \
54do { \
55 static_assert(ARRAY_LENGTH(src) == ARRAY_LENGTH(dst), \
56 "src and dst sizes must match"); \
57 static_assert(sizeof(src) == sizeof(dst), \
58 "src and dst sizes must match"); \
59 memcpy((src), (dst), sizeof(src)); \
60} while (0)
61#endif
62
Jon Cruzd618f682015-06-15 15:37:09 -070063/**
64 * Returns the smaller of two values.
65 *
66 * @param x the first item to compare.
67 * @param y the second item to compare.
68 * @return the value that evaluates to lesser than the other.
69 */
70#ifndef MIN
71#define MIN(x,y) (((x) < (y)) ? (x) : (y))
72#endif
73
Jon Cruz867d50e2015-06-15 15:37:10 -070074/**
Quentin Glidic8f9d90a2016-08-12 10:41:36 +020075 * Returns the bigger of two values.
76 *
77 * @param x the first item to compare.
78 * @param y the second item to compare.
79 * @return the value that evaluates to more than the other.
80 */
81#ifndef MAX
82#define MAX(x,y) (((x) > (y)) ? (x) : (y))
83#endif
84
85/**
Abdur Rehman5735eed2017-01-01 19:46:41 +050086 * Returns a pointer to the containing struct of a given member item.
Jon Cruz867d50e2015-06-15 15:37:10 -070087 *
88 * To demonstrate, the following example retrieves a pointer to
89 * `example_container` given only its `destroy_listener` member:
90 *
91 * @code
92 * struct example_container {
93 * struct wl_listener destroy_listener;
94 * // other members...
95 * };
96 *
97 * void example_container_destroy(struct wl_listener *listener, void *data)
98 * {
99 * struct example_container *ctr;
100 *
101 * ctr = wl_container_of(listener, ctr, destroy_listener);
102 * // destroy ctr...
103 * }
104 * @endcode
105 *
106 * @param ptr A valid pointer to the contained item.
107 *
108 * @param type A pointer to the type of content that the list item
109 * stores. Type does not need be a valid pointer; a null or
110 * an uninitialised pointer will suffice.
111 *
112 * @param member The named location of ptr within the sample type.
113 *
114 * @return The container for the specified pointer.
115 */
116#ifndef container_of
117#define container_of(ptr, type, member) ({ \
118 const __typeof__( ((type *)0)->member ) *__mptr = (ptr); \
119 (type *)( (char *)__mptr - offsetof(type,member) );})
120#endif
121
Daniel Stone11f91bb2018-07-09 13:05:59 +0100122/**
123 * Build-time static assertion support
124 *
125 * A build-time equivalent to assert(), will generate a compilation error
126 * if the supplied condition does not evaluate true.
127 *
128 * The following example demonstrates use of static_assert to ensure that
129 * arrays which are supposed to mirror each other have a consistent
130 * size.
131 *
132 * This is only a fallback definition; support must be provided by the
133 * compiler itself.
134 *
135 * @code
136 * int small[4];
137 * long expanded[4];
138 *
139 * static_assert(ARRAY_LENGTH(small) == ARRAY_LENGTH(expanded),
140 * "size mismatch between small and expanded arrays");
141 * for (i = 0; i < ARRAY_LENGTH(small); i++)
142 * expanded[i] = small[4];
143 * @endcode
144 *
Marius Vlad48392642019-06-14 13:04:52 +0300145 * @param cond Expression to check for truth
Daniel Stone11f91bb2018-07-09 13:05:59 +0100146 * @param msg Message to print on failure
147 */
148#ifndef static_assert
149# ifdef _Static_assert
150# define static_assert(cond, msg) _Static_assert(cond, msg)
151# else
152# define static_assert(cond, msg)
153# endif
154#endif
155
Pekka Paalaneneb5a95b2021-02-15 13:34:01 +0200156/** Ensure argument is of given type */
157#ifndef TYPEVERIFY
158#define TYPEVERIFY(type, arg) ({ \
159 typeof(arg) tmp___ = (arg); \
160 (void)((type)0 == tmp___); \
161 tmp___; })
162#endif
163
Jon Cruz35b2eaa2015-06-15 15:37:08 -0700164#ifdef __cplusplus
165}
166#endif
167
168#endif /* WESTON_HELPERS_H */