82
errorx(1, _("internal error, tried to emalloc(0)"));
82
errorx2(1, _("internal error, tried to emalloc(0)"));
84
84
if ((ptr = malloc(size)) == NULL)
85
errorx(1, _("unable to allocate memory"));
85
errorx2(1, _("unable to allocate memory"));
98
98
if (nmemb == 0 || size == 0)
99
errorx(1, _("internal error, tried to emalloc2(0)"));
99
errorx2(1, _("internal error, tried to emalloc2(0)"));
100
100
if (nmemb > SIZE_MAX / size)
101
errorx(1, _("internal error, emalloc2() overflow"));
101
errorx2(1, _("internal error, emalloc2() overflow"));
104
104
if ((ptr = malloc(size)) == NULL)
105
errorx(1, _("unable to allocate memory"));
105
errorx2(1, _("unable to allocate memory"));
110
* ecalloc() allocates nmemb * size bytes and exits with an error
111
* if overflow would occur or if the system malloc(3) fails.
112
* On success, the allocated space is zero-filled.
115
ecalloc(size_t nmemb, size_t size)
119
if (nmemb == 0 || size == 0)
120
errorx2(1, _("internal error, tried to ecalloc(0)"));
122
if (nmemb > SIZE_MAX / size)
123
errorx2(1, _("internal error, ecalloc() overflow"));
126
if ((ptr = malloc(size)) == NULL)
127
errorx2(1, _("unable to allocate memory"));
128
memset(ptr, 0, size);
119
errorx(1, _("internal error, tried to erealloc(0)"));
142
errorx2(1, _("internal error, tried to erealloc(0)"));
121
144
ptr = ptr ? realloc(ptr, size) : malloc(size);
123
errorx(1, _("unable to allocate memory"));
146
errorx2(1, _("unable to allocate memory"));
137
160
if (nmemb == 0 || size == 0)
138
errorx(1, _("internal error, tried to erealloc3(0)"));
161
errorx2(1, _("internal error, tried to erealloc3(0)"));
139
162
if (nmemb > SIZE_MAX / size)
140
errorx(1, _("internal error, erealloc3() overflow"));
163
errorx2(1, _("internal error, erealloc3() overflow"));
143
166
ptr = ptr ? realloc(ptr, size) : malloc(size);
145
errorx(1, _("unable to allocate memory"));
168
errorx2(1, _("unable to allocate memory"));
174
* erecalloc() realloc(3)s nmemb * msize bytes and exits with an error
175
* if overflow would occur or if the system malloc(3)/realloc(3) fails.
176
* On success, the new space is zero-filled. You can call ereallocz()
177
* with a NULL pointer even if the system realloc(3) does not support this.
180
erecalloc(void *ptr, size_t onmemb, size_t nmemb, size_t msize)
184
if (nmemb == 0 || msize == 0)
185
errorx2(1, _("internal error, tried to erealloc3(0)"));
186
if (nmemb > SIZE_MAX / msize)
187
errorx2(1, _("internal error, erealloc3() overflow"));
189
size = nmemb * msize;
190
ptr = ptr ? realloc(ptr, size) : malloc(size);
192
errorx2(1, _("unable to allocate memory"));
193
if (nmemb > onmemb) {
194
size = (nmemb - onmemb) * msize;
195
memset((char *)ptr + (onmemb * msize), 0, size);
150
202
* estrdup() is like strdup(3) except that it exits with an error if
173
225
estrndup(const char *src, size_t maxlen)
175
227
char *dst = NULL;
178
230
if (src != NULL) {
231
while (maxlen != 0 && src[len] != '\0') {
182
235
dst = (char *) emalloc(len + 1);
183
236
(void) memcpy(dst, src, len);
216
269
if ((len = vasprintf(ret, format, args)) == -1)
217
errorx(1, _("unable to allocate memory"));
270
errorx2(1, _("unable to allocate memory"));