From patchwork Wed Sep 21 14:05:58 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 15843 Received: (qmail 56630 invoked by alias); 21 Sep 2016 14:06:16 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 56554 invoked by uid 89); 21 Sep 2016 14:06:16 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=__set_errno, 3511 X-HELO: mail-yw0-f179.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:subject:date:message-id; bh=zNkEtj7lpfuouyF6aZSrJ1D/dxFbl9ivXqsqprlmmNw=; b=I+x8UO/ZapEaAxXT5HmOURB9vt1E8p/E3NrzZPbRYojKx0IU1tJawaKMgH1pJJMUrj 7h4FIRe3+tyokoitVfhPfJNtsVdlFDI0ejIdTFppBVVEjJBx3oLgRs+QryzBhINuInhV 35jPyaSrpTiWJOLdQL4EkQQcOco58sgX+QI9xUKc1WUwer4+kh4mrDQJbJ5IOcN04hfF Q9pWKftebQwFI1Zd/qKTYY847cg7F+gB8QZb/a75371HXC2DeMro3hfFwXhb2cthvUKW 4MejjWhUK26a79XX6w82dZVQ4GorO4IS9SaiGitkw22pJ+OpmCIRNAQvoFi4cw86amhX /XDw== X-Gm-Message-State: AE9vXwOWEsoO5RRVNquOoxlL5FTThZq7Hco3xv/DJO3Tfd8GP6ohNq4L58gbn0ZRcWZ7qTuX X-Received: by 10.13.229.1 with SMTP id o1mr34849078ywe.95.1474466763988; Wed, 21 Sep 2016 07:06:03 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH] libio: Add small optimization on fmemopen Date: Wed, 21 Sep 2016 11:05:58 -0300 Message-Id: <1474466758-26544-1-git-send-email-adhemerval.zanella@linaro.org> 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; }