From patchwork Sun Sep 29 00:50:05 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Terekhov, Mikhail via Gdb-patches" X-Patchwork-Id: 34711 Received: (qmail 124122 invoked by alias); 29 Sep 2019 00:50:13 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 124103 invoked by uid 89); 29 Sep 2019 00:50:11 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-21.8 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_STOCKGEN, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=H*MI:google X-HELO: mail-vk1-f201.google.com Received: from mail-vk1-f201.google.com (HELO mail-vk1-f201.google.com) (209.85.221.201) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sun, 29 Sep 2019 00:50:10 +0000 Received: by mail-vk1-f201.google.com with SMTP id t200so4635610vkd.21 for ; Sat, 28 Sep 2019 17:50:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=kyn8IJbkpBGnjfwgiiPz/kh0GIyUQvIyhDRYIsGr3KE=; b=mKhYreZRbkYNtF4rBDVDNrZw94L4pzwD/p1zpLxhS8+iZ/n9NpASouBsjqMiyrhzW5 EbTvVB+Pag2bFoVg6+DlbQY7Vjd6k1ZW+ElCRlCTPX6L0C5Df8bn/mBJ1gjycDcuif+a mVynIrlE6HRgQqZ++CaQWzN2CdMUtfKyeT6jiDU4pW0pQn0pO8f6Gjozu5obRtvDQvKA mr1wcyIkrKsm5czKKJhk+fAtUlPbfvviHl9BtZiZfhmUYF9ieWYq1bXHw99nYy8bj7sV FLeBi+l8NDj4FLQLE+0rF77aeqR727bXfkL5WBn11GYfOoMScK+qpO6bpYBTXrF64Dn7 iWUg== Date: Sat, 28 Sep 2019 19:50:05 -0500 Message-Id: <20190929005005.67668-1-cbiesinger@google.com> Mime-Version: 1.0 Subject: [PATCH] Use std::sort instead of qsort in minsyms.c X-Patchwork-Original-From: "Christian Biesinger via gdb-patches" From: "Terekhov, Mikhail via Gdb-patches" Reply-To: Christian Biesinger To: gdb-patches@sourceware.org Cc: Christian Biesinger X-IsSubscribed: yes This has better typesafety and is also marginally faster (either due to inlining or because it avoids indirection through a function pointer). Note that in this change: - return 1; /* fn1 has no name, so it is "less". */ + return true; /* fn1 has no name, so it is "less". */ else if (name1) /* fn2 has no name, so it is "less". */ - return -1; + return false; I am fairly sure the old code was wrong (ie. code didn't match the comment and the comment seemed correct), so I fixed it. gdb/ChangeLog: 2019-09-28 Christian Biesinger * minsyms.c (compare_minimal_symbols): Rename to... (minimal_symbol_is_less_than): ...this, and adjust to STL conventions (return bool, take arguments as references) (minimal_symbol_reader::install): Call std::sort instead of qsort. --- gdb/minsyms.c | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/gdb/minsyms.c b/gdb/minsyms.c index f06de4d88e..6384e7d72a 100644 --- a/gdb/minsyms.c +++ b/gdb/minsyms.c @@ -1128,37 +1128,31 @@ minimal_symbol_reader::record_full (const char *name, int name_len, on unsigned comparisons, so that we sort into unsigned numeric order. Within groups with the same address, sort by name. */ -static int -compare_minimal_symbols (const void *fn1p, const void *fn2p) +static inline bool +minimal_symbol_is_less_than (const minimal_symbol &fn1, const minimal_symbol &fn2) { - const struct minimal_symbol *fn1; - const struct minimal_symbol *fn2; - - fn1 = (const struct minimal_symbol *) fn1p; - fn2 = (const struct minimal_symbol *) fn2p; - - if (MSYMBOL_VALUE_RAW_ADDRESS (fn1) < MSYMBOL_VALUE_RAW_ADDRESS (fn2)) + if (MSYMBOL_VALUE_RAW_ADDRESS (&fn1) < MSYMBOL_VALUE_RAW_ADDRESS (&fn2)) { - return (-1); /* addr 1 is less than addr 2. */ + return true; /* addr 1 is less than addr 2. */ } - else if (MSYMBOL_VALUE_RAW_ADDRESS (fn1) > MSYMBOL_VALUE_RAW_ADDRESS (fn2)) + else if (MSYMBOL_VALUE_RAW_ADDRESS (&fn1) > MSYMBOL_VALUE_RAW_ADDRESS (&fn2)) { - return (1); /* addr 1 is greater than addr 2. */ + return false; /* addr 1 is greater than addr 2. */ } else /* addrs are equal: sort by name */ { - const char *name1 = MSYMBOL_LINKAGE_NAME (fn1); - const char *name2 = MSYMBOL_LINKAGE_NAME (fn2); + const char *name1 = MSYMBOL_LINKAGE_NAME (&fn1); + const char *name2 = MSYMBOL_LINKAGE_NAME (&fn2); if (name1 && name2) /* both have names */ - return strcmp (name1, name2); + return strcmp (name1, name2) < 0; else if (name2) - return 1; /* fn1 has no name, so it is "less". */ + return true; /* fn1 has no name, so it is "less". */ else if (name1) /* fn2 has no name, so it is "less". */ - return -1; + return false; else - return (0); /* Neither has a name, so they're equal. */ + return false; /* Neither has a name, so they're equal. */ } } @@ -1315,8 +1309,7 @@ minimal_symbol_reader::install () /* Sort the minimal symbols by address. */ - qsort (msymbols, mcount, sizeof (struct minimal_symbol), - compare_minimal_symbols); + std::sort (msymbols, msymbols + mcount, minimal_symbol_is_less_than); /* Compact out any duplicates, and free up whatever space we are no longer using. */