blob: 9dcc8595123cd70c8e3a7808ab36da8402141d08 [file] [log] [blame]
Yann Collet32fb4072017-08-18 16:52:05 -07001/*
W. Felix Handte5d693cc2022-12-20 12:49:47 -05002 * Copyright (c) Meta Platforms, Inc. and affiliates.
Yann Collet4ded9e52016-08-30 10:04:33 -07003 * All rights reserved.
4 *
Yann Collet32fb4072017-08-18 16:52:05 -07005 * 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).
Yann Collet3128e032017-09-08 00:09:23 -07008 * You may select, at your option, one of the above-listed licenses.
Yann Collet4ded9e52016-08-30 10:04:33 -07009 */
Yann Collet977f1f32016-01-21 15:38:47 +010010
Yann Collet977f1f32016-01-21 15:38:47 +010011/* Note : this module is expected to remain private, do not expose it */
12
13#ifndef ERROR_H_MODULE
14#define ERROR_H_MODULE
15
Yann Collet72bff502016-02-03 12:06:24 +010016/* ****************************************
17* Dependencies
Yann Collet977f1f32016-01-21 15:38:47 +010018******************************************/
Nick Terrell09149be2021-04-30 15:02:12 -070019#include "../zstd_errors.h" /* enum list */
Nick Terrella5f2c452021-09-17 11:43:04 -070020#include "compiler.h"
21#include "debug.h"
Nick Terrell09149be2021-04-30 15:02:12 -070022#include "zstd_deps.h" /* size_t */
Yann Collet977f1f32016-01-21 15:38:47 +010023
Yann Collet72bff502016-02-03 12:06:24 +010024/* ****************************************
Yann Collet977f1f32016-01-21 15:38:47 +010025* Compiler-specific
26******************************************/
27#if defined(__GNUC__)
28# define ERR_STATIC static __attribute__((unused))
29#elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
30# define ERR_STATIC static inline
31#elif defined(_MSC_VER)
32# define ERR_STATIC static __inline
33#else
34# define ERR_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
35#endif
36
37
Yann Collet72bff502016-02-03 12:06:24 +010038/*-****************************************
Yann Colletde406ee2016-03-20 15:46:10 +010039* Customization (error_public.h)
Yann Collet977f1f32016-01-21 15:38:47 +010040******************************************/
Yann Collet982ffc72016-02-05 02:33:10 +010041typedef ZSTD_ErrorCode ERR_enum;
Yann Collet977f1f32016-01-21 15:38:47 +010042#define PREFIX(name) ZSTD_error_##name
43
Yann Collet72bff502016-02-03 12:06:24 +010044
45/*-****************************************
46* Error codes handling
47******************************************/
Yann Collet20bd2462020-05-11 19:29:36 -070048#undef ERROR /* already defined on Visual Studio */
Nick Terrell7c365eb2017-08-25 17:44:32 -070049#define ERROR(name) ZSTD_ERROR(name)
50#define ZSTD_ERROR(name) ((size_t)-PREFIX(name))
Yann Collet977f1f32016-01-21 15:38:47 +010051
52ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); }
53
Yann Colletc75e4c22016-05-10 17:47:11 +020054ERR_STATIC ERR_enum ERR_getErrorCode(size_t code) { if (!ERR_isError(code)) return (ERR_enum)0; return (ERR_enum) (0-code); }
Yann Collet977f1f32016-01-21 15:38:47 +010055
Carl Woffendenedd9a072020-04-07 11:02:06 +020056/* check and forward error code */
Nick Terrell81932502023-11-21 13:26:25 -080057#define CHECK_V_F(e, f) \
58 size_t const e = f; \
59 do { \
60 if (ERR_isError(e)) \
61 return e; \
62 } while (0)
63#define CHECK_F(f) do { CHECK_V_F(_var_err__, f); } while (0)
Carl Woffendenedd9a072020-04-07 11:02:06 +020064
Yann Collet72bff502016-02-03 12:06:24 +010065
66/*-****************************************
Yann Collet977f1f32016-01-21 15:38:47 +010067* Error Strings
68******************************************/
69
Yann Colletef2357d2016-10-11 17:24:50 -070070const char* ERR_getErrorString(ERR_enum code); /* error_private.c */
Yann Collet977f1f32016-01-21 15:38:47 +010071
Yann Colletc75e4c22016-05-10 17:47:11 +020072ERR_STATIC const char* ERR_getErrorName(size_t code)
73{
74 return ERR_getErrorString(ERR_getErrorCode(code));
75}
Yann Collet977f1f32016-01-21 15:38:47 +010076
Nick Terrella5f2c452021-09-17 11:43:04 -070077/**
78 * Ignore: this is an internal helper.
79 *
80 * This is a helper function to help force C99-correctness during compilation.
81 * Under strict compilation modes, variadic macro arguments can't be empty.
82 * However, variadic function arguments can be. Using a function therefore lets
83 * us statically check that at least one (string) argument was passed,
84 * independent of the compilation flags.
85 */
86static INLINE_KEYWORD UNUSED_ATTR
87void _force_has_format_string(const char *format, ...) {
88 (void)format;
89}
90
91/**
92 * Ignore: this is an internal helper.
93 *
94 * We want to force this function invocation to be syntactically correct, but
95 * we don't want to force runtime evaluation of its arguments.
96 */
Nick Terrell81932502023-11-21 13:26:25 -080097#define _FORCE_HAS_FORMAT_STRING(...) \
98 do { \
99 if (0) { \
100 _force_has_format_string(__VA_ARGS__); \
101 } \
102 } while (0)
Nick Terrella5f2c452021-09-17 11:43:04 -0700103
104#define ERR_QUOTE(str) #str
105
106/**
107 * Return the specified error if the condition evaluates to true.
108 *
109 * In debug modes, prints additional information.
110 * In order to do that (particularly, printing the conditional that failed),
111 * this can't just wrap RETURN_ERROR().
112 */
Nick Terrell81932502023-11-21 13:26:25 -0800113#define RETURN_ERROR_IF(cond, err, ...) \
114 do { \
115 if (cond) { \
116 RAWLOG(3, "%s:%d: ERROR!: check %s failed, returning %s", \
117 __FILE__, __LINE__, ERR_QUOTE(cond), ERR_QUOTE(ERROR(err))); \
118 _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
119 RAWLOG(3, ": " __VA_ARGS__); \
120 RAWLOG(3, "\n"); \
121 return ERROR(err); \
122 } \
123 } while (0)
Nick Terrella5f2c452021-09-17 11:43:04 -0700124
125/**
126 * Unconditionally return the specified error.
127 *
128 * In debug modes, prints additional information.
129 */
Nick Terrell81932502023-11-21 13:26:25 -0800130#define RETURN_ERROR(err, ...) \
131 do { \
132 RAWLOG(3, "%s:%d: ERROR!: unconditional check failed, returning %s", \
133 __FILE__, __LINE__, ERR_QUOTE(ERROR(err))); \
134 _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
135 RAWLOG(3, ": " __VA_ARGS__); \
136 RAWLOG(3, "\n"); \
137 return ERROR(err); \
138 } while(0)
Nick Terrella5f2c452021-09-17 11:43:04 -0700139
140/**
141 * If the provided expression evaluates to an error code, returns that error code.
142 *
143 * In debug modes, prints additional information.
144 */
Nick Terrell81932502023-11-21 13:26:25 -0800145#define FORWARD_IF_ERROR(err, ...) \
146 do { \
147 size_t const err_code = (err); \
148 if (ERR_isError(err_code)) { \
149 RAWLOG(3, "%s:%d: ERROR!: forwarding error in %s: %s", \
150 __FILE__, __LINE__, ERR_QUOTE(err), ERR_getErrorName(err_code)); \
151 _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
152 RAWLOG(3, ": " __VA_ARGS__); \
153 RAWLOG(3, "\n"); \
154 return err_code; \
155 } \
156 } while(0)
Nick Terrella5f2c452021-09-17 11:43:04 -0700157
Yann Collet977f1f32016-01-21 15:38:47 +0100158#endif /* ERROR_H_MODULE */