* src/native/localref.cpp: Using free list instead of array scanning.
authorStefan Ring <stefan@complang.tuwien.ac.at>
Wed, 29 Sep 2010 10:47:29 +0200
changeset 936334bcb84b9dc0
parent 9362 2fbc0e8c73f8
child 9364 fc1ebc00673f
* src/native/localref.cpp: Using free list instead of array scanning.
* src/native/localref.hpp: Adapted localref_table to use a free list.
src/native/localref.cpp
src/native/localref.hpp
     1.1 --- a/src/native/localref.cpp	Fri Sep 24 10:22:02 2010 +0200
     1.2 +++ b/src/native/localref.cpp	Wed Sep 29 10:47:29 2010 +0200
     1.3 @@ -1,6 +1,6 @@
     1.4  /* src/native/localref.cpp - Management of local reference tables
     1.5  
     1.6 -   Copyright (C) 1996-2005, 2006, 2007, 2008
     1.7 +   Copyright (C) 1996-2005, 2006, 2007, 2008, 2010
     1.8     CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
     1.9  
    1.10     This file is part of CACAO.
    1.11 @@ -50,7 +50,7 @@
    1.12  		if (opt_DebugLocalReferences) { \
    1.13  			localref_table *dlrt = LOCALREFTABLE; \
    1.14  			log_start(); \
    1.15 -			log_print("[local reference %-12s: lrt=%016p frame=%d capacity=%d used=%d", message, dlrt, dlrt->localframes, dlrt->capacity, dlrt->used); \
    1.16 +			log_print("[local reference %-12s: lrt=%016p frame=%d capacity=%d used=%d hwm=%d", message, dlrt, dlrt->localframes, dlrt->capacity, dlrt->used, dlrt->hwm); \
    1.17  			if (index >= 0) \
    1.18  				log_print(" localref=%p object=%p", &(dlrt->refs[index]), dlrt->refs[index]); \
    1.19  			log_print("]"); \
    1.20 @@ -150,9 +150,8 @@
    1.21  	lrt->localframes = 1;
    1.22  	lrt->prev        = LOCALREFTABLE;
    1.23  
    1.24 -	/* clear the references array (memset is faster the a for-loop) */
    1.25 -
    1.26 -	MSET(lrt->refs, 0, void*, LOCALREFTABLE_CAPACITY);
    1.27 +	lrt->hwm = 0;
    1.28 +	lrt->firstfree = -1;
    1.29  
    1.30  	/* add given local references table to this thread */
    1.31  
    1.32 @@ -286,10 +285,6 @@
    1.33  
    1.34  		DEBUGLOCALREF("frame pop", -1);
    1.35  
    1.36 -		/* clear all reference entries */
    1.37 -
    1.38 -		MSET(lrt->refs, 0, void*, lrt->capacity);
    1.39 -
    1.40  		lrt->prev = NULL;
    1.41  
    1.42  #if !defined(ENABLE_GC_BOEHM)
    1.43 @@ -350,28 +345,34 @@
    1.44  
    1.45  	/* insert the reference into the local reference table */
    1.46  
    1.47 -	for (i = 0; i < lrt->capacity; i++) {
    1.48 -		if (lrt->refs[i] == NULL) {
    1.49 -			lrt->refs[i] = o;
    1.50 -			lrt->used++;
    1.51 +	i = lrt->hwm;
    1.52 +	if (i == lrt->capacity) {
    1.53 +		if (lrt->firstfree >= 0) {
    1.54 +			i = lrt->firstfree;
    1.55 +			lrt->firstfree = lrt->refs[i].nextfree;
    1.56 +		}
    1.57 +		else {
    1.58 +			/* this should not happen */
    1.59 +
    1.60 +			log_println("localref_add: WARNING: unable to add localref for %p", o);
    1.61 +
    1.62 +			return NULL;
    1.63 +		}
    1.64 +	} else
    1.65 +		lrt->hwm++;
    1.66 +
    1.67 +	lrt->refs[i].ptr = o;
    1.68 +	lrt->used++;
    1.69  
    1.70  #if defined(ENABLE_HANDLES)
    1.71 -			h = (java_handle_t *) &(lrt->refs[i]);
    1.72 +	h = (java_handle_t *) &(lrt->refs[i].ptr);
    1.73  #else
    1.74 -			h = (java_handle_t *) o;
    1.75 +	h = (java_handle_t *) o;
    1.76  #endif
    1.77  
    1.78 -			/*DEBUGLOCALREF("entry add", i);*/
    1.79 +	/*DEBUGLOCALREF("entry add", i);*/
    1.80  
    1.81 -			return h;
    1.82 -		}
    1.83 -	}
    1.84 -
    1.85 -	/* this should not happen */
    1.86 -
    1.87 -	log_println("localref_add: WARNING: unable to add localref for %p", o);
    1.88 -
    1.89 -	return NULL;
    1.90 +	return h;
    1.91  }
    1.92  
    1.93  
    1.94 @@ -403,17 +404,18 @@
    1.95  
    1.96  		/* and try to remove the reference */
    1.97      
    1.98 -		for (i = 0; i < lrt->capacity; i++) {
    1.99 +		for (i = 0; i < lrt->hwm; i++) {
   1.100  #if defined(ENABLE_HANDLES)
   1.101 -			h = (java_handle_t *) &(lrt->refs[i]);
   1.102 +			h = (java_handle_t *) &(lrt->refs[i].ptr);
   1.103  #else
   1.104 -			h = (java_handle_t *) lrt->refs[i];
   1.105 +			h = (java_handle_t *) lrt->refs[i].ptr;
   1.106  #endif
   1.107  
   1.108  			if (h == localref) {
   1.109  				DEBUGLOCALREF("entry delete", i);
   1.110  
   1.111 -				lrt->refs[i] = NULL;
   1.112 +				lrt->refs[i].nextfree = lrt->firstfree;
   1.113 +				lrt->firstfree = i;
   1.114  				lrt->used--;
   1.115  
   1.116  				return;
   1.117 @@ -539,7 +541,7 @@
   1.118  # define LOCALREF_DUMP_REFS_PER_LINE 4
   1.119  void localref_dump()
   1.120  {
   1.121 -	localref_table *lrt;
   1.122 +	localref_table *lrt, dlrt;
   1.123  	int i, j;
   1.124  
   1.125  	/* get current local reference table from thread */
   1.126 @@ -549,21 +551,27 @@
   1.127  	log_println("--------- Local Reference Tables Dump ---------");
   1.128  
   1.129  	while (lrt != NULL) {
   1.130 -		log_println("Frame #%d, Used=%d, Capacity=%d, Addr=%p:", lrt->localframes, lrt->used, lrt->capacity, (void *) lrt);
   1.131 +		log_println("Frame #%d, Used=%d, Capacity=%d, Hwm=%d, Addr=%p:", lrt->localframes, lrt->used, lrt->capacity, lrt->hwm, (void *) lrt);
   1.132  
   1.133  			if (lrt->used != 0) {
   1.134  
   1.135 +				dlrt = *lrt;			// copy it for dumping
   1.136 +				for (i = dlrt.firstfree; i >= 0; i = j) {
   1.137 +					j = dlrt.refs[i].nextfree;
   1.138 +					dlrt.refs[i].ptr = NULL;
   1.139 +				}
   1.140 +
   1.141  				log_start();
   1.142  
   1.143  				j = 0;
   1.144 -				for (i = 0; i < lrt->capacity; i++) {
   1.145 -					if (lrt->refs[i] != NULL) {
   1.146 +				for (i = 0; i < dlrt.hwm; i++) {
   1.147 +					if (dlrt.refs[i].ptr != NULL) {
   1.148  						if (j != 0 && j % LOCALREF_DUMP_REFS_PER_LINE == 0) {
   1.149  							log_finish();
   1.150  							log_start();
   1.151  						}
   1.152  						j++;
   1.153 -						log_print("\t0x%016lx ", (intptr_t) lrt->refs[i]);
   1.154 +						log_print("\t0x%016lx ", (intptr_t) dlrt.refs[i].ptr);
   1.155  					}
   1.156  				}
   1.157  
   1.158 @@ -604,10 +612,9 @@
   1.159  	for (; localframes > 0; localframes--) {
   1.160  		lrt_used += lrt->used;
   1.161  
   1.162 -		for (i = 0; i < lrt->capacity; i++) {
   1.163 -			if (lrt->refs[i] != NULL)
   1.164 -				lrt_uncleared++;
   1.165 -		}
   1.166 +		lrt_uncleared += lrt->hwm;
   1.167 +		for (i = lrt->firstfree; i >= 0; i = lrt->refs[i].nextfree)
   1.168 +			lrt_uncleared--;
   1.169  
   1.170  		lrt = lrt->prev;
   1.171  	}
     2.1 --- a/src/native/localref.hpp	Fri Sep 24 10:22:02 2010 +0200
     2.2 +++ b/src/native/localref.hpp	Wed Sep 29 10:47:29 2010 +0200
     2.3 @@ -1,6 +1,6 @@
     2.4  /* src/native/localref.hpp - Management of local reference tables
     2.5  
     2.6 -   Copyright (C) 1996-2005, 2006, 2007, 2008
     2.7 +   Copyright (C) 1996-2005, 2006, 2007, 2008, 2010
     2.8     CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
     2.9  
    2.10     This file is part of CACAO.
    2.11 @@ -50,10 +50,15 @@
    2.12  struct localref_table {
    2.13  	s4                 capacity;        /* table size                         */
    2.14  	s4                 used;            /* currently used references          */
    2.15 +	s4                 firstfree;       /* head of the free list              */
    2.16 +	s4                 hwm;             /* high water mark                    */
    2.17  	s4                 localframes;     /* number of current frames           */
    2.18  	s4                 PADDING;         /* 8-byte padding                     */
    2.19  	localref_table    *prev;            /* link to prev table (LocalFrame)    */
    2.20 -	java_object_t     *refs[LOCALREFTABLE_CAPACITY]; /* references            */
    2.21 +	union {
    2.22 +		java_object_t *ptr;
    2.23 +		s4 nextfree;
    2.24 +	} refs[LOCALREFTABLE_CAPACITY];     /* references            */
    2.25  };
    2.26  
    2.27