From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from progateway7-pub.mail.pro1.eigbox.com (gproxy5-pub.mail.unifiedlayer.com [67.222.38.55]) by sourceware.org (Postfix) with ESMTPS id 59D813857359 for ; Sat, 16 Apr 2022 16:58:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 59D813857359 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tromey.com Received: from cmgw15.mail.unifiedlayer.com (unknown [10.0.90.130]) by progateway7.mail.pro1.eigbox.com (Postfix) with ESMTP id B62E010047807 for ; Sat, 16 Apr 2022 16:58:23 +0000 (UTC) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with ESMTP id flkdn4upPkku4flkdnMN3e; Sat, 16 Apr 2022 16:58:23 +0000 X-Authority-Reason: nr=8 X-Authority-Analysis: v=2.4 cv=XPf19StE c=1 sm=1 tr=0 ts=625af5af a=ApxJNpeYhEAb1aAlGBBbmA==:117 a=ApxJNpeYhEAb1aAlGBBbmA==:17 a=dLZJa+xiwSxG16/P+YVxDGlgEgI=:19 a=z0gMJWrwH1QA:10:nop_rcvd_month_year a=Qbun_eYptAEA:10:endurance_base64_authed_username_1 a=2y_yLsEuXCydm9qD2PsA: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:References:In-Reply-To: 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:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=Uc6uZH/+zBhYjL8PwIdGQ5ipu13JjqpPIgAdy1bfV28=; b=uUdAqtp9AtXtioCLJOP3V8QJrV B4WoDdj678CzB+BNGufYRxAamltAqAeZ7rw+nsxtIbjEkHtb2LQ9QSBC4niHFr3b0Q590dih4fNz6 yfHRn8FEaVaJyf8fC07VHFlSI; Received: from 71-211-154-204.hlrn.qwest.net ([71.211.154.204]:45548 helo=prentzel.Home) by box5379.bluehost.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1nflkc-003XP7-QW; Sat, 16 Apr 2022 10:58:22 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH 1/9] Use inheritance for addrmap Date: Sat, 16 Apr 2022 10:58:13 -0600 Message-Id: <20220416165821.3648899-2-tom@tromey.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220416165821.3648899-1-tom@tromey.com> References: <20220416165821.3648899-1-tom@tromey.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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: 71.211.154.204 X-Source-L: No X-Exim-ID: 1nflkc-003XP7-QW X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 71-211-154-204.hlrn.qwest.net (prentzel.Home) [71.211.154.204]:45548 X-Source-Auth: tom+tromey.com X-Email-Count: 2 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-Spam-Status: No, score=-3029.8 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, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 16 Apr 2022 16:58:26 -0000 This is a simply C++-ification of the basics of addrmap: it uses virtual methods rather than a table of function pointers, and it changes the concrete implementations to be subclasses. --- gdb/addrmap.c | 186 +++++++++++++++++++++----------------------------- 1 file changed, 77 insertions(+), 109 deletions(-) diff --git a/gdb/addrmap.c b/gdb/addrmap.c index f88880614ec..f1df53cbe20 100644 --- a/gdb/addrmap.c +++ b/gdb/addrmap.c @@ -29,26 +29,18 @@ gdb_static_assert (sizeof (splay_tree_key) >= sizeof (CORE_ADDR *)); gdb_static_assert (sizeof (splay_tree_value) >= sizeof (void *)); -/* The "abstract class". */ - -/* Functions implementing the addrmap functions for a particular - implementation. */ -struct addrmap_funcs -{ - void (*set_empty) (struct addrmap *self, - CORE_ADDR start, CORE_ADDR end_inclusive, - void *obj); - void *(*find) (struct addrmap *self, CORE_ADDR addr); - struct addrmap *(*create_fixed) (struct addrmap *self, - struct obstack *obstack); - void (*relocate) (struct addrmap *self, CORE_ADDR offset); - int (*foreach) (struct addrmap *self, addrmap_foreach_fn fn); -}; - +/* The base class for addrmaps. */ -struct addrmap +struct addrmap : public allocate_on_obstack { - const struct addrmap_funcs *funcs; + virtual ~addrmap () = default; + + virtual void set_empty (CORE_ADDR start, CORE_ADDR end_inclusive, + void *obj) = 0; + virtual void *find (CORE_ADDR addr) = 0; + virtual struct addrmap *create_fixed (struct obstack *obstack) = 0; + virtual void relocate (CORE_ADDR offset) = 0; + virtual int foreach (addrmap_foreach_fn fn) = 0; }; @@ -57,21 +49,21 @@ addrmap_set_empty (struct addrmap *map, CORE_ADDR start, CORE_ADDR end_inclusive, void *obj) { - map->funcs->set_empty (map, start, end_inclusive, obj); + map->set_empty (start, end_inclusive, obj); } void * addrmap_find (struct addrmap *map, CORE_ADDR addr) { - return map->funcs->find (map, addr); + return map->find (addr); } struct addrmap * addrmap_create_fixed (struct addrmap *original, struct obstack *obstack) { - return original->funcs->create_fixed (original, obstack); + return original->create_fixed (obstack); } @@ -80,14 +72,14 @@ addrmap_create_fixed (struct addrmap *original, struct obstack *obstack) void addrmap_relocate (struct addrmap *map, CORE_ADDR offset) { - map->funcs->relocate (map, offset); + map->relocate (offset); } int addrmap_foreach (struct addrmap *map, addrmap_foreach_fn fn) { - return map->funcs->foreach (map, fn); + return map->foreach (fn); } /* Fixed address maps. */ @@ -102,9 +94,14 @@ struct addrmap_transition }; -struct addrmap_fixed +struct addrmap_fixed : public addrmap { - struct addrmap addrmap; + void set_empty (CORE_ADDR start, CORE_ADDR end_inclusive, + void *obj) override; + void *find (CORE_ADDR addr) override; + struct addrmap *create_fixed (struct obstack *obstack) override; + void relocate (CORE_ADDR offset) override; + int foreach (addrmap_foreach_fn fn) override; /* The number of transitions in TRANSITIONS. */ size_t num_transitions; @@ -114,14 +111,13 @@ struct addrmap_fixed ADDR - 1 is mapped to something different, we have an entry here containing ADDR and VALUE. (Note that this means we always have an entry for address 0). */ - struct addrmap_transition transitions[1]; + struct addrmap_transition *transitions; }; -static void -addrmap_fixed_set_empty (struct addrmap *self, - CORE_ADDR start, CORE_ADDR end_inclusive, - void *obj) +void +addrmap_fixed::set_empty (CORE_ADDR start, CORE_ADDR end_inclusive, + void *obj) { internal_error (__FILE__, __LINE__, "addrmap_fixed_set_empty: " @@ -129,12 +125,11 @@ addrmap_fixed_set_empty (struct addrmap *self, } -static void * -addrmap_fixed_find (struct addrmap *self, CORE_ADDR addr) +void * +addrmap_fixed::find (CORE_ADDR addr) { - struct addrmap_fixed *map = (struct addrmap_fixed *) self; - struct addrmap_transition *bottom = &map->transitions[0]; - struct addrmap_transition *top = &map->transitions[map->num_transitions - 1]; + struct addrmap_transition *bottom = &transitions[0]; + struct addrmap_transition *top = &transitions[num_transitions - 1]; while (bottom < top) { @@ -162,8 +157,8 @@ addrmap_fixed_find (struct addrmap *self, CORE_ADDR addr) } -static struct addrmap * -addrmap_fixed_create_fixed (struct addrmap *self, struct obstack *obstack) +struct addrmap * +addrmap_fixed::create_fixed (struct obstack *obstack) { internal_error (__FILE__, __LINE__, _("addrmap_create_fixed is not implemented yet " @@ -171,26 +166,24 @@ addrmap_fixed_create_fixed (struct addrmap *self, struct obstack *obstack) } -static void -addrmap_fixed_relocate (struct addrmap *self, CORE_ADDR offset) +void +addrmap_fixed::relocate (CORE_ADDR offset) { - struct addrmap_fixed *map = (struct addrmap_fixed *) self; size_t i; - for (i = 0; i < map->num_transitions; i++) - map->transitions[i].addr += offset; + for (i = 0; i < num_transitions; i++) + transitions[i].addr += offset; } -static int -addrmap_fixed_foreach (struct addrmap *self, addrmap_foreach_fn fn) +int +addrmap_fixed::foreach (addrmap_foreach_fn fn) { - struct addrmap_fixed *map = (struct addrmap_fixed *) self; size_t i; - for (i = 0; i < map->num_transitions; i++) + for (i = 0; i < num_transitions; i++) { - int res = fn (map->transitions[i].addr, map->transitions[i].value); + int res = fn (transitions[i].addr, transitions[i].value); if (res != 0) return res; @@ -200,22 +193,17 @@ addrmap_fixed_foreach (struct addrmap *self, addrmap_foreach_fn fn) } -static const struct addrmap_funcs addrmap_fixed_funcs = -{ - addrmap_fixed_set_empty, - addrmap_fixed_find, - addrmap_fixed_create_fixed, - addrmap_fixed_relocate, - addrmap_fixed_foreach -}; - - /* Mutable address maps. */ -struct addrmap_mutable +struct addrmap_mutable : public addrmap { - struct addrmap addrmap; + void set_empty (CORE_ADDR start, CORE_ADDR end_inclusive, + void *obj) override; + void *find (CORE_ADDR addr) override; + struct addrmap *create_fixed (struct obstack *obstack) override; + void relocate (CORE_ADDR offset) override; + int foreach (addrmap_foreach_fn fn) override; /* The obstack to use for allocations for this map. */ struct obstack *obstack; @@ -334,12 +322,10 @@ force_transition (struct addrmap_mutable *self, CORE_ADDR addr) } -static void -addrmap_mutable_set_empty (struct addrmap *self, - CORE_ADDR start, CORE_ADDR end_inclusive, - void *obj) +void +addrmap_mutable::set_empty (CORE_ADDR start, CORE_ADDR end_inclusive, + void *obj) { - struct addrmap_mutable *map = (struct addrmap_mutable *) self; splay_tree_node n, next; void *prior_value; @@ -356,14 +342,14 @@ addrmap_mutable_set_empty (struct addrmap *self, - Second pass: remove any unnecessary transitions. */ /* Establish transitions at the start and end. */ - force_transition (map, start); + force_transition (this, start); if (end_inclusive < CORE_ADDR_MAX) - force_transition (map, end_inclusive + 1); + force_transition (this, end_inclusive + 1); /* Walk the area, changing all NULL regions to OBJ. */ - for (n = addrmap_splay_tree_lookup (map, start), gdb_assert (n); + for (n = addrmap_splay_tree_lookup (this, start), gdb_assert (n); n && addrmap_node_key (n) <= end_inclusive; - n = addrmap_splay_tree_successor (map, addrmap_node_key (n))) + n = addrmap_splay_tree_successor (this, addrmap_node_key (n))) { if (! addrmap_node_value (n)) addrmap_node_set_value (n, obj); @@ -372,34 +358,33 @@ addrmap_mutable_set_empty (struct addrmap *self, /* Walk the area again, removing transitions from any value to itself. Be sure to visit both the transitions we forced above. */ - n = addrmap_splay_tree_predecessor (map, start); + n = addrmap_splay_tree_predecessor (this, start); prior_value = n ? addrmap_node_value (n) : NULL; - for (n = addrmap_splay_tree_lookup (map, start), gdb_assert (n); + for (n = addrmap_splay_tree_lookup (this, start), gdb_assert (n); n && (end_inclusive == CORE_ADDR_MAX || addrmap_node_key (n) <= end_inclusive + 1); n = next) { - next = addrmap_splay_tree_successor (map, addrmap_node_key (n)); + next = addrmap_splay_tree_successor (this, addrmap_node_key (n)); if (addrmap_node_value (n) == prior_value) - addrmap_splay_tree_remove (map, addrmap_node_key (n)); + addrmap_splay_tree_remove (this, addrmap_node_key (n)); else prior_value = addrmap_node_value (n); } } -static void * -addrmap_mutable_find (struct addrmap *self, CORE_ADDR addr) +void * +addrmap_mutable::find (CORE_ADDR addr) { - struct addrmap_mutable *map = (struct addrmap_mutable *) self; - splay_tree_node n = addrmap_splay_tree_lookup (map, addr); + splay_tree_node n = addrmap_splay_tree_lookup (this, addr); if (n != nullptr) { gdb_assert (addrmap_node_key (n) == addr); return addrmap_node_value (n); } - n = addrmap_splay_tree_predecessor (map, addr); + n = addrmap_splay_tree_predecessor (this, addr); if (n != nullptr) { gdb_assert (addrmap_node_key (n) < addr); @@ -438,43 +423,40 @@ splay_foreach_copy (splay_tree_node n, void *closure) } -static struct addrmap * -addrmap_mutable_create_fixed (struct addrmap *self, struct obstack *obstack) +struct addrmap * +addrmap_mutable::create_fixed (struct obstack *obstack) { - struct addrmap_mutable *mutable_obj = (struct addrmap_mutable *) self; struct addrmap_fixed *fixed; size_t num_transitions; - size_t alloc_len; /* Count the number of transitions in the tree. */ num_transitions = 0; - splay_tree_foreach (mutable_obj->tree, splay_foreach_count, &num_transitions); + splay_tree_foreach (tree, splay_foreach_count, &num_transitions); /* Include an extra entry for the transition at zero (which fixed maps have, but mutable maps do not.) */ num_transitions++; - alloc_len = sizeof (*fixed) - + (num_transitions * sizeof (fixed->transitions[0])); - fixed = (struct addrmap_fixed *) obstack_alloc (obstack, alloc_len); - fixed->addrmap.funcs = &addrmap_fixed_funcs; + fixed = new (obstack) struct addrmap_fixed; fixed->num_transitions = 1; + fixed->transitions = XOBNEWVEC (obstack, struct addrmap_transition, + num_transitions); fixed->transitions[0].addr = 0; fixed->transitions[0].value = NULL; /* Copy all entries from the splay tree to the array, in order of increasing address. */ - splay_tree_foreach (mutable_obj->tree, splay_foreach_copy, fixed); + splay_tree_foreach (tree, splay_foreach_copy, fixed); /* We should have filled the array. */ gdb_assert (fixed->num_transitions == num_transitions); - return (struct addrmap *) fixed; + return fixed; } -static void -addrmap_mutable_relocate (struct addrmap *self, CORE_ADDR offset) +void +addrmap_mutable::relocate (CORE_ADDR offset) { /* Not needed yet. */ internal_error (__FILE__, __LINE__, @@ -494,26 +476,13 @@ addrmap_mutable_foreach_worker (splay_tree_node node, void *data) } -static int -addrmap_mutable_foreach (struct addrmap *self, addrmap_foreach_fn fn) +int +addrmap_mutable::foreach (addrmap_foreach_fn fn) { - struct addrmap_mutable *mutable_obj = (struct addrmap_mutable *) self; - - return splay_tree_foreach (mutable_obj->tree, addrmap_mutable_foreach_worker, - &fn); + return splay_tree_foreach (tree, addrmap_mutable_foreach_worker, &fn); } -static const struct addrmap_funcs addrmap_mutable_funcs = -{ - addrmap_mutable_set_empty, - addrmap_mutable_find, - addrmap_mutable_create_fixed, - addrmap_mutable_relocate, - addrmap_mutable_foreach -}; - - static void * splay_obstack_alloc (int size, void *closure) { @@ -570,9 +539,8 @@ splay_compare_CORE_ADDR_ptr (splay_tree_key ak, splay_tree_key bk) struct addrmap * addrmap_create_mutable (struct obstack *obstack) { - struct addrmap_mutable *map = XOBNEW (obstack, struct addrmap_mutable); + struct addrmap_mutable *map = new (obstack) struct addrmap_mutable; - map->addrmap.funcs = &addrmap_mutable_funcs; map->obstack = obstack; /* splay_tree_new_with_allocator uses the provided allocation @@ -587,7 +555,7 @@ addrmap_create_mutable (struct obstack *obstack) splay_obstack_free, map); - return (struct addrmap *) map; + return map; } /* See addrmap.h. */ -- 2.34.1