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
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
178
  
179
  
180
  
181
  
182
  
183
  
184
  
185
  
186
  
187
  
188
  
189
  
190
  
191
  
192
  
193
  
194
  
195
  
196
  
197
  
198
  
199
  
200
  
201
  
202
  
203
  
204
  
205
  
206
  
207
  
208
  
209
  
210
  
211
  
212
  
213
  
214
  
215
  
216
  
217
  
218
  
219
  
220
  
221
  
222
  
223
  
224
  
225
  
226
  
227
  
228
  
229
  
230
  
231
  
232
  
233
  
234
  
235
  
236
  
237
  
238
  
239
  
240
  
241
  
242
  
243
  
244
  
245
  
246
  
247
  
248
  
249
  
250
  
251
  
252
  
253
  
254
  
255
  
256
  
257
  
258
  
259
  
260
  
261
  
262
  
263
  
264
  
265
  
266
  
267
  
268
  
269
  
270
  
271
  
272
  
273
  
274
  
275
  
276
  
277
  
278
  
279
  
280
  
281
  
282
  
283
  
284
  
285
  
286
  
287
  
288
  
289
  
290
  
291
  
292
  
293
  
294
  
295
  
296
  
297
  
298
  
299
  
300
  
301
  
302
  
303
  
304
  
305
  
306
  
307
  
308
  
309
  
310
  
311
  
312
  
313
  
314
  
315
  
316
  
317
  
318
  
319
  
320
  
321
  
322
  
323
  
324
  
325
  
326
  
327
  
328
  
329
  
330
  
331
  
332
  
333
  
334
  
335
  
336
  
337
  
338
  
339
  
340
  
341
  
342
  
343
  
344
  
345
  
346
  
347
  
348
  
349
  
350
  
351
  
352
  
353
  
354
  
355
  
356
  
357
  
358
  
359
  
360
  
361
  
362
  
363
  
364
  
365
  
366
  
367
  
368
  
369
  
370
  
371
  
372
  
373
  
374
  
375
  
376
  
377
  
378
  
379
  
380
  
381
  
382
  
383
  
384
  
385
  
386
  
387
  
388
  
389
  
390
  
391
  
392
  
393
  
394
  
395
  
396
  
397
  
398
  
399
  
400
  
401
  
402
  
403
  
404
  
405
  
406
  
407
  
408
  
409
  
410
  
411
  
412
  
413
  
414
  
415
  
416
  
417
  
418
  
419
  
420
  
421
  
422
  
423
  
424
  
425
  
426
  
427
  
428
  
429
  
430
  
431
  
432
  
433
  
434
  
435
  
436
  
437
  
438
  
439
  
440
  
441
  
442
  
443
  
444
  
445
  
446
  
447
  
448
  
449
  
450
  
451
  
452
  
453
  
454
  
455
  
456
  
457
  
458
  
459
  
460
  
461
  
462
  
463
  
464
  
465
  
466
  
467
  
468
  
469
  
470
  
471
  
472
  
473
  
474
  
475
  
476
  
477
  
478
  
479
  
480
  
481
  
482
  
483
  
484
  
485
  
486
  
487
  
488
  
489
  
490
  
491
  
492
  
493
  
494
  
495
  
496
  
497
  
498
  
499
  
500
  
501
  
502
  
503
  
504
  
505
  
506
  
507
  
508
  
509
  
510
  
511
  
512
  
513
  
514
  
515
  
516
  
517
  
518
  
519
  
520
  
521
  
522
  
523
  
524
  
525
  
526
  
527
  
528
  
529
  
530
  
531
  
532
  
533
  
534
  
535
  
536
  
537
  
538
  
539
  
540
  
541
  
542
  
543
  
544
  
545
  
546
  
547
  
548
  
549
  
550
  
551
  
552
  
553
  
554
  
555
  
556
  
557
  
558
  
559
  
560
  
561
  
562
  
563
  
564
  
565
  
566
  
567
  
568
  
569
  
570
  
571
  
572
  
573
  
574
  
575
  
576
  
577
  
578
  
579
  
580
  
581
  
582
  
583
  
584
  
585
  
586
  
587
  
588
  
589
  
590
  
591
  
592
  
593
  
594
  
595
  
596
  
597
  
598
  
599
  
600
  
601
  
602
  
603
  
604
  
605
  
606
  
607
  
608
  
609
  
610
  
611
  
612
  
613
  
614
  
615
  
616
  
617
  
618
  
619
  
620
  
/*\ 
||| This file a part of Pike, and is copyright by Fredrik Hubinette 
||| Pike is distributed as GPL (General Public License) 
||| See the files COPYING and DISCLAIMER for more information. 
\*/ 
 
#include "global.h" 
 
struct callback *gc_evaluator_callback=0; 
 
#include "array.h" 
#include "multiset.h" 
#include "mapping.h" 
#include "object.h" 
#include "program.h" 
#include "stralloc.h" 
#include "stuff.h" 
#include "error.h" 
#include "pike_memory.h" 
#include "pike_macros.h" 
#include "pike_types.h" 
#include "time_stuff.h" 
 
#include "gc.h" 
#include "main.h" 
#include <math.h> 
 
RCSID("$Id: gc.c,v 1.30 1998/03/28 15:31:05 grubba Exp $"); 
 
/* 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 MAX_ALLOC_THRESHOLD 10000000 
#define MULTIPLIER 0.9 
#define MARKER_CHUNK_SIZE 1023 
 
INT32 num_objects =0; 
INT32 num_allocs =0; 
INT32 alloc_threshold = MIN_ALLOC_THRESHOLD; 
static int in_gc = 0; 
 
static double objects_alloced = 0.0; 
static double objects_freed = 0.0; 
 
struct callback_list gc_callbacks; 
 
struct callback *add_gc_callback(callback_func call, 
                                 void *arg, 
                                 callback_func free_func) 
{ 
  return add_to_callback(&gc_callbacks, call, arg, free_func); 
} 
 
#define GC_REFERENCED 1 
#define GC_XREFERENCED 2 
 
struct marker 
{ 
  INT32 refs; 
#ifdef DEBUG 
  INT32 xrefs; 
#endif 
  INT32 flags; 
  struct marker *next; 
  void *marked; 
}; 
 
struct marker_chunk 
{ 
  struct marker_chunk *next; 
  struct marker markers[MARKER_CHUNK_SIZE]; 
}; 
 
static struct marker_chunk *chunk=0; 
static int markers_left_in_chunk=0; 
 
static struct marker *new_marker(void) 
{ 
  if(!markers_left_in_chunk) 
  { 
    struct marker_chunk *m; 
    m=(struct marker_chunk *)xalloc(sizeof(struct marker_chunk)); 
    m->next=chunk; 
    chunk=m; 
    markers_left_in_chunk=MARKER_CHUNK_SIZE; 
  } 
  markers_left_in_chunk--; 
 
  return chunk->markers + markers_left_in_chunk; 
} 
 
static struct marker **hash=0; 
static unsigned long hashsize=0; 
 
static struct marker *getmark(void *a) 
{ 
  int hashval; 
  struct marker *m; 
 
  hashval=((unsigned long)a)%hashsize; 
 
  for(m=hash[hashval];m;m=m->next) 
    if(m->marked == a) 
      return m; 
 
  m=new_marker(); 
  m->marked=a; 
  m->refs=0; 
#ifdef DEBUG 
  m->xrefs=0; 
#endif 
  m->flags=0; 
  m->next=hash[hashval]; 
  hash[hashval]=m; 
 
  return m; 
} 
 
#ifdef DEBUG 
 
time_t last_gc; 
 
void dump_gc_info(void) 
{ 
  fprintf(stderr,"Current number of objects: %ld\n",(long)num_objects); 
  fprintf(stderr,"Objects allocated total  : %ld\n",(long)num_allocs); 
  fprintf(stderr," threshold for next gc() : %ld\n",(long)alloc_threshold); 
  fprintf(stderr,"Average allocs per gc()  : %f\n",objects_alloced); 
  fprintf(stderr,"Average frees per gc()   : %f\n",objects_freed); 
  fprintf(stderr,"Second since last gc()   : %ld\n", (long)TIME(0) - (long)last_gc); 
  fprintf(stderr,"Projected garbage        : %f\n", objects_freed * (double) num_allocs / (double) alloc_threshold); 
  fprintf(stderr,"in_gc                    : %d\n", in_gc); 
} 
 
TYPE_T attempt_to_identify(void *something) 
{ 
  struct array *a; 
  struct object *o; 
  struct program *p; 
  struct mapping *m; 
  struct multiset *mu; 
 
  a=&empty_array; 
  do 
  { 
    if(a==(struct array *)something) return T_ARRAY; 
    a=a->next; 
  }while(a!=&empty_array); 
 
  for(o=first_object;o;o=o->next) 
    if(o==(struct object *)something) 
      return T_OBJECT; 
 
  for(p=first_program;p;p=p->next) 
    if(p==(struct program *)something) 
      return T_PROGRAM; 
 
  for(m=first_mapping;m;m=m->next) 
    if(m==(struct mapping *)something) 
      return T_MAPPING; 
 
  for(mu=first_multiset;mu;mu=mu->next) 
    if(mu==(struct multiset *)something) 
      return T_MULTISET; 
 
  if(safe_debug_findstring((struct pike_string *)something)) 
    return T_STRING; 
 
  return T_UNKNOWN; 
} 
 
static void *check_for =0; 
static char *found_where=""; 
static void *found_in=0; 
static int found_in_type=0; 
void *gc_svalue_location=0; 
 
void describe_location(void *memblock, TYPE_T type, void *location) 
{ 
  if(!location) return; 
  fprintf(stderr,"**Location of (short) svalue: %p\n",location); 
 
  switch(type) 
  { 
    case T_PROGRAM: 
    { 
      struct program *p=(struct program *)memblock; 
      char *ptr=(char *)location; 
      if(ptr >= (char *)p->inherits  && ptr<(char*)(p->inherits+p->num_inherits)) 
        fprintf(stderr,"**In inherit block.\n"); 
 
      if(ptr >= (char *)p->strings  && ptr<(char*)(p->strings+p->num_strings)) 
        fprintf(stderr,"**In string block.\n"); 
 
      if(ptr >= (char *)p->identifiers  && ptr<(char*)(p->identifiers+p->num_identifiers)) 
        fprintf(stderr,"**In identifier block.\n"); 
       
      return; 
    } 
     
    case T_OBJECT: 
    { 
      struct object *o=(struct object *)memblock; 
      if(o->prog) 
      { 
        INT32 e,d; 
        for(e=0;e<(INT32)o->prog->num_inherits;e++) 
        { 
          struct inherit tmp=o->prog->inherits[e]; 
          char *base=o->storage + tmp.storage_offset; 
           
          for(d=0;d<(INT32)tmp.prog->num_identifiers;d++) 
          { 
            struct identifier *id=tmp.prog->identifiers+d; 
            if(!IDENTIFIER_IS_VARIABLE(id->identifier_flags)) continue; 
             
            if(location == (void *)(base + id->func.offset)) 
            { 
              fprintf(stderr,"**In variable %s\n",id->name->str); 
            } 
          } 
        } 
      } 
      return; 
    } 
 
    case T_ARRAY: 
    { 
      struct array *a=(struct array *)memblock; 
      struct svalue *s=(struct svalue *)location; 
      fprintf(stderr,"**In index %ld\n",(long)(s-ITEM(a))); 
      return; 
    } 
  } 
} 
 
static void gdb_gc_stop_here(void *a) 
{ 
  fprintf(stderr,"***One ref found%s.\n",found_where); 
  describe_something(found_in, found_in_type); 
  describe_location(found_in, found_in_type, gc_svalue_location); 
} 
 
void debug_gc_xmark_svalues(struct svalue *s, int num, char *fromwhere) 
{ 
  found_in=(void *)fromwhere; 
  found_in_type=-1; 
  gc_xmark_svalues(s,num); 
  found_in_type=T_UNKNOWN; 
  found_in=0; 
} 
 
TYPE_FIELD debug_gc_check_svalues(struct svalue *s, int num, TYPE_T t, void *data) 
{ 
  TYPE_FIELD ret; 
  found_in=data; 
  found_in_type=t; 
  ret=gc_check_svalues(s,num); 
  found_in_type=T_UNKNOWN; 
  found_in=0; 
  return ret; 
} 
 
void debug_gc_check_short_svalue(union anything *u, TYPE_T type, TYPE_T t, void *data) 
{ 
  found_in=data; 
  found_in_type=t; 
  gc_check_short_svalue(u,type); 
  found_in_type=T_UNKNOWN; 
  found_in=0; 
} 
 
 
int debug_gc_check(void *x, TYPE_T t, void *data) 
{ 
  int ret; 
  found_in=data; 
  found_in_type=t; 
  ret=gc_check(x); 
  found_in_type=T_UNKNOWN; 
  found_in=0; 
  return ret; 
} 
 
void describe_something(void *a, int t) 
{ 
  struct program *p=(struct program *)a; 
  if(!a) return; 
  if(t==-1) 
  { 
    fprintf(stderr,"**Location description: %s\n",(char *)a); 
    return; 
  } 
 
  fprintf(stderr,"**Location: %p  Type: %s  Refs: %d\n",a, 
          get_name_of_type(t), 
          *(INT32 *)a); 
 
  switch(t) 
  { 
    case T_OBJECT: 
      p=((struct object *)a)->prog; 
      if(!p) 
      { 
        fprintf(stderr,"**The object is destructed.\n"); 
        break; 
      } 
      fprintf(stderr,"**Attempting to describe program object was instantiated from:\n"); 
       
    case T_PROGRAM: 
    { 
      char *tmp; 
      INT32 line,pos; 
 
      fprintf(stderr,"**Program id: %ld\n",(long)(p->id)); 
      if(!p->num_linenumbers) 
      { 
        int e; 
        fprintf(stderr,"**The program was written in C.\n"); 
        fprintf(stderr,"**identifiers:\n"); 
        for(e=0;e<p->num_identifiers;e++) 
          fprintf(stderr,"*** %s\n",p->identifiers[e].name->str); 
      } 
 
      for(pos=0;pos<(long)p->num_program && pos<100;pos++) 
      { 
        tmp=get_line(p->program+pos, p, &line); 
        if(tmp && line) 
        { 
          fprintf(stderr,"**Location: %s:%ld\n",tmp,(long)line); 
          break; 
        } 
      } 
      break; 
    } 
       
    case T_ARRAY: 
      fprintf(stderr,"**Describing array:\n"); 
      debug_dump_array((struct array *)a); 
      break; 
 
    case T_MAPPING: 
      fprintf(stderr,"**Describing mapping:\n"); 
      debug_dump_mapping((struct mapping *)a); 
      break; 
 
    case T_STRING: 
    { 
      struct pike_string *s=(struct pike_string *)a; 
      fprintf(stderr,"**String length is %d:\n",s->len); 
      if(s->len>77) 
      { 
        fprintf(stderr,"** \"%60s ...\"\n",s->str); 
      }else{ 
        fprintf(stderr,"** \"%s\"\n",s->str); 
      } 
      break; 
    } 
  } 
} 
 
#endif 
 
INT32 gc_check(void *a) 
{ 
#ifdef DEBUG 
  if(check_for) 
  { 
    if(check_for == a) 
    { 
      gdb_gc_stop_here(a); 
    } 
    return 0; 
  } 
#endif 
  return getmark(a)->refs++; 
} 
 
static void init_gc(void) 
{ 
  INT32 tmp3; 
  /* init hash , hashsize will be a prime between num_objects/8 and 
   * num_objects/4, this will assure that no re-hashing is needed. 
   */ 
  tmp3=my_log2(num_objects); 
 
  if(!d_flag) tmp3-=2; 
  if(tmp3<0) tmp3=0; 
  if(tmp3>=(long)NELEM(hashprimes)) tmp3=NELEM(hashprimes)-1; 
  hashsize=hashprimes[tmp3]; 
 
  hash=(struct marker **)xalloc(sizeof(struct marker **)*hashsize); 
  MEMSET((char *)hash,0,sizeof(struct marker **)*hashsize); 
  markers_left_in_chunk=0; 
} 
 
static void exit_gc(void) 
{ 
  struct marker_chunk *m; 
  /* Free hash table */ 
  free((char *)hash); 
  while((m=chunk)) 
  { 
    chunk=m->next; 
    free((char *)m); 
  } 
} 
 
#ifdef DEBUG 
void locate_references(void *a) 
{ 
  if(!in_gc) 
    init_gc(); 
   
  fprintf(stderr,"**Looking for references:\n"); 
   
  check_for=a; 
 
  found_where=" in an array"; 
  gc_check_all_arrays(); 
   
  found_where=" in a multiset"; 
  gc_check_all_multisets(); 
   
  found_where=" in a mapping"; 
  gc_check_all_mappings(); 
   
  found_where=" in a program"; 
  gc_check_all_programs(); 
   
  found_where=" in an object"; 
  gc_check_all_objects(); 
   
  found_where=" in a module"; 
  call_callback(& gc_callbacks, (void *)0); 
   
  found_where=""; 
  check_for=0; 
   
  if(!in_gc) 
    exit_gc(); 
} 
#endif 
 
int gc_is_referenced(void *a) 
{ 
  struct marker *m; 
  m=getmark(a); 
#ifdef DEBUG 
  if(m->refs + m->xrefs > *(INT32 *)a) 
  { 
    INT32 refs=m->refs; 
    INT32 xrefs=m->xrefs; 
    TYPE_T t=attempt_to_identify(a); 
 
    fprintf(stderr,"**Something has %ld references, while gc() found %ld + %ld external.\n",(long)*(INT32 *)a,(long)refs,(long)xrefs); 
    describe_something(a, t); 
 
    locate_references(a); 
 
    fatal("Ref counts are wrong (has %d, found %d + %d external)\n", 
          *(INT32 *)a, 
          refs, 
          xrefs); 
  } 
#endif 
  return m->refs < *(INT32 *)a; 
} 
 
#ifdef DEBUG 
int gc_external_mark(void *a) 
{ 
  struct marker *m; 
  if(check_for) 
  { 
    if(a==check_for) 
    { 
      char *tmp=found_where; 
      found_where=" externally"; 
      gdb_gc_stop_here(a); 
      found_where=tmp; 
 
      return 1; 
    } 
    return 0; 
  } 
  m=getmark(a); 
  m->xrefs++; 
  m->flags|=GC_XREFERENCED; 
  gc_is_referenced(a); 
  return 0; 
} 
#endif 
 
int gc_mark(void *a) 
{ 
  struct marker *m; 
  m=getmark(a); 
 
  if(m->flags & GC_REFERENCED) 
  { 
    return 0; 
  }else{ 
    m->flags |= GC_REFERENCED; 
    return 1; 
  } 
} 
 
int gc_do_free(void *a) 
{ 
  struct marker *m; 
  m=getmark(a); 
  return !(m->flags & GC_REFERENCED); 
} 
 
void do_gc(void) 
{ 
  double tmp; 
  INT32 tmp2; 
  double multiplier; 
 
  if(in_gc) return; 
  in_gc=1; 
 
  if(gc_evaluator_callback) 
  { 
    remove_callback(gc_evaluator_callback); 
    gc_evaluator_callback=0; 
  } 
 
  tmp2=num_objects; 
 
#ifdef DEBUG 
  if(t_flag) 
    fprintf(stderr,"Garbage collecting ... "); 
  if(num_objects < 0) 
    fatal("Panic, less than zero objects!\n"); 
 
  last_gc=TIME(0); 
 
#endif 
 
  multiplier=pow(MULTIPLIER, (double) num_allocs / (double) alloc_threshold); 
  objects_alloced*=multiplier; 
  objects_alloced += (double) num_allocs; 
   
  objects_freed*=multiplier; 
  objects_freed += (double) num_objects; 
 
 
  init_gc(); 
 
  /* First we count internal references */ 
  gc_check_all_arrays(); 
  gc_check_all_multisets(); 
  gc_check_all_mappings(); 
  gc_check_all_programs(); 
  gc_check_all_objects(); 
  call_callback(& gc_callbacks, (void *)0); 
 
  /* Next we mark anything with external references */ 
  gc_mark_all_arrays(); 
  gc_mark_all_multisets(); 
  gc_mark_all_mappings(); 
  gc_mark_all_programs(); 
  gc_mark_all_objects(); 
 
  if(d_flag) 
    gc_mark_all_strings(); 
 
  /* Now we free the unused stuff */ 
  gc_free_all_unreferenced_arrays(); 
  gc_free_all_unreferenced_multisets(); 
  gc_free_all_unreferenced_mappings(); 
  gc_free_all_unreferenced_programs(); 
  gc_free_all_unreferenced_objects(); 
 
  exit_gc(); 
 
  destruct_objects_to_destruct(); 
   
  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; 
  if(alloc_threshold > MAX_ALLOC_THRESHOLD) 
    alloc_threshold = MAX_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 
 
#ifdef ALWAYS_GC 
  ADD_GC_CALLBACK(); 
#else 
  if(d_flag > 3) ADD_GC_CALLBACK(); 
#endif 
  in_gc=0; 
}