2017-08-27 12:05:17 +02:00
|
|
|
/*
|
2023-05-22 14:32:14 +02:00
|
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
2017-06-09 03:43:56 +02:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2017-08-27 12:05:17 +02:00
|
|
|
* This source code is licensed under both the BSD-style license (found in the
|
|
|
|
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
|
|
|
|
* in the COPYING file in the root directory of this source tree).
|
2017-10-26 22:41:47 +02:00
|
|
|
* You may select, at your option, one of the above-listed licenses.
|
2017-06-09 03:43:56 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Note : this module is expected to remain private, do not expose it */
|
|
|
|
|
|
|
|
#ifndef ERROR_H_MODULE
|
|
|
|
#define ERROR_H_MODULE
|
|
|
|
|
|
|
|
#if defined (__cplusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/* ****************************************
|
|
|
|
* Dependencies
|
|
|
|
******************************************/
|
2021-11-19 12:54:45 +01:00
|
|
|
#include "../zstd_errors.h" /* enum list */
|
2022-01-24 11:04:45 +01:00
|
|
|
#include "compiler.h"
|
|
|
|
#include "debug.h"
|
2021-11-19 12:54:45 +01:00
|
|
|
#include "zstd_deps.h" /* size_t */
|
2017-06-09 03:43:56 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* ****************************************
|
|
|
|
* Compiler-specific
|
|
|
|
******************************************/
|
|
|
|
#if defined(__GNUC__)
|
|
|
|
# define ERR_STATIC static __attribute__((unused))
|
|
|
|
#elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
|
|
|
|
# define ERR_STATIC static inline
|
|
|
|
#elif defined(_MSC_VER)
|
|
|
|
# define ERR_STATIC static __inline
|
|
|
|
#else
|
|
|
|
# define ERR_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/*-****************************************
|
|
|
|
* Customization (error_public.h)
|
|
|
|
******************************************/
|
|
|
|
typedef ZSTD_ErrorCode ERR_enum;
|
|
|
|
#define PREFIX(name) ZSTD_error_##name
|
|
|
|
|
|
|
|
|
|
|
|
/*-****************************************
|
|
|
|
* Error codes handling
|
|
|
|
******************************************/
|
2020-09-18 21:38:36 +02:00
|
|
|
#undef ERROR /* already defined on Visual Studio */
|
2017-10-26 22:41:47 +02:00
|
|
|
#define ERROR(name) ZSTD_ERROR(name)
|
|
|
|
#define ZSTD_ERROR(name) ((size_t)-PREFIX(name))
|
2017-06-09 03:43:56 +02:00
|
|
|
|
|
|
|
ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); }
|
|
|
|
|
|
|
|
ERR_STATIC ERR_enum ERR_getErrorCode(size_t code) { if (!ERR_isError(code)) return (ERR_enum)0; return (ERR_enum) (0-code); }
|
|
|
|
|
2020-09-18 21:38:36 +02:00
|
|
|
/* check and forward error code */
|
|
|
|
#define CHECK_V_F(e, f) size_t const e = f; if (ERR_isError(e)) return e
|
|
|
|
#define CHECK_F(f) { CHECK_V_F(_var_err__, f); }
|
|
|
|
|
2017-06-09 03:43:56 +02:00
|
|
|
|
|
|
|
/*-****************************************
|
|
|
|
* Error Strings
|
|
|
|
******************************************/
|
|
|
|
|
|
|
|
const char* ERR_getErrorString(ERR_enum code); /* error_private.c */
|
|
|
|
|
|
|
|
ERR_STATIC const char* ERR_getErrorName(size_t code)
|
|
|
|
{
|
|
|
|
return ERR_getErrorString(ERR_getErrorCode(code));
|
|
|
|
}
|
|
|
|
|
2022-01-24 11:04:45 +01:00
|
|
|
/**
|
|
|
|
* Ignore: this is an internal helper.
|
|
|
|
*
|
|
|
|
* This is a helper function to help force C99-correctness during compilation.
|
|
|
|
* Under strict compilation modes, variadic macro arguments can't be empty.
|
|
|
|
* However, variadic function arguments can be. Using a function therefore lets
|
|
|
|
* us statically check that at least one (string) argument was passed,
|
|
|
|
* independent of the compilation flags.
|
|
|
|
*/
|
|
|
|
static INLINE_KEYWORD UNUSED_ATTR
|
|
|
|
void _force_has_format_string(const char *format, ...) {
|
|
|
|
(void)format;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ignore: this is an internal helper.
|
|
|
|
*
|
|
|
|
* We want to force this function invocation to be syntactically correct, but
|
|
|
|
* we don't want to force runtime evaluation of its arguments.
|
|
|
|
*/
|
|
|
|
#define _FORCE_HAS_FORMAT_STRING(...) \
|
|
|
|
if (0) { \
|
|
|
|
_force_has_format_string(__VA_ARGS__); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define ERR_QUOTE(str) #str
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the specified error if the condition evaluates to true.
|
|
|
|
*
|
|
|
|
* In debug modes, prints additional information.
|
|
|
|
* In order to do that (particularly, printing the conditional that failed),
|
|
|
|
* this can't just wrap RETURN_ERROR().
|
|
|
|
*/
|
|
|
|
#define RETURN_ERROR_IF(cond, err, ...) \
|
|
|
|
if (cond) { \
|
|
|
|
RAWLOG(3, "%s:%d: ERROR!: check %s failed, returning %s", \
|
|
|
|
__FILE__, __LINE__, ERR_QUOTE(cond), ERR_QUOTE(ERROR(err))); \
|
|
|
|
_FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
|
|
|
|
RAWLOG(3, ": " __VA_ARGS__); \
|
|
|
|
RAWLOG(3, "\n"); \
|
|
|
|
return ERROR(err); \
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unconditionally return the specified error.
|
|
|
|
*
|
|
|
|
* In debug modes, prints additional information.
|
|
|
|
*/
|
|
|
|
#define RETURN_ERROR(err, ...) \
|
|
|
|
do { \
|
|
|
|
RAWLOG(3, "%s:%d: ERROR!: unconditional check failed, returning %s", \
|
|
|
|
__FILE__, __LINE__, ERR_QUOTE(ERROR(err))); \
|
|
|
|
_FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
|
|
|
|
RAWLOG(3, ": " __VA_ARGS__); \
|
|
|
|
RAWLOG(3, "\n"); \
|
|
|
|
return ERROR(err); \
|
|
|
|
} while(0);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the provided expression evaluates to an error code, returns that error code.
|
|
|
|
*
|
|
|
|
* In debug modes, prints additional information.
|
|
|
|
*/
|
|
|
|
#define FORWARD_IF_ERROR(err, ...) \
|
|
|
|
do { \
|
|
|
|
size_t const err_code = (err); \
|
|
|
|
if (ERR_isError(err_code)) { \
|
|
|
|
RAWLOG(3, "%s:%d: ERROR!: forwarding error in %s: %s", \
|
|
|
|
__FILE__, __LINE__, ERR_QUOTE(err), ERR_getErrorName(err_code)); \
|
|
|
|
_FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
|
|
|
|
RAWLOG(3, ": " __VA_ARGS__); \
|
|
|
|
RAWLOG(3, "\n"); \
|
|
|
|
return err_code; \
|
|
|
|
} \
|
|
|
|
} while(0);
|
|
|
|
|
2017-06-09 03:43:56 +02:00
|
|
|
#if defined (__cplusplus)
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* ERROR_H_MODULE */
|