Project Alice
Loading...
Searching...
No Matches
error_private.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
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
16#if defined (__cplusplus)
17extern "C" {
18#endif
19
20
21/* ****************************************
22* Dependencies
23******************************************/
24#include "../zstd_errors.h" /* enum list */
25#include "compiler.h"
26#include "debug.h"
27#include "zstd_deps.h" /* size_t */
28
29
30/* ****************************************
31* Compiler-specific
32******************************************/
33#if defined(__GNUC__)
34# define ERR_STATIC static __attribute__((unused))
35#elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
36# define ERR_STATIC static inline
37#elif defined(_MSC_VER)
38# define ERR_STATIC static __inline
39#else
40# define ERR_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
41#endif
42
43
44/*-****************************************
45* Customization (error_public.h)
46******************************************/
48#define PREFIX(name) ZSTD_error_##name
49
50
51/*-****************************************
52* Error codes handling
53******************************************/
54#undef ERROR /* already defined on Visual Studio */
55#define ERROR(name) ZSTD_ERROR(name)
56#define ZSTD_ERROR(name) ((size_t)-PREFIX(name))
57
58ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); }
59
60ERR_STATIC ERR_enum ERR_getErrorCode(size_t code) { if (!ERR_isError(code)) return (ERR_enum)0; return (ERR_enum) (0-code); }
61
62/* check and forward error code */
63#define CHECK_V_F(e, f) \
64 size_t const e = f; \
65 do { \
66 if (ERR_isError(e)) \
67 return e; \
68 } while (0)
69#define CHECK_F(f) do { CHECK_V_F(_var_err__, f); } while (0)
70
71
72/*-****************************************
73* Error Strings
74******************************************/
75
76const char* ERR_getErrorString(ERR_enum code); /* error_private.c */
77
78ERR_STATIC const char* ERR_getErrorName(size_t code)
79{
81}
82
93void _force_has_format_string(const char *format, ...) {
94 (void)format;
95}
96
103#define _FORCE_HAS_FORMAT_STRING(...) \
104 do { \
105 if (0) { \
106 _force_has_format_string(__VA_ARGS__); \
107 } \
108 } while (0)
109
110#define ERR_QUOTE(str) #str
111
119#define RETURN_ERROR_IF(cond, err, ...) \
120 do { \
121 if (cond) { \
122 RAWLOG(3, "%s:%d: ERROR!: check %s failed, returning %s", \
123 __FILE__, __LINE__, ERR_QUOTE(cond), ERR_QUOTE(ERROR(err))); \
124 _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
125 RAWLOG(3, ": " __VA_ARGS__); \
126 RAWLOG(3, "\n"); \
127 return ERROR(err); \
128 } \
129 } while (0)
130
136#define RETURN_ERROR(err, ...) \
137 do { \
138 RAWLOG(3, "%s:%d: ERROR!: unconditional check failed, returning %s", \
139 __FILE__, __LINE__, ERR_QUOTE(ERROR(err))); \
140 _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
141 RAWLOG(3, ": " __VA_ARGS__); \
142 RAWLOG(3, "\n"); \
143 return ERROR(err); \
144 } while(0)
145
151#define FORWARD_IF_ERROR(err, ...) \
152 do { \
153 size_t const err_code = (err); \
154 if (ERR_isError(err_code)) { \
155 RAWLOG(3, "%s:%d: ERROR!: forwarding error in %s: %s", \
156 __FILE__, __LINE__, ERR_QUOTE(err), ERR_getErrorName(err_code)); \
157 _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
158 RAWLOG(3, ": " __VA_ARGS__); \
159 RAWLOG(3, "\n"); \
160 return err_code; \
161 } \
162 } while(0)
163
164#if defined (__cplusplus)
165}
166#endif
167
168#endif /* ERROR_H_MODULE */
#define UNUSED_ATTR
Definition: compiler.h:60
#define INLINE_KEYWORD
Definition: compiler.h:27
const char * ERR_getErrorString(ERR_enum code)
Definition: error_private.c:15
#define ERR_STATIC
Definition: error_private.h:40
ZSTD_ErrorCode ERR_enum
Definition: error_private.h:47
#define ERROR(name)
Definition: error_private.h:55
ERR_STATIC ERR_enum ERR_getErrorCode(size_t code)
Definition: error_private.h:60
ERR_STATIC unsigned ERR_isError(size_t code)
Definition: error_private.h:58
ERR_STATIC const char * ERR_getErrorName(size_t code)
Definition: error_private.h:78
ZSTD_ErrorCode
Definition: zstd_errors.h:64