1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
/*\ 
||| This file a part of uLPC, and is copyright by Fredrik Hubinette 
||| uLPC is distributed as GPL (General Public License) 
||| See the files COPYING and DISCLAIMER for more information. 
\*/ 
 
#include "global.h" 
 
#ifdef GC2 
#include "gc.h" 
#include "main.h" 
 
/* Run garbage collect approximate every time we have 
 * 20 percent of all arrays, objects and programs is 
 * garbage. 
 */ 
 
#define GC_CONST 20 
#define MIN_ALLOC_THRESHOLD 1000 
#define MULTIPLIER 0.9 
 
void *gc_ptr; 
INT32 gc_refs; 
INT32 num_objects; 
INT32 num_allocs; 
INT32 alloc_threshold = MIN_ALLOC_THRESHOLD; 
 
static double objects_alloced; 
static double objects_freed; 
 
void do_gc() 
{ 
  double tmp; 
  INT32 tmp2; 
 
  tmp2=num_objects; 
 
#ifdef DEBUG 
  if(t_flag) 
    fprintf(stderr,"Garbage collecting ... "); 
#endif 
 
  objects_alloced*=MULTIPLIER; 
  objects_alloced += (double) num_allocs; 
   
  objects_freed*=MULTIPLIER; 
  objects_freed += (double) num_objects; 
   
  gc_clear_array_marks(); 
  gc_clear_object_marks(); 
  gc_clear_program_marks(); 
   
  gc_check_all_arrays(); 
  gc_check_all_programs(); 
  gc_check_all_objects(); 
   
  objects_freed -= (double) num_objects; 
 
  tmp=(double)num_objects; 
  tmp=tmp * GC_CONST/100.0 * (objects_alloced+1.0) / (objects_freed+1.0); 
   
  if((int)tmp < alloc_threshold + num_allocs) 
  { 
    alloc_threshold=(int)tmp; 
  }else{ 
    alloc_threshold+=num_allocs; 
  } 
 
  if(alloc_threshold < MIN_ALLOC_THRESHOLD) 
    alloc_threshold = MIN_ALLOC_THRESHOLD; 
  num_allocs=0; 
 
#ifdef DEBUG 
  if(t_flag) 
    fprintf(stderr,"done (freed %ld of %ld objects).\n", 
            (long)(tmp2-num_objects),(long)tmp2); 
#endif 
} 
#endif