/
gc.c
3141 lines (2811 loc) · 72.5 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 <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_MALLOC_LIMIT
#if defined(MSDOS) || defined(__human68k__)
#define GC_MALLOC_LIMIT 200000
#else
#define GC_MALLOC_LIMIT (2000000*sizeof(VALUE))
#endif
#endif
#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)
static VALUE *stack_limit, *gc_stack_limit;
static size_t malloc_increase = 0;
static size_t malloc_limit = GC_MALLOC_LIMIT;
static size_t unstressed_malloc_limit = GC_MALLOC_LIMIT;
static void run_final();
static VALUE nomem_error;
static void garbage_collect();
/*
* call-seq:
* GC.stress => true or false
*
* returns current status of GC stress mode.
*/
static VALUE
gc_stress_get(self)
VALUE self;
{
return malloc_limit ? Qfalse : Qtrue;
}
/*
* 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_stress_set(self, bool)
VALUE self, bool;
{
rb_secure(2);
if (!RTEST(bool))
malloc_limit = unstressed_malloc_limit;
else if (malloc_limit > 0) {
unstressed_malloc_limit = malloc_limit;
malloc_limit = 0;
}
return bool;
}
#ifdef MBARI_API
/*
* call-seq:
* GC.limit => increase limit in bytes
*
* Get the # of bytes that may be allocated before triggering
* a mark and sweep by the garbarge collector to reclaim unused storage.
*
* <i>Only available when MBARI_API extentions are enabled at build time</i>
*/
static VALUE gc_getlimit(VALUE mod)
{
return ULONG2NUM(malloc_limit);
}
/*
* call-seq:
* GC.limit= => updated increase limit in bytes
*
* Set the # of bytes that may be allocated before triggering
* a mark and sweep by the garbarge collector to reclaim unused storage.
* Attempts to set the GC.limit= less than 0 will be ignored.
*
* GC.limit=5000000 #=> 5000000
* GC.limit #=> 5000000
* GC.limit=-50 #=> 5000000
* GC.limit=0 #=> 0 #functionally equivalent to GC.stress=true
*
* <i>Only available when MBARI_API extentions are enabled at build time</i>
*/
static VALUE gc_setlimit(VALUE mod, VALUE newLimit)
{
long limit = NUM2LONG(newLimit);
rb_secure(2);
if (limit < 0) return gc_getlimit(mod);
malloc_limit = limit;
return newLimit;
}
/*
* call-seq:
* GC.growth
*
* Get # of bytes that have been allocated since the last mark & sweep
*
* <i>Only available when MBARI_API extentions are enabled at build time</i>
*/
static VALUE gc_growth(VALUE mod)
{
return ULONG2NUM(malloc_increase);
}
/*
* call-seq:
* GC.exorcise
*
* Purge ghost references from recently freed stack space
*
* <i>Only available when MBARI_API extentions are enabled at build time</i>
*/
static VALUE gc_exorcise(VALUE mod)
{
rb_gc_wipe_stack();
return Qnil;
}
#endif
/*
* restore default malloc_limit
*/
void rb_gc_unstress(void)
{
malloc_limit = unstressed_malloc_limit;
}
NORETURN(void rb_exc_jump _((VALUE)));
static unsigned long live_objects = 0;
unsigned long rb_os_live_objects()
{ return live_objects; }
#if defined(HAVE_LONG_LONG)
static unsigned long long allocated_objects = 0;
unsigned long long rb_os_allocated_objects()
{ return allocated_objects; }
#else
static unsigned long allocated_objects = 0;
unsigned long rb_os_allocated_objects()
{ return allocated_objects; }
#endif
void
rb_memerror()
{
rb_thread_t th = rb_curr_thread;
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);
}
long gc_allocated_size = 0;
long gc_num_allocations = 0;
static int gc_statistics = 0;
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;
if ((malloc_increase+=size) > malloc_limit) {
garbage_collect();
malloc_increase = size;
}
RUBY_CRITICAL(mem = malloc(size));
if (!mem) {
garbage_collect();
RUBY_CRITICAL(mem = malloc(size));
if (!mem) {
rb_memerror();
}
}
if (gc_statistics) {
gc_allocated_size += size;
gc_num_allocations += 1;
}
#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;
if ((malloc_increase+=size) > malloc_limit) {
garbage_collect();
malloc_increase = size;
}
RUBY_CRITICAL(mem = realloc(ptr, size));
if (!mem) {
garbage_collect();
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));
}
#if HAVE_LONG_LONG
#define GC_TIME_TYPE LONG_LONG
#else
#define GC_TIME_TYPE long
#endif
extern int ruby_in_compile;
static int dont_gc;
static GC_TIME_TYPE gc_time = 0;
static int gc_collections = 0;
static int during_gc;
static int need_call_final = 0;
static st_table *finalizer_table = 0;
/************************************************************
* Heap and copy-on-write debugging support functions
************************************************************/
/* Compound structure, containing debugging options. */
static struct {
FILE *terminal;
/* Whether to allocate Ruby heaps by mmapping a file. This makes it easier to see how many
* bytes in heaps have been made dirty, using memory analysis tools.
*/
int alloc_heap_with_file;
/* Whether to ask the user to press Enter, before garbage collection starts.
* Can be used to check how many pages are made dirty by the garbage collector.
*/
int prompt_before_gc;
/* Whether to ask the user to press Enter before the sweep phase of the garbage
* collector starts. */
int prompt_before_sweep;
/* Whether to ask the user to press Enter after the sweep phase of the garbage
* collector starts. */
int prompt_after_sweep;
int print_sweeped_objects;
} debug_options;
#define OPTION_ENABLED(name) (getenv((name)) && *getenv((name)) && *getenv((name)) != '0')
static VALUE
rb_gc_init_debugging(VALUE self)
{
if (debug_options.terminal != NULL) {
fclose(debug_options.terminal);
debug_options.terminal = NULL;
}
if (getenv("RD_TERMINAL")) {
debug_options.terminal = fopen(getenv("RD_TERMINAL"), "a+");
if (debug_options.terminal == NULL) {
int e = errno;
fprintf(stderr, "Cannot open %s: %s (%d)\n", getenv("RD_TERMINAL"), strerror(e), e);
fflush(stderr);
}
}
debug_options.alloc_heap_with_file = OPTION_ENABLED("RD_ALLOC_HEAP_WITH_FILE");
debug_options.prompt_before_gc = OPTION_ENABLED("RD_PROMPT_BEFORE_GC");
debug_options.prompt_before_sweep = OPTION_ENABLED("RD_PROMPT_BEFORE_SWEEP");
debug_options.prompt_after_sweep = OPTION_ENABLED("RD_PROMPT_AFTER_SWEEP");
debug_options.print_sweeped_objects = OPTION_ENABLED("RD_PRINT_SWEEPED_OBJECTS");
return Qnil;
}
static void
debug_print(const char *message, ...)
{
va_list ap;
va_start(ap, message);
if (debug_options.terminal != NULL) {
vfprintf(debug_options.terminal, message, ap);
fflush(debug_options.terminal);
} else {
vfprintf(stderr, message, ap);
fflush(stderr);
}
va_end(ap);
}
#define debug_prompt(prompt) \
do { \
if (debug_options.terminal != NULL) { \
fprintf(debug_options.terminal, prompt); \
fflush(debug_options.terminal); \
getc(debug_options.terminal); \
} else { \
fprintf(stderr, prompt); \
fflush(stderr); \
getchar(); \
} \
} while (0)
/************************************
* Heap (de)allocation functions
************************************/
typedef struct {
int fd;
size_t size;
} FileHeapAllocatorMetaData;
static void *
alloc_ruby_heap_with_file(size_t size)
{
FileHeapAllocatorMetaData meta;
meta.fd = open("/dev/zero", O_RDONLY);
meta.size = size;
if (meta.fd == -1) {
return NULL;
} else {
void *memory = mmap(NULL, size + sizeof(meta), PROT_READ | PROT_WRITE,
MAP_PRIVATE, meta.fd, 0);
if (memory == NULL) {
return NULL;
} else {
memcpy(memory, &meta, sizeof(meta));
return memory + sizeof(meta);
}
}
}
static void *
alloc_ruby_heap(size_t size)
{
if (debug_options.alloc_heap_with_file) {
return alloc_ruby_heap_with_file(size);
} else {
return malloc(size);
}
}
static void
free_ruby_heap_with_file(void *heap)
{
FileHeapAllocatorMetaData *meta = (FileHeapAllocatorMetaData *)
(heap - sizeof(FileHeapAllocatorMetaData));
close(meta->fd);
munmap(heap, meta->size + sizeof(FileHeapAllocatorMetaData));
}
static void
free_ruby_heap(void *heap)
{
if (debug_options.alloc_heap_with_file) {
free_ruby_heap_with_file(heap);
} else {
free(heap);
}
}
/*******************************************************************/
/*
* 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;
}
/*
* call-seq:
* GC.enable_stats => true or false
*
* Enables garbage collection statistics, returning <code>true</code> if garbage
* collection statistics was already enabled.
*
* GC.enable_stats #=> false or true
* GC.enable_stats #=> true
*
*/
VALUE
rb_gc_enable_stats()
{
int old = gc_statistics;
gc_statistics = Qtrue;
return old;
}
/*
* call-seq:
* GC.disable_stats => true or false
*
* Disables garbage collection statistics, returning <code>true</code> if garbage
* collection statistics was already disabled.
*
* GC.disable_stats #=> false or true
* GC.disable_stats #=> true
*
*/
VALUE
rb_gc_disable_stats()
{
int old = gc_statistics;
gc_statistics = Qfalse;
gc_allocated_size = 0;
gc_num_allocations = 0;
return old;
}
/*
* call-seq:
* GC.clear_stats => nil
*
* Clears garbage collection statistics, returning nil. This resets the number
* of collections (GC.collections) and the time used (GC.time) to 0.
*
* GC.clear_stats #=> nil
*
*/
VALUE
rb_gc_clear_stats()
{
gc_collections = 0;
gc_time = 0;
gc_allocated_size = 0;
gc_num_allocations = 0;
return Qnil;
}
/*
* call-seq:
* GC.allocated_size => Integer
*
* Returns the size of memory (in bytes) allocated since GC statistics collection
* was enabled.
*
* GC.allocated_size #=> 35
*
*/
VALUE
rb_gc_allocated_size()
{
return INT2NUM(gc_allocated_size);
}
/*
* call-seq:
* GC.num_allocations => Integer
*
* Returns the number of memory allocations since GC statistics collection
* was enabled.
*
* GC.num_allocations #=> 150
*
*/
VALUE
rb_gc_num_allocations()
{
return INT2NUM(gc_num_allocations);
}
/*
* call-seq:
* GC.collections => Integer
*
* Returns the number of garbage collections performed while GC statistics collection
* was enabled.
*
* GC.collections #=> 35
*
*/
VALUE
rb_gc_collections()
{
return INT2NUM(gc_collections);
}
/*
* call-seq:
* GC.time => Integer
*
* Returns the time spent during garbage collection while GC statistics collection
* was enabled (in micro seconds).
*
* GC.time #=> 20000
*
*/
VALUE
rb_gc_time()
{
#if HAVE_LONG_LONG
return LL2NUM(gc_time);
#else
return LONG2NUM(gc_time);
#endif
}
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;
}
}
#undef GC_DEBUG
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
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
char *file;
int line;
#endif
} RVALUE;
#if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__CYGWIN__)
#pragma pack(pop)
#endif
static RVALUE *freelist = 0;
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;
} *heaps;
static int heaps_length = 0;
static int heaps_used = 0;
static int heap_min_slots = 10000;
static int heap_slots = 10000;
static int heap_free_min = 4096;
static int heap_slots_increment = 10000;
static double heap_slots_growth_factor = 1.8;
static int verbose_gc_stats = Qfalse;
static FILE* gc_data_file = NULL;
static RVALUE *himem, *lomem;
#include "marktable.h"
#include "marktable.c"
#include "fastmarktable.c"
static int gc_cycles = 0;
static void set_gc_parameters()
{
char *gc_stats_ptr, *min_slots_ptr, *free_min_ptr, *heap_slots_incr_ptr,
*heap_incr_ptr, *malloc_limit_ptr, *gc_heap_file_ptr, *heap_slots_growth_factor_ptr;
gc_data_file = stderr;
gc_stats_ptr = getenv("RUBY_GC_STATS");
if (gc_stats_ptr != NULL) {
int gc_stats_i = atoi(gc_stats_ptr);
if (gc_stats_i > 0) {
verbose_gc_stats = Qtrue;
}
}
gc_heap_file_ptr = getenv("RUBY_GC_DATA_FILE");
if (gc_heap_file_ptr != NULL) {
FILE* data_file = fopen(gc_heap_file_ptr, "w");
if (data_file != NULL) {
gc_data_file = data_file;
}
else {
fprintf(stderr,
"can't open gc log file %s for writing, using default\n", gc_heap_file_ptr);
}
}
min_slots_ptr = getenv("RUBY_HEAP_MIN_SLOTS");
if (min_slots_ptr != NULL) {
int min_slots_i = atoi(min_slots_ptr);
if (verbose_gc_stats) {
fprintf(gc_data_file, "RUBY_HEAP_MIN_SLOTS=%s\n", min_slots_ptr);
}
if (min_slots_i > 0) {
heap_slots = min_slots_i;
heap_min_slots = min_slots_i;
}
}
free_min_ptr = getenv("RUBY_HEAP_FREE_MIN");
if (free_min_ptr != NULL) {
int free_min_i = atoi(free_min_ptr);
if (verbose_gc_stats) {
fprintf(gc_data_file, "RUBY_HEAP_FREE_MIN=%s\n", free_min_ptr);
}
if (free_min_i > 0) {
heap_free_min = free_min_i;
}
}
heap_incr_ptr = getenv("RUBY_HEAP_INCREMENT");
if (heap_incr_ptr != NULL) {
int heap_incr_i = atoi(heap_incr_ptr);
if (verbose_gc_stats) {
fprintf(gc_data_file, "RUBY_HEAP_INCREMENT=%s\n", heap_incr_ptr);
}
if (heap_incr_i > 0) {
heaps_increment = heap_incr_i;
}
}
heap_slots_incr_ptr = getenv("RUBY_HEAP_SLOTS_INCREMENT");
if (heap_slots_incr_ptr != NULL) {
int heap_slots_incr_i = atoi(heap_slots_incr_ptr);
if (verbose_gc_stats) {
fprintf(gc_data_file, "RUBY_HEAP_SLOTS_INCREMENT=%s\n", heap_slots_incr_ptr);
}
if (heap_slots_incr_i > 0) {
heap_slots_increment = heap_slots_incr_i;
}
}
heap_slots_growth_factor_ptr = getenv("RUBY_HEAP_SLOTS_GROWTH_FACTOR");
if (heap_slots_growth_factor_ptr != NULL) {
double heap_slots_growth_factor_d = atoi(heap_slots_growth_factor_ptr);
if (verbose_gc_stats) {
fprintf(gc_data_file, "RUBY_HEAP_SLOTS_GROWTH_FACTOR=%s\n", heap_slots_growth_factor_ptr);
}
if (heap_slots_growth_factor_d > 0) {
heap_slots_growth_factor = heap_slots_growth_factor_d;
}
}
malloc_limit_ptr = getenv("RUBY_GC_MALLOC_LIMIT");
if (malloc_limit_ptr != NULL) {
int malloc_limit_i = atol(malloc_limit_ptr);
if (verbose_gc_stats) {
fprintf(gc_data_file, "RUBY_GC_MALLOC_LIMIT=%s\n", malloc_limit_ptr);
}
if (malloc_limit_i > 0) {
malloc_limit = malloc_limit_i;
}
}
}
/*
* call-seq:
* GC.dump => nil
*
* dumps information about the current GC data structures to the GC log file
*
* GC.dump #=> nil
*
*/
VALUE
rb_gc_dump()
{
int i;
for (i = 0; i < heaps_used; i++) {
int heap_size = heaps[i].limit;
fprintf(gc_data_file, "HEAP[%2d]: size=%7d\n", i, heap_size);
}
return Qnil;
}
/*
* 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 void
add_heap()
{
RVALUE *p, *pend;
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;
}
else {
p = heaps = (struct heaps_slot *)malloc(length);
});
if (p == 0) rb_memerror();
}
for (;;) {
RUBY_CRITICAL(p = (RVALUE*)alloc_ruby_heap(sizeof(RVALUE)*(heap_slots+1)));
if (p == 0) {
if (heap_slots == heap_min_slots) {
rb_memerror();
}
heap_slots = heap_min_slots;
continue;
}
heaps[heaps_used].membase = p;
if ((VALUE)p % sizeof(RVALUE) == 0)
heap_slots += 1;
else
p = (RVALUE*)((VALUE)p + sizeof(RVALUE) - ((VALUE)p % sizeof(RVALUE)));
heaps[heaps_used].slot = p;
heaps[heaps_used].limit = heap_slots;
heaps[heaps_used].slotlimit = p + heap_slots;
heaps[heaps_used].marks_size = (int) (ceil(heap_slots / (sizeof(int) * 8.0)));
heaps[heaps_used].marks = (int *) calloc(heaps[heaps_used].marks_size, sizeof(int));
break;
}
pend = p + heap_slots;
if (lomem == 0 || lomem > p) lomem = p;
if (himem < pend) himem = pend;
heaps_used++;
heap_slots += heap_slots_increment;
heap_slots_increment *= heap_slots_growth_factor;
if (heap_slots <= 0) heap_slots = heap_min_slots;
while (p < pend) {
p->as.free.flags = 0;
p->as.free.next = freelist;
freelist = p;
p++;
}
}
#define RANY(o) ((RVALUE*)(o))
int
rb_during_gc()
{
return during_gc;
}
VALUE
rb_newobj()
{
VALUE obj;
if (during_gc)
rb_bug("object allocation during garbage collection phase");
if (!malloc_limit || !freelist) garbage_collect();