blob: 886d2757df487be5add413f37928554345bfa3ae [file] [log] [blame]
Brandon Maierdbe88da2023-01-12 10:27:45 -06001/*
2 * Copyright (c) Yann Collet, Facebook, Inc.
3 * All rights reserved.
4 *
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.
9 */
10
11/* Note : this module is expected to remain private, do not expose it */
12
13#ifndef ERROR_H_MODULE
14#define ERROR_H_MODULE
15
Brandon Maierdbe88da2023-01-12 10:27:45 -060016/* ****************************************
17* Dependencies
18******************************************/
19#include <linux/zstd_errors.h> /* enum list */
20#include "compiler.h"
21#include "debug.h"
22#include "zstd_deps.h" /* size_t */
23
Brandon Maierdbe88da2023-01-12 10:27:45 -060024/* ****************************************
25* Compiler-specific
26******************************************/
27#define ERR_STATIC static __attribute__((unused))
28
Brandon Maierdbe88da2023-01-12 10:27:45 -060029/*-****************************************
30* Customization (error_public.h)
31******************************************/
32typedef ZSTD_ErrorCode ERR_enum;
33#define PREFIX(name) ZSTD_error_##name
34
Brandon Maierdbe88da2023-01-12 10:27:45 -060035/*-****************************************
36* Error codes handling
37******************************************/
38#undef ERROR /* already defined on Visual Studio */
39#define ERROR(name) ZSTD_ERROR(name)
40#define ZSTD_ERROR(name) ((size_t)-PREFIX(name))
41
42ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); }
43
44ERR_STATIC ERR_enum ERR_getErrorCode(size_t code) { if (!ERR_isError(code)) return (ERR_enum)0; return (ERR_enum) (0-code); }
45
46/* check and forward error code */
47#define CHECK_V_F(e, f) size_t const e = f; if (ERR_isError(e)) return e
48#define CHECK_F(f) { CHECK_V_F(_var_err__, f); }
49
Brandon Maierdbe88da2023-01-12 10:27:45 -060050/*-****************************************
51* Error Strings
52******************************************/
53
54const char* ERR_getErrorString(ERR_enum code); /* error_private.c */
55
56ERR_STATIC const char* ERR_getErrorName(size_t code)
57{
58 return ERR_getErrorString(ERR_getErrorCode(code));
59}
60
61/*
62 * Ignore: this is an internal helper.
63 *
64 * This is a helper function to help force C99-correctness during compilation.
65 * Under strict compilation modes, variadic macro arguments can't be empty.
66 * However, variadic function arguments can be. Using a function therefore lets
67 * us statically check that at least one (string) argument was passed,
68 * independent of the compilation flags.
69 */
70static INLINE_KEYWORD UNUSED_ATTR
71void _force_has_format_string(const char *format, ...) {
72 (void)format;
73}
74
75/*
76 * Ignore: this is an internal helper.
77 *
78 * We want to force this function invocation to be syntactically correct, but
79 * we don't want to force runtime evaluation of its arguments.
80 */
81#define _FORCE_HAS_FORMAT_STRING(...) \
82 if (0) { \
83 _force_has_format_string(__VA_ARGS__); \
84 }
85
86#define ERR_QUOTE(str) #str
87
88/*
89 * Return the specified error if the condition evaluates to true.
90 *
91 * In debug modes, prints additional information.
92 * In order to do that (particularly, printing the conditional that failed),
93 * this can't just wrap RETURN_ERROR().
94 */
95#define RETURN_ERROR_IF(cond, err, ...) \
96 if (cond) { \
97 RAWLOG(3, "%s:%d: ERROR!: check %s failed, returning %s", \
98 __FILE__, __LINE__, ERR_QUOTE(cond), ERR_QUOTE(ERROR(err))); \
99 _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
100 RAWLOG(3, ": " __VA_ARGS__); \
101 RAWLOG(3, "\n"); \
102 return ERROR(err); \
103 }
104
105/*
106 * Unconditionally return the specified error.
107 *
108 * In debug modes, prints additional information.
109 */
110#define RETURN_ERROR(err, ...) \
111 do { \
112 RAWLOG(3, "%s:%d: ERROR!: unconditional check failed, returning %s", \
113 __FILE__, __LINE__, ERR_QUOTE(ERROR(err))); \
114 _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
115 RAWLOG(3, ": " __VA_ARGS__); \
116 RAWLOG(3, "\n"); \
117 return ERROR(err); \
118 } while(0);
119
120/*
121 * If the provided expression evaluates to an error code, returns that error code.
122 *
123 * In debug modes, prints additional information.
124 */
125#define FORWARD_IF_ERROR(err, ...) \
126 do { \
127 size_t const err_code = (err); \
128 if (ERR_isError(err_code)) { \
129 RAWLOG(3, "%s:%d: ERROR!: forwarding error in %s: %s", \
130 __FILE__, __LINE__, ERR_QUOTE(err), ERR_getErrorName(err_code)); \
131 _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
132 RAWLOG(3, ": " __VA_ARGS__); \
133 RAWLOG(3, "\n"); \
134 return err_code; \
135 } \
136 } while(0);
137
Brandon Maierdbe88da2023-01-12 10:27:45 -0600138#endif /* ERROR_H_MODULE */