1
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
58
59
60
61
62
63
64
65
66
67
68
70
71
72
73
74
75
76
81
82
83
86
87
94
95
104
105
113
114
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* ... */
/* ... */
#include "argtable3.h"
#ifndef ARG_AMALGAMATION
#include "argtable3_private.h"
#endif
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void panic(const char* fmt, ...);
static arg_panicfn* s_panic = panic;
void dbg_printf(const char* fmt, ...) {
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
}{ ... }
static void panic(const char* fmt, ...) {
va_list args;
char* s;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable : 4996)/* ... */
#endif
s = getenv("EF_DUMPCORE");
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
if (s != NULL && *s != '\0') {
abort();
}{...} else {
exit(EXIT_FAILURE);
}{...}
}{ ... }
void arg_set_panic(arg_panicfn* proc) {
s_panic = proc;
}{ ... }
void* xmalloc(size_t size) {
void* ret = malloc(size);
if (!ret) {
s_panic("Out of memory!\n");
}{...}
return ret;
}{ ... }
void* xcalloc(size_t count, size_t size) {
size_t allocated_count = count && size ? count : 1;
size_t allocated_size = count && size ? size : 1;
void* ret = calloc(allocated_count, allocated_size);
if (!ret) {
s_panic("Out of memory!\n");
}{...}
return ret;
}{ ... }
void* xrealloc(void* ptr, size_t size) {
size_t allocated_size = size ? size : 1;
void* ret = realloc(ptr, allocated_size);
if (!ret) {
s_panic("Out of memory!\n");
}{...}
return ret;
}{ ... }
void xfree(void* ptr) {
free(ptr);
}{ ... }
static void merge(void* data, int esize, int i, int j, int k, arg_comparefn* comparefn) {
char* a = (char*)data;
char* m;
int ipos, jpos, mpos;
ipos = i;
jpos = j + 1;
mpos = 0;
m = (char*)xmalloc((size_t)(esize * ((k - i) + 1)));
while (ipos <= j || jpos <= k) {
if (ipos > j) {
while (jpos <= k) {
memcpy(&m[mpos * esize], &a[jpos * esize], (size_t)esize);
jpos++;
mpos++;
}{...}
continue;
}{...} else if (jpos > k) {
while (ipos <= j) {
memcpy(&m[mpos * esize], &a[ipos * esize], (size_t)esize);
ipos++;
mpos++;
}{...}
continue;
}{...}
if (comparefn(&a[ipos * esize], &a[jpos * esize]) < 0) {
memcpy(&m[mpos * esize], &a[ipos * esize], (size_t)esize);
ipos++;
mpos++;
}{...} else {
memcpy(&m[mpos * esize], &a[jpos * esize], (size_t)esize);
jpos++;
mpos++;
}{...}
}{...}
memcpy(&a[i * esize], m, (size_t)(esize * ((k - i) + 1)));
xfree(m);
}{ ... }
void arg_mgsort(void* data, int size, int esize, int i, int k, arg_comparefn* comparefn) {
int j;
if (i < k) {
j = (int)(((i + k - 1)) / 2);
arg_mgsort(data, size, esize, i, j, comparefn);
arg_mgsort(data, size, esize, j + 1, k, comparefn);
merge(data, esize, i, j, k, comparefn);
}{...}
}{ ... }