From patchwork Sat Apr 6 18:39:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 88135 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 537463858415 for ; Sat, 6 Apr 2024 18:39:43 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from omta040.useast.a.cloudfilter.net (omta040.useast.a.cloudfilter.net [44.202.169.39]) by sourceware.org (Postfix) with ESMTPS id CF6B23858C53 for ; Sat, 6 Apr 2024 18:39:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CF6B23858C53 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tromey.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org CF6B23858C53 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=44.202.169.39 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712428763; cv=none; b=EJc0GgpajzFXH3lY9eLeLDXKStFxrOnW3pBLfloWmZ7tYEkltBykZdzGbdaJt/Y2l7oybpYNUB1rGIj5F4QV0wn48D4iWuUhTkP6yt1YffFqwKwEfDENg3lizlexmeXsJ+ONRMMmJHiD+iPWIH5WBwMtbrOIx0Q12CDOjBfEASU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712428763; c=relaxed/simple; bh=yAE/B1F3eMxYdszMrPjihyospK/Zup5mb3LwTz2s7QM=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=WcVciu9tnpuzkIDU1pfmNMNaylLnyx/eqCWzKnavoiEx4fVRLOXg9mhgHOQFMX6lNlw+mIRJVqQ/T2IGO+18vCwMgHPx5vUgiTrecXAnAmkgXFgJJCSWM8f/Nbhz/RXHEd304hoXe0X9wiqYG3nAaw1t6r2R9jam/Ywes9Kpvhk= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from eig-obgw-5009a.ext.cloudfilter.net ([10.0.29.176]) by cmsmtp with ESMTPS id syBXrKlwhl9dRtAwgrOOwK; Sat, 06 Apr 2024 18:39:18 +0000 Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with ESMTPS id tAwfr5QexELAWtAwfrtMZ1; Sat, 06 Apr 2024 18:39:17 +0000 X-Authority-Analysis: v=2.4 cv=EfzOQumC c=1 sm=1 tr=0 ts=661196d5 a=ApxJNpeYhEAb1aAlGBBbmA==:117 a=ApxJNpeYhEAb1aAlGBBbmA==:17 a=raytVjVEu-sA:10 a=Qbun_eYptAEA:10 a=DMXXYwvQGLeCL-0hunEA:9 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=Content-Transfer-Encoding:MIME-Version:Message-ID:Date:Subject: Cc:To:From:Sender:Reply-To:Content-Type:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=Kd2Vzx8kv9xXnVhcOoh1yt29Gq1UI2I+bNt9eWTMo9c=; b=HbJd5P6AuP80Xp1ZZzjuGMhnNL sfbQymL64tK9OjjRdYULJSe5s9fdg25qxz+v9yZlmMQl4BNgtXYKz1vfb9h6kcxBDeGzYxgYs2cEp yeaL86kBIjKiJcwjxZpzJMn7C; Received: from 97-122-82-115.hlrn.qwest.net ([97.122.82.115]:48960 helo=localhost.localdomain) by box5379.bluehost.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96.2) (envelope-from ) id 1rtAwe-000yhy-2t; Sat, 06 Apr 2024 12:39:16 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH] Use std::vector in event-loop.cc Date: Sat, 6 Apr 2024 12:39:07 -0600 Message-ID: <20240406183907.451889-1-tom@tromey.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - box5379.bluehost.com X-AntiAbuse: Original Domain - sourceware.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tromey.com X-BWhitelist: no X-Source-IP: 97.122.82.115 X-Source-L: No X-Exim-ID: 1rtAwe-000yhy-2t X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 97-122-82-115.hlrn.qwest.net (localhost.localdomain) [97.122.82.115]:48960 X-Source-Auth: tom+tromey.com X-Email-Count: 1 X-Org: HG=bhshared;ORG=bluehost; X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-CMAE-Envelope: MS4xfHznHTai1BAg/FY2adL52U7ju5sot2LlP6oE7O79/6peChH9EemNcTVvtUWSkEE4NKzuyM+RqI1hIhqyLlZprKSksgi3J9AK6EecHQneEV7ZxSBBc8W9 dVKVSpehdxEKvqG3mS3UvIAGeZJG1aR3Bn79A3cg+BHGeLH8rwJFa7d86eUFP7vCsIXQlRDLU9PXymoK14O5MwwQ8cpz/kgl/0Y= X-Spam-Status: No, score=-3021.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, JMQ_SPF_NEUTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org In my occasional and continuing campaign against realloc, this patch changes event-loop.cc to use std::vector to keep track of pollfd objects. Regression tested on x86-64 Fedora 38. --- gdbsupport/event-loop.cc | 57 +++++++++++++--------------------------- 1 file changed, 18 insertions(+), 39 deletions(-) diff --git a/gdbsupport/event-loop.cc b/gdbsupport/event-loop.cc index b18a9fdaac0..4f7ad36d8d6 100644 --- a/gdbsupport/event-loop.cc +++ b/gdbsupport/event-loop.cc @@ -112,8 +112,8 @@ static struct file_handler *next_file_handler; #ifdef HAVE_POLL - /* Ptr to array of pollfd structures. */ - struct pollfd *poll_fds; + /* Descriptors to poll. */ + std::vector poll_fds; /* Next file descriptor to handle, for the poll variant. To level the fairness across event sources, we poll the file descriptors @@ -336,17 +336,11 @@ create_file_handler (int fd, int mask, handler_func * proc, if (use_poll) { gdb_notifier.num_fds++; - if (gdb_notifier.poll_fds) - gdb_notifier.poll_fds = - (struct pollfd *) xrealloc (gdb_notifier.poll_fds, - (gdb_notifier.num_fds - * sizeof (struct pollfd))); - else - gdb_notifier.poll_fds = - XNEW (struct pollfd); - (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->fd = fd; - (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->events = mask; - (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->revents = 0; + struct pollfd new_fd; + new_fd.fd = fd; + new_fd.events = mask; + new_fd.revents = 0; + gdb_notifier.poll_fds.push_back (new_fd); } else #endif /* HAVE_POLL */ @@ -426,28 +420,13 @@ delete_file_handler (int fd) #ifdef HAVE_POLL if (use_poll) { - int j; - struct pollfd *new_poll_fds; - - /* Create a new poll_fds array by copying every fd's information - but the one we want to get rid of. */ - - new_poll_fds = (struct pollfd *) - xmalloc ((gdb_notifier.num_fds - 1) * sizeof (struct pollfd)); - - for (i = 0, j = 0; i < gdb_notifier.num_fds; i++) - { - if ((gdb_notifier.poll_fds + i)->fd != fd) - { - (new_poll_fds + j)->fd = (gdb_notifier.poll_fds + i)->fd; - (new_poll_fds + j)->events = (gdb_notifier.poll_fds + i)->events; - (new_poll_fds + j)->revents - = (gdb_notifier.poll_fds + i)->revents; - j++; - } - } - xfree (gdb_notifier.poll_fds); - gdb_notifier.poll_fds = new_poll_fds; + auto iter = std::remove_if (gdb_notifier.poll_fds.begin (), + gdb_notifier.poll_fds.end (), + [=] (const pollfd &item) + { + return item.fd == fd; + }); + gdb_notifier.poll_fds.erase (iter, gdb_notifier.poll_fds.end()); gdb_notifier.num_fds--; } else @@ -605,7 +584,7 @@ gdb_wait_for_event (int block) else timeout = 0; - num_found = poll (gdb_notifier.poll_fds, + num_found = poll (gdb_notifier.poll_fds.data (), (unsigned long) gdb_notifier.num_fds, timeout); /* Don't print anything if we get out of poll because of a @@ -676,7 +655,7 @@ gdb_wait_for_event (int block) i = gdb_notifier.next_poll_fds_index++; gdb_assert (i < gdb_notifier.num_fds); - if ((gdb_notifier.poll_fds + i)->revents) + if (gdb_notifier.poll_fds[i].revents) break; } @@ -684,12 +663,12 @@ gdb_wait_for_event (int block) file_ptr != NULL; file_ptr = file_ptr->next_file) { - if (file_ptr->fd == (gdb_notifier.poll_fds + i)->fd) + if (file_ptr->fd == gdb_notifier.poll_fds[i].fd) break; } gdb_assert (file_ptr != NULL); - mask = (gdb_notifier.poll_fds + i)->revents; + mask = gdb_notifier.poll_fds[i].revents; handle_file_event (file_ptr, mask); return 1; }