* src/native/localref.cpp: Using free list instead of array scanning.
authorStefan Ring <stefan@complang.tuwien.ac.at>
Wed Sep 29 10:47:29 2010 +0200 (2 years ago)
changeset 936334bcb84b9dc0
parent 93622fbc0e8c73f8
child 9364fc1ebc00673f
* 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 --- a/src/native/localref.cpp	Fri Sep 24 10:22:02 2010 +0200
       2 +++ b/src/native/localref.cpp	Wed Sep 29 10:47:29 2010 +0200
       3 @@ -1,6 +1,6 @@
       4  /* src/native/localref.cpp - Management of local reference tables
       5  
       6 -   Copyright (C) 1996-2005, 2006, 2007, 2008
       7 +   Copyright (C) 1996-2005, 2006, 2007, 2008, 2010
       8     CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
       9  
      10     This file is part of CACAO.
      11 @@ -50,7 +50,7 @@
      12  		if (opt_DebugLocalReferences) { \
      13  			localref_table *dlrt = LOCALREFTABLE; \
      14  			log_start(); \
      15 -			log_print("[local reference %-12s: lrt=%016p frame=%d capacity=%d used=%d", message, dlrt, dlrt->localframes, dlrt->capacity, dlrt->used); \
      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); \
      17  			if (index >= 0) \
      18  				log_print(" localref=%p object=%p", &(dlrt->refs[index]), dlrt->refs[index]); \
      19  			log_print("]"); \
      20 @@ -150,9 +150,8 @@
      21  	lrt->localframes = 1;
      22  	lrt->prev        = LOCALREFTABLE;
      23  
      24 -	/* clear the references array (memset is faster the a for-loop) */
      25 -
      26 -	MSET(lrt->refs, 0, void*, LOCALREFTABLE_CAPACITY);
      27 +	lrt->hwm = 0;
      28 +	lrt->firstfree = -1;
      29  
      30  	/* add given local references table to this thread */
      31  
      32 @@ -286,10 +285,6 @@
      33  
      34  		DEBUGLOCALREF("frame pop", -1);
      35  
      36 -		/* clear all reference entries */
      37 -
      38 -		MSET(lrt->refs, 0, void*, lrt->capacity);
      39 -
      40  		lrt->prev = NULL;
      41  
      42  #if !defined(ENABLE_GC_BOEHM)
      43 @@ -350,28 +345,34 @@
      44  
      45  	/* insert the reference into the local reference table */
      46  
      47 -	for (i = 0; i < lrt->capacity; i++) {
      48 -		if (lrt->refs[i] == NULL) {
      49 -			lrt->refs[i] = o;
      50 -			lrt->used++;
      51 +	i = lrt->hwm;
      52 +	if (i == lrt->capacity) {
      53 +		if (lrt->firstfree >= 0) {
      54 +			i = lrt->firstfree;
      55 +			lrt->firstfree = lrt->refs[i].nextfree;
      56 +		}
      57 +		else {
      58 +			/* this should not happen */
      59 +
      60 +			log_println("localref_add: WARNING: unable to add localref for %p", o);
      61 +
      62 +			return NULL;
      63 +		}
      64 +	} else
      65 +		lrt->hwm++;
      66 +
      67 +	lrt->refs[i].ptr = o;
      68 +	lrt->used++;
      69  
      70  #if defined(ENABLE_HANDLES)
      71 -			h = (java_handle_t *) &(lrt->refs[i]);
      72 +	h = (java_handle_t *) &(lrt->refs[i].ptr);
      73  #else
      74 -			h = (java_handle_t *) o;
      75 +	h = (java_handle_t *) o;
      76  #endif
      77  
      78 -			/*DEBUGLOCALREF("entry add", i);*/
      79 +	/*DEBUGLOCALREF("entry add", i);*/
      80  
      81 -			return h;
      82 -		}
      83 -	}
      84 -
      85 -	/* this should not happen */
      86 -
      87 -	log_println("localref_add: WARNING: unable to add localref for %p", o);
      88 -
      89 -	return NULL;
      90 +	return h;
      91  }
      92  
      93  
      94 @@ -403,17 +404,18 @@
      95  
      96  		/* and try to remove the reference */
      97      
      98 -		for (i = 0; i < lrt->capacity; i++) {
      99 +		for (i = 0; i < lrt->hwm; i++) {
     100  #if defined(ENABLE_HANDLES)
     101 -			h = (java_handle_t *) &(lrt->refs[i]);
     102 +			h = (java_handle_t *) &(lrt->refs[i].ptr);
     103  #else
     104 -			h = (java_handle_t *) lrt->refs[i];
     105 +			h = (java_handle_t *) lrt->refs[i].ptr;
     106  #endif
     107  
     108  			if (h == localref) {
     109  				DEBUGLOCALREF("entry delete", i);
     110  
     111 -				lrt->refs[i] = NULL;
     112 +				lrt->refs[i].nextfree = lrt->firstfree;
     113 +				lrt->firstfree = i;
     114  				lrt->used--;
     115  
     116  				return;
     117 @@ -539,7 +541,7 @@
     118  # define LOCALREF_DUMP_REFS_PER_LINE 4
     119  void localref_dump()
     120  {
     121 -	localref_table *lrt;
     122 +	localref_table *lrt, dlrt;
     123  	int i, j;
     124  
     125  	/* get current local reference table from thread */
     126 @@ -549,21 +551,27 @@
     127  	log_println("--------- Local Reference Tables Dump ---------");
     128  
     129  	while (lrt != NULL) {
     130 -		log_println("Frame #%d, Used=%d, Capacity=%d, Addr=%p:", lrt->localframes, lrt->used, lrt->capacity, (void *) lrt);
     131 +		log_println("Frame #%d, Used=%d, Capacity=%d, Hwm=%d, Addr=%p:", lrt->localframes, lrt->used, lrt->capacity, lrt->hwm, (void *) lrt);
     132  
     133  			if (lrt->used != 0) {
     134 +
     135 +				dlrt = *lrt;			// copy it for dumping
     136 +				for (i = dlrt.firstfree; i >= 0; i = j) {
     137 +					j = dlrt.refs[i].nextfree;
     138 +					dlrt.refs[i].ptr = NULL;
     139 +				}
     140  
     141  				log_start();
     142  
     143  				j = 0;
     144 -				for (i = 0; i < lrt->capacity; i++) {
     145 -					if (lrt->refs[i] != NULL) {
     146 +				for (i = 0; i < dlrt.hwm; i++) {
     147 +					if (dlrt.refs[i].ptr != NULL) {
     148  						if (j != 0 && j % LOCALREF_DUMP_REFS_PER_LINE == 0) {
     149  							log_finish();
     150  							log_start();
     151  						}
     152  						j++;
     153 -						log_print("\t0x%016lx ", (intptr_t) lrt->refs[i]);
     154 +						log_print("\t0x%016lx ", (intptr_t) dlrt.refs[i].ptr);
     155  					}
     156  				}
     157  
     158 @@ -604,10 +612,9 @@
     159  	for (; localframes > 0; localframes--) {
     160  		lrt_used += lrt->used;
     161  
     162 -		for (i = 0; i < lrt->capacity; i++) {
     163 -			if (lrt->refs[i] != NULL)
     164 -				lrt_uncleared++;
     165 -		}
     166 +		lrt_uncleared += lrt->hwm;
     167 +		for (i = lrt->firstfree; i >= 0; i = lrt->refs[i].nextfree)
     168 +			lrt_uncleared--;
     169  
     170  		lrt = lrt->prev;
     171  	}
     1.1 --- a/src/native/localref.hpp	Fri Sep 24 10:22:02 2010 +0200
     1.2 +++ b/src/native/localref.hpp	Wed Sep 29 10:47:29 2010 +0200
     1.3 @@ -1,6 +1,6 @@
     1.4  /* src/native/localref.hpp - 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,10 +50,15 @@
    1.12  struct localref_table {
    1.13  	s4                 capacity;        /* table size                         */
    1.14  	s4                 used;            /* currently used references          */
    1.15 +	s4                 firstfree;       /* head of the free list              */
    1.16 +	s4                 hwm;             /* high water mark                    */
    1.17  	s4                 localframes;     /* number of current frames           */
    1.18  	s4                 PADDING;         /* 8-byte padding                     */
    1.19  	localref_table    *prev;            /* link to prev table (LocalFrame)    */
    1.20 -	java_object_t     *refs[LOCALREFTABLE_CAPACITY]; /* references            */
    1.21 +	union {
    1.22 +		java_object_t *ptr;
    1.23 +		s4 nextfree;
    1.24 +	} refs[LOCALREFTABLE_CAPACITY];     /* references            */
    1.25  };
    1.26  
    1.27