-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathgmem.h
203 lines (165 loc) · 4.71 KB
/
gmem.h
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
/*
* gmem.h
*
* Memory routines with out-of-memory checking.
*
* Copyright 1996-2003 Glyph & Cog, LLC
*/
//========================================================================
//
// Modified under the Poppler project - http://poppler.freedesktop.org
//
// All changes made under the Poppler project to this file are licensed
// under GPL version 2 or later
//
// Copyright (C) 2005 Takashi Iwai <[email protected]>
// Copyright (C) 2007-2010, 2017, 2019, 2022 Albert Astals Cid <[email protected]>
// Copyright (C) 2008 Jonathan Kew <[email protected]>
// Copyright (C) 2018 Adam Reichold <[email protected]>
// Copyright (C) 2021 Even Rouault <[email protected]>
//
// To see a description of the changes please see the Changelog file that
// came with your tarball or type make ChangeLog if you are building from git
//
//========================================================================
#ifndef GMEM_H
#define GMEM_H
#include <cassert>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include "GooCheckedOps.h"
/// Same as malloc, but prints error message and exits if malloc() returns NULL.
inline void *gmalloc(size_t size, bool checkoverflow = false)
{
if (size == 0) {
return nullptr;
}
if (void *p = std::malloc(size)) {
return p;
}
std::fputs("Out of memory\n", stderr);
if (checkoverflow) {
return nullptr;
}
std::abort();
}
inline void *gmalloc_checkoverflow(size_t size)
{
return gmalloc(size, true);
}
/// Same as free
inline void gfree(void *p)
{
std::free(p);
}
/// Same as realloc, but prints error message and exits if realloc() returns NULL.
/// If <p> is NULL, calls malloc() instead of realloc().
inline void *grealloc(void *p, size_t size, bool checkoverflow = false)
{
if (size == 0) {
gfree(p);
return nullptr;
}
if (void *q = p ? std::realloc(p, size) : std::malloc(size)) {
return q;
}
std::fputs("Out of memory\n", stderr);
if (checkoverflow) {
return nullptr;
}
std::abort();
}
inline void *grealloc_checkoverflow(void *p, size_t size)
{
return grealloc(p, size, true);
}
/*
* These are similar to gmalloc and grealloc, but take an object count
* and size. The result is similar to allocating <count> * <size>
* bytes, but there is an additional error check that the total size
* doesn't overflow an int.
* The gmallocn_checkoverflow variant returns NULL instead of exiting
* the application if a overflow is detected.
*/
inline void *gmallocn(int count, int size, bool checkoverflow = false)
{
if (count == 0) {
return nullptr;
}
int bytes;
if (count < 0 || size <= 0 || checkedMultiply(count, size, &bytes)) {
std::fputs("Bogus memory allocation size\n", stderr);
if (checkoverflow) {
return nullptr;
}
std::abort();
}
return gmalloc(bytes, checkoverflow);
}
inline void *gmallocn_checkoverflow(int count, int size)
{
return gmallocn(count, size, true);
}
inline void *gmallocn3(int width, int height, int size, bool checkoverflow = false)
{
if (width == 0 || height == 0) {
return nullptr;
}
int count;
int bytes;
if (width < 0 || height < 0 || size <= 0 || checkedMultiply(width, height, &count) || checkedMultiply(count, size, &bytes)) {
std::fputs("Bogus memory allocation size\n", stderr);
if (checkoverflow) {
return nullptr;
}
std::abort();
}
return gmalloc(bytes, checkoverflow);
}
inline void *greallocn(void *p, int count, int size, bool checkoverflow = false, bool free_p = true)
{
if (count == 0) {
if (free_p) {
gfree(p);
}
return nullptr;
}
int bytes;
if (count < 0 || size <= 0 || checkedMultiply(count, size, &bytes)) {
std::fputs("Bogus memory allocation size\n", stderr);
if (checkoverflow) {
if (free_p) {
gfree(p);
}
return nullptr;
}
std::abort();
}
assert(bytes > 0);
if (void *q = grealloc(p, bytes, checkoverflow)) {
return q;
}
if (free_p) {
gfree(p);
}
return nullptr;
}
inline void *greallocn_checkoverflow(void *p, int count, int size)
{
return greallocn(p, count, size, true);
}
/// Allocate memory and copy a string into it.
inline char *copyString(const char *s)
{
char *r = static_cast<char *>(gmalloc(std::strlen(s) + 1, false));
return std::strcpy(r, s);
}
/// Allocate memory and copy a limited-length string to it.
inline char *copyString(const char *s, size_t n)
{
char *r = static_cast<char *>(gmalloc(n + 1, false));
r[n] = '\0';
return std::strncpy(r, s, n);
}
#endif // GMEM_H