-
Notifications
You must be signed in to change notification settings - Fork 0
/
tinygc.c
executable file
·3931 lines (3637 loc) · 102 KB
/
tinygc.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
/*
* @(#) tinygc.c -- TinyGC (Tiny Garbage Collector) source.
* Copyright (C) 2006-2010 Ivan Maidanski <ivmai@mail.ru> All rights reserved.
**
* Version: 2.6
* See also files: gc.h, gc_gcj.h, gc_mark.h, javaxfc.h
* Required: any ANSI C compiler (assume GC-safe compilation).
*/
/*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
**
* This software is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License (GPL) for more details.
**
* Linking this library statically or dynamically with other modules is
* making a combined work based on this library. Thus, the terms and
* conditions of the GNU General Public License cover the whole
* combination.
**
* As a special exception, the copyright holders of this library give you
* permission to link this library with independent modules to produce an
* executable, regardless of the license terms of these independent
* modules, and to copy and distribute the resulting executable under
* terms of your choice, provided that you also meet, for each linked
* independent module, the terms and conditions of the license of that
* module. An independent module is a module which is not derived from
* or based on this library. If you modify this library, you may extend
* this exception to your version of the library, but you are not
* obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/
/*
* Control macros: ALL_INTERIOR_POINTERS, DONT_ADD_BYTE_AT_END,
* FINALIZE_ON_DEMAND, GC_DLL, GC_DONT_EXPAND, GC_GCJ_SUPPORT, GC_GETENV_SKIP,
* GC_IGNORE_GCJ_INFO, GC_MISC_EXCLUDE, GC_NO_DLINKS, GC_NO_FNLZ,
* GC_NO_GCBASE, GC_NO_INACTIVE, GC_NO_REGISTER_DLINK,
* GC_OMIT_REGISTER_KEYWORD, GC_PRINT_MSGS, GC_THREADS, GC_USE_GETTIMEOFDAY,
* GC_USE_WIN32_SYSTEMTIME, GC_WIN32_THREADS, GC_WIN32_WCE,
* JAVA_FINALIZATION_NOT_NEEDED.
**
* Macros for tuning (also see in gc.h): CONST, GC_ASYNC_PUSHREGS_BEGIN,
* GC_ASYNC_PUSHREGS_END, GC_CLIBDECL, GC_CORE_API, GC_CORE_CALL,
* GC_CORE_FREE, GC_CORE_MALLOC, GC_DATASTATIC, GC_DATASTART, GC_DATASTART2,
* GC_DATAEND, GC_DATAEND2, GC_FATAL_ABORT, GC_FREE_SPACE_DIVISOR,
* GC_MAX_RETRIES, GC_FASTCALL, GC_INLINE_STATIC, GC_LAZYREFILL_BIGCNT,
* GC_LAZYREFILL_COUNT, GC_LOG2_OFFIGNORE, GC_NEW_LINE, GC_PUSHREGS_BEGIN,
* GC_PUSHREGS_END, GC_SIG_SUSPEND, GC_STACKBOTTOM, GC_STACKBOTTOMVAR,
* GC_STACKLEN, GC_STACKLENVAR, GC_STATIC, GC_THREAD_MUTEX_DEFATTR,
* GC_THREAD_YIELD, GC_WIN32_CONTEXT_SP_NAME, GC_YIELD_MAX_ATTEMPT, INLINE,
* MARK_DESCR_OFFSET.
*/
#ifndef _SETJMP_H
#include <setjmp.h>
/* int setjmp(jmp_buf); */
#endif
#ifndef _STDLIB_H
#include <stdlib.h>
/* long atol(const char *); */
/* void exit(int); */
/* void free(void *); */
/* char *getenv(const char *); */
/* void *malloc(size_t); */
#endif
#ifndef _STRING_H
#include <string.h>
/* void *memset(void *, int, size_t); */
#endif
#ifndef _LIMITS_H
#include <limits.h>
#endif
#ifdef GC_WIN32_THREADS
#ifndef _WINDOWS_H
#include <windows.h>
/* BOOL CloseHandle(HANDLE); */
/* HANDLE CreateEvent(SECURITY_ATTRIBUTES *, BOOL, BOOL, LPCTSTR); */
/* BOOL DuplicateHandle(HANDLE, HANDLE, HANDLE, HANDLE *, DWORD, BOOL, DWORD); */
/* HANDLE GetCurrentProcess(void); */
/* HANDLE GetCurrentThread(void); */
/* DWORD GetCurrentThreadId(void); */
/* BOOL GetThreadContext(HANDLE, CONTEXT *); */
/* LONG InterlockedExchange(LONG *, LONG); */
/* DWORD ResumeThread(HANDLE); */
/* BOOL SetEvent(HANDLE); */
/* void Sleep(DWORD); */
/* DWORD SuspendThread(HANDLE); */
/* DWORD WaitForSingleObject(HANDLE, DWORD); */
#endif
#ifndef GC_THREADS
#define GC_THREADS 1
#endif
#else /* GC_WIN32_THREADS */
#ifdef GC_THREADS
#ifndef _ERRNO_H
#include <errno.h>
/* int errno; */
#endif
#ifndef _SIGNAL_H
#include <signal.h>
/* void (*signal(int, void (*)(int)))(int); */
#endif
#ifndef _PTHREAD_H
#include <pthread.h>
/* int pthread_kill(pthread_t, int); */
/* int pthread_mutex_init(pthread_mutex_t *, const pthread_mutexattr_t *); */
/* int pthread_mutex_lock(pthread_mutex_t *); */
/* int pthread_mutex_unlock(pthread_mutex_t *); */
/* pthread_t pthread_self(void); */
#endif
#ifndef _SCHED_H
#include <sched.h>
/* int sched_yield(void); */
#endif
#ifdef pthread_usleep_np
/* #include <pthread.h> */
/* unsigned pthread_usleep_np(unsigned); */
#else
#ifndef _UNISTD_H
#include <unistd.h>
/* int usleep(useconds_t); */
#endif
#define pthread_usleep_np usleep
#endif
#endif /* GC_THREADS */
#endif /* ! GC_WIN32_THREADS */
#ifdef GC_PRINT_MSGS
#ifndef _STDIO_H
#include <stdio.h>
/* int fprintf(FILE *, const char *, ...); */
/* FILE * const stderr; */
/* FILE * const stdout; */
#endif
#ifdef GC_USE_WIN32_SYSTEMTIME
#ifndef _WINDOWS_H
#include <windows.h>
/* void GetSystemTime(SYSTEMTIME *); */
#endif
#define GC_CURTIME_T SYSTEMTIME
#define GC_CURTIME_GETMS(pcurt) (GetSystemTime(pcurt), ((((unsigned long)(pcurt)->wDay * 24 + (unsigned long)(pcurt)->wHour) * 60 + (unsigned long)(pcurt)->wMinute) * 60 + (unsigned long)(pcurt)->wSecond) * 1000 + (unsigned long)(pcurt)->wMilliseconds)
#else /* GC_USE_WIN32_SYSTEMTIME */
#ifdef GC_USE_GETTIMEOFDAY
#ifndef _SYS_TIME_H
#include <sys/time.h>
/* int gettimeofday(struct timeval *, void *); */
#endif
#define GC_CURTIME_T struct timeval
#ifdef _SVID_GETTOD
#define GC_CURTIME_GETMS(pcurt) (gettimeofday((void *)(pcurt)), (unsigned long)(pcurt)->tv_sec * 1000 + (unsigned long)(pcurt)->tv_usec / 1000)
#else
#define GC_CURTIME_GETMS(pcurt) (gettimeofday((void *)(pcurt), NULL), (unsigned long)(pcurt)->tv_sec * 1000 + (unsigned long)(pcurt)->tv_usec / 1000)
#endif
#else /* GC_USE_GETTIMEOFDAY */
#ifndef _TIME_H
#include <time.h>
#endif
#ifndef _SYS_TIMEB_H
#include <sys/timeb.h>
/* void ftime(struct timeb *); */
#endif
#define GC_CURTIME_T struct timeb
#define GC_CURTIME_GETMS(pcurt) (ftime(pcurt), (unsigned long)(pcurt)->time * 1000 + (unsigned long)(pcurt)->millitm)
#endif /* ! GC_USE_GETTIMEOFDAY */
#endif /* ! GC_USE_WIN32_SYSTEMTIME */
#define GC_SIZE_TO_ULKB(size) ((unsigned long)((size) >> 10))
#ifndef GC_NEW_LINE
#define GC_NEW_LINE "\n"
#endif
#endif /* GC_PRINT_MSGS */
#ifndef GC_API
#ifdef GC_DLL
#define GC_API __declspec(dllexport)
#endif
#endif
#include "gc.h"
#include "gc_gcj.h"
#include "gc_mark.h"
#include "javaxfc.h"
#ifndef NULL
#define NULL (void *)0
#endif
#ifndef CHAR_BIT
#define CHAR_BIT 8
#endif
#ifndef CONST
#define CONST const
#endif
#ifndef GC_FATAL_ABORT
#define GC_FATAL_ABORT exit(-1) /* abort(), DebugBreak() */
#endif
#ifndef GC_DATASTATIC
#define GC_DATASTATIC static
#endif
#ifndef GC_STATIC
#define GC_STATIC static
#endif
#ifndef GC_INLINE_STATIC
#ifdef INLINE
#define GC_INLINE_STATIC GC_STATIC INLINE
#else
#define GC_INLINE_STATIC GC_STATIC __inline
#endif
#endif
#ifndef GC_FASTCALL
#define GC_FASTCALL __fastcall
#endif
#ifndef GC_CORE_API
#define GC_CORE_API extern
#endif
#ifndef GC_CORE_CALL
#define GC_CORE_CALL GC_CALL
#endif
#ifdef GC_PUSHREGS_BEGIN
#ifndef GC_PUSHREGS_END
#define GC_PUSHREGS_END (void)0
#endif
#else
#define GC_PUSHREGS_BEGIN jmp_buf buf; (void)setjmp(buf)
#ifndef GC_PUSHREGS_END
#define GC_PUSHREGS_END GC_noop1((GC_word)(&buf))
#endif
#endif
#define GC_MEM_BZERO(ptr, size) (void)memset(ptr, '\0', (size_t)(size))
#ifdef GC_THREADS
#ifdef GC_WIN32_THREADS
#ifndef GC_WIN32_CONTEXT_SP_NAME
#ifdef _M_AMD64
#define GC_WIN32_CONTEXT_SP_NAME Rsp
#else
#ifdef _M_X64
#define GC_WIN32_CONTEXT_SP_NAME Rsp
#else
#ifdef __x86_64
#define GC_WIN32_CONTEXT_SP_NAME Rsp
#endif
#endif
#endif
#endif
#ifndef GC_WIN32_CONTEXT_SP_NAME
#ifdef _M_ALPHA
#define GC_WIN32_CONTEXT_SP_NAME IntSp
#else
#ifdef _ALPHA_
#define GC_WIN32_CONTEXT_SP_NAME IntSp
#else
#ifdef _M_MRX000
#define GC_WIN32_CONTEXT_SP_NAME IntSp
#else
#ifdef _MIPS_
#define GC_WIN32_CONTEXT_SP_NAME IntSp
#endif
#endif
#endif
#endif
#endif
#ifndef GC_WIN32_CONTEXT_SP_NAME
#ifdef _M_ARM
#define GC_WIN32_CONTEXT_SP_NAME Sp
#else
#ifdef _ARM_
#define GC_WIN32_CONTEXT_SP_NAME Sp
#else
#ifdef _M_PPC
#define GC_WIN32_CONTEXT_SP_NAME Gpr1
#else
#ifdef _PPC_
#define GC_WIN32_CONTEXT_SP_NAME Gpr1
#else
#ifdef _M_SH
#define GC_WIN32_CONTEXT_SP_NAME R15
#else
#ifdef SHx
#define GC_WIN32_CONTEXT_SP_NAME R15
#endif
#endif
#endif
#endif
#endif
#endif
#endif
#ifndef GC_WIN32_CONTEXT_SP_NAME
#define GC_WIN32_CONTEXT_SP_NAME Esp /* x86 */
#endif
#ifndef GC_THREAD_YIELD
#define GC_THREAD_YIELD Sleep(10) /* "long" yield */
#endif
#define GC_THREAD_ID_T DWORD
#ifdef GC_WIN32_WCE
#define GC_THREAD_HANDLE(stkroot) ((HANDLE)(GC_word)(stkroot)->thread_id)
#else
#define GC_THREAD_HANDLE(stkroot) ((stkroot)->thread_handle)
#endif
#else /* GC_WIN32_THREADS */
#ifndef GC_SIG_SUSPEND
#ifdef SIGPWR
#define GC_SIG_SUSPEND SIGPWR
#else
#ifdef SIGUSR1
#define GC_SIG_SUSPEND SIGUSR1
#else
#define GC_SIG_SUSPEND SIGILL
#endif
#endif
#endif
#ifndef GC_CLIBDECL
#ifdef __CLIB
#define GC_CLIBDECL __CLIB
#else
#ifdef _USERENTRY
#define GC_CLIBDECL _USERENTRY
#else
#ifdef _RTL_FUNC
#define GC_CLIBDECL _RTL_FUNC
#else
#define GC_CLIBDECL __cdecl
#endif
#endif
#endif
#endif
#ifdef GC_ASYNC_PUSHREGS_BEGIN
#ifndef GC_ASYNC_PUSHREGS_END
#define GC_ASYNC_PUSHREGS_END (void)0
#endif
#else
#define GC_ASYNC_PUSHREGS_BEGIN GC_PUSHREGS_BEGIN
#ifndef GC_ASYNC_PUSHREGS_END
#define GC_ASYNC_PUSHREGS_END GC_PUSHREGS_END
#endif
#endif
#ifndef GC_THREAD_MUTEX_DEFATTR
#ifdef pthread_mutexattr_default
#define GC_THREAD_MUTEX_DEFATTR pthread_mutexattr_default
#else
#define GC_THREAD_MUTEX_DEFATTR NULL
#endif
#endif
#ifndef GC_THREAD_YIELD
#define GC_THREAD_YIELD (void)sched_yield()
#endif
#ifndef GC_YIELD_MAX_ATTEMPT
#define GC_YIELD_MAX_ATTEMPT 2
#endif
#define GC_ERRNO_SET(value) (void)(errno = (value))
#define GC_THREAD_ID_T pthread_t
#endif /* ! GC_WIN32_THREADS */
#endif /* GC_THREADS */
#ifndef MARK_DESCR_OFFSET
#define MARK_DESCR_OFFSET sizeof(GC_word)
#endif
#ifndef GC_LOG2_OFFIGNORE
#define GC_LOG2_OFFIGNORE 8 /* must be at least 3 */
#endif
#ifndef GC_FREE_SPACE_DIVISOR
#define GC_FREE_SPACE_DIVISOR 3
#endif
#ifndef GC_MAX_RETRIES
#define GC_MAX_RETRIES 2
#endif
#ifndef GC_LAZYREFILL_COUNT
#define GC_LAZYREFILL_COUNT 10 /* must be at least 3 */
#endif
#ifndef GC_LAZYREFILL_BIGCNT
#define GC_LAZYREFILL_BIGCNT 1024
#endif
#ifdef GC_OMIT_REGISTER_KEYWORD
#define GC_REGISTER_KEYWORD /* empty */
#else
#define GC_REGISTER_KEYWORD register
#endif
#define GC_DEFAULT_LOG2_OBJSIZE 8
#define GC_DEFAULT_LOG2_SIZE 3
#define GC_MEM_SIZELIMIT ((GC_word)((~(size_t)0) >> 1) - ((GC_word)1 << (sizeof(int) << 1)))
#define GC_ATOMIC_MASK (((~(GC_word)0) >> 1) + 1)
#ifdef GC_GCJ_SUPPORT
#define GC_HASDSLEN_MASK (((GC_word)GC_ATOMIC_MASK) >> 1)
#else
#define GC_HASDSLEN_MASK 0
#endif
#define GC_NEVER_COLLECT (int)((((unsigned)-1) >> 1) + 1)
#ifndef GC_NO_DLINKS
#define GC_HIDE_POINTER(ptr) (~(GC_word)(ptr))
#endif
#define GC_RANDOM_SEED(gcdata) (((gcdata)->total_heapsize ^ (gcdata)->allocd_before_gc) + ((gcdata)->bytes_allocd ^ (gcdata)->marked_bytes) + ((gcdata)->free_bytes ^ (gcdata)->obj_htable.pending_free_size))
#define GC_HASH_INDEX(word_value, seed, log2_size) ((((word_value) ^ (seed)) * (GC_word)0x9E3779B1L) >> (sizeof(GC_word) * CHAR_BIT - (log2_size)))
#define GC_HASH_RESIZECOND(count, log2_size) (((GC_word)3 << ((log2_size) - 2)) <= (count))
#define GC_LEAVE(gcdata) GC_leave()
#ifdef GC_CORE_MALLOC
GC_CORE_API void *GC_CORE_CALL GC_CORE_MALLOC(size_t size);
#else
#define GC_CORE_MALLOC malloc
#endif
#ifdef GC_CORE_FREE
GC_CORE_API void GC_CORE_CALL GC_CORE_FREE(void *ptr);
#else
#define GC_CORE_FREE free
#endif
#ifdef GC_STACKBOTTOMVAR
extern char *GC_STACKBOTTOMVAR;
#endif
#ifdef GC_STACKLENVAR
extern GC_word GC_STACKLENVAR;
#endif
#ifndef GC_STACKBOTTOM
#ifdef GC_STACKBOTTOMVAR
#define GC_STACKBOTTOM GC_STACKBOTTOMVAR
#else
#define GC_STACKBOTTOM 0
#endif
#endif
#ifndef GC_STACKLEN
#ifdef GC_STACKLENVAR
#define GC_STACKLEN GC_STACKLENVAR
#else
#define GC_STACKLEN 0
#endif
#endif
struct GC_objlink_s
{
void *obj;
struct GC_objlink_s *next;
GC_word atomic_and_size;
};
struct GC_obj_htable_s
{
struct GC_objlink_s **hroots;
struct GC_objlink_s *free_list;
struct GC_objlink_s *marked_list;
struct GC_objlink_s *follow_list;
struct GC_objlink_s *unlinked_list;
GC_word min_obj_addr;
GC_word max_obj_addr;
GC_word count;
GC_word log2_size;
GC_word pending_free_size;
};
#ifndef GC_NO_DLINKS
struct GC_dlink_s
{
struct GC_dlink_s *next;
struct GC_objlink_s *objlink;
GC_word hidden_link;
};
struct GC_dlink_htable_s
{
struct GC_dlink_s **hroots;
struct GC_dlink_s *free_list;
GC_word count;
GC_word log2_size;
GC_word seed;
};
#endif /* ! GC_NO_DLINKS */
#ifndef GC_NO_FNLZ
struct GC_fnlz_s
{
struct GC_fnlz_s *next;
struct GC_objlink_s *objlink;
void *client_data;
GC_finalization_proc fn;
};
struct GC_fnlz_htable_s
{
struct GC_fnlz_s **hroots;
struct GC_fnlz_s *ready_fnlz;
struct GC_fnlz_s *single_free;
GC_word count;
GC_word log2_size;
GC_word seed;
int has_client_ptrs;
};
#endif /* ! GC_NO_FNLZ */
#ifndef GC_NO_INACTIVE
struct GC_activation_frame_s
{
GC_word inactive_sp;
CONST struct GC_activation_frame_s *prev;
};
#endif /* ! GC_NO_INACTIVE */
struct GC_dataroot_s
{
struct GC_dataroot_s *next;
GC_word begin_addr;
GC_word end_addr;
};
struct GC_stkroot_s
{
GC_word begin_addr;
GC_word end_addr;
#ifndef GC_NO_INACTIVE
CONST struct GC_activation_frame_s *activation_frame;
#endif
#ifdef GC_THREADS
struct GC_stkroot_s *next;
GC_THREAD_ID_T thread_id;
#ifdef GC_WIN32_THREADS
#ifndef GC_WIN32_WCE
HANDLE thread_handle;
#endif
#else
volatile int suspend_ack;
#endif
#endif
#ifndef GC_NO_INACTIVE
int inactive;
#endif
#ifndef GC_NO_FNLZ
int inside_fnlz;
#endif
};
#ifdef GC_THREADS
struct GC_stkroot_htable_s
{
struct GC_stkroot_s **hroots;
GC_word count;
GC_word log2_size;
GC_word seed;
};
#endif /* GC_THREADS */
struct GC_gcdata_s
{
struct GC_obj_htable_s obj_htable;
void *objlinks_block_list;
#ifndef GC_NO_DLINKS
struct GC_dlink_htable_s dlink_htable;
#endif
#ifndef GC_NO_FNLZ
struct GC_fnlz_htable_s fnlz_htable;
GC_word notifier_gc_no;
GC_word bytes_finalized;
#endif
struct GC_stkroot_s *cur_stack;
struct GC_dataroot_s *dataroots;
GC_word dataroot_size;
GC_word expanded_heapsize;
GC_word total_heapsize;
GC_word allocd_before_gc;
GC_word bytes_allocd;
GC_word marked_bytes;
GC_word free_bytes;
GC_word followscan_size;
#ifdef GC_THREADS
struct GC_stkroot_htable_s stkroot_htable;
#endif
int recycling;
#ifdef GC_GCJ_SUPPORT
#ifndef GC_GETENV_SKIP
#ifndef GC_IGNORE_GCJ_INFO
int ignore_gcj_info;
#endif
#endif
#endif
};
volatile GC_word GC_noop_sink;
GC_DATASTATIC GC_word GC_gc_no = 0;
GC_DATASTATIC GC_word GC_free_space_divisor = (GC_FREE_SPACE_DIVISOR);
GC_DATASTATIC GC_word GC_max_retries = (GC_MAX_RETRIES);
GC_DATASTATIC GC_finalizer_notifier_proc GC_finalizer_notifier = 0;
GC_DATASTATIC GC_start_callback_proc GC_start_call_back = 0;
#ifdef ALL_INTERIOR_POINTERS
GC_DATASTATIC int GC_all_interior_pointers = 1;
#else
GC_DATASTATIC int GC_all_interior_pointers = 0;
#endif
#ifdef FINALIZE_ON_DEMAND
GC_DATASTATIC int GC_finalize_on_demand = 1;
#else
GC_DATASTATIC int GC_finalize_on_demand = 0;
#endif
GC_DATASTATIC int GC_dont_gc = 0;
#ifdef GC_DONT_EXPAND
GC_DATASTATIC int GC_dont_expand = 1;
#else
GC_DATASTATIC int GC_dont_expand = 0;
#endif
GC_STATIC int GC_CALLBACK GC_never_stop_func(void);
GC_DATASTATIC GC_stop_func GC_default_stop_func = GC_never_stop_func;
#ifndef GC_MISC_EXCLUDE
GC_STATIC void GC_CALLBACK GC_default_warn_proc(char *msg, GC_word arg);
GC_DATASTATIC GC_warn_proc GC_current_warn_proc =
GC_default_warn_proc; /* ignored */
#endif
GC_DATASTATIC int GC_stack_grows_up = 0;
GC_DATASTATIC struct GC_gcdata_s *GC_gcdata_global = NULL;
GC_DATASTATIC GC_word GC_max_heapsize = ~(GC_word)0;
GC_DATASTATIC CONST struct GC_objlink_s GC_nil_objlink = { NULL, NULL, 0 };
#ifndef GC_NO_DLINKS
GC_DATASTATIC CONST struct GC_dlink_s GC_nil_dlink = { NULL, NULL, 0 };
#endif
#ifndef GC_NO_FNLZ
GC_DATASTATIC CONST struct GC_fnlz_s GC_nil_fnlz = { NULL, NULL, NULL, 0 };
#endif
#ifdef GC_PRINT_MSGS
GC_DATASTATIC int GC_verbose_gc = 0;
#endif
#ifdef GC_THREADS
#ifdef GC_WIN32_THREADS
struct GC_mutex_s
{
LONG state;
HANDLE event;
};
GC_DATASTATIC struct GC_mutex_s GC_allocate_ml = { 0, 0 };
#else /* GC_WIN32_THREADS */
volatile int GC_inside_collect = -1;
GC_DATASTATIC pthread_mutex_t GC_allocate_ml;
GC_STATIC void GC_CLIBDECL GC_suspend_handler(int sig);
#endif /* ! GC_WIN32_THREADS */
GC_STATIC void GC_FASTCALL GC_stkroot_add(struct GC_gcdata_s *gcdata,
GC_THREAD_ID_T thread_id, struct GC_stkroot_s *new_stkroot);
GC_STATIC void GC_FASTCALL GC_stkroot_tblresize(struct GC_gcdata_s *gcdata,
struct GC_stkroot_s **new_hroots, GC_word new_log2_size);
#else /* GC_THREADS */
GC_DATASTATIC int GC_allocate_ml = 0;
#endif /* ! GC_THREADS */
GC_STATIC void *GC_FASTCALL GC_alloc_hroots(struct GC_gcdata_s *gcdata,
GC_word new_log2_size, CONST void *nil_ptr);
GC_STATIC void *GC_FASTCALL GC_core_malloc_with_gc(struct GC_gcdata_s *gcdata,
GC_word size, int *pres);
GC_STATIC int GC_FASTCALL GC_heap_expand(struct GC_gcdata_s *gcdata,
GC_word incsize);
GC_STATIC void *GC_FASTCALL GC_inner_core_malloc(struct GC_gcdata_s *gcdata,
GC_word size, int dont_expand);
GC_STATIC int GC_FASTCALL GC_roots_add(struct GC_gcdata_s *gcdata,
GC_word begin_addr, GC_word end_addr);
void GC_noop1(GC_word value)
{
GC_noop_sink = value;
}
GC_word GC_approx_sp(void)
{
volatile GC_word value;
value = (GC_word)(&value);
GC_noop1(value);
return value;
}
GC_STATIC int GC_FASTCALL GC_roots_autodetect(struct GC_gcdata_s *gcdata)
{
int res = GC_roots_add(gcdata, 0, 0);
#ifdef GC_DATASTART
#ifdef GC_DATAEND
res |= GC_roots_add(gcdata, (GC_word)GC_DATASTART, (GC_word)GC_DATAEND);
#endif
#endif
#ifdef GC_DATASTART2
#ifdef GC_DATAEND2
res |= GC_roots_add(gcdata, (GC_word)GC_DATASTART2, (GC_word)GC_DATAEND2);
#endif
#endif
return res;
}
GC_INLINE_STATIC GC_word GC_FASTCALL GC_stack_detectbase(void)
{
return (GC_word)(GC_STACKBOTTOM) + (GC_STACKLEN);
}
GC_INLINE_STATIC GC_word GC_FASTCALL GC_stack_approx_size(
CONST struct GC_gcdata_s *gcdata)
{
struct GC_stkroot_s *cur_stack = gcdata->cur_stack;
GC_word totalsize = cur_stack != NULL ? cur_stack->end_addr -
cur_stack->begin_addr : sizeof(GC_word);
#ifdef GC_THREADS
totalsize = gcdata->stkroot_htable.count * totalsize;
#endif
return totalsize;
}
GC_INLINE_STATIC int GC_FASTCALL GC_guess_collect(
CONST struct GC_gcdata_s *gcdata, GC_word objsize)
{
return (((GC_stack_approx_size(gcdata) + gcdata->followscan_size) << 1) +
gcdata->dataroot_size +
((GC_word)sizeof(GC_word) << gcdata->obj_htable.log2_size) +
#ifndef GC_NO_DLINKS
((GC_word)sizeof(GC_word) << gcdata->dlink_htable.log2_size) +
#endif
((gcdata->marked_bytes + gcdata->bytes_allocd -
gcdata->followscan_size) >> 2)) / GC_free_space_divisor <=
#ifndef GC_NO_FNLZ
gcdata->bytes_finalized +
#endif
gcdata->bytes_allocd + objsize ? 1 : 0;
}
GC_INLINE_STATIC GC_word GC_FASTCALL GC_guess_expand_size(
CONST struct GC_gcdata_s *gcdata, GC_word objsize)
{
GC_word space_divisor = GC_free_space_divisor + 1;
return (gcdata->marked_bytes + gcdata->bytes_allocd) / space_divisor >=
gcdata->free_bytes ? gcdata->free_bytes * space_divisor +
(gcdata->bytes_allocd >> 3) + (objsize << 2) +
gcdata->dataroot_size : 0;
}
GC_STATIC void GC_FASTCALL GC_abort_badptr(CONST void *ptr)
{
#ifdef GC_PRINT_MSGS
fprintf(stderr, " GC: Illegal pointer specified: 0x%lX." GC_NEW_LINE,
(unsigned long)((GC_word)ptr));
#else
GC_noop1((GC_word)ptr);
#endif
GC_FATAL_ABORT;
}
GC_INLINE_STATIC int GC_FASTCALL GC_config_set(struct GC_gcdata_s *gcdata)
{
int res = 0;
#ifdef GC_GETENV_SKIP
#ifdef GC_PRINT_MSGS
GC_verbose_gc = 1;
#endif
GC_noop1((GC_word)gcdata);
#else
char *str;
GC_word value;
if ((str = getenv("GC_ALL_INTERIOR_POINTERS")) != NULL && *str)
GC_all_interior_pointers = *str != '0' || *(str + 1) ? 1 : 0;
if ((str = getenv("GC_DONT_GC")) != NULL && *str)
GC_dont_gc = GC_NEVER_COLLECT;
#ifdef GC_GCJ_SUPPORT
#ifndef GC_IGNORE_GCJ_INFO
if ((str = getenv("GC_IGNORE_GCJ_INFO")) != NULL && *str)
gcdata->ignore_gcj_info = 1;
#endif
#endif
#ifdef GC_PRINT_MSGS
if ((str = getenv("GC_PRINT_STATS")) != NULL && *str)
GC_verbose_gc = 1;
#endif
if (((str = getenv("GC_FREE_SPACE_DIVISOR")) != NULL && *str &&
((GC_free_space_divisor = (GC_word)atol(str)) == 0 ||
GC_free_space_divisor == ~(GC_word)0)) ||
((str = getenv("GC_MAXIMUM_HEAP_SIZE")) != NULL && *str &&
(GC_max_heapsize = (GC_word)atol(str)) == 0) ||
((str = getenv("GC_INITIAL_HEAP_SIZE")) != NULL && *str &&
((value = (GC_word)atol(str)) - (GC_word)1 >= GC_max_heapsize ||
(gcdata->total_heapsize < value && GC_heap_expand(gcdata,
value - gcdata->total_heapsize) < 0))))
res = -1;
#endif
return res;
}
GC_STATIC int GC_CALLBACK GC_never_stop_func(void)
{
return 0;
}
GC_API GC_word GC_CALL GC_get_gc_no(void)
{
return GC_gc_no;
}
GC_API void GC_CALL GC_set_finalize_on_demand(int value)
{
GC_finalize_on_demand = value;
}
GC_API void GC_CALL GC_set_java_finalization(int value)
{
if (!value)
GC_abort_badptr(NULL);
}
GC_API void GC_CALL GC_set_max_heap_size(GC_word size)
{
GC_max_heapsize = size ? size : ~(GC_word)0;
}
#ifndef GC_MISC_EXCLUDE
GC_API void GC_CALL GC_set_free_space_divisor(GC_word value)
{
if (!value || value == ~(GC_word)0)
GC_abort_badptr(NULL);
GC_free_space_divisor = value;
}
GC_API void GC_CALL GC_set_all_interior_pointers(int value)
{
GC_all_interior_pointers = value;
}
GC_API void GC_CALL GC_set_dont_expand(int value)
{
GC_dont_expand = value;
}
GC_API void GC_CALL GC_set_no_dls(int value)
{
/* dummy */
GC_noop1((GC_word)value);
}
GC_API void GC_CALL GC_set_dont_precollect(int value)
{
/* dummy */
GC_noop1((GC_word)value);
}
GC_API void GC_CALL GC_set_force_unmap_on_gcollect(int value)
{
/* dummy */
GC_noop1((GC_word)value);
}
GC_API void GC_CALL GC_set_max_retries(GC_word value)
{
GC_max_retries = value;
}
GC_STATIC void GC_CALLBACK GC_default_warn_proc(char *msg, GC_word arg)
{
/* dummy */
GC_noop1((GC_word)msg ^ arg);
}
GC_API void GC_CALLBACK GC_ignore_warn_proc(char *msg, GC_word arg)
{
GC_default_warn_proc(msg, arg);
}
#endif /* ! GC_MISC_EXCLUDE */
GC_API void *GC_CALL GC_call_with_stack_base(GC_stack_base_func fn,
void *client_data)
{
GC_word stack_data;
struct GC_stack_base sb;
sb.mem_base = (void *)&stack_data;
return (*fn)(&sb, client_data);
}
GC_API int GC_CALL GC_get_stack_base(struct GC_stack_base *sb)
{
if (sb == NULL)
GC_abort_badptr(NULL);
return GC_UNIMPLEMENTED;
}
GC_INLINE_STATIC struct GC_gcdata_s *GC_FASTCALL GC_gcdata_alloc(void)
{
struct GC_gcdata_s *gcdata;
if ((gcdata = GC_CORE_MALLOC(sizeof(struct GC_gcdata_s))) != NULL)
{
GC_MEM_BZERO(gcdata, sizeof(struct GC_gcdata_s));
if ((gcdata->obj_htable.hroots = GC_alloc_hroots(gcdata,