From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 26646 invoked by alias); 17 Jul 2009 18:42:05 -0000 Received: (qmail 26638 invoked by uid 22791); 17 Jul 2009 18:42:04 -0000 X-SWARE-Spam-Status: No, hits=-2.5 required=5.0 tests=AWL,BAYES_00 X-Spam-Check-By: sourceware.org Received: from NaN.false.org (HELO nan.false.org) (208.75.86.248) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 17 Jul 2009 18:41:57 +0000 Received: from nan.false.org (localhost [127.0.0.1]) by nan.false.org (Postfix) with ESMTP id 15BE310623; Fri, 17 Jul 2009 18:41:55 +0000 (GMT) Received: from caradoc.them.org (209.195.188.212.nauticom.net [209.195.188.212]) by nan.false.org (Postfix) with ESMTP id 7EA4210620; Fri, 17 Jul 2009 18:41:54 +0000 (GMT) Received: from drow by caradoc.them.org with local (Exim 4.69) (envelope-from ) id 1MRsNV-00020a-2E; Fri, 17 Jul 2009 14:41:53 -0400 Date: Fri, 17 Jul 2009 19:03:00 -0000 From: Daniel Jacobowitz To: gdb-patches@sourceware.org Cc: Tom Tromey , Vladimir Prus Subject: Value reference counting Message-ID: <20090717184152.GA6863@caradoc.them.org> Mail-Followup-To: gdb-patches@sourceware.org, Tom Tromey , Vladimir Prus MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.20 (2009-06-14) X-IsSubscribed: yes Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2009-07/txt/msg00434.txt.bz2 This patch, based on an old patch from Vladimir, implements reference counting for values. Tom, this is the approach I discussed with you on IRC: instead of treating the value chain as a normal reference and using release_value to take references, this separates the value chain (which is boolean; a value is either on it or not) from references (which are counted). So you take a reference with value_incref. release_value transforms the value chain's reference into a normal reference. That's an entirely theoretical operation, by which I mean release_value doesn't have to do anything special. Does this look OK? Tom, will it work for the Python code? Tested on x86_64-linux, no regressions. -- Daniel Jacobowitz CodeSourcery 2009-07-17 Daniel Jacobowitz Vladimir Prus * value.c (struct value): Add reference_count field. (allocate_value_lazy): Initialize reference_count. (value_incref): New function. (value_free): Check the reference count. * value.h (value_incref): New prototype. --- gdb/value.c | 27 +++++++++++++++++++++++++++ gdb/value.h | 2 ++ 2 files changed, 29 insertions(+) Index: src/gdb/value.c =================================================================== --- src.orig/gdb/value.c 2009-07-17 09:52:16.000000000 -0400 +++ src/gdb/value.c 2009-07-17 10:07:10.000000000 -0400 @@ -194,6 +194,11 @@ struct value /* Actual contents of the value. Target byte-order. NULL or not valid if lazy is nonzero. */ gdb_byte *contents; + + /* The number of references to this value. This initially includes + one reference from the value chain; if release_value is called, + it converts that into a normal reference. */ + int reference_count; }; /* Prototypes for local functions. */ @@ -259,6 +264,10 @@ allocate_value_lazy (struct type *type) val->pointed_to_offset = 0; val->modifiable = 1; val->initialized = 1; /* Default to initialized. */ + + /* Values start out on the all_values chain. */ + val->reference_count = 1; + return val; } @@ -583,11 +592,29 @@ value_mark (void) return all_values; } +/* Take a reference to VAL. VAL will not be deallocated until all + references are released. */ + +void +value_incref (struct value *val) +{ + val->reference_count++; +} + +/* Release a reference to VAL, which was acquired with value_incref. + This function is also called to deallocate values from the value + chain. */ + void value_free (struct value *val) { if (val) { + gdb_assert (val->reference_count > 0); + val->reference_count--; + if (val->reference_count > 0) + return; + if (VALUE_LVAL (val) == lval_computed) { struct lval_funcs *funcs = val->location.computed.funcs; Index: src/gdb/value.h =================================================================== --- src.orig/gdb/value.h 2009-07-17 10:05:12.000000000 -0400 +++ src/gdb/value.h 2009-07-17 10:05:17.000000000 -0400 @@ -582,6 +582,8 @@ extern int unop_user_defined_p (enum exp extern int destructor_name_p (const char *name, const struct type *type); +extern void value_incref (struct value *val); + extern void value_free (struct value *val); extern void free_all_values (void);