-
Notifications
You must be signed in to change notification settings - Fork 1
/
gc.c
3420 lines (3008 loc) · 81.1 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$
created at: Tue Oct 5 09:44:46 JST 1993
Copyright (C) 1993-2007 Yukihiro Matsumoto
Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
Copyright (C) 2000 Information-technology Promotion Agency, Japan
**********************************************************************/
#include "ruby/ruby.h"
#include "ruby/st.h"
#include "ruby/re.h"
#include "ruby/io.h"
#include "ruby/util.h"
#include "eval_intern.h"
#include "vm_core.h"
#include "gc.h"
#include <stdio.h>
#include <setjmp.h>
#include <sys/types.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
#ifdef HAVE_VALGRIND_MEMCHECK_H
# include <valgrind/memcheck.h>
# ifndef VALGRIND_MAKE_MEM_DEFINED
# define VALGRIND_MAKE_MEM_DEFINED(p, n) VALGRIND_MAKE_READABLE(p, n)
# endif
# ifndef VALGRIND_MAKE_MEM_UNDEFINED
# define VALGRIND_MAKE_MEM_UNDEFINED(p, n) VALGRIND_MAKE_WRITABLE(p, n)
# endif
#else
# define VALGRIND_MAKE_MEM_DEFINED(p, n) /* empty */
# define VALGRIND_MAKE_MEM_UNDEFINED(p, n) /* empty */
#endif
int rb_io_fptr_finalize(struct rb_io_t*);
#define rb_setjmp(env) RUBY_SETJMP(env)
#define rb_jmp_buf rb_jmpbuf_t
/* Make alloca work the best possible way. */
#ifdef __GNUC__
# ifndef atarist
# ifndef alloca
# define alloca __builtin_alloca
# endif
# endif /* atarist */
#else
# ifdef HAVE_ALLOCA_H
# include <alloca.h>
# else
# ifdef _AIX
#pragma alloca
# else
# ifndef alloca /* predefined by HP cc +Olibcalls */
void *alloca ();
# endif
# endif /* AIX */
# endif /* HAVE_ALLOCA_H */
#endif /* __GNUC__ */
#ifndef GC_MALLOC_LIMIT
#define GC_MALLOC_LIMIT 8000000
#endif
#define nomem_error GET_VM()->special_exceptions[ruby_error_nomemory]
#define MARK_STACK_MAX 1024
int ruby_gc_debug_indent = 0;
/* for GC profile */
#define GC_PROFILE_MORE_DETAIL 0
typedef struct gc_profile_record {
double gc_time;
double gc_mark_time;
double gc_sweep_time;
double gc_invoke_time;
size_t heap_use_slots;
size_t heap_live_objects;
size_t heap_free_objects;
size_t heap_total_objects;
size_t heap_use_size;
size_t heap_total_size;
int have_finalize;
int is_marked;
size_t allocate_increase;
size_t allocate_limit;
} gc_profile_record;
static double
getrusage_time(void)
{
#ifdef RUSAGE_SELF
struct rusage usage;
struct timeval time;
getrusage(RUSAGE_SELF, &usage);
time = usage.ru_utime;
return time.tv_sec + time.tv_usec * 1e-6;
#elif defined _WIN32
FILETIME creation_time, exit_time, kernel_time, user_time;
ULARGE_INTEGER ui;
LONG_LONG q;
double t;
if (GetProcessTimes(GetCurrentProcess(),
&creation_time, &exit_time, &kernel_time, &user_time) == 0)
{
return 0.0;
}
memcpy(&ui, &user_time, sizeof(FILETIME));
q = ui.QuadPart / 10L;
t = (DWORD)(q % 1000000L) * 1e-6;
q /= 1000000L;
#ifdef __GNUC__
t += q;
#else
t += (double)(DWORD)(q >> 16) * (1 << 16);
t += (DWORD)q & ~(~0 << 16);
#endif
return t;
#else
return 0.0;
#endif
}
#define GC_PROF_TIMER_START do {\
if (objspace->profile.run) {\
if (!objspace->profile.record) {\
objspace->profile.size = 1000;\
objspace->profile.record = malloc(sizeof(gc_profile_record) * objspace->profile.size);\
}\
if (count >= objspace->profile.size) {\
objspace->profile.size += 1000;\
objspace->profile.record = realloc(objspace->profile.record, sizeof(gc_profile_record) * objspace->profile.size);\
}\
if (!objspace->profile.record) {\
rb_bug("gc_profile malloc or realloc miss");\
}\
MEMZERO(&objspace->profile.record[count], gc_profile_record, 1);\
gc_time = getrusage_time();\
objspace->profile.record[count].gc_invoke_time = gc_time - objspace->profile.invoke_time;\
}\
} while(0)
#define GC_PROF_TIMER_STOP(marked) do {\
if (objspace->profile.run) {\
gc_time = getrusage_time() - gc_time;\
if (gc_time < 0) gc_time = 0;\
objspace->profile.record[count].gc_time = gc_time;\
objspace->profile.record[count].is_marked = !!(marked);\
GC_PROF_SET_HEAP_INFO(objspace->profile.record[count]);\
objspace->profile.count++;\
}\
} while(0)
#if GC_PROFILE_MORE_DETAIL
#define INIT_GC_PROF_PARAMS double gc_time = 0, sweep_time = 0;\
size_t count = objspace->profile.count, total = 0, live = 0
#define GC_PROF_MARK_TIMER_START double mark_time = 0;\
do {\
if (objspace->profile.run) {\
mark_time = getrusage_time();\
}\
} while(0)
#define GC_PROF_MARK_TIMER_STOP do {\
if (objspace->profile.run) {\
mark_time = getrusage_time() - mark_time;\
if (mark_time < 0) mark_time = 0;\
objspace->profile.record[objspace->profile.count].gc_mark_time = mark_time;\
}\
} while(0)
#define GC_PROF_SWEEP_TIMER_START do {\
if (objspace->profile.run) {\
sweep_time = getrusage_time();\
}\
} while(0)
#define GC_PROF_SWEEP_TIMER_STOP do {\
if (objspace->profile.run) {\
sweep_time = getrusage_time() - sweep_time;\
if (sweep_time < 0) sweep_time = 0;\
objspace->profile.record[count].gc_sweep_time = sweep_time;\
}\
} while(0)
#define GC_PROF_SET_MALLOC_INFO do {\
if (objspace->profile.run) {\
gc_profile_record *record = &objspace->profile.record[objspace->profile.count];\
record->allocate_increase = malloc_increase;\
record->allocate_limit = malloc_limit; \
}\
} while(0)
#define GC_PROF_SET_HEAP_INFO(record) do {\
live = objspace->heap.live_num;\
total = heaps_used * HEAP_OBJ_LIMIT;\
record.heap_use_slots = heaps_used;\
record.heap_live_objects = live;\
record.heap_free_objects = total - live;\
record.heap_total_objects = total;\
record.have_finalize = deferred_final_list ? Qtrue : Qfalse;\
record.heap_use_size = live * sizeof(RVALUE);\
record.heap_total_size = total * sizeof(RVALUE);\
} while(0)
#define GC_PROF_INC_LIVE_NUM objspace->heap.live_num++
#define GC_PROF_DEC_LIVE_NUM objspace->heap.live_num--
#else
#define INIT_GC_PROF_PARAMS double gc_time = 0;\
size_t count = objspace->profile.count, total = 0, live = 0
#define GC_PROF_MARK_TIMER_START
#define GC_PROF_MARK_TIMER_STOP
#define GC_PROF_SWEEP_TIMER_START
#define GC_PROF_SWEEP_TIMER_STOP
#define GC_PROF_SET_MALLOC_INFO
#define GC_PROF_SET_HEAP_INFO(record) do {\
live = objspace->heap.live_num;\
total = heaps_used * HEAP_OBJ_LIMIT;\
record.heap_total_objects = total;\
record.heap_use_size = live * sizeof(RVALUE);\
record.heap_total_size = total * sizeof(RVALUE);\
} while(0)
#define GC_PROF_INC_LIVE_NUM
#define GC_PROF_DEC_LIVE_NUM
#endif
#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 {
VALUE 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 RTypedData typeddata;
struct RStruct rstruct;
struct RBignum bignum;
struct RFile file;
struct RNode node;
struct RMatch match;
struct RRational rational;
struct RComplex complex;
} as;
#ifdef GC_DEBUG
const char *file;
int line;
#endif
} RVALUE;
#if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__CYGWIN__)
#pragma pack(pop)
#endif
struct heaps_slot {
void *membase;
RVALUE *slot;
size_t limit;
struct heaps_slot *next;
struct heaps_slot *prev;
};
struct sorted_heaps_slot {
RVALUE *start;
RVALUE *end;
struct heaps_slot *slot;
};
#define HEAP_MIN_SLOTS 10000
#define FREE_MIN 4096
struct gc_list {
VALUE *varptr;
struct gc_list *next;
};
#define CALC_EXACT_MALLOC_SIZE 0
typedef struct rb_objspace {
struct {
size_t limit;
size_t increase;
#if CALC_EXACT_MALLOC_SIZE
size_t allocated_size;
size_t allocations;
#endif
} malloc_params;
struct {
size_t increment;
struct heaps_slot *ptr;
struct heaps_slot *sweep_slots;
struct sorted_heaps_slot *sorted;
size_t length;
size_t used;
RVALUE *freelist;
RVALUE *range[2];
RVALUE *freed;
size_t live_num;
size_t free_num;
size_t free_min;
size_t do_heap_free;
} heap;
struct {
int dont_gc;
int during_gc;
} flags;
struct {
st_table *table;
RVALUE *deferred;
} final;
struct {
VALUE buffer[MARK_STACK_MAX];
VALUE *ptr;
int overflow;
} markstack;
struct {
int run;
gc_profile_record *record;
size_t count;
size_t size;
double invoke_time;
} profile;
struct gc_list *global_list;
unsigned int count;
int gc_stress;
} rb_objspace_t;
#if defined(ENABLE_VM_OBJSPACE) && ENABLE_VM_OBJSPACE
#define rb_objspace (*GET_VM()->objspace)
static int ruby_initial_gc_stress = 0;
int *ruby_initial_gc_stress_ptr = &ruby_initial_gc_stress;
#else
static rb_objspace_t rb_objspace = {{GC_MALLOC_LIMIT}, {HEAP_MIN_SLOTS}};
int *ruby_initial_gc_stress_ptr = &rb_objspace.gc_stress;
#endif
#define malloc_limit objspace->malloc_params.limit
#define malloc_increase objspace->malloc_params.increase
#define heaps objspace->heap.ptr
#define heaps_length objspace->heap.length
#define heaps_used objspace->heap.used
#define freelist objspace->heap.freelist
#define lomem objspace->heap.range[0]
#define himem objspace->heap.range[1]
#define heaps_inc objspace->heap.increment
#define heaps_freed objspace->heap.freed
#define dont_gc objspace->flags.dont_gc
#define during_gc objspace->flags.during_gc
#define finalizer_table objspace->final.table
#define deferred_final_list objspace->final.deferred
#define mark_stack objspace->markstack.buffer
#define mark_stack_ptr objspace->markstack.ptr
#define mark_stack_overflow objspace->markstack.overflow
#define global_List objspace->global_list
#define ruby_gc_stress objspace->gc_stress
#define need_call_final (finalizer_table && finalizer_table->num_entries)
static void rb_objspace_call_finalizer(rb_objspace_t *objspace);
#if defined(ENABLE_VM_OBJSPACE) && ENABLE_VM_OBJSPACE
rb_objspace_t *
rb_objspace_alloc(void)
{
rb_objspace_t *objspace = malloc(sizeof(rb_objspace_t));
memset(objspace, 0, sizeof(*objspace));
malloc_limit = GC_MALLOC_LIMIT;
ruby_gc_stress = ruby_initial_gc_stress;
return objspace;
}
void
rb_objspace_free(rb_objspace_t *objspace)
{
rb_objspace_call_finalizer(objspace);
if (objspace->profile.record) {
free(objspace->profile.record);
objspace->profile.record = 0;
}
if (global_List) {
struct gc_list *list, *next;
for (list = global_List; list; list = next) {
next = list->next;
free(list);
}
}
if (objspace->heap.sorted) {
size_t i;
for (i = 0; i < heaps_used; ++i) {
free(objspace->heap.sorted[i].slot->membase);
free(objspace->heap.sorted[i].slot);
}
free(objspace->heap.sorted);
heaps_used = 0;
heaps = 0;
}
free(objspace);
}
#endif
/* tiny heap size */
/* 32KB */
/*#define HEAP_SIZE 0x8000 */
/* 128KB */
/*#define HEAP_SIZE 0x20000 */
/* 64KB */
/*#define HEAP_SIZE 0x10000 */
/* 16KB */
#define HEAP_SIZE 0x4000
/* 8KB */
/*#define HEAP_SIZE 0x2000 */
/* 4KB */
/*#define HEAP_SIZE 0x1000 */
/* 2KB */
/*#define HEAP_SIZE 0x800 */
#define HEAP_OBJ_LIMIT (HEAP_SIZE / sizeof(struct RVALUE))
extern VALUE rb_cMutex;
extern st_table *rb_class_tbl;
int ruby_disable_gc_stress = 0;
static void run_final(rb_objspace_t *objspace, VALUE obj);
static int garbage_collect(rb_objspace_t *objspace);
static int gc_lazy_sweep(rb_objspace_t *objspace);
void
rb_global_variable(VALUE *var)
{
rb_gc_register_address(var);
}
static void *
ruby_memerror_body(void *dummy)
{
rb_memerror();
return 0;
}
static void
ruby_memerror(void)
{
if (ruby_thread_has_gvl_p()) {
rb_memerror();
}
else {
if (ruby_native_thread_p()) {
rb_thread_call_with_gvl(ruby_memerror_body, 0);
}
else {
/* no ruby thread */
fprintf(stderr, "[FATAL] failed to allocate memory\n");
exit(EXIT_FAILURE);
}
}
}
void
rb_memerror(void)
{
rb_thread_t *th = GET_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_thread_raised_clear(th);
GET_THREAD()->errinfo = nomem_error;
JUMP_TAG(TAG_RAISE);
}
rb_thread_raised_set(th, RAISED_NOMEMORY);
rb_exc_raise(nomem_error);
}
/*
* call-seq:
* GC.stress -> true or false
*
* returns current status of GC stress mode.
*/
static VALUE
gc_stress_get(VALUE self)
{
rb_objspace_t *objspace = &rb_objspace;
return ruby_gc_stress ? Qtrue : Qfalse;
}
/*
* 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(VALUE self, VALUE flag)
{
rb_objspace_t *objspace = &rb_objspace;
rb_secure(2);
ruby_gc_stress = RTEST(flag);
return flag;
}
/*
* call-seq:
* GC::Profiler.enable? -> true or false
*
* returns current status of GC profile mode.
*/
static VALUE
gc_profile_enable_get(VALUE self)
{
rb_objspace_t *objspace = &rb_objspace;
return objspace->profile.run;
}
/*
* call-seq:
* GC::Profiler.enable -> nil
*
* updates GC profile mode.
* start profiler for GC.
*
*/
static VALUE
gc_profile_enable(void)
{
rb_objspace_t *objspace = &rb_objspace;
objspace->profile.run = TRUE;
return Qnil;
}
/*
* call-seq:
* GC::Profiler.disable -> nil
*
* updates GC profile mode.
* stop profiler for GC.
*
*/
static VALUE
gc_profile_disable(void)
{
rb_objspace_t *objspace = &rb_objspace;
objspace->profile.run = FALSE;
return Qnil;
}
/*
* call-seq:
* GC::Profiler.clear -> nil
*
* clear before profile data.
*
*/
static VALUE
gc_profile_clear(void)
{
rb_objspace_t *objspace = &rb_objspace;
MEMZERO(objspace->profile.record, gc_profile_record, objspace->profile.size);
objspace->profile.count = 0;
return Qnil;
}
static void *
negative_size_allocation_error_with_gvl(void *ptr)
{
rb_raise(rb_eNoMemError, "%s", (const char *)ptr);
return 0; /* should not be reached */
}
static void
negative_size_allocation_error(const char *msg)
{
if (ruby_thread_has_gvl_p()) {
rb_raise(rb_eNoMemError, "%s", msg);
}
else {
if (ruby_native_thread_p()) {
rb_thread_call_with_gvl(negative_size_allocation_error_with_gvl, (void *)msg);
}
else {
fprintf(stderr, "[FATAL] %s\n", msg);
exit(EXIT_FAILURE);
}
}
}
static void *
gc_with_gvl(void *ptr)
{
return (void *)(VALUE)garbage_collect((rb_objspace_t *)ptr);
}
static int
garbage_collect_with_gvl(rb_objspace_t *objspace)
{
if (dont_gc) return TRUE;
if (ruby_thread_has_gvl_p()) {
return garbage_collect(objspace);
}
else {
if (ruby_native_thread_p()) {
return (int)(VALUE)rb_thread_call_with_gvl(gc_with_gvl, (void *)objspace);
}
else {
/* no ruby thread */
fprintf(stderr, "[FATAL] failed to allocate memory\n");
exit(EXIT_FAILURE);
}
}
}
static void vm_xfree(rb_objspace_t *objspace, void *ptr);
static void *
vm_xmalloc(rb_objspace_t *objspace, size_t size)
{
void *mem;
if ((ssize_t)size < 0) {
negative_size_allocation_error("negative allocation size (or too big)");
}
if (size == 0) size = 1;
#if CALC_EXACT_MALLOC_SIZE
size += sizeof(size_t);
#endif
if ((ruby_gc_stress && !ruby_disable_gc_stress) ||
(malloc_increase+size) > malloc_limit) {
garbage_collect_with_gvl(objspace);
}
mem = malloc(size);
if (!mem) {
if (garbage_collect_with_gvl(objspace)) {
mem = malloc(size);
}
if (!mem) {
ruby_memerror();
}
}
malloc_increase += size;
#if CALC_EXACT_MALLOC_SIZE
objspace->malloc_params.allocated_size += size;
objspace->malloc_params.allocations++;
((size_t *)mem)[0] = size;
mem = (size_t *)mem + 1;
#endif
return mem;
}
static void *
vm_xrealloc(rb_objspace_t *objspace, void *ptr, size_t size)
{
void *mem;
if ((ssize_t)size < 0) {
negative_size_allocation_error("negative re-allocation size");
}
if (!ptr) return vm_xmalloc(objspace, size);
if (size == 0) {
vm_xfree(objspace, ptr);
return 0;
}
if (ruby_gc_stress && !ruby_disable_gc_stress)
garbage_collect_with_gvl(objspace);
#if CALC_EXACT_MALLOC_SIZE
size += sizeof(size_t);
objspace->malloc_params.allocated_size -= size;
ptr = (size_t *)ptr - 1;
#endif
mem = realloc(ptr, size);
if (!mem) {
if (garbage_collect_with_gvl(objspace)) {
mem = realloc(ptr, size);
}
if (!mem) {
ruby_memerror();
}
}
malloc_increase += size;
#if CALC_EXACT_MALLOC_SIZE
objspace->malloc_params.allocated_size += size;
((size_t *)mem)[0] = size;
mem = (size_t *)mem + 1;
#endif
return mem;
}
static void
vm_xfree(rb_objspace_t *objspace, void *ptr)
{
#if CALC_EXACT_MALLOC_SIZE
size_t size;
ptr = ((size_t *)ptr) - 1;
size = ((size_t*)ptr)[0];
objspace->malloc_params.allocated_size -= size;
objspace->malloc_params.allocations--;
#endif
free(ptr);
}
void *
ruby_xmalloc(size_t size)
{
return vm_xmalloc(&rb_objspace, size);
}
void *
ruby_xmalloc2(size_t n, size_t size)
{
size_t len = size * n;
if (n != 0 && size != len / n) {
rb_raise(rb_eArgError, "malloc: possible integer overflow");
}
return vm_xmalloc(&rb_objspace, len);
}
void *
ruby_xcalloc(size_t n, size_t size)
{
void *mem = ruby_xmalloc2(n, size);
memset(mem, 0, n * size);
return mem;
}
void *
ruby_xrealloc(void *ptr, size_t size)
{
return vm_xrealloc(&rb_objspace, ptr, size);
}
void *
ruby_xrealloc2(void *ptr, size_t n, size_t size)
{
size_t len = size * n;
if (n != 0 && size != len / n) {
rb_raise(rb_eArgError, "realloc: possible integer overflow");
}
return ruby_xrealloc(ptr, len);
}
void
ruby_xfree(void *x)
{
if (x)
vm_xfree(&rb_objspace, x);
}
/*
* 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(void)
{
rb_objspace_t *objspace = &rb_objspace;
int old = dont_gc;
dont_gc = FALSE;
return old ? Qtrue : Qfalse;
}
/*
* 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(void)
{
rb_objspace_t *objspace = &rb_objspace;
int old = dont_gc;
dont_gc = TRUE;
return old ? Qtrue : Qfalse;
}
VALUE rb_mGC;
void
rb_gc_register_mark_object(VALUE obj)
{
VALUE ary = GET_THREAD()->vm->mark_object_ary;
rb_ary_push(ary, obj);
}
void
rb_gc_register_address(VALUE *addr)
{
rb_objspace_t *objspace = &rb_objspace;
struct gc_list *tmp;
tmp = ALLOC(struct gc_list);
tmp->next = global_List;
tmp->varptr = addr;
global_List = tmp;
}
void
rb_gc_unregister_address(VALUE *addr)
{
rb_objspace_t *objspace = &rb_objspace;
struct gc_list *tmp = global_List;
if (tmp->varptr == addr) {
global_List = tmp->next;
xfree(tmp);
return;
}
while (tmp->next) {
if (tmp->next->varptr == addr) {
struct gc_list *t = tmp->next;
tmp->next = tmp->next->next;
xfree(t);
break;
}
tmp = tmp->next;
}
}
static void
allocate_sorted_heaps(rb_objspace_t *objspace, size_t next_heaps_length)
{
struct sorted_heaps_slot *p;
size_t size;
size = next_heaps_length*sizeof(struct sorted_heaps_slot);
if (heaps_used > 0) {
p = (struct sorted_heaps_slot *)realloc(objspace->heap.sorted, size);
if (p) objspace->heap.sorted = p;
}
else {
p = objspace->heap.sorted = (struct sorted_heaps_slot *)malloc(size);
}
if (p == 0) {
during_gc = 0;
rb_memerror();
}
heaps_length = next_heaps_length;
}
static void
assign_heap_slot(rb_objspace_t *objspace)
{
RVALUE *p, *pend, *membase;
struct heaps_slot *slot;
size_t hi, lo, mid;
size_t objs;
objs = HEAP_OBJ_LIMIT;
p = (RVALUE*)malloc(HEAP_SIZE);
slot = (struct heaps_slot *)malloc(sizeof(struct heaps_slot));
MEMZERO((void*)slot, struct heaps_slot, 1);
if (p == 0 || slot == 0) {
during_gc = 0;
rb_memerror();
}
slot->next = heaps;
if (heaps) heaps->prev = slot;
heaps = slot;
membase = p;
if ((VALUE)p % sizeof(RVALUE) != 0) {
p = (RVALUE*)((VALUE)p + sizeof(RVALUE) - ((VALUE)p % sizeof(RVALUE)));
if ((HEAP_SIZE - HEAP_OBJ_LIMIT * sizeof(RVALUE)) < (size_t)((char*)p - (char*)membase)) {
objs--;
}
}
lo = 0;
hi = heaps_used;
while (lo < hi) {
register RVALUE *mid_membase;
mid = (lo + hi) / 2;
mid_membase = objspace->heap.sorted[mid].slot->membase;
if (mid_membase < membase) {
lo = mid + 1;
}
else if (mid_membase > membase) {
hi = mid;
}
else {
rb_bug("same heap slot is allocated: %p at %"PRIuVALUE, (void *)membase, (VALUE)mid);
}
}
if (hi < heaps_used) {
MEMMOVE(&objspace->heap.sorted[hi+1], &objspace->heap.sorted[hi], struct sorted_heaps_slot, heaps_used - hi);
}
objspace->heap.sorted[hi].slot = slot;
objspace->heap.sorted[hi].start = p;
objspace->heap.sorted[hi].end = (p + objs);
heaps->membase = membase;
heaps->slot = p;
heaps->limit = objs;
objspace->heap.free_num += objs;
pend = p + objs;
if (lomem == 0 || lomem > p) lomem = p;
if (himem < pend) himem = pend;
heaps_used++;
while (p < pend) {
p->as.free.flags = 0;
p->as.free.next = freelist;
freelist = p;
p++;
}
}
static void
init_heap(rb_objspace_t *objspace)
{
size_t add, i;
add = HEAP_MIN_SLOTS / HEAP_OBJ_LIMIT;
if (!add) {
add = 1;
}
if ((heaps_used + add) > heaps_length) {
allocate_sorted_heaps(objspace, heaps_used + add);
}