xref: /freebsd/sys/contrib/openzfs/module/zstd/lib/common/error_private.h (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-only
2 /*
3  * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
4  * All rights reserved.
5  *
6  * This source code is licensed under both the BSD-style license (found in the
7  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
8  * in the COPYING file in the root directory of this source tree).
9  * You may select, at your option, one of the above-listed licenses.
10  */
11 
12 /* Note : this module is expected to remain private, do not expose it */
13 
14 #ifndef ERROR_H_MODULE
15 #define ERROR_H_MODULE
16 
17 #if defined (__cplusplus)
18 extern "C" {
19 #endif
20 
21 
22 /* ****************************************
23 *  Dependencies
24 ******************************************/
25 #include <stddef.h>        /* size_t */
26 #include "zstd_errors.h"  /* enum list */
27 
28 
29 /* ****************************************
30 *  Compiler-specific
31 ******************************************/
32 #if defined(__GNUC__)
33 #  define ERR_STATIC static __attribute__((unused))
34 #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
35 #  define ERR_STATIC static inline
36 #elif defined(_MSC_VER)
37 #  define ERR_STATIC static __inline
38 #else
39 #  define ERR_STATIC static  /* this version may generate warnings for unused static functions; disable the relevant warning */
40 #endif
41 
42 
43 /*-****************************************
44 *  Customization (error_public.h)
45 ******************************************/
46 typedef ZSTD_ErrorCode ERR_enum;
47 #define PREFIX(name) ZSTD_error_##name
48 
49 
50 /*-****************************************
51 *  Error codes handling
52 ******************************************/
53 #undef ERROR   /* already defined on Visual Studio */
54 #define ERROR(name) ZSTD_ERROR(name)
55 #define ZSTD_ERROR(name) ((size_t)-PREFIX(name))
56 
ERR_isError(size_t code)57 ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); }
58 
ERR_getErrorCode(size_t code)59 ERR_STATIC ERR_enum ERR_getErrorCode(size_t code) { if (!ERR_isError(code)) return (ERR_enum)0; return (ERR_enum) (0-code); }
60 
61 /* check and forward error code */
62 #define CHECK_V_F(e, f) size_t const e = f; if (ERR_isError(e)) return e
63 #define CHECK_F(f)   { CHECK_V_F(_var_err__, f); }
64 
65 
66 /*-****************************************
67 *  Error Strings
68 ******************************************/
69 
70 const char* ERR_getErrorString(ERR_enum code);   /* error_private.c */
71 
ERR_getErrorName(size_t code)72 ERR_STATIC const char* ERR_getErrorName(size_t code)
73 {
74     return ERR_getErrorString(ERR_getErrorCode(code));
75 }
76 
77 #if defined (__cplusplus)
78 }
79 #endif
80 
81 #endif /* ERROR_H_MODULE */
82