From patchwork Tue May 27 20:56:40 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?T25kxZllaiBCw61sa2E=?= X-Patchwork-Id: 353116 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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 6D1A51400A8 for ; Wed, 28 May 2014 06:56:52 +1000 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:date:from:to:cc:subject:message-id:references :mime-version:content-type:content-transfer-encoding :in-reply-to; q=dns; s=default; b=sjeUDbuho6eR03w9qKKn3C+tVtf7WL jKaYu6KIFBREUfJbIrk+0XoQ93pNJO27vf2w1XDhTHPglFHHG9jWgKujdtKnSou+ 96K2Trea/UbX9rTViUpttMbIJIR6T4w9/LssEJhv7+850m7WGIS1nWA0xqc47yUD QhzylTjntRMk4= 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:date:from:to:cc:subject:message-id:references :mime-version:content-type:content-transfer-encoding :in-reply-to; s=default; bh=BsohZTFIVK8zgU8QxcqCV5VXJc8=; b=vdJE oJT32CUzo/OovR1Uh1gRyM4cYpkSQtEz88C7kNIFFRpcoorO3TzzmkQNks3smikK 5lXclE4yyu1BxFJ4C6qRRZtZbdG4lpjnjGxUNF/uk+SNXhAnqilW/GaDmRmkjMVk ZfZCdT54lqg0d3p1F8AO+WT/Ryw7wr6NLCH4urU= Received: (qmail 29459 invoked by alias); 27 May 2014 20:56:47 -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 29446 invoked by uid 89); 27 May 2014 20:56:46 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.7 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, SPF_NEUTRAL autolearn=no version=3.3.2 X-HELO: popelka.ms.mff.cuni.cz Date: Tue, 27 May 2014 22:56:40 +0200 From: =?utf-8?B?T25kxZllaiBCw61sa2E=?= To: Andreas Schwab Cc: libc-alpha@sourceware.org Subject: [PATCH v2] Clean up check_pf allocation pattern. addresses Message-ID: <20140527205640.GB21440@domone.podge> References: <20140324165711.GA6719@domone.podge> <87k3bjzcpb.fsf@igel.home> <20140325214344.GA23074@domone.podge> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.20 (2009-06-14) On Wed, Mar 26, 2014 at 10:53:17AM +0100, Andreas Schwab wrote: > Ondřej Bílka writes: > > > diff --git a/sysdeps/unix/sysv/linux/check_pf.c b/sysdeps/unix/sysv/linux/check_pf.c > > index e6a12ed..40b1af2 100644 > > --- a/sysdeps/unix/sysv/linux/check_pf.c > > +++ b/sysdeps/unix/sysv/linux/check_pf.c > > @@ -106,6 +106,12 @@ cache_valid_p (void) > > static struct cached_data * > > make_request (int fd, pid_t pid) > > { > > + > > Extra empty line. > > > @@ -241,40 +230,36 @@ make_request (int fd, pid_t pid) > > } > > } > > > > - struct in6ailist *newp; > > - if (__libc_use_alloca (alloca_used + sizeof (*newp))) > > + if (result_len == 0 || result_len == result_cap) > > { > > - newp = alloca_account (sizeof (*newp), alloca_used); > > - newp->use_malloc = false; > > + result_cap = 2 * result_cap; > > + result = realloc (result, sizeof (*result) > > + + result_cap * sizeof (struct in6addrinfo)); > > Wrong indent. > > > + info->flags = (((ifam->ifa_flags > > & (IFA_F_DEPRECATED > > Wrong indent. > > Andreas. > > -- > Andreas Schwab, SUSE Labs, schwab@suse.de > GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE 1748 E4D4 88E3 0EEA B9D7 > "And now for something completely different." OK, here is v2 * sysdeps/unix/sysv/linux/check_pf.c (make_pf): Simplify allocation strategy. diff --git a/sysdeps/unix/sysv/linux/check_pf.c b/sysdeps/unix/sysv/linux/check_pf.c index e6a12ed..2c053d0 100644 --- a/sysdeps/unix/sysv/linux/check_pf.c +++ b/sysdeps/unix/sysv/linux/check_pf.c @@ -106,6 +106,11 @@ cache_valid_p (void) static struct cached_data * make_request (int fd, pid_t pid) { + struct cached_data *result = NULL; + + size_t result_len = 0; + size_t result_cap = 32; + struct req { struct nlmsghdr nlh; @@ -137,20 +142,9 @@ make_request (int fd, pid_t pid) #else const size_t buf_size = __getpagesize (); #endif - bool use_malloc = false; char *buf; - size_t alloca_used = 0; - if (__libc_use_alloca (buf_size)) - buf = alloca_account (buf_size, alloca_used); - else - { - buf = malloc (buf_size); - if (buf != NULL) - use_malloc = true; - else - goto out_fail; - } + buf = alloca (buf_size); struct iovec iov = { buf, buf_size }; @@ -160,13 +154,7 @@ make_request (int fd, pid_t pid) goto out_fail; bool done = false; - struct in6ailist - { - struct in6addrinfo info; - struct in6ailist *next; - bool use_malloc; - } *in6ailist = NULL; - size_t in6ailistlen = 0; + bool seen_ipv4 = false; bool seen_ipv6 = false; @@ -182,10 +170,10 @@ make_request (int fd, pid_t pid) ssize_t read_len = TEMP_FAILURE_RETRY (__recvmsg (fd, &msg, 0)); if (read_len < 0) - goto out_fail2; + goto out_fail; if (msg.msg_flags & MSG_TRUNC) - goto out_fail2; + goto out_fail; struct nlmsghdr *nlmh; for (nlmh = (struct nlmsghdr *) buf; @@ -241,40 +229,35 @@ make_request (int fd, pid_t pid) } } - struct in6ailist *newp; - if (__libc_use_alloca (alloca_used + sizeof (*newp))) - { - newp = alloca_account (sizeof (*newp), alloca_used); - newp->use_malloc = false; - } - else + if (result_len == 0 || result_len == result_cap) { - newp = malloc (sizeof (*newp)); - if (newp == NULL) - goto out_fail2; - newp->use_malloc = true; + result_cap = 2 * result_cap; + result = realloc (result, sizeof (*result) + + result_cap + * sizeof (struct in6addrinfo)); } - newp->info.flags = (((ifam->ifa_flags - & (IFA_F_DEPRECATED - | IFA_F_OPTIMISTIC)) - ? in6ai_deprecated : 0) - | ((ifam->ifa_flags - & IFA_F_HOMEADDRESS) - ? in6ai_homeaddress : 0)); - newp->info.prefixlen = ifam->ifa_prefixlen; - newp->info.index = ifam->ifa_index; + + if (!result) + goto out_fail; + + struct in6addrinfo *info = &result->in6ai[result_len++]; + + info->flags = (((ifam->ifa_flags + & (IFA_F_DEPRECATED | IFA_F_OPTIMISTIC)) + ? in6ai_deprecated : 0) + | ((ifam->ifa_flags & IFA_F_HOMEADDRESS) + ? in6ai_homeaddress : 0)); + info->prefixlen = ifam->ifa_prefixlen; + info->index = ifam->ifa_index; if (ifam->ifa_family == AF_INET) { - newp->info.addr[0] = 0; - newp->info.addr[1] = 0; - newp->info.addr[2] = htonl (0xffff); - newp->info.addr[3] = *(const in_addr_t *) address; + info->addr[0] = 0; + info->addr[1] = 0; + info->addr[2] = htonl (0xffff); + info->addr[3] = *(const in_addr_t *) address; } else - memcpy (newp->info.addr, address, sizeof (newp->info.addr)); - newp->next = in6ailist; - in6ailist = newp; - ++in6ailistlen; + memcpy (info->addr, address, sizeof (info->addr)); } else if (nlmh->nlmsg_type == NLMSG_DONE) /* We found the end, leave the loop. */ @@ -283,53 +266,29 @@ make_request (int fd, pid_t pid) } while (! done); - struct cached_data *result; - if (seen_ipv6 && in6ailist != NULL) + if (seen_ipv6 && result != NULL) { - result = malloc (sizeof (*result) - + in6ailistlen * sizeof (struct in6addrinfo)); - if (result == NULL) - goto out_fail2; - result->timestamp = get_nl_timestamp (); result->usecnt = 2; result->seen_ipv4 = seen_ipv4; result->seen_ipv6 = true; - result->in6ailen = in6ailistlen; - - do - { - result->in6ai[--in6ailistlen] = in6ailist->info; - struct in6ailist *next = in6ailist->next; - if (in6ailist->use_malloc) - free (in6ailist); - in6ailist = next; - } - while (in6ailist != NULL); + result->in6ailen = result_len; } else { + free (result); + atomic_add (&noai6ai_cached.usecnt, 2); noai6ai_cached.seen_ipv4 = seen_ipv4; noai6ai_cached.seen_ipv6 = seen_ipv6; result = &noai6ai_cached; } - if (use_malloc) - free (buf); return result; - out_fail2: - while (in6ailist != NULL) - { - struct in6ailist *next = in6ailist->next; - if (in6ailist->use_malloc) - free (in6ailist); - in6ailist = next; - } out_fail: - if (use_malloc) - free (buf); + + free (result); return NULL; }