-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.c
More file actions
216 lines (199 loc) · 6.13 KB
/
error.c
File metadata and controls
216 lines (199 loc) · 6.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "@/public/error.h"
#include "@/public/refcount.h"
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
u7_error u7_error_acquire(u7_error self) {
if (self.payload) {
u7_refcount_increment(&self.payload->refcount);
}
return self;
}
void u7_error_release(u7_error self) {
// Avoid reccursion.
struct u7_error_payload* payload = (struct u7_error_payload*)self.payload;
while (payload && !u7_refcount_skewed_decrement(&payload->refcount)) {
struct u7_error_payload* const cause_payload =
(struct u7_error_payload*)payload->cause.payload;
if (payload->dispose_fn != NULL) {
payload->dispose_fn(payload);
}
payload = cause_payload;
}
}
void u7_error_clear(u7_error* self) {
u7_error_release(*self);
self->error_code = 0;
self->payload = NULL;
}
// Returns an error object with given category.
u7_error u7_verrorf_with_cause(struct u7_error_category const* category,
int error_code, u7_error cause,
const char* format, va_list arg) {
const int kN = 128;
u7_error result;
int message_length = -1;
char* message = NULL;
{ // Sanity check.
assert(error_code != 0);
if (error_code == 0) {
u7_error_release(cause);
return u7_ok();
}
}
{
// Fast case: format=""
if (format[0] == '\0') {
goto ret;
}
}
{
// Fast case: format="%s"
if (format[0] == '%' && format[1] == 's' && format[2] == '\0') {
const char* tmp = va_arg(arg, const char*);
message_length = strlen(tmp);
message = malloc(message_length + 1);
memcpy(message, tmp, message_length + 1);
goto ret;
}
}
{
// Fast case: format="%.*s"
if (format[0] == '%' && format[1] == '.' && format[2] == '*' &&
format[3] == 's' && format[4] == '\0') {
message_length = va_arg(arg, int);
message = malloc(message_length + 1);
memcpy(message, va_arg(arg, const char*), message_length);
message[message_length] = '\0';
goto ret;
}
}
{
// Short message.
message = (char*)malloc(kN);
if (message == NULL) {
goto fail;
}
va_list arg_copy;
va_copy(arg_copy, arg);
int n = vsnprintf(message, kN, format, arg_copy);
va_end(arg_copy);
assert(n >= 0);
if (n < 0) {
goto fail;
}
message_length = n;
if (message_length < kN) {
goto ret;
}
}
{
// Long message.
char* tmp = (char*)realloc(message, message_length + 1);
if (!tmp) {
goto fail;
}
message = tmp;
int n = vsnprintf(message, message_length + 1, format, arg);
assert(n == message_length);
if (n != message_length) {
goto fail;
}
goto ret;
}
fail:
free(message);
message = NULL;
message_length = -1;
ret:
result.error_code = error_code;
result.payload =
category->make_payload_fn(category, message, message_length, cause);
return result;
}
u7_error u7_errorf_with_cause(struct u7_error_category const* category,
int error_code, u7_error cause,
const char* format, ...) {
va_list arg;
va_start(arg, format);
u7_error error =
u7_verrorf_with_cause(category, error_code, cause, format, arg);
va_end(arg);
return error;
}
u7_error u7_verrorf(struct u7_error_category const* category, int error_code,
const char* format, va_list arg) {
return u7_verrorf_with_cause(category, error_code, u7_ok(), format, arg);
}
u7_error u7_errorf(struct u7_error_category const* category, int error_code,
const char* format, ...) {
va_list arg;
va_start(arg, format);
u7_error error =
u7_verrorf_with_cause(category, error_code, u7_ok(), format, arg);
va_end(arg);
return error;
}
static struct u7_error_payload const* u7_errno_category_make_payload_fn(
struct u7_error_category const* self, char* message, int message_length,
u7_error cause);
static struct u7_error_category u7_errno_category_static_category = {
.name = "Errno",
.make_payload_fn = &u7_errno_category_make_payload_fn,
};
static const char u7_errno_category_static_fallback_message[] =
"<u7_error:fail>";
static void u7_errno_category_payload_dispose_fn(
struct u7_error_payload* self) {
assert(self->category == &u7_errno_category_static_category);
free((char*)self->message);
free(self);
}
static struct u7_error_payload u7_errno_category_static_fallback_payload = {
.refcount = U7_REFCOUNT_INIT,
.dispose_fn = NULL,
.category = &u7_errno_category_static_category,
.message = u7_errno_category_static_fallback_message,
.message_length = sizeof(u7_errno_category_static_fallback_message) - 1,
.cause = {.error_code = 0, .payload = NULL},
};
static struct u7_error_payload const* u7_errno_category_make_payload_fn(
struct u7_error_category const* self, char* message, int message_length,
u7_error cause) {
(void)self;
assert(self == &u7_errno_category_static_category);
if (message_length < 0) {
free(message);
u7_error_release(cause);
u7_refcount_increment(&u7_errno_category_static_fallback_payload.refcount);
return &u7_errno_category_static_fallback_payload;
}
struct u7_error_payload* result =
(struct u7_error_payload*)malloc(sizeof(struct u7_error_payload));
if (result == NULL) {
free(message);
u7_error_release(cause);
u7_refcount_increment(&u7_errno_category_static_fallback_payload.refcount);
return &u7_errno_category_static_fallback_payload;
}
u7_refcount_init(&result->refcount);
result->dispose_fn = &u7_errno_category_payload_dispose_fn;
result->category = &u7_errno_category_static_category;
result->message = message;
result->message_length = message_length;
result->cause = cause;
return result;
}
struct u7_error_category const* u7_errno_category(void) {
return &u7_errno_category_static_category;
}
u7_error u7_errnof(int errno_code, const char* format, ...) {
va_list arg;
va_start(arg, format);
u7_error error = u7_verrorf_with_cause(u7_errno_category(), errno_code,
u7_ok(), format, arg);
va_end(arg);
return error;
}