2 * Copyright (c) Yann Collet, Facebook, Inc.
5 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
8 * You may select, at your option, one of the above-listed licenses.
11 /* Note : this module is expected to remain private, do not expose it */
13 #ifndef ERROR_H_MODULE
14 #define ERROR_H_MODULE
18 /* ****************************************
20 ******************************************/
21 #include <linux/zstd_errors.h> /* enum list */
24 #include "zstd_deps.h" /* size_t */
27 /* ****************************************
29 ******************************************/
30 #define ERR_STATIC static __attribute__((unused))
33 /*-****************************************
34 * Customization (error_public.h)
35 ******************************************/
36 typedef ZSTD_ErrorCode ERR_enum
;
37 #define PREFIX(name) ZSTD_error_##name
40 /*-****************************************
41 * Error codes handling
42 ******************************************/
43 #undef ERROR /* already defined on Visual Studio */
44 #define ERROR(name) ZSTD_ERROR(name)
45 #define ZSTD_ERROR(name) ((size_t)-PREFIX(name))
47 ERR_STATIC
unsigned ERR_isError(size_t code
) { return (code
> ERROR(maxCode
)); }
49 ERR_STATIC ERR_enum
ERR_getErrorCode(size_t code
) { if (!ERR_isError(code
)) return (ERR_enum
)0; return (ERR_enum
) (0-code
); }
51 /* check and forward error code */
52 #define CHECK_V_F(e, f) size_t const e = f; if (ERR_isError(e)) return e
53 #define CHECK_F(f) { CHECK_V_F(_var_err__, f); }
56 /*-****************************************
58 ******************************************/
60 const char* ERR_getErrorString(ERR_enum code
); /* error_private.c */
62 ERR_STATIC
const char* ERR_getErrorName(size_t code
)
64 return ERR_getErrorString(ERR_getErrorCode(code
));
68 * Ignore: this is an internal helper.
70 * This is a helper function to help force C99-correctness during compilation.
71 * Under strict compilation modes, variadic macro arguments can't be empty.
72 * However, variadic function arguments can be. Using a function therefore lets
73 * us statically check that at least one (string) argument was passed,
74 * independent of the compilation flags.
76 static INLINE_KEYWORD UNUSED_ATTR
77 void _force_has_format_string(const char *format
, ...) {
82 * Ignore: this is an internal helper.
84 * We want to force this function invocation to be syntactically correct, but
85 * we don't want to force runtime evaluation of its arguments.
87 #define _FORCE_HAS_FORMAT_STRING(...) \
89 _force_has_format_string(__VA_ARGS__); \
92 #define ERR_QUOTE(str) #str
95 * Return the specified error if the condition evaluates to true.
97 * In debug modes, prints additional information.
98 * In order to do that (particularly, printing the conditional that failed),
99 * this can't just wrap RETURN_ERROR().
101 #define RETURN_ERROR_IF(cond, err, ...) \
103 RAWLOG(3, "%s:%d: ERROR!: check %s failed, returning %s", \
104 __FILE__, __LINE__, ERR_QUOTE(cond), ERR_QUOTE(ERROR(err))); \
105 _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
106 RAWLOG(3, ": " __VA_ARGS__); \
112 * Unconditionally return the specified error.
114 * In debug modes, prints additional information.
116 #define RETURN_ERROR(err, ...) \
118 RAWLOG(3, "%s:%d: ERROR!: unconditional check failed, returning %s", \
119 __FILE__, __LINE__, ERR_QUOTE(ERROR(err))); \
120 _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
121 RAWLOG(3, ": " __VA_ARGS__); \
127 * If the provided expression evaluates to an error code, returns that error code.
129 * In debug modes, prints additional information.
131 #define FORWARD_IF_ERROR(err, ...) \
133 size_t const err_code = (err); \
134 if (ERR_isError(err_code)) { \
135 RAWLOG(3, "%s:%d: ERROR!: forwarding error in %s: %s", \
136 __FILE__, __LINE__, ERR_QUOTE(err), ERR_getErrorName(err_code)); \
137 _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
138 RAWLOG(3, ": " __VA_ARGS__); \
145 #endif /* ERROR_H_MODULE */