1 // Copyright 2012 Intel Corporation
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // - Redistributions of source code must retain the above copyright notice, this
9 //   list of conditions and the following disclaimer.
10 //
11 // - Redistributions in binary form must reproduce the above copyright notice,
12 //   this list of conditions and the following disclaimer in the documentation
13 //   and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 #pragma once
27 
28 #include <stddef.h>
29 #include <string.h>
30 #include "c99_compat.h"
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 #define container_of(ptr, type, member) ({                              \
37         const __typeof__(((type *)0)->member ) *__mptr = (ptr);         \
38         (type*)((void*)__mptr - offsetof(type, member));                \
39      })
40 
41 /// @brief Safe downcast using container_of().
42 ///
43 /// If given a null pointer, return null.
44 #define DEFINE_CONTAINER_CAST_FUNC(func_name,                           \
45                                    container_type,                      \
46                                    member_type,                         \
47                                    member)                              \
48                                                                         \
49     static inline container_type*                                       \
50     func_name(member_type *member##_self)                               \
51     {                                                                   \
52         if (member##_self)                                              \
53             return container_of(member##_self, container_type, member); \
54         else                                                            \
55             return 0;                                                   \
56     }
57 
58 /// @brief Addition that detects arithmetic overflow.
59 ///
60 /// If the addition would result in overflow, then return false and do not
61 /// update @a res.
62 bool
63 wcore_add_size(size_t *res, size_t x, size_t y);
64 
65 /// @brief In-place variant of wcore_add_size().
66 static inline bool
wcore_iadd_size(size_t * x,size_t y)67 wcore_iadd_size(size_t *x, size_t y) {
68     return wcore_add_size(x, *x, y);
69 }
70 
71 /// @brief Multiplication that detects arithmetic overflow.
72 ///
73 /// If the multiplication would result in overflow, then return false and do
74 /// not update @a res.
75 bool
76 wcore_mul_size(size_t *res, size_t x, size_t y);
77 
78 /// @brief In-place variant of wcore_mul_size().
79 static inline bool
wcore_imul_size(size_t * x,size_t y)80 wcore_imul_size(size_t *x, size_t y) {
81     return wcore_mul_size(x, *x, y);
82 }
83 
84 /// @brief Wrapper around malloc() that emits error if allocation fails.
85 void*
86 wcore_malloc(size_t size);
87 
88 /// @brief Wrapper around calloc() that emits error if allocation fails.
89 ///
90 /// This has the signature of malloc(), not calloc(), because calloc is too
91 /// easy to use incorrectly.
92 void*
93 wcore_calloc(size_t size);
94 
95 /// @brief Wrapper around realloc() that emits error if allocation fails.
96 void*
97 wcore_realloc(void *ptr, size_t size);
98 
99 /// @brief Wrapper around strdup() that emits error if allocation fails.
100 char*
101 wcore_strdup(const char *str);
102 
103 /// @brief Create one of `union waffle_native_*`.
104 ///
105 /// The example below allocates n_dpy and n_dpy->glx, then sets both
106 /// variables.
107 ///
108 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.c}
109 /// union waffle_native_display *n_dpy;
110 /// WCORE_CREATE_NATIVE_UNION(n_dpy, glx);
111 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
112 ///
113 #define WCORE_CREATE_NATIVE_UNION(union_var, union_member)              \
114         do {                                                            \
115             union_var = wcore_malloc(sizeof(*union_var) +               \
116                                      sizeof(*union_var->union_member)); \
117             if (union_var)                                              \
118                 union_var->union_member = (void*) (union_var + 1);      \
119         } while (0)
120 
121 const char*
122 wcore_enum_to_string(int32_t e);
123 
124 #ifdef __cplusplus
125 }
126 #endif
127