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 Netto X-Patchwork-Id: 76689 Delivered-To: patch@linaro.org Received: by 10.140.106.72 with SMTP id d66csp2069627qgf; Wed, 21 Sep 2016 07:06:37 -0700 (PDT) X-Received: by 10.67.15.5 with SMTP id fk5mr40719556pad.81.1474466797620; Wed, 21 Sep 2016 07:06:37 -0700 (PDT) Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id ho3si40514218pac.170.2016.09.21.07.06.37 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 21 Sep 2016 07:06:37 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-73395-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-73395-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-73395-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id; q=dns; s= default; b=IIGAy8X6ona1WL4NDG+ojxbwcPQquLX19jA7sUqa5U5xOQF8Y1fPa 8CitV7vtxe6gAAffGJK4ilO3eBqntuHDLM5tHWAI645Fa8LMhgvtLASJzLFHC4aQ npyXlklwYhQwFu8YzWXTjUNP0ZzPGtqil3joWjXn0+0oAauiD/PODc= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id; s=default; bh=aEAFb0tSqdNC1Dtjj1Wf7KX+TWw=; b=mWu27nh9fqqRfOW+/TpY5nRD8ko3 OVeHmKa6NVMC5py1Wsyd2BtVgdgKPRiAsjbm9a/qGIy6jewKUizg6NuS492UGw3B 0OXaOswUzFG4PjxzAhtoJZzdKgO5CZtCl3fncCk1fAFexeu5cHcrjeVHAR57PxXa tsVJ9S66iXM7i00= 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(-) -- 2.7.4 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; }