Commit d74fd3c7 by Janne Blomqvist

Introduce xrealloc, use it.

2014-05-26  Janne Blomqvist  <jb@gcc.gnu.org>

	* libgfortran.h (xrealloc): New prototype.
	* runtime/memory.c (xrealloc): New function.
	* io/fbuf.c (fbuf_alloc): Use xrealloc.
	* io/list_read.c (push_char_default): Likewise.
	(push_char4): Likewise.

From-SVN: r210948
parent b4fb1c21
2014-05-26 Janne Blomqvist <jb@gcc.gnu.org> 2014-05-26 Janne Blomqvist <jb@gcc.gnu.org>
* libgfortran.h (xrealloc): New prototype.
* runtime/memory.c (xrealloc): New function.
* io/fbuf.c (fbuf_alloc): Use xrealloc.
* io/list_read.c (push_char_default): Likewise.
(push_char4): Likewise.
2014-05-26 Janne Blomqvist <jb@gcc.gnu.org>
PR libfortran/61310 PR libfortran/61310
* intrinsics/ctime.c (strctime): Rename to gf_ctime, use snprintf * intrinsics/ctime.c (strctime): Rename to gf_ctime, use snprintf
instead of strftime. instead of strftime.
......
...@@ -121,10 +121,7 @@ fbuf_alloc (gfc_unit * u, int len) ...@@ -121,10 +121,7 @@ fbuf_alloc (gfc_unit * u, int len)
{ {
/* Round up to nearest multiple of the current buffer length. */ /* Round up to nearest multiple of the current buffer length. */
newlen = ((u->fbuf->pos + len) / u->fbuf->len + 1) * u->fbuf->len; newlen = ((u->fbuf->pos + len) / u->fbuf->len + 1) * u->fbuf->len;
dest = realloc (u->fbuf->buf, newlen); u->fbuf->buf = xrealloc (u->fbuf->buf, newlen);
if (dest == NULL)
return NULL;
u->fbuf->buf = dest;
u->fbuf->len = newlen; u->fbuf->len = newlen;
} }
......
...@@ -79,7 +79,7 @@ typedef unsigned char uchar; ...@@ -79,7 +79,7 @@ typedef unsigned char uchar;
static void static void
push_char_default (st_parameter_dt *dtp, int c) push_char_default (st_parameter_dt *dtp, int c)
{ {
char *new;
if (dtp->u.p.saved_string == NULL) if (dtp->u.p.saved_string == NULL)
{ {
...@@ -92,13 +92,11 @@ push_char_default (st_parameter_dt *dtp, int c) ...@@ -92,13 +92,11 @@ push_char_default (st_parameter_dt *dtp, int c)
if (dtp->u.p.saved_used >= dtp->u.p.saved_length) if (dtp->u.p.saved_used >= dtp->u.p.saved_length)
{ {
dtp->u.p.saved_length = 2 * dtp->u.p.saved_length; dtp->u.p.saved_length = 2 * dtp->u.p.saved_length;
new = realloc (dtp->u.p.saved_string, dtp->u.p.saved_length); dtp->u.p.saved_string =
if (new == NULL) xrealloc (dtp->u.p.saved_string, dtp->u.p.saved_length);
generate_error (&dtp->common, LIBERROR_OS, NULL);
dtp->u.p.saved_string = new;
// Also this should not be necessary. // Also this should not be necessary.
memset (new + dtp->u.p.saved_used, 0, memset (dtp->u.p.saved_string + dtp->u.p.saved_used, 0,
dtp->u.p.saved_length - dtp->u.p.saved_used); dtp->u.p.saved_length - dtp->u.p.saved_used);
} }
...@@ -126,10 +124,7 @@ push_char4 (st_parameter_dt *dtp, int c) ...@@ -126,10 +124,7 @@ push_char4 (st_parameter_dt *dtp, int c)
if (dtp->u.p.saved_used >= dtp->u.p.saved_length) if (dtp->u.p.saved_used >= dtp->u.p.saved_length)
{ {
dtp->u.p.saved_length = 2 * dtp->u.p.saved_length; dtp->u.p.saved_length = 2 * dtp->u.p.saved_length;
new = realloc (p, dtp->u.p.saved_length * sizeof (gfc_char4_t)); p = xrealloc (p, dtp->u.p.saved_length * sizeof (gfc_char4_t));
if (new == NULL)
generate_error (&dtp->common, LIBERROR_OS, NULL);
p = new;
memset4 (new + dtp->u.p.saved_used, 0, memset4 (new + dtp->u.p.saved_used, 0,
dtp->u.p.saved_length - dtp->u.p.saved_used); dtp->u.p.saved_length - dtp->u.p.saved_used);
......
...@@ -771,6 +771,8 @@ internal_proto(xmalloc); ...@@ -771,6 +771,8 @@ internal_proto(xmalloc);
extern void *xcalloc (size_t, size_t) __attribute__ ((malloc)); extern void *xcalloc (size_t, size_t) __attribute__ ((malloc));
internal_proto(xcalloc); internal_proto(xcalloc);
extern void *xrealloc (void *, size_t);
internal_proto(xrealloc);
/* environ.c */ /* environ.c */
......
...@@ -58,3 +58,17 @@ xcalloc (size_t nmemb, size_t size) ...@@ -58,3 +58,17 @@ xcalloc (size_t nmemb, size_t size)
return p; return p;
} }
void *
xrealloc (void *ptr, size_t size)
{
if (size == 0)
size = 1;
void *newp = realloc (ptr, size);
if (!newp)
os_error ("Memory allocation failure in xrealloc");
return newp;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment