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
|
#include "fitz.h"
void *
fz_malloc(fz_context *ctx, unsigned int size)
{
void *p = malloc(size);
if (!p)
{
fprintf(stderr, "fatal error: out of memory\n");
abort();
}
return p;
}
void *
fz_malloc_array(fz_context *ctx, unsigned int count, unsigned int size)
{
void *p;
if (count == 0 || size == 0)
return 0;
if (count > UINT_MAX / size)
{
fprintf(stderr, "fatal error: out of memory (integer overflow)\n");
abort();
}
p = malloc(count * size);
if (!p)
{
fprintf(stderr, "fatal error: out of memory\n");
abort();
}
return p;
}
void *
fz_calloc(fz_context *ctx, unsigned int count, unsigned int size)
{
void *p;
if (count == 0 || size == 0)
return 0;
if (count > UINT_MAX / size)
{
fprintf(stderr, "fatal error: out of memory (integer overflow)\n");
abort();
}
p = calloc(count, size);
if (!p)
{
fprintf(stderr, "fatal error: out of memory\n");
abort();
}
return p;
}
void *
fz_resize_array(fz_context *ctx, void *p, unsigned int count, unsigned int size)
{
void *np;
if (count == 0 || size == 0)
{
fz_free(ctx, p);
return 0;
}
if (count > UINT_MAX / size)
{
fprintf(stderr, "fatal error: out of memory (integer overflow)\n");
abort();
}
np = realloc(p, count * size);
if (np == NULL)
{
fprintf(stderr, "fatal error: out of memory\n");
abort();
}
return np;
}
void
fz_free(fz_context *ctx, void *p)
{
free(p);
}
char *
fz_strdup(fz_context *ctx, char *s)
{
int len = strlen(s) + 1;
char *ns = fz_malloc(ctx, len);
memcpy(ns, s, len);
return ns;
}
|