version 1.1.1.1, 2012/02/21 16:23:02
|
version 1.1.1.2, 2012/05/29 12:26:49
|
Line 79 emalloc(size_t size)
|
Line 79 emalloc(size_t size)
|
void *ptr; |
void *ptr; |
|
|
if (size == 0) |
if (size == 0) |
errorx(1, _("internal error, tried to emalloc(0)")); | errorx2(1, _("internal error, tried to emalloc(0)")); |
|
|
if ((ptr = malloc(size)) == NULL) |
if ((ptr = malloc(size)) == NULL) |
errorx(1, _("unable to allocate memory")); | errorx2(1, _("unable to allocate memory")); |
return ptr; |
return ptr; |
} |
} |
|
|
Line 96 emalloc2(size_t nmemb, size_t size)
|
Line 96 emalloc2(size_t nmemb, size_t size)
|
void *ptr; |
void *ptr; |
|
|
if (nmemb == 0 || size == 0) |
if (nmemb == 0 || size == 0) |
errorx(1, _("internal error, tried to emalloc2(0)")); | errorx2(1, _("internal error, tried to emalloc2(0)")); |
if (nmemb > SIZE_MAX / size) |
if (nmemb > SIZE_MAX / size) |
errorx(1, _("internal error, emalloc2() overflow")); | errorx2(1, _("internal error, emalloc2() overflow")); |
|
|
size *= nmemb; |
size *= nmemb; |
if ((ptr = malloc(size)) == NULL) |
if ((ptr = malloc(size)) == NULL) |
errorx(1, _("unable to allocate memory")); | errorx2(1, _("unable to allocate memory")); |
return ptr; |
return ptr; |
} |
} |
|
|
/* |
/* |
|
* ecalloc() allocates nmemb * size bytes and exits with an error |
|
* if overflow would occur or if the system malloc(3) fails. |
|
* On success, the allocated space is zero-filled. |
|
*/ |
|
void * |
|
ecalloc(size_t nmemb, size_t size) |
|
{ |
|
void *ptr; |
|
|
|
if (nmemb == 0 || size == 0) |
|
errorx2(1, _("internal error, tried to ecalloc(0)")); |
|
if (nmemb != 1) { |
|
if (nmemb > SIZE_MAX / size) |
|
errorx2(1, _("internal error, ecalloc() overflow")); |
|
size *= nmemb; |
|
} |
|
if ((ptr = malloc(size)) == NULL) |
|
errorx2(1, _("unable to allocate memory")); |
|
memset(ptr, 0, size); |
|
return ptr; |
|
} |
|
|
|
/* |
* erealloc() calls the system realloc(3) and exits with an error if |
* erealloc() calls the system realloc(3) and exits with an error if |
* realloc(3) fails. You can call erealloc() with a NULL pointer even |
* realloc(3) fails. You can call erealloc() with a NULL pointer even |
* if the system realloc(3) does not support this. |
* if the system realloc(3) does not support this. |
Line 116 erealloc(void *ptr, size_t size)
|
Line 139 erealloc(void *ptr, size_t size)
|
{ |
{ |
|
|
if (size == 0) |
if (size == 0) |
errorx(1, _("internal error, tried to erealloc(0)")); | errorx2(1, _("internal error, tried to erealloc(0)")); |
|
|
ptr = ptr ? realloc(ptr, size) : malloc(size); |
ptr = ptr ? realloc(ptr, size) : malloc(size); |
if (ptr == NULL) |
if (ptr == NULL) |
errorx(1, _("unable to allocate memory")); | errorx2(1, _("unable to allocate memory")); |
return ptr; |
return ptr; |
} |
} |
|
|
Line 135 erealloc3(void *ptr, size_t nmemb, size_t size)
|
Line 158 erealloc3(void *ptr, size_t nmemb, size_t size)
|
{ |
{ |
|
|
if (nmemb == 0 || size == 0) |
if (nmemb == 0 || size == 0) |
errorx(1, _("internal error, tried to erealloc3(0)")); | errorx2(1, _("internal error, tried to erealloc3(0)")); |
if (nmemb > SIZE_MAX / size) |
if (nmemb > SIZE_MAX / size) |
errorx(1, _("internal error, erealloc3() overflow")); | errorx2(1, _("internal error, erealloc3() overflow")); |
|
|
size *= nmemb; |
size *= nmemb; |
ptr = ptr ? realloc(ptr, size) : malloc(size); |
ptr = ptr ? realloc(ptr, size) : malloc(size); |
if (ptr == NULL) |
if (ptr == NULL) |
errorx(1, _("unable to allocate memory")); | errorx2(1, _("unable to allocate memory")); |
return ptr; |
return ptr; |
} |
} |
|
|
|
#ifdef notyet |
/* |
/* |
|
* erecalloc() realloc(3)s nmemb * msize bytes and exits with an error |
|
* if overflow would occur or if the system malloc(3)/realloc(3) fails. |
|
* On success, the new space is zero-filled. You can call ereallocz() |
|
* with a NULL pointer even if the system realloc(3) does not support this. |
|
*/ |
|
void * |
|
erecalloc(void *ptr, size_t onmemb, size_t nmemb, size_t msize) |
|
{ |
|
size_t size; |
|
|
|
if (nmemb == 0 || msize == 0) |
|
errorx2(1, _("internal error, tried to erealloc3(0)")); |
|
if (nmemb > SIZE_MAX / msize) |
|
errorx2(1, _("internal error, erealloc3() overflow")); |
|
|
|
size = nmemb * msize; |
|
ptr = ptr ? realloc(ptr, size) : malloc(size); |
|
if (ptr == NULL) |
|
errorx2(1, _("unable to allocate memory")); |
|
if (nmemb > onmemb) { |
|
size = (nmemb - onmemb) * msize; |
|
memset((char *)ptr + (onmemb * msize), 0, size); |
|
} |
|
return ptr; |
|
} |
|
#endif |
|
|
|
/* |
* estrdup() is like strdup(3) except that it exits with an error if |
* estrdup() is like strdup(3) except that it exits with an error if |
* malloc(3) fails. NOTE: unlike strdup(3), estrdup(NULL) is legal. |
* malloc(3) fails. NOTE: unlike strdup(3), estrdup(NULL) is legal. |
*/ |
*/ |
Line 173 char *
|
Line 225 char *
|
estrndup(const char *src, size_t maxlen) |
estrndup(const char *src, size_t maxlen) |
{ |
{ |
char *dst = NULL; |
char *dst = NULL; |
size_t len; | size_t len = 0; |
|
|
if (src != NULL) { |
if (src != NULL) { |
len = strlen(src); | while (maxlen != 0 && src[len] != '\0') { |
if (len > maxlen) | len++; |
len = maxlen; | maxlen--; |
| } |
dst = (char *) emalloc(len + 1); |
dst = (char *) emalloc(len + 1); |
(void) memcpy(dst, src, len); |
(void) memcpy(dst, src, len); |
dst[len] = '\0'; |
dst[len] = '\0'; |
Line 200 easprintf(char **ret, const char *fmt, ...)
|
Line 253 easprintf(char **ret, const char *fmt, ...)
|
va_end(ap); |
va_end(ap); |
|
|
if (len == -1) |
if (len == -1) |
errorx(1, _("unable to allocate memory")); | errorx2(1, _("unable to allocate memory")); |
return len; |
return len; |
} |
} |
|
|
Line 214 evasprintf(char **ret, const char *format, va_list arg
|
Line 267 evasprintf(char **ret, const char *format, va_list arg
|
int len; |
int len; |
|
|
if ((len = vasprintf(ret, format, args)) == -1) |
if ((len = vasprintf(ret, format, args)) == -1) |
errorx(1, _("unable to allocate memory")); | errorx2(1, _("unable to allocate memory")); |
return len; |
return len; |
} |
} |
|
|