summaryrefslogtreecommitdiff
path: root/fitz/base_memory.c
blob: 43ec0dd9ba0df92da1a9f28704a38394a4df05bd (plain)
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
#include "fitz.h"

void *
fz_malloc(fz_context *ctx, unsigned int size)
{
	void *p = ctx->alloc->malloc(ctx->alloc->user, size);
	if (p == NULL)
		fz_throw(ctx, "malloc of %d bytes failed", size);
	return p;
}

void *
fz_malloc_no_throw(fz_context *ctx, unsigned int size)
{
	return ctx->alloc->malloc(ctx->alloc->user, size);
}

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)
		fz_throw(ctx, "malloc of array (%d x %d bytes) failed (integer overflow)", count, size);

	p = ctx->alloc->malloc(ctx->alloc->user, count * size);
	if (p == NULL)
		fz_throw(ctx, "malloc of array (%d x %d bytes) failed", count, size);
	return p;
}

void *
fz_malloc_array_no_throw(fz_context *ctx, unsigned int count, unsigned int size)
{
	if (count == 0 || size == 0)
		return 0;

	if (count > UINT_MAX / size)
	{
		fprintf(stderr, "error: malloc of array (%d x %d bytes) failed (integer overflow)", count, size);
		return NULL;
	}

	return ctx->alloc->malloc(ctx->alloc->user, count * size);
}

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)
	{
		fz_throw(ctx, "calloc (%d x %d bytes) failed (integer overflow)", count, size);
	}

	p = ctx->alloc->malloc(ctx->alloc->user, count * size);
	if (p == NULL)
	{
		fz_throw(ctx, "calloc (%d x %d bytes) failed", count, size);
	}
	memset(p, 0, count*size);
	return p;
}

void *
fz_calloc_no_throw(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, "error: calloc (%d x %d bytes) failed (integer overflow)\n", count, size);
		return NULL;
	}

	p = ctx->alloc->malloc(ctx->alloc->user, count * size);
	if (p != NULL)
	{
		memset(p, 0, count*size);
	}
	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)
		fz_throw(ctx, "resize array (%d x %d bytes) failed (integer overflow)", count, size);

	np = ctx->alloc->realloc(ctx->alloc->user, p, count * size);
	if (np == NULL)
		fz_throw(ctx, "resize array (%d x %d bytes) failed", count, size);
	return np;
}

void *
fz_resize_array_no_throw(fz_context *ctx, void *p, unsigned int count, unsigned int size)
{
	if (count == 0 || size == 0)
	{
		fz_free(ctx, p);
		return 0;
	}

	if (count > UINT_MAX / size)
	{
		fprintf(stderr, "error: resize array (%d x %d bytes) failed (integer overflow)\n", count, size);
		return NULL;
	}

	return ctx->alloc->realloc(ctx->alloc->user, p, count * size);
}

void
fz_free(fz_context *ctx, void *p)
{
	ctx->alloc->free(ctx->alloc->user, 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;
}

char *
fz_strdup_no_throw(fz_context *ctx, char *s)
{
	int len = strlen(s) + 1;
	char *ns = fz_malloc_no_throw(ctx, len);
	if (ns != NULL)
		memcpy(ns, s, len);
	return ns;
}

static void *
fz_malloc_default(void *opaque, unsigned int size)
{
	return malloc(size);
}

static void *
fz_realloc_default(void *opaque, void *old, unsigned int size)
{
	return realloc(old, size);
}

static void
fz_free_default(void *opaque, void *ptr)
{
	free(ptr);
}

fz_alloc_context fz_alloc_default =
{
	NULL,
	fz_malloc_default,
	fz_realloc_default,
	fz_free_default
} ;