This repository has been archived by the owner on Nov 18, 2021. It is now read-only.
forked from FooBarWidget/rubyenterpriseedition187-248
-
Notifications
You must be signed in to change notification settings - Fork 5
/
gc.c
3748 lines (3343 loc) · 100 KB
/
gc.c
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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**********************************************************************
gc.c -
$Author$
$Date$
created at: Tue Oct 5 09:44:46 JST 1993
Copyright (C) 1993-2003 Yukihiro Matsumoto
Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
Copyright (C) 2000 Information-technology Promotion Agency, Japan
**********************************************************************/
#include "ruby.h"
#include "rubysig.h"
#include "st.h"
#include "node.h"
#include "env.h"
#include "re.h"
#include <stdio.h>
#include <setjmp.h>
#include <math.h>
#include <sys/types.h>
#include <ctype.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdarg.h>
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif
#if defined _WIN32 || defined __CYGWIN__
#include <windows.h>
#endif
void re_free_registers _((struct re_registers*));
void rb_io_fptr_finalize _((struct rb_io_t*));
#define rb_setjmp(env) RUBY_SETJMP(env)
#define rb_jmp_buf rb_jmpbuf_t
#ifdef __CYGWIN__
int _setjmp(), _longjmp();
#endif
#define T_DEFERRED 0x3a
#ifndef GC_LEVEL_MAX /*maximum # of VALUEs on 'C' stack during GC*/
#define GC_LEVEL_MAX 8000
#endif
#ifndef GC_STACK_PAD
#define GC_STACK_PAD 200 /* extra padding VALUEs for GC stack */
#endif
#define GC_STACK_MAX (GC_LEVEL_MAX+GC_STACK_PAD)
/* The address of the end of the main thread's application stack. When the
* main thread is active, application code may not cause the stack to grow
* past this point. Past this point there's still a small area reserved for
* garbage collector operations.
*/
static VALUE *stack_limit;
/*
* The address of the end of the current thread's GC stack. When running
* the GC, the stack may not grow past this point.
* The value of this variable is reset every time garbage_collect() is
* called.
*/
static VALUE *gc_stack_limit;
static void run_final();
static VALUE nomem_error;
static void garbage_collect(const char* reason);
static void add_to_longlife_recent_allocations(VALUE ptr);
#define DEFAULT_LONGLIFE_LAZINESS 0.05
static float longlife_laziness = DEFAULT_LONGLIFE_LAZINESS;
static int longlife_collection = Qfalse;
static int longlife_recent_allocations = 0;
int ruby_in_longlife_context = Qfalse;
typedef enum lifetime {
lifetime_longlife,
lifetime_eden
} lifetime_t;
#define OBJ_TYPE_COUNT (T_MASK + 1 + NODE_LAST)
#if HAVE_LONG_LONG
#define GC_TIME_TYPE LONG_LONG
#else
#define GC_TIME_TYPE long
#endif
static const char lifetime_name[][9] = { "Longlife", "Eden" };
#ifdef GC_DEBUG
static char *gc_data_file_name;
static char *gc_dump_file_pattern;
static char *backtrace_str_buffer = 0;
static int backtrace_str_buffer_len = 0;
int gc_debug_on = Qfalse;
static int gc_eden_cycles_since_last_longlife = 0;
static int gc_debug_summary = 0;
int gc_debug_dump = 0;
static int gc_debug_longlife_disabled = Qfalse;
static int gc_debug_stress = Qfalse;
static int gc_debug_always_mark = Qfalse;
#define SOURCE_POS_INIT_SIZE 100000
#define SOURCE_POS_INIT_TMP_SIZE 10000
static st_table *source_positions;
NODE* search_method(VALUE klass, ID id, VALUE *origin);
static const char lifetime_name_lower[][9] = { "longlife", "eden" };
#else
#define gc_debug_stress (0)
#define gc_debug_always_mark (0)
#endif
#ifdef GC_DEBUG
/*
* call-seq:
* GC.stress => true or false
*
* returns current status of GC stress mode.
*/
static VALUE
gc_debug_stress_get(self)
VALUE self;
{
return gc_debug_stress;
}
/*
* call-seq:
* GC.stress = bool => bool
*
* updates GC stress mode.
*
* When GC.stress = true, GC is invoked for all GC opportunity:
* all memory and object allocation.
*
* Since it makes Ruby very slow, it is only for debugging.
*/
static VALUE
gc_debug_stress_set(self, bool)
VALUE self, bool;
{
rb_secure(2);
gc_debug_stress = RTEST(bool) ? Qtrue : Qfalse;
return gc_debug_stress;
}
/*
* call-seq:
* GC.exorcise
*
* Purge ghost references from recently freed stack space
*
*/
static VALUE gc_exorcise(VALUE mod)
{
rb_gc_wipe_stack();
return Qnil;
}
#endif
NORETURN(void rb_exc_jump _((VALUE)));
#if defined(HAVE_LONG_LONG)
static unsigned long long allocated_objects = 0;
#else
static unsigned long allocated_objects = 0;
#endif
static int during_gc = 0;
void
rb_memerror()
{
// If we throw a NoMemoryError, we're no longer doing GC. This will allow
// further allocations to occur in the handler for this error. Normally,
// it goes unhandled and terminates the VM, but even in that case,
// rb_write_error2() will create one new string as part of printing the
// error message to stderr. Allowing allocations in NoMemoryError handler
// is okay -- by that time some or all of the stack frames were unwound,
// so some memory can be realistically allocated again; even a GC can
// succeed.
during_gc = 0;
rb_thread_t th = rb_curr_thread;
during_gc = 0;
if (!nomem_error ||
(rb_thread_raised_p(th, RAISED_NOMEMORY) && rb_safe_level() < 4)) {
fprintf(stderr, "[FATAL] failed to allocate memory\n");
exit(EXIT_FAILURE);
}
if (rb_thread_raised_p(th, RAISED_NOMEMORY)) {
rb_exc_jump(nomem_error);
}
rb_thread_raised_set(th, RAISED_NOMEMORY);
rb_exc_raise(nomem_error);
}
void *
ruby_xmalloc(size)
long size;
{
void *mem;
if (size < 0) {
rb_raise(rb_eNoMemError, "negative allocation size (or too big)");
}
if (size == 0) {
size = 1;
}
RUBY_CRITICAL(mem = malloc(size));
if (!mem) {
longlife_collection = Qtrue;
garbage_collect("OOM in malloc");
RUBY_CRITICAL(mem = malloc(size));
if (!mem) {
rb_memerror();
}
}
#if STACK_WIPE_SITES & 0x100
rb_gc_update_stack_extent();
#endif
return mem;
}
void *
ruby_xcalloc(n, size)
long n, size;
{
void *mem;
mem = xmalloc(n * size);
memset(mem, 0, n * size);
return mem;
}
void *
ruby_xrealloc(ptr, size)
void *ptr;
long size;
{
void *mem;
if (size < 0) {
rb_raise(rb_eArgError, "negative re-allocation size");
}
if (!ptr) {
return xmalloc(size);
}
if (size == 0) {
size = 1;
}
RUBY_CRITICAL(mem = realloc(ptr, size));
if (!mem) {
longlife_collection = Qtrue;
garbage_collect("OOM in realloc()");
RUBY_CRITICAL(mem = realloc(ptr, size));
if (!mem) {
rb_memerror();
}
}
#if STACK_WIPE_SITES & 0x200
rb_gc_update_stack_extent();
#endif
return mem;
}
void
ruby_xfree(x)
void *x;
{
if (x) {
RUBY_CRITICAL(free(x));
}
}
static int dont_gc;
static int need_call_final = 0;
static st_table *finalizer_table = 0;
/*******************************************************************/
/*
* call-seq:
* GC.enable => true or false
*
* Enables garbage collection, returning <code>true</code> if garbage
* collection was previously disabled.
*
* GC.disable #=> false
* GC.enable #=> true
* GC.enable #=> false
*
*/
VALUE
rb_gc_enable()
{
int old = dont_gc;
dont_gc = Qfalse;
return old;
}
/*
* call-seq:
* GC.disable => true or false
*
* Disables garbage collection, returning <code>true</code> if garbage
* collection was already disabled.
*
* GC.disable #=> false
* GC.disable #=> true
*
*/
VALUE
rb_gc_disable()
{
int old = dont_gc;
dont_gc = Qtrue;
return old;
}
VALUE rb_mGC;
static struct gc_list {
VALUE *varptr;
struct gc_list *next;
} *global_List = 0;
void
rb_gc_register_address(addr)
VALUE *addr;
{
struct gc_list *tmp;
tmp = ALLOC(struct gc_list);
tmp->next = global_List;
tmp->varptr = addr;
global_List = tmp;
}
void
rb_gc_unregister_address(addr)
VALUE *addr;
{
struct gc_list *tmp = global_List;
if (tmp->varptr == addr) {
global_List = tmp->next;
RUBY_CRITICAL(free(tmp));
return;
}
while (tmp->next) {
if (tmp->next->varptr == addr) {
struct gc_list *t = tmp->next;
tmp->next = tmp->next->next;
RUBY_CRITICAL(free(t));
break;
}
tmp = tmp->next;
}
}
void
rb_global_variable(var)
VALUE *var;
{
rb_gc_register_address(var);
}
#if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__CYGWIN__)
#pragma pack(push, 1) /* magic for reducing sizeof(RVALUE): 24 -> 20 */
#endif
static FILE* gc_data_file = NULL;
typedef struct RVALUE {
union {
struct {
unsigned long flags; /* always 0 for freed obj */
struct RVALUE *next;
} free;
struct RBasic basic;
struct RObject object;
struct RClass klass;
struct RFloat flonum;
struct RString string;
struct RArray array;
struct RRegexp regexp;
struct RHash hash;
struct RData data;
struct RStruct rstruct;
struct RBignum bignum;
struct RFile file;
struct RNode node;
struct RMatch match;
struct RVarmap varmap;
struct SCOPE scope;
} as;
#ifdef GC_DEBUG
source_position_t *source_pos;
#endif
} RVALUE;
#ifdef GC_DEBUG
int
gc_debug_check_printable(char *str)
{
int j, str_len;
if (!str) {
return 0;
}
str_len = strlen(str);
for (j = 0; j < str_len; j++) {
if (!isprint(str[j])) {
return 0;
}
}
return 1;
}
static int
source_position_compare(source_position_t *x, source_position_t *y)
{
return x->frames_hash != y->frames_hash;
}
static int
source_position_hash(source_position_t *x)
{
return x->frames_hash;
}
static struct st_hash_type source_positions_type = {
compare: source_position_compare,
hash: source_position_hash
};
char *
gc_debug_get_backtrace(source_position_t *source_pos)
{
size_t len;
size_t backtrace_len = 0;
char line_str[11];
char cfunc_str[63];
char *func_str;
/* FIXME Some source_pos aren't correctly marked; thus the guards. */
while (source_pos && gc_debug_check_printable(source_pos->file) && source_pos->line > -2 && source_pos->line < 1000000) {
if (snprintf(line_str, sizeof(line_str), "%d", source_pos->line) > sizeof(line_str)) {
rb_bug("Overflow on line_str");
}
func_str = rb_id2name(source_pos->func);
if (!func_str) {
/* Unknown internal address; can be converted to a function name via atos (OS X) or addr2name (Linux) */
if (snprintf(cfunc_str, sizeof(cfunc_str), "<0x%x>", (unsigned int) source_pos->func) > sizeof(cfunc_str)) {
rb_bug("Overflow on cfunc_str");
}
func_str = cfunc_str;
}
len = 3 + strlen(source_pos->file) + strlen(line_str) + (func_str ? strlen(func_str) + 1 : 0);
if (backtrace_str_buffer_len < backtrace_len + len) {
backtrace_str_buffer_len = backtrace_len + len;
backtrace_str_buffer = realloc(backtrace_str_buffer, backtrace_str_buffer_len);
if (!backtrace_str_buffer) {
rb_bug("OOM on backtrace_str_buffer realloc");
}
}
snprintf(backtrace_str_buffer + backtrace_len, len,
" %s:%d%s%s",
source_pos->file,
source_pos->line,
func_str ? "#" : "",
func_str ? func_str : "");
/* gc_debug_check_printable(backtrace_str_buffer); */
backtrace_len += len - 1; /* overwrite \0 on next pass */
source_pos = source_pos->parent;
}
return backtrace_str_buffer;
}
static int
gc_debug_print_source_locations(source_position_t *source_pos, int *counts, FILE *output_file)
{
int i;
char *backtrace_str = 0;
for(i = 0; i < OBJ_TYPE_COUNT; i++) {
if (counts[i] > 0) {
if (!backtrace_str) {
backtrace_str = gc_debug_get_backtrace(source_pos);
}
fprintf(output_file, "%8d %-15s%s\n",
counts[i],
i < T_NODE ? gc_debug_obj_type(i) : gc_debug_node_type(i - T_NODE),
backtrace_str);
}
}
free(counts);
return ST_CONTINUE;
}
static source_position_t *
gc_debug_new_source_pos(char *file, int line, struct FRAME *frame)
{
source_position_t *new_source_pos;
source_position_t *source_pos;
source_position_t *parent;
NODE *func_node;
ID func = 0;
new_source_pos = malloc(sizeof(source_position_t));
if (!new_source_pos) {
rb_bug("OOM during source_position_list allocation");
}
if(frame) {
parent = frame->source_pos;
if (frame->last_func) {
func = frame->last_func;
} else if (frame->node) {
if (nd_type(frame->node) == NODE_IFUNC) {
func = (ID)frame->node->nd_cfnc;
} else {
func = frame->node->nd_mid;
}
}
}
else {
parent = 0;
}
if (!func) {
func = ruby_sourcefunc;
}
if(!file && frame) {
func_node = search_method(frame->last_class, func, 0);
if (func_node && func_node->nd_file) {
file = func_node->nd_file;
}
}
if (!file) {
file = rb_source_filename("(ruby)");
}
new_source_pos->func = func;
new_source_pos->file = file;
new_source_pos->line = line;
new_source_pos->parent = parent;
new_source_pos->frames_hash =
(parent ? parent->frames_hash * 31 * 31 * 31 : 0) +
(VALUE) file * 31 * 31 +
(VALUE) func * 31 +
line;
if (!st_lookup(source_positions, (st_data_t)new_source_pos, (st_data_t *)&source_pos)) {
source_pos = new_source_pos;
st_insert(source_positions, (st_data_t)(source_pos), (long int) source_pos);
} else {
free(new_source_pos);
}
return source_pos;
}
/**
* Creates a source position for a stack frame. It will describe the location
* of the call site for the call this frame belongs to.
*/
void
gc_debug_get_frame_source_pos(struct FRAME *frame) {
char *file = 0;
int line = 0;
if (!(GC_DEBUG_ON && gc_debug_dump)) {
return;
}
if (frame->node) {
// Location of the call site in the caller
file = frame->node->nd_file;
line = nd_line(frame->node);
}
frame->source_pos = gc_debug_new_source_pos(file, line, frame->prev);
}
static source_position_t *
gc_debug_get_obj_source_pos()
{
struct FRAME *frame = ruby_frame;
char *file = 0;
int line = 0;
if (frame->last_func == ID_ALLOCATOR) {
frame = frame->prev;
}
if (ruby_current_node) {
file = ruby_current_node->nd_file;
line = nd_line(ruby_current_node);
}
return gc_debug_new_source_pos(file, line, frame);
}
static void
gc_debug_add_to_source_pos_table(st_table *table, RVALUE *p, int type)
{
int *counts;
if (!st_lookup(table, (st_data_t)p->source_pos, (st_data_t *)&counts)) {
counts = malloc(sizeof(int) * OBJ_TYPE_COUNT);
MEMZERO(counts, int, OBJ_TYPE_COUNT);
st_insert(table, (st_data_t)p->source_pos, (long int) counts);
}
if (type == T_NODE) type += nd_type(p);
counts[type] +=1;
}
static void
gc_debug_dump_source_pos_table(st_table *table, lifetime_t lt, char *suffix)
{
char fname[255];
/* You can parse the output file with simple Unix tools. For example, to
see objects that remain on the eden heap after collection, coalesce
them by the first 5 backtrace lines, and pretty-print the output, run:
cat /tmp/rb_gc_debug_objects.eden.live.txt |
awk '
BEGIN {} { sums[$2," ", $3, " ", $4, " ", $5, " ", $6, " ", $7, " ", $8] += $1 }
END { for (i in sums) { print sums[i], i } }' |
sort -rni |
head -n 30 |
ruby -e "STDIN.readlines.each {|l| puts l.split}"
*/
snprintf(fname, 255, gc_dump_file_pattern, lifetime_name_lower[lt], suffix);
FILE* output_file = fopen(fname, "w");
if (!output_file) {
GC_DEBUG_PRINTF("ERROR: Can't open %s for writing\n", fname);
return;
}
st_foreach(table, gc_debug_print_source_locations, (long int)output_file);
}
#endif /* GC_DEBUG */
#if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__CYGWIN__)
#pragma pack(pop)
#endif
static RVALUE *deferred_final_list = 0;
static int heaps_increment = 10;
static struct heaps_slot {
void *membase;
RVALUE *slot;
int limit;
RVALUE *slotlimit;
int *marks;
int marks_size;
enum lifetime lifetime;
} *heaps;
static int heaps_length = 0;
static int heaps_used = 0;
/* Too large a heap size and you can never free a page, due to fragmentation. Too
small, and you have too many heaps and get stack errors. */
static int heap_size = 32768;
static int heap_increase_rate = 4;
static int eden_heaps = 24;
static int eden_preemptive_heaps = 4;
static int eden_preemptive_total_free_slots;
typedef struct heaps_space {
int num_heaps;
unsigned int total_slots;
unsigned int total_free_slots;
enum lifetime lifetime;
RVALUE *freelist;
} heaps_space_t;
static heaps_space_t eden_heaps_space;
static heaps_space_t longlife_heaps_space;
typedef struct remembered_set {
RVALUE *obj;
struct remembered_set *next;
} remembered_set_t;
static remembered_set_t *remembered_set_ptr;
static remembered_set_t *remembered_set_freed;
typedef struct longlife_recent_allocations_set {
RVALUE *obj;
struct longlife_recent_allocations_set *next;
} longlife_recent_allocations_set_t;
static longlife_recent_allocations_set_t *longlife_recent_allocations_set_ptr;
static longlife_recent_allocations_set_t *longlife_recent_allocations_set_freed;
static RVALUE *himem, *lomem;
#include "marktable.h"
#include "marktable.c"
static int gc_cycles = 0;
static int gc_longlife_cycles = 0;
static void set_gc_parameters()
{
#define WITH_ENV_VAR(varname, variable, type, conv, cond, fmt) \
do { \
char* ptr = getenv(varname); \
type val = variable; \
char* __varname__ = varname; \
char* __fmt__ = fmt; \
if(ptr != NULL) { \
val = conv(ptr); \
if(cond) {
#define END_WITH_ENV_VAR } } if (gc_data_file) { GC_DEBUG_PRINTF(__fmt__, __varname__, val) } } while(0);
#define WITH_INT_ENV_VAR(varname, variable) WITH_ENV_VAR(varname, variable, int, atoi, val > 0, "%s=%d\n")
#define WITH_FLOAT_ENV_VAR(varname, variable) WITH_ENV_VAR(varname, variable, double, atof, val > 0, "%s=%f\n")
#define SET_INT_ENV_VAR(varname, variable) WITH_INT_ENV_VAR(varname, variable) variable = val; END_WITH_ENV_VAR
#define SET_FLOAT_ENV_VAR(varname, variable) WITH_FLOAT_ENV_VAR(varname, variable) variable = val; END_WITH_ENV_VAR
#define SET_BOOLEAN_ENV_VAR(varname, variable) WITH_INT_ENV_VAR(varname, variable) variable = Qtrue; END_WITH_ENV_VAR
#ifdef GC_DEBUG
SET_BOOLEAN_ENV_VAR("RUBY_GC_DEBUG", gc_debug_on)
if (gc_debug_on) {
gc_data_file_name = getenv("RUBY_GC_DATA_FILE");
if (gc_data_file_name == NULL) {
gc_data_file_name = "/dev/stderr";
}
FILE* data_file = fopen(gc_data_file_name, "w");
if (data_file != NULL) {
gc_data_file = data_file;
// stderr is always unbuffered. user-specified files should be likewise
setbuf(gc_data_file, NULL);
} else {
fprintf(stderr, "Can't open RUBY_GC_DATA_FILE for writing\n");
gc_data_file_name = "/dev/stderr";
gc_data_file = fopen(gc_data_file_name, "w");
}
}
gc_dump_file_pattern = getenv("RUBY_GC_DUMP_FILE_PATTERN");
if(gc_dump_file_pattern == NULL) {
gc_dump_file_pattern = "/tmp/rb_gc_debug_objects.%s.%s.txt";
}
#endif
SET_INT_ENV_VAR("RUBY_GC_HEAP_SIZE", heap_size)
SET_INT_ENV_VAR("RUBY_GC_HEAP_INCREASE_RATE", heap_increase_rate)
SET_INT_ENV_VAR("RUBY_GC_EDEN_HEAPS", eden_heaps)
SET_INT_ENV_VAR("RUBY_GC_EDEN_PREEMPTIVE_HEAPS", eden_preemptive_heaps)
eden_preemptive_total_free_slots = eden_preemptive_heaps * heap_size;
WITH_FLOAT_ENV_VAR("RUBY_GC_LONGLIFE_LAZINESS", longlife_laziness)
if (val >= 1) {
val = DEFAULT_LONGLIFE_LAZINESS;
}
longlife_laziness = val;
END_WITH_ENV_VAR
#ifdef GC_DEBUG
GC_DEBUG_PRINT("GC_DEBUG is available\n")
SET_BOOLEAN_ENV_VAR("RUBY_GC_DEBUG_LONGLIFE_DISABLE", gc_debug_longlife_disabled)
SET_BOOLEAN_ENV_VAR("RUBY_GC_DEBUG_STRESS", gc_debug_stress)
SET_BOOLEAN_ENV_VAR("RUBY_GC_DEBUG_ALWAYS_MARK", gc_debug_always_mark)
if (GC_DEBUG_ON) {
SET_BOOLEAN_ENV_VAR("RUBY_GC_DEBUG_SUMMARY", gc_debug_summary)
SET_BOOLEAN_ENV_VAR("RUBY_GC_DEBUG_DUMP", gc_debug_dump)
}
#else
GC_DEBUG_PRINT("GC_DEBUG not available (configure with --enable-gc-debug)\n")
#endif
}
/*
* call-seq:
* GC.log String => String
*
* Logs string to the GC data file and returns it.
*
* GC.log "manual GC call" #=> "manual GC call"
*
*/
VALUE
rb_gc_log(self, original_str)
VALUE self, original_str;
{
if (original_str == Qnil) {
fprintf(gc_data_file, "\n");
}
else {
VALUE str = StringValue(original_str);
char *p = RSTRING(str)->ptr;
fprintf(gc_data_file, "%s\n", p);
}
return original_str;
}
static inline void push_freelist(heaps_space_t *heaps_space, RVALUE *p)
{
MEMZERO((void*)p, RVALUE, 1);
p->as.free.next = heaps_space->freelist;
heaps_space->freelist = p;
}
static int
add_heap(heaps_space_t *heaps_space)
{
RVALUE *p, *pend;
int new_heap_size = heap_size;
if (heaps_used == heaps_length) {
/* Realloc heaps */
struct heaps_slot *p;
int length;
heaps_length += heaps_increment;
length = heaps_length*sizeof(struct heaps_slot);
RUBY_CRITICAL(
if (heaps_used > 0) {
p = (struct heaps_slot *)realloc(heaps, length);
if (p) {
heaps = p;
/* Clear the last_heap cache to remove potentially erroneous references. */
rb_mark_table_prepare();
}
}
else {
p = heaps = (struct heaps_slot *)malloc(length);
});
if (p == 0) {
rb_memerror();
}
}
for (;;) {
RUBY_CRITICAL(p = (RVALUE*)malloc(sizeof(RVALUE)*(new_heap_size)));
if (p == 0) {
rb_memerror();
}
heaps[heaps_used].membase = p;
// Align heap pointer to RVALUE size, if necessary
if ((VALUE)p % sizeof(RVALUE) != 0) {
p = (RVALUE*)((VALUE)p + sizeof(RVALUE) - ((VALUE)p % sizeof(RVALUE)));
new_heap_size--;
}
heaps[heaps_used].slot = p;
heaps[heaps_used].limit = new_heap_size;
heaps[heaps_used].slotlimit = p + new_heap_size;
heaps[heaps_used].marks_size = (int) (ceil(new_heap_size / (sizeof(int) * 8.0)));
heaps[heaps_used].marks = (int *) calloc(heaps[heaps_used].marks_size, sizeof(int));
heaps[heaps_used].lifetime = heaps_space->lifetime;
break;
}
pend = p + new_heap_size;
if (lomem == 0 || lomem > p) {
lomem = p;
}
if (himem < pend) {
himem = pend;
}
heaps_space->total_slots += new_heap_size;
heaps_space->total_free_slots += new_heap_size;
heaps_space->num_heaps++;
heaps_used++;
/* Add to freelist in reverse order. */
while (pend > p) {
pend--;
push_freelist(heaps_space, pend);
}
return new_heap_size;
}
#define RANY(o) ((RVALUE*)(o))
int
rb_during_gc()
{
return during_gc;
}
static inline VALUE
pop_freelist(heaps_space_t* heaps_space)
{
VALUE obj = (VALUE)heaps_space->freelist;
heaps_space->freelist = heaps_space->freelist->as.free.next;
heaps_space->total_free_slots--;
RANY(obj)->as.free.next = 0;
#ifdef GC_DEBUG
MEMZERO((void*)obj, RVALUE, 1);
if (GC_DEBUG_ON && gc_debug_dump) {
RANY(obj)->source_pos = gc_debug_get_obj_source_pos();
}
#endif
return obj;
}
static inline void
add_heap_if_needed(heaps_space_t* heaps_space)
{
int new_heap_size;
if (!heaps_space->freelist) {
new_heap_size = add_heap(heaps_space);
GC_DEBUG_PRINTF("*** %s heap added (out of space) (size %d) ***\n",
lifetime_name[heaps_space->lifetime], new_heap_size)
}
}
/* Legacy gem compatibility only. Do not use. */
VALUE
rb_newobj() {
return rb_newobj_eden();
}
VALUE
rb_newobj_eden()
{
VALUE obj;
#ifdef GC_DEBUG
if (during_gc) {
rb_bug("object allocation during garbage collection phase");
}
if (gc_debug_stress) {
longlife_collection = Qtrue;
garbage_collect("GC stress is enabled");
}
#endif
if (!eden_heaps_space.freelist) {
garbage_collect("no free space in the eden");
}
add_heap_if_needed(&eden_heaps_space);
obj = pop_freelist(&eden_heaps_space);
allocated_objects++;
return obj;
}
VALUE
rb_newobj_longlife()
{
VALUE obj;
#ifdef GC_DEBUG
if (gc_debug_longlife_disabled) {
return rb_newobj_eden();
}
if (during_gc) {
rb_bug("object allocation during garbage collection phase");
}
if (gc_debug_stress) {
longlife_collection = Qtrue;