xref: /haiku/headers/libs/zydis/Zycore/Status.h (revision 909af08f4328301fbdef1ffb41f566c3b5bec0c7)
1 /***************************************************************************************************
2 
3   Zyan Core Library (Zyan-C)
4 
5   Original Author : Florian Bernd, Joel Hoener
6 
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24 
25 ***************************************************************************************************/
26 
27 /**
28  * @file
29  * Status code definitions and check macros.
30  */
31 
32 #ifndef ZYCORE_STATUS_H
33 #define ZYCORE_STATUS_H
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 #include <Zycore/Types.h>
40 
41 /* ============================================================================================== */
42 /* Enums and types                                                                                */
43 /* ============================================================================================== */
44 
45 /**
46  * Defines the `ZyanStatus` data type.
47  */
48 typedef ZyanU32 ZyanStatus;
49 
50 /* ============================================================================================== */
51 /* Macros                                                                                         */
52 /* ============================================================================================== */
53 
54 /* ---------------------------------------------------------------------------------------------- */
55 /* Definition                                                                                     */
56 /* ---------------------------------------------------------------------------------------------- */
57 
58 /**
59  * Defines a zyan status code.
60  *
61  * @param   error   `1`, if the status code signals an error or `0`, if not.
62  * @param   module  The module id.
63  * @param   code    The actual code.
64  *
65  * @return  The zyan status code.
66  */
67 #define ZYAN_MAKE_STATUS(error, module, code) \
68     (ZyanStatus)((((error) & 0x01u) << 31u) | (((module) & 0x7FFu) << 20u) | ((code) & 0xFFFFFu))
69 
70 /* ---------------------------------------------------------------------------------------------- */
71 /* Checks                                                                                         */
72 /* ---------------------------------------------------------------------------------------------- */
73 
74 /**
75  * Checks if a zyan operation was successful.
76  *
77  * @param   status  The zyan status-code to check.
78  *
79  * @return  `ZYAN_TRUE`, if the operation succeeded or `ZYAN_FALSE`, if not.
80  */
81 #define ZYAN_SUCCESS(status) \
82     (!((status) & 0x80000000u))
83 
84 /**
85  * Checks if a zyan operation failed.
86  *
87  * @param   status  The zyan status-code to check.
88  *
89  * @return  `ZYAN_TRUE`, if the operation failed or `ZYAN_FALSE`, if not.
90  */
91 #define ZYAN_FAILED(status) \
92     ((status) & 0x80000000u)
93 
94 /**
95  * Checks if a zyan operation was successful and returns with the status-code, if not.
96  *
97  * @param   status  The zyan status-code to check.
98  */
99 #define ZYAN_CHECK(status) \
100     do \
101     { \
102         const ZyanStatus status_047620348 = (status); \
103         if (!ZYAN_SUCCESS(status_047620348)) \
104         { \
105             return status_047620348; \
106         } \
107     } while (0)
108 
109 /* ---------------------------------------------------------------------------------------------- */
110 /* Information                                                                                    */
111 /* ---------------------------------------------------------------------------------------------- */
112 
113  /**
114  * Returns the module id of a zyan status-code.
115  *
116  * @param   status  The zyan status-code.
117  *
118  * @return  The module id of the zyan status-code.
119  */
120 #define ZYAN_STATUS_MODULE(status) \
121     (((status) >> 20) & 0x7FFu)
122 
123  /**
124  * Returns the code of a zyan status-code.
125  *
126  * @param   status  The zyan status-code.
127  *
128  * @return  The code of the zyan status-code.
129  */
130 #define ZYAN_STATUS_CODE(status) \
131     ((status) & 0xFFFFFu)
132 
133 /* ============================================================================================== */
134 /* Status codes                                                                                   */
135 /* ============================================================================================== */
136 
137 /* ---------------------------------------------------------------------------------------------- */
138 /* Module IDs                                                                                     */
139 /* ---------------------------------------------------------------------------------------------- */
140 
141 /**
142  * The zycore generic module id.
143  */
144 #define ZYAN_MODULE_ZYCORE      0x001u
145 
146 /**
147  * The zycore arg-parse submodule id.
148  */
149 #define ZYAN_MODULE_ARGPARSE    0x003u
150 
151 /**
152  * The base module id for user-defined status codes.
153  */
154 #define ZYAN_MODULE_USER        0x3FFu
155 
156 /* ---------------------------------------------------------------------------------------------- */
157 /* Status codes (general purpose)                                                                 */
158 /* ---------------------------------------------------------------------------------------------- */
159 
160 /**
161  * The operation completed successfully.
162  */
163 #define ZYAN_STATUS_SUCCESS \
164     ZYAN_MAKE_STATUS(0u, ZYAN_MODULE_ZYCORE, 0x00u)
165 
166 /**
167  * The operation failed with an generic error.
168  */
169 #define ZYAN_STATUS_FAILED \
170     ZYAN_MAKE_STATUS(1u, ZYAN_MODULE_ZYCORE, 0x01u)
171 
172 /**
173  * The operation completed successfully and returned `ZYAN_TRUE`.
174  */
175 #define ZYAN_STATUS_TRUE \
176     ZYAN_MAKE_STATUS(0u, ZYAN_MODULE_ZYCORE, 0x02u)
177 
178 /**
179  * The operation completed successfully and returned `ZYAN_FALSE`.
180  */
181 #define ZYAN_STATUS_FALSE \
182     ZYAN_MAKE_STATUS(0u, ZYAN_MODULE_ZYCORE, 0x03u)
183 
184 /**
185  * An invalid argument was passed to a function.
186  */
187 #define ZYAN_STATUS_INVALID_ARGUMENT \
188     ZYAN_MAKE_STATUS(1u, ZYAN_MODULE_ZYCORE, 0x04u)
189 
190 /**
191  * An attempt was made to perform an invalid operation.
192  */
193 #define ZYAN_STATUS_INVALID_OPERATION \
194     ZYAN_MAKE_STATUS(1u, ZYAN_MODULE_ZYCORE, 0x05u)
195 
196 /**
197  * Insufficient privileges to perform the requested operation.
198  */
199 #define ZYAN_STATUS_ACCESS_DENIED \
200     ZYAN_MAKE_STATUS(1u, ZYAN_MODULE_ZYCORE, 0x06u)
201 
202 /**
203  * The requested entity was not found.
204  */
205 #define ZYAN_STATUS_NOT_FOUND \
206     ZYAN_MAKE_STATUS(1u, ZYAN_MODULE_ZYCORE, 0x07u)
207 
208 /**
209  * An index passed to a function was out of bounds.
210  */
211 #define ZYAN_STATUS_OUT_OF_RANGE \
212     ZYAN_MAKE_STATUS(1u, ZYAN_MODULE_ZYCORE, 0x08u)
213 
214 /**
215  * A buffer passed to a function was too small to complete the requested operation.
216  */
217 #define ZYAN_STATUS_INSUFFICIENT_BUFFER_SIZE \
218     ZYAN_MAKE_STATUS(1u, ZYAN_MODULE_ZYCORE, 0x09u)
219 
220 /**
221  * Insufficient memory to perform the operation.
222  */
223 #define ZYAN_STATUS_NOT_ENOUGH_MEMORY \
224     ZYAN_MAKE_STATUS(1u, ZYAN_MODULE_ZYCORE, 0x0Au)
225 
226 /**
227  * An unknown error occurred during a system function call.
228  */
229 #define ZYAN_STATUS_BAD_SYSTEMCALL \
230     ZYAN_MAKE_STATUS(1u, ZYAN_MODULE_ZYCORE, 0x0Bu)
231 
232 /**
233  * The process ran out of resources while performing an operation.
234  */
235 #define ZYAN_STATUS_OUT_OF_RESOURCES \
236     ZYAN_MAKE_STATUS(1u, ZYAN_MODULE_ZYCORE, 0x0Cu)
237 
238 /**
239  * A dependency library was not found or does have an unexpected version number or
240  * feature-set.
241  */
242 #define ZYAN_STATUS_MISSING_DEPENDENCY \
243     ZYAN_MAKE_STATUS(1u, ZYAN_MODULE_ZYCORE, 0x0Du)
244 
245 /* ---------------------------------------------------------------------------------------------- */
246 /* Status codes (arg parse)                                                                       */
247 /* ---------------------------------------------------------------------------------------------- */
248 
249 /**
250  * Argument was not expected.
251  */
252 #define ZYAN_STATUS_ARG_NOT_UNDERSTOOD \
253     ZYAN_MAKE_STATUS(1u, ZYAN_MODULE_ARGPARSE, 0x00u)
254 
255 /**
256  * Too few arguments were provided.
257  */
258 #define ZYAN_STATUS_TOO_FEW_ARGS \
259     ZYAN_MAKE_STATUS(1u, ZYAN_MODULE_ARGPARSE, 0x01u)
260 
261 /**
262  * Too many arguments were provided.
263  */
264 #define ZYAN_STATUS_TOO_MANY_ARGS \
265     ZYAN_MAKE_STATUS(1u, ZYAN_MODULE_ARGPARSE, 0x02u)
266 
267 /**
268  * An argument that expected a value misses its value.
269  */
270 #define ZYAN_STATUS_ARG_MISSES_VALUE \
271     ZYAN_MAKE_STATUS(1u, ZYAN_MODULE_ARGPARSE, 0x03u)
272 
273 /**
274 * A required argument is missing.
275 */
276 #define ZYAN_STATUS_REQUIRED_ARG_MISSING \
277     ZYAN_MAKE_STATUS(1u, ZYAN_MODULE_ARGPARSE, 0x04u)
278 
279 /* ---------------------------------------------------------------------------------------------- */
280 
281 /* ============================================================================================== */
282 
283 #ifdef __cplusplus
284 }
285 #endif
286 
287 #endif /* ZYCORE_STATUS_H */
288