codegen: support hacked ABI if using pointer compression
[ajla.git] / error.h
blobeec2e0eb0607639d6fbdba2dde77ae6bdaab22ca
1 /*
2 * Copyright (C) 2024 Mikulas Patocka
4 * This file is part of Ajla.
6 * Ajla is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU General Public License as published by the Free Software
8 * Foundation, either version 3 of the License, or (at your option) any later
9 * version.
11 * Ajla is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along with
16 * Ajla. If not, see <https://www.gnu.org/licenses/>.
19 #ifndef AJLA_ERROR_H
20 #define AJLA_ERROR_H
22 #ifdef EINTR
23 #define EINTR_LOOP_VAL(ret_, err_, call_) \
24 do { \
25 (ret_) = (call_); \
26 } while (unlikely((ret_) == (err_)) && unlikely(errno == EINTR))
27 #else
28 #define EINTR_LOOP_VAL(ret_, err_, call_) \
29 do { \
30 (ret_) = (call_); \
31 } while (0)
32 #endif
34 #define EINTR_LOOP(ret_, call_) EINTR_LOOP_VAL(ret_, -1, call_)
37 typedef struct {
38 short error_class;
39 short error_type;
40 int error_aux;
41 #if defined(DEBUG_ERROR) && defined(DEBUG_TRACK_FILE_LINE)
42 char position[20];
43 #endif
44 } ajla_error_t;
46 const char *error_decode(ajla_error_t);
47 void trace_v(const char *, va_list);
48 void trace(const char *, ...) attr_printf(1, 2);
49 void stderr_msg_v(const char *m, va_list l);
50 void stderr_msg(const char *, ...) attr_printf(1, 2);
51 void debug_v(const char *, va_list);
52 void debug(const char *, ...) attr_printf(1, 2);
53 void warning_v(const char *, va_list);
54 void warning(const char *, ...) attr_printf(1, 2);
55 attr_noreturn fatal_v(const char *, va_list);
56 attr_noreturn attr_printf(1, 2) fatal(const char *, ...);
57 attr_noreturn internal_v(const char *, const char *, va_list);
58 attr_noreturn attr_printf(2, 3) internal(const char *, const char *, ...);
60 #define MEM_DONT_TRY_TO_FREE ((ajla_error_t *)SPECIAL_POINTER_1)
62 void fatal_mayfail(ajla_error_t, ajla_error_t *, const char *, ...) attr_printf(3, 4);
63 void fatal_warning_mayfail(ajla_error_t, ajla_error_t *, const char *, ...) attr_printf(3, 4);
65 static inline ajla_error_t error_ajla_aux_(int ec, int type, int aux argument_position)
67 ajla_error_t e;
68 if (unlikely(type < AJLA_ERROR_BASE) ||
69 unlikely(type >= AJLA_ERROR_N))
70 internal(file_line, "invalid ajla error type %d", type);
71 e.error_class = ec;
72 e.error_type = type;
73 e.error_aux = aux;
74 #if defined(DEBUG_ERROR) && defined(DEBUG_TRACK_FILE_LINE)
75 strncpy(e.position, position_arg, sizeof e.position - 1);
76 e.position[sizeof e.position - 1] = 0;
77 #endif
78 return e;
81 #define error_ajla_aux(ec, type, aux) error_ajla_aux_(ec, type, aux pass_file_line)
83 #define error_ajla_system(ec, aux) error_ajla_aux_(ec, AJLA_ERROR_SYSTEM, aux pass_file_line)
85 #define error_ajla(ec, type) error_ajla_aux_(ec, type, 0 pass_file_line)
87 ajla_error_t error_from_errno(int ec, int errn);
89 void error_init_multithreaded(void);
90 void error_done_multithreaded(void);
91 void error_init(void);
92 void error_done(void);
94 #endif