From: Adhemerval Zanella <adhemerval.zanella@linaro.org>
To: libc-alpha@sourceware.org
Subject: [PATCH] libio: Add small optimization on fmemopen
Date: Wed, 21 Sep 2016 14:06:00 -0000 [thread overview]
Message-ID: <1474466758-26544-1-git-send-email-adhemerval.zanella@linaro.org> (raw)
This patch uses C99 variable-lenght array on internal fmemopen cookie
allocation to avoid to use two mallocs for the case if an internal
buffer must be allocated. No functional changes are done.
Tested on x86_64 and i686.
* libio/fmemopen.c (fmemopen_cookie_t): Remove mybuffer and add
variable-length array member.
(fmemopen_close): Do no free the internal buffer.
(__fmemopen): Use C99 variable-length array to allocate both the
fmemopen cookie and internal buffer (for null arguments).
---
libio/fmemopen.c | 40 ++++++++++++++++------------------------
2 files changed, 24 insertions(+), 24 deletions(-)
diff --git a/libio/fmemopen.c b/libio/fmemopen.c
index 0f65590..d7df1be 100644
--- a/libio/fmemopen.c
+++ b/libio/fmemopen.c
@@ -35,11 +35,11 @@ typedef struct fmemopen_cookie_struct fmemopen_cookie_t;
struct fmemopen_cookie_struct
{
char *buffer; /* memory buffer. */
- int mybuffer; /* allocated my buffer? */
int append; /* buffer open for append? */
size_t size; /* buffer length in bytes. */
_IO_off64_t pos; /* current position at the buffer. */
size_t maxpos; /* max position in buffer. */
+ char data[];
};
@@ -136,11 +136,7 @@ fmemopen_seek (void *cookie, _IO_off64_t *p, int w)
static int
fmemopen_close (void *cookie)
{
- fmemopen_cookie_t *c = (fmemopen_cookie_t *) cookie;
-
- if (c->mybuffer)
- free (c->buffer);
- free (c);
+ free (cookie);
return 0;
}
@@ -153,23 +149,24 @@ __fmemopen (void *buf, size_t len, const char *mode)
fmemopen_cookie_t *c;
FILE *result;
- c = (fmemopen_cookie_t *) calloc (sizeof (fmemopen_cookie_t), 1);
- if (c == NULL)
- return NULL;
-
- c->mybuffer = (buf == NULL);
-
- if (c->mybuffer)
+ size_t clen = sizeof (fmemopen_cookie_t);
+ if (buf == NULL)
{
- c->buffer = (char *) malloc (len);
- if (c->buffer == NULL)
+ if (__glibc_unlikely (len >= (SIZE_MAX - clen)))
{
- free (c);
+ __set_errno (ENOMEM);
return NULL;
}
- c->buffer[0] = '\0';
+ clen += len;
}
- else
+
+ c = (fmemopen_cookie_t *) calloc (clen, 1);
+ if (c == NULL)
+ return NULL;
+
+ c->buffer = c->data;
+
+ if (buf != NULL)
{
if (__glibc_unlikely ((uintptr_t) len > -(uintptr_t) buf))
{
@@ -214,12 +211,7 @@ __fmemopen (void *buf, size_t len, const char *mode)
result = _IO_fopencookie (c, mode, iof);
if (__glibc_unlikely (result == NULL))
- {
- if (c->mybuffer)
- free (c->buffer);
-
- free (c);
- }
+ free (c);
return result;
}
--
2.7.4
next reply other threads:[~2016-09-21 14:06 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-09-21 14:06 Adhemerval Zanella [this message]
2016-09-21 14:57 ` Florian Weimer
2016-09-21 18:18 ` Adhemerval Zanella
2016-09-21 17:22 ` Paul Eggert
2016-09-21 18:17 ` Adhemerval Zanella
2016-09-21 18:23 ` Florian Weimer
2016-09-21 18:26 ` Adhemerval Zanella
2016-09-21 18:43 ` Paul Eggert
2016-09-21 18:47 ` Florian Weimer
2016-09-21 18:57 ` Paul Eggert
2016-09-21 19:27 ` Florian Weimer
2016-09-21 20:16 ` Adhemerval Zanella
2016-09-21 20:42 ` Paul Eggert
2016-09-23 5:17 ` Ondřej Bílka
2016-09-23 5:34 ` Florian Weimer
2016-09-23 18:14 ` Ondřej Bílka
2016-09-23 20:38 ` Florian Weimer
2016-09-23 18:29 ` Paul Eggert
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1474466758-26544-1-git-send-email-adhemerval.zanella@linaro.org \
--to=adhemerval.zanella@linaro.org \
--cc=libc-alpha@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).