From patchwork Fri Jan 18 18:27:46 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Ploujnikov X-Patchwork-Id: 1027683 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=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-494324-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=oracle.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="hBaXLs/v"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=oracle.com header.i=@oracle.com header.b="bCq9KCF9"; 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 43h8ZW6Ltlz9sBQ for ; Sat, 19 Jan 2019 05:28:02 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to :from:subject:message-id:date:mime-version:content-type; q=dns; s=default; b=Yc75WA9WaVsZZHkGLg9T73wdcPLmoydTzg0VVF2/zO1wnWyHia vUYLdynTFLyJz7TMl+YpqSEFJHu5rmCDPvvbDDSqC/+rVMzsaGyexE+B/AIBv6A7 7m5biOI4URWA0RMot43zzBYPMvgDTKyMVx/ZAtQFpVnp0ugynyAlRIDMA= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to :from:subject:message-id:date:mime-version:content-type; s= default; bh=fv2F9rg8J3egIgW7KqW5c37Sg8o=; b=hBaXLs/vIMFkysJBm01a M/mKch7hB7eXJ19rqWYl8gSMOCOiB8sp3o95i9KmAUUDbytMJbr98oF8AcfLjRjj AZZW0i9w0AANxTYj8+Sj2U8O/KXbEwMjpCjFLZyc6cwmDyfOy34KxxahcG8Hw0sQ KMpZ5SS++koEpO41NsNIfKw= Received: (qmail 100076 invoked by alias); 18 Jan 2019 18:27:54 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 100062 invoked by uid 89); 18 Jan 2019 18:27:54 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, SPF_HELO_PASS, SPF_PASS autolearn=ham version=3.3.2 spammy=xcalloc X-HELO: aserp2130.oracle.com Received: from aserp2130.oracle.com (HELO aserp2130.oracle.com) (141.146.126.79) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 18 Jan 2019 18:27:52 +0000 Received: from pps.filterd (aserp2130.oracle.com [127.0.0.1]) by aserp2130.oracle.com (8.16.0.22/8.16.0.22) with SMTP id x0IIOONp044394 for ; Fri, 18 Jan 2019 18:27:50 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=to : from : subject : message-id : date : mime-version : content-type; s=corp-2018-07-02; bh=QRzzGz5m8JfvUlwMpl2t56MgyYJ63O4XbG8x7qglJfM=; b=bCq9KCF9Oofn6zAVd2pqP4whSw2DRgWLMCH6I/oeRzzLqgZFLCYBGqmbdPjd0m+btd1k SObdPEVAs7T7+kLb1oO64GHipSNQn5HMIrPr3AKVziE9tKyauE5YE/8bLVfra3K7mZR/ IHA5XrO0+Sw/is7P59bYQz0voURejIWwAvNwbX3Lj+s++vEso3b4KdF8MR7dxIKiT11n CEWTX+ICqq0XAvxz79GHYW1C2XV8zE39E0DWhAFB2jJmgmjvKckK+QFW2xUujxQnzqhP DcZu6rrSg9xsWNVotldaspOj0i9A95lWyuxn4NYShJOZ4MFxUmioEvJqIgs38BNpXmuf AQ== Received: from aserv0022.oracle.com (aserv0022.oracle.com [141.146.126.234]) by aserp2130.oracle.com with ESMTP id 2pybjp71gx-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Fri, 18 Jan 2019 18:27:50 +0000 Received: from userv0121.oracle.com (userv0121.oracle.com [156.151.31.72]) by aserv0022.oracle.com (8.14.4/8.14.4) with ESMTP id x0IIRnCH025475 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Fri, 18 Jan 2019 18:27:50 GMT Received: from abhmp0016.oracle.com (abhmp0016.oracle.com [141.146.116.22]) by userv0121.oracle.com (8.14.4/8.13.8) with ESMTP id x0IIRnaW002404 for ; Fri, 18 Jan 2019 18:27:49 GMT Received: from [10.39.247.183] (/10.39.247.183) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Fri, 18 Jan 2019 10:27:48 -0800 To: GCC Patches From: Michael Ploujnikov Subject: [PATCH] String contents hash map key example Openpgp: preference=signencrypt Message-ID: Date: Fri, 18 Jan 2019 13:27:46 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.2.1 MIME-Version: 1.0 X-IsSubscribed: yes I thought it would be useful to others who are new to the GCC codebase to have an example of how to hash keys based on string contents rather than pointer addresses (the fact that some hash maps key based on semi-reliable pointers (due to ggc_mark_stringpool) into the symtab gives a false sense of security). - Michael From 3433efe4ac558de05410a9b185f4ff0a01e7e5df Mon Sep 17 00:00:00 2001 From: Michael Ploujnikov Date: Fri, 11 Jan 2019 09:22:14 -0500 Subject: [PATCH] Document how to hash based on key string contents. gcc: 2019-01-18 Michael Ploujnikov * hash-map-tests.c (test_map_of_strings_to_int): Document how to hash based on key string contents. --- gcc/hash-map-tests.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git gcc/hash-map-tests.c gcc/hash-map-tests.c index 98b5830497..61da8233c4 100644 --- gcc/hash-map-tests.c +++ gcc/hash-map-tests.c @@ -77,6 +77,26 @@ test_map_of_strings_to_int () m.remove (eric); ASSERT_EQ (5, m.elements ()); ASSERT_EQ (NULL, m.get (eric)); + + /* A plain char * key is hashed based on its value (address), rather + than the string it points to. */ + char *another_ant = static_cast (xcalloc (4, 1)); + another_ant[0] = 'a'; + another_ant[1] = 'n'; + another_ant[2] = 't'; + another_ant[3] = 0; + ASSERT_NE (ant, another_ant); + unsigned prev_size = m.elements (); + ASSERT_EQ (false, m.put (another_ant, 7)); + ASSERT_EQ (prev_size + 1, m.elements ()); + + /* Need to use string_hash or nofree_string_hash key types to hash + based on the string contents. */ + hash_map string_map; + ASSERT_EQ (false, string_map.put (ant, 1)); + ASSERT_EQ (1, string_map.elements ()); + ASSERT_EQ (true, string_map.put (another_ant, 5)); + ASSERT_EQ (1, string_map.elements ()); } /* Run all of the selftests within this file. */ -- 2.19.1