From patchwork Wed Aug 21 14:40:58 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 1150895 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=sourceware.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=libc-alpha-return-104649-incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b="ui4e2p4S"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 46D9MV1g3sz9sBF for ; Thu, 22 Aug 2019 00:41:09 +1000 (AEST) 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:mime-version :content-type; q=dns; s=default; b=VXj5QZiMOs19bcZZo70QUjg7LNJ/y JehpgFoRJjjTsjH4ccT/mBhfGNicTdFyrowsThvVM/qG3/cVVfCz4ORsh0khM75y 0d+N2rfV6k5GIdw4KwgZbqTk/UszKsLR7+Y6HQeaT2zPVAVFsMah5/oZN6FRdlTq Qb+Y03uKNRBtEE= 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:mime-version :content-type; s=default; bh=irtNh4a5ApydIMDgFtlq3D/Sh8A=; b=ui4 e2p4SnTPfEUIbHP4lkO58QL/Ps82T2t5RMF/8P69VJ8sijPn9yE40Ek2IbulZAic pG8TS4BsmJY0RT5pfSh5ezK6aQEWXR3jKaWx1iPPAJwAocC/jD2VA0GmmN1ixN9h PVQTbLUgeDOl+lK3kDrWZ20m0Epb3VKihAmy2saE= Received: (qmail 48716 invoked by alias); 21 Aug 2019 14:41:03 -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 48707 invoked by uid 89); 21 Aug 2019 14:41:03 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-18.7 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, SPF_HELO_PASS autolearn=ham version=3.3.1 spammy=HX-Languages-Length:1943 X-HELO: mx1.redhat.com From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH] misc: Use allocate_once in getmntent Date: Wed, 21 Aug 2019 16:40:58 +0200 Message-ID: <8736huh98l.fsf@oldenburg2.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.2 (gnu/linux) MIME-Version: 1.0 Both the buffer and struct mntent are now allocated on the heap. This results in a slight reduction of RSS usage. 2019-08-21 Florian Weimer * misc/mntent.c (struct mntent_buffer): Define. (mntent_buffer): Adjust type to void *. (allocate): Adjust for allocate_once. (deallocate): New function. (getmntent): Call allocate_once. Reviewed-by: Adhemerval Zanella diff --git a/misc/mntent.c b/misc/mntent.c index 980ea40967..8fae6064c6 100644 --- a/misc/mntent.c +++ b/misc/mntent.c @@ -18,36 +18,41 @@ #include #include -#include +#include + +struct mntent_buffer +{ + struct mntent m; + char buffer[4096]; +}; /* We don't want to allocate the static buffer all the time since it - is not always used (in fact, rather infrequently). Accept the - extra cost of a `malloc'. */ -libc_freeres_ptr (static char *getmntent_buffer); - -/* This is the size of the buffer. This is really big. */ -#define BUFFER_SIZE 4096 + is not always used (in fact, rather infrequently). */ +libc_freeres_ptr (static void *mntent_buffer); +static void * +allocate (void *closure) +{ + return malloc (sizeof (struct mntent_buffer)); +} static void -allocate (void) +deallocate (void *closure, void *ptr) { - getmntent_buffer = (char *) malloc (BUFFER_SIZE); + free (ptr); } - struct mntent * getmntent (FILE *stream) { - static struct mntent m; - __libc_once_define (static, once); - __libc_once (once, allocate); - - if (getmntent_buffer == NULL) + struct mntent_buffer *buffer = allocate_once (&mntent_buffer, + allocate, deallocate, NULL); + if (buffer == NULL) /* If no core is available we don't have a chance to run the program successfully and so returning NULL is an acceptable result. */ return NULL; - return __getmntent_r (stream, &m, getmntent_buffer, BUFFER_SIZE); + return __getmntent_r (stream, &buffer->m, + buffer->buffer, sizeof (buffer->buffer)); }