2 * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3 * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
4 * Copyright (c) 1996 by Silicon Graphics. All rights reserved.
6 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
7 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
9 * Permission is hereby granted to use or copy this program
10 * for any purpose, provided the above notices are retained on all copies.
11 * Permission to modify the code and to distribute modified code is granted,
12 * provided the above notices are retained, and a notice that the code was
13 * modified is included with the above copyright notice.
15 /* An incomplete test for the garbage collector. */
16 /* Some more obscure entry points are not tested at all. */
17 /* This must be compiled with the same flags used to build the */
18 /* GC. It uses GC internals to allow more precise results */
19 /* checking for some of the tests. */
23 #if defined(DBG_HDRS_ALL) || defined(MAKE_BACK_GRAPH)
27 # if defined(mips) && defined(SYSTYPE_BSD43)
35 # define assert ASSERT
37 # include <assert.h> /* Not normally used, but handy for debugging. */
40 # include "gc_typed.h"
41 # include "private/gc_priv.h" /* For output, locking, MIN_WORDS, */
42 /* and some statistics, and gcconfig.h. */
44 # if defined(MSWIN32) || defined(MSWINCE)
47 # define GC_print_stats 0 /* Not exported from DLL */
48 /* Redefine to 1 to generate output. */
53 # include "th/PCR_ThCrSec.h"
54 # include "th/PCR_Th.h"
55 # define GC_printf printf
58 # if defined(GC_PTHREADS)
62 # if defined(GC_WIN32_THREADS) && !defined(GC_PTHREADS)
63 static CRITICAL_SECTION incr_cs;
70 /* Call GC_INIT only on platforms on which we think we really need it, */
71 /* so that we can test automatic initialization on the rest. */
72 #if defined(CYGWIN32) || defined (AIX) || defined(DARWIN)
73 # define GC_COND_INIT() GC_INIT()
75 # define GC_COND_INIT()
78 /* Allocation Statistics */
79 int stubborn_count = 0;
80 int uncollectable_count = 0;
81 int collectable_count = 0;
83 int realloc_count = 0;
85 #if defined(GC_AMIGA_FASTALLOC) && defined(AMIGA)
87 extern void GC_amiga_free_all_mem(void);
88 void Amiga_Fail(void){GC_amiga_free_all_mem();abort();}
89 # define FAIL (void)Amiga_Fail()
90 void *GC_amiga_gctest_malloc_explicitly_typed(size_t lb, GC_descr d){
91 void *ret=GC_malloc_explicitly_typed(lb,d);
95 ret=GC_malloc_explicitly_typed(lb,d);
98 GC_printf("Out of memory, (typed allocations are not directly "
99 "supported with the GC_AMIGA_FASTALLOC option.)\n");
105 void *GC_amiga_gctest_calloc_explicitly_typed(size_t a,size_t lb, GC_descr d){
106 void *ret=GC_calloc_explicitly_typed(a,lb,d);
110 ret=GC_calloc_explicitly_typed(a,lb,d);
113 GC_printf("Out of memory, (typed allocations are not directly "
114 "supported with the GC_AMIGA_FASTALLOC option.)\n");
120 # define GC_malloc_explicitly_typed(a,b) GC_amiga_gctest_malloc_explicitly_typed(a,b)
121 # define GC_calloc_explicitly_typed(a,b,c) GC_amiga_gctest_calloc_explicitly_typed(a,b,c)
123 #else /* !AMIGA_FASTALLOC */
126 # define FAIL (void)abort()
129 # define FAIL DebugBreak()
131 # define FAIL GC_abort("Test failed");
135 #endif /* !AMIGA_FASTALLOC */
137 /* AT_END may be defined to exercise the interior pointer test */
138 /* if the collector is configured with ALL_INTERIOR_POINTERS. */
139 /* As it stands, this test should succeed with either */
140 /* configuration. In the FIND_LEAK configuration, it should */
141 /* find lots of leaks, since we free almost nothing. */
144 struct SEXPR * sexpr_car;
145 struct SEXPR * sexpr_cdr;
149 typedef struct SEXPR * sexpr;
151 # define INT_TO_SEXPR(x) ((sexpr)(GC_word)(x))
152 # define SEXPR_TO_INT(x) ((int)(GC_word)(x))
155 # define nil (INT_TO_SEXPR(0))
156 # define car(x) ((x) -> sexpr_car)
157 # define cdr(x) ((x) -> sexpr_cdr)
158 # define is_nil(x) ((x) == nil)
161 int extra_count = 0; /* Amount of space wasted in cons node */
163 /* Silly implementation of Lisp cons. Intentionally wastes lots of space */
164 /* to test collector. */
165 # ifdef VERY_SMALL_CONFIG
166 # define cons small_cons
168 sexpr cons (sexpr x, sexpr y)
172 int my_extra = extra_count;
175 r = (sexpr) GC_MALLOC_STUBBORN(sizeof(struct SEXPR) + my_extra);
177 (void)GC_printf("Out of memory\n");
181 ((char *)p) < ((char *)r) + my_extra + sizeof(struct SEXPR); p++) {
183 (void)GC_printf("Found nonzero at %p - allocator is broken\n", p);
186 *p = (int)((13 << 12) + ((p - (int *)r) & 0xfff));
189 r = (sexpr)((char *)r + (my_extra & ~7));
194 if ( my_extra >= 5000 ) {
197 extra_count = my_extra;
199 GC_END_STUBBORN_CHANGE((char *)r);
204 #ifdef GC_GCJ_SUPPORT
209 /* The following struct emulates the vtable in gcj. */
210 /* This assumes the default value of MARK_DESCR_OFFSET. */
212 void * dummy; /* class pointer in real gcj. */
216 struct fake_vtable gcj_class_struct1 = { 0, sizeof(struct SEXPR)
217 + sizeof(struct fake_vtable *) };
218 /* length based descriptor. */
219 struct fake_vtable gcj_class_struct2 =
220 { 0, (3l << (CPP_WORDSZ - 3)) | GC_DS_BITMAP};
221 /* Bitmap based descriptor. */
223 struct GC_ms_entry * fake_gcj_mark_proc(word * addr,
224 struct GC_ms_entry *mark_stack_ptr,
225 struct GC_ms_entry *mark_stack_limit,
230 /* Object allocated with debug allocator. */
231 addr = (word *)GC_USR_PTR_FROM_BASE(addr);
233 x = (sexpr)(addr + 1); /* Skip the vtable pointer. */
234 mark_stack_ptr = GC_MARK_AND_PUSH(
235 (void *)(x -> sexpr_cdr), mark_stack_ptr,
236 mark_stack_limit, (void * *)&(x -> sexpr_cdr));
237 mark_stack_ptr = GC_MARK_AND_PUSH(
238 (void *)(x -> sexpr_car), mark_stack_ptr,
239 mark_stack_limit, (void * *)&(x -> sexpr_car));
240 return(mark_stack_ptr);
243 #endif /* GC_GCJ_SUPPORT */
246 sexpr small_cons (sexpr x, sexpr y)
251 r = (sexpr) GC_MALLOC(sizeof(struct SEXPR));
253 (void)GC_printf("Out of memory\n");
261 sexpr small_cons_uncollectable (sexpr x, sexpr y)
265 uncollectable_count++;
266 r = (sexpr) GC_MALLOC_UNCOLLECTABLE(sizeof(struct SEXPR));
268 (void)GC_printf("Out of memory\n");
272 r -> sexpr_cdr = (sexpr)(~(GC_word)y);
276 #ifdef GC_GCJ_SUPPORT
279 sexpr gcj_cons(sexpr x, sexpr y)
283 static int count = 0;
285 r = (GC_word *) GC_GCJ_MALLOC(sizeof(struct SEXPR)
286 + sizeof(struct fake_vtable*),
289 (void)GC_printf("Out of memory\n");
292 result = (sexpr)(r + 1);
293 result -> sexpr_car = x;
294 result -> sexpr_cdr = y;
299 /* Return reverse(x) concatenated with y */
300 sexpr reverse1(sexpr x, sexpr y)
305 return( reverse1(cdr(x), cons(car(x), y)) );
309 sexpr reverse(sexpr x)
311 # ifdef TEST_WITH_SYSTEM_MALLOC
314 return( reverse1(x, nil) );
317 sexpr ints(int low, int up)
322 return(small_cons(small_cons(INT_TO_SEXPR(low), nil), ints(low+1, up)));
326 #ifdef GC_GCJ_SUPPORT
327 /* Return reverse(x) concatenated with y */
328 sexpr gcj_reverse1(sexpr x, sexpr y)
333 return( gcj_reverse1(cdr(x), gcj_cons(car(x), y)) );
337 sexpr gcj_reverse(sexpr x)
339 return( gcj_reverse1(x, nil) );
342 sexpr gcj_ints(int low, int up)
347 return(gcj_cons(gcj_cons(INT_TO_SEXPR(low), nil), gcj_ints(low+1, up)));
350 #endif /* GC_GCJ_SUPPORT */
352 /* To check uncollectable allocation we build lists with disguised cdr */
353 /* pointers, and make sure they don't go away. */
354 sexpr uncollectable_ints(int low, int up)
359 return(small_cons_uncollectable(small_cons(INT_TO_SEXPR(low), nil),
360 uncollectable_ints(low+1, up)));
364 void check_ints(sexpr list, int low, int up)
366 if (SEXPR_TO_INT(car(car(list))) != low) {
368 "List reversal produced incorrect list - collector is broken\n");
372 if (cdr(list) != nil) {
373 (void)GC_printf("List too long - collector is broken\n");
377 check_ints(cdr(list), low+1, up);
381 # define UNCOLLECTABLE_CDR(x) (sexpr)(~(GC_word)(cdr(x)))
383 void check_uncollectable_ints(sexpr list, int low, int up)
385 if (SEXPR_TO_INT(car(car(list))) != low) {
387 "Uncollectable list corrupted - collector is broken\n");
391 if (UNCOLLECTABLE_CDR(list) != nil) {
392 (void)GC_printf("Uncollectable list too long - collector is broken\n");
396 check_uncollectable_ints(UNCOLLECTABLE_CDR(list), low+1, up);
400 /* Not used, but useful for debugging: */
401 void print_int_list(sexpr x)
404 (void)GC_printf("NIL\n");
406 (void)GC_printf("(%d)", SEXPR_TO_INT(car(car(x))));
407 if (!is_nil(cdr(x))) {
408 (void)GC_printf(", ");
409 (void)print_int_list(cdr(x));
411 (void)GC_printf("\n");
417 void check_marks_int_list(sexpr x)
419 if (!GC_is_marked((ptr_t)x))
420 GC_printf("[unm:%p]", x);
422 GC_printf("[mkd:%p]", x);
424 (void)GC_printf("NIL\n");
426 if (!GC_is_marked((ptr_t)car(x))) GC_printf("[unm car:%p]", car(x));
427 (void)GC_printf("(%d)", SEXPR_TO_INT(car(car(x))));
428 if (!is_nil(cdr(x))) {
429 (void)GC_printf(", ");
430 (void)check_marks_int_list(cdr(x));
432 (void)GC_printf("\n");
438 * A tiny list reversal test to check thread creation.
442 # if defined(GC_WIN32_THREADS) && !defined(GC_PTHREADS)
443 DWORD __stdcall tiny_reverse_test(void * arg)
445 void * tiny_reverse_test(void * arg)
449 for (i = 0; i < 5; ++i) {
450 check_ints(reverse(reverse(ints(1,10))), 1, 10);
455 # if defined(GC_PTHREADS)
460 if ((code = pthread_create(&t, 0, tiny_reverse_test, 0)) != 0) {
461 (void)GC_printf("Small thread creation failed %d\n", code);
464 if ((code = pthread_join(t, 0)) != 0) {
465 (void)GC_printf("Small thread join failed %d\n", code);
470 # elif defined(GC_WIN32_THREADS)
475 h = GC_CreateThread(NULL, 0, tiny_reverse_test, 0, 0, &thread_id);
476 if (h == (HANDLE)NULL) {
477 (void)GC_printf("Small thread creation failed %d\n",
481 if (WaitForSingleObject(h, INFINITE) != WAIT_OBJECT_0) {
482 (void)GC_printf("Small thread wait failed %d\n",
490 # define fork_a_thread()
496 # define fork_a_thread()
500 /* Try to force a to be strangely aligned */
508 * Repeatedly reverse lists built out of very different sized cons cells.
509 * Check that we didn't lose anything.
519 # if defined(MSWIN32) || defined(MACOS)
520 /* Win32S only allows 128K stacks */
524 /* PCR default stack is 100K. Stack frames are up to 120 bytes. */
528 /* WinCE only allows 64K stacks */
532 /* OSF has limited stack space by default, and large frames. */
545 d = uncollectable_ints(1, 100);
546 e = uncollectable_ints(1, 1);
547 /* Check that realloc updates object descriptors correctly */
549 f = (sexpr *)GC_MALLOC(4 * sizeof(sexpr));
551 f = (sexpr *)GC_REALLOC((void *)f, 6 * sizeof(sexpr));
554 g = (sexpr *)GC_MALLOC(513 * sizeof(sexpr));
556 g = (sexpr *)GC_REALLOC((void *)g, 800 * sizeof(sexpr));
559 h = (sexpr *)GC_MALLOC(1025 * sizeof(sexpr));
561 h = (sexpr *)GC_REALLOC((void *)h, 2000 * sizeof(sexpr));
562 # ifdef GC_GCJ_SUPPORT
563 h[1999] = gcj_ints(1,200);
564 for (i = 0; i < 51; ++i)
565 h[1999] = gcj_reverse(h[1999]);
566 /* Leave it as the reveresed list for now. */
568 h[1999] = ints(1,200);
570 /* Try to force some collections and reuse of small list elements */
571 for (i = 0; i < 10; i++) {
574 /* Superficially test interior pointer recognition on stack */
575 c = (sexpr)((char *)c + sizeof(char *));
576 d = (sexpr)((char *)d + sizeof(char *));
585 for (i = 0; i < 50; i++) {
587 b = reverse(reverse(b));
591 for (i = 0; i < 60; i++) {
592 if (i % 10 == 0) fork_a_thread();
593 /* This maintains the invariant that a always points to a list of */
594 /* 49 integers. Thus this is thread safe without locks, */
595 /* assuming atomic pointer assignments. */
596 a = reverse(reverse(a));
597 # if !defined(AT_END) && !defined(THREADS)
598 /* This is not thread safe, since realloc explicitly deallocates */
600 a = (sexpr)GC_REALLOC((void *)a, 500);
602 a = (sexpr)GC_REALLOC((void *)a, 8200);
608 c = (sexpr)((char *)c - sizeof(char *));
609 d = (sexpr)((char *)d - sizeof(char *));
611 check_uncollectable_ints(d, 1, 100);
612 check_ints(f[5], 1,17);
613 check_ints(g[799], 1,18);
614 # ifdef GC_GCJ_SUPPORT
615 h[1999] = gcj_reverse(h[1999]);
617 check_ints(h[1999], 1,200);
627 * The rest of this builds balanced binary trees, checks that they don't
628 * disappear, and tests finalization.
630 typedef struct treenode {
632 struct treenode * lchild;
633 struct treenode * rchild;
636 int finalizable_count = 0;
637 int finalized_count = 0;
638 volatile int dropped_something = 0;
641 void finalizer(void * obj, void * client_data)
643 void finalizer(obj, client_data)
651 PCR_ThCrSec_EnterSys();
653 # if defined(GC_PTHREADS)
654 static pthread_mutex_t incr_lock = PTHREAD_MUTEX_INITIALIZER;
655 pthread_mutex_lock(&incr_lock);
656 # elif defined(GC_WIN32_THREADS)
657 EnterCriticalSection(&incr_cs);
659 if ((int)(GC_word)client_data != t -> level) {
660 (void)GC_printf("Wrong finalization data - collector is broken\n");
664 t -> level = -1; /* detect duplicate finalization immediately */
666 PCR_ThCrSec_ExitSys();
668 # if defined(GC_PTHREADS)
669 pthread_mutex_unlock(&incr_lock);
670 # elif defined(GC_WIN32_THREADS)
671 LeaveCriticalSection(&incr_cs);
677 # define MAX_FINALIZED 8000
680 GC_FAR GC_word live_indicators[MAX_FINALIZED] = {0};
682 /* Too big for THINK_C. have to allocate it dynamically. */
683 GC_word *live_indicators = 0;
686 int live_indicators_count = 0;
690 tn * result = (tn *)GC_MALLOC(sizeof(tn));
694 /* get around static data limitations. */
695 if (!live_indicators)
697 (GC_word*)NewPtrClear(MAX_FINALIZED * sizeof(GC_word));
698 if (!live_indicators) {
699 (void)GC_printf("Out of memory\n");
703 if (n == 0) return(0);
705 (void)GC_printf("Out of memory\n");
709 result -> lchild = mktree(n-1);
710 result -> rchild = mktree(n-1);
711 if (counter++ % 17 == 0 && n >= 2) {
712 tn * tmp = result -> lchild -> rchild;
714 result -> lchild -> rchild = result -> rchild -> lchild;
715 result -> rchild -> lchild = tmp;
717 if (counter++ % 119 == 0) {
722 PCR_ThCrSec_EnterSys();
724 # if defined(GC_PTHREADS)
725 static pthread_mutex_t incr_lock = PTHREAD_MUTEX_INITIALIZER;
726 pthread_mutex_lock(&incr_lock);
727 # elif defined(GC_WIN32_THREADS)
728 EnterCriticalSection(&incr_cs);
730 /* Losing a count here causes erroneous report of failure. */
732 my_index = live_indicators_count++;
734 PCR_ThCrSec_ExitSys();
736 # if defined(GC_PTHREADS)
737 pthread_mutex_unlock(&incr_lock);
738 # elif defined(GC_WIN32_THREADS)
739 LeaveCriticalSection(&incr_cs);
743 GC_REGISTER_FINALIZER((void *)result, finalizer, (void *)(GC_word)n,
744 (GC_finalization_proc *)0, (void * *)0);
745 if (my_index >= MAX_FINALIZED) {
746 GC_printf("live_indicators overflowed\n");
749 live_indicators[my_index] = 13;
750 if (GC_GENERAL_REGISTER_DISAPPEARING_LINK(
751 (void * *)(&(live_indicators[my_index])),
752 (void *)result) != 0) {
753 GC_printf("GC_general_register_disappearing_link failed\n");
756 if (GC_unregister_disappearing_link(
758 (&(live_indicators[my_index]))) == 0) {
759 GC_printf("GC_unregister_disappearing_link failed\n");
762 if (GC_GENERAL_REGISTER_DISAPPEARING_LINK(
763 (void * *)(&(live_indicators[my_index])),
764 (void *)result) != 0) {
765 GC_printf("GC_general_register_disappearing_link failed 2\n");
768 GC_reachable_here(result);
773 void chktree(tn *t, int n)
775 if (n == 0 && t != 0) {
776 (void)GC_printf("Clobbered a leaf - collector is broken\n");
780 if (t -> level != n) {
781 (void)GC_printf("Lost a node at level %d - collector is broken\n", n);
784 if (counter++ % 373 == 0) {
786 (void) GC_MALLOC(counter%5001);
788 chktree(t -> lchild, n-1);
789 if (counter++ % 73 == 0) {
791 (void) GC_MALLOC(counter%373);
793 chktree(t -> rchild, n-1);
797 #if defined(GC_PTHREADS)
798 pthread_key_t fl_key;
802 # if defined(SMALL_CONFIG) || defined(GC_DEBUG)
804 return(GC_MALLOC(8));
806 void ** my_free_list_ptr;
809 my_free_list_ptr = (void **)pthread_getspecific(fl_key);
810 if (my_free_list_ptr == 0) {
811 uncollectable_count++;
812 my_free_list_ptr = GC_NEW_UNCOLLECTABLE(void *);
813 if (pthread_setspecific(fl_key, my_free_list_ptr) != 0) {
814 (void)GC_printf("pthread_setspecific failed\n");
818 my_free_list = *my_free_list_ptr;
819 if (my_free_list == 0) {
820 my_free_list = GC_malloc_many(8);
821 if (my_free_list == 0) {
822 (void)GC_printf("alloc8bytes out of memory\n");
826 *my_free_list_ptr = GC_NEXT(my_free_list);
827 GC_NEXT(my_free_list) = 0;
829 return(my_free_list);
834 # define alloc8bytes() GC_MALLOC_ATOMIC(8)
837 void alloc_small(int n)
841 for (i = 0; i < n; i += 8) {
843 if (alloc8bytes() == 0) {
844 (void)GC_printf("Out of memory\n");
850 # if defined(THREADS) && defined(GC_DEBUG)
851 # ifdef VERY_SMALL_CONFIG
852 # define TREE_HEIGHT 12
854 # define TREE_HEIGHT 15
857 # ifdef VERY_SMALL_CONFIG
858 # define TREE_HEIGHT 13
860 # define TREE_HEIGHT 16
868 root = mktree(TREE_HEIGHT);
869 # ifndef VERY_SMALL_CONFIG
870 alloc_small(5000000);
872 chktree(root, TREE_HEIGHT);
873 if (finalized_count && ! dropped_something) {
874 (void)GC_printf("Premature finalization - collector is broken\n");
877 dropped_something = 1;
878 GC_noop(root); /* Root needs to remain live until */
879 /* dropped_something is set. */
880 root = mktree(TREE_HEIGHT);
881 chktree(root, TREE_HEIGHT);
882 for (i = TREE_HEIGHT; i >= 0; i--) {
886 # ifndef VERY_SMALL_CONFIG
887 alloc_small(5000000);
891 unsigned n_tests = 0;
893 GC_word bm_huge[10] = {
906 /* A very simple test of explicitly typed allocation */
909 GC_word * old, * new;
912 GC_word bm_large = 0xf7ff7fff;
913 GC_descr d1 = GC_make_descriptor(&bm3, 2);
914 GC_descr d2 = GC_make_descriptor(&bm2, 2);
915 GC_descr d3 = GC_make_descriptor(&bm_large, 32);
916 GC_descr d4 = GC_make_descriptor(bm_huge, 320);
917 GC_word * x = (GC_word *)GC_malloc_explicitly_typed(2000, d4);
921 (void)GC_make_descriptor(&bm_large, 32);
925 for (i = 0; i < 4000; i++) {
927 new = (GC_word *) GC_malloc_explicitly_typed(4 * sizeof(GC_word), d1);
928 if (0 != new[0] || 0 != new[1]) {
929 GC_printf("Bad initialization by GC_malloc_explicitly_typed\n");
933 new[1] = (GC_word)old;
936 new = (GC_word *) GC_malloc_explicitly_typed(4 * sizeof(GC_word), d2);
938 new[1] = (GC_word)old;
941 new = (GC_word *) GC_malloc_explicitly_typed(33 * sizeof(GC_word), d3);
943 new[1] = (GC_word)old;
946 new = (GC_word *) GC_calloc_explicitly_typed(4, 2 * sizeof(GC_word),
949 new[1] = (GC_word)old;
953 new = (GC_word *) GC_calloc_explicitly_typed(7, 3 * sizeof(GC_word),
956 new = (GC_word *) GC_calloc_explicitly_typed(1001,
959 if (0 != new[0] || 0 != new[1]) {
960 GC_printf("Bad initialization by GC_malloc_explicitly_typed\n");
965 new[1] = (GC_word)old;
968 for (i = 0; i < 20000; i++) {
970 (void)GC_printf("typed alloc failed at %lu\n",
976 new = (GC_word *)(old[1]);
995 void fail_proc1(void * x)
1000 static void uniq(void *p, ...) {
1006 for (;(q[n] = va_arg(a,void *));n++) ;
1012 "Apparently failed to mark from some function arguments.\n"
1013 "Perhaps GC_push_regs was configured incorrectly?\n"
1019 #endif /* __STDC__ */
1022 # define TEST_FAIL_COUNT(n) 1
1024 # define TEST_FAIL_COUNT(n) (fail_count >= (n))
1034 char *y = (char *)(size_t)fail_proc1;
1036 CLOCK_TYPE start_time;
1037 CLOCK_TYPE reverse_time;
1038 CLOCK_TYPE typed_time;
1039 CLOCK_TYPE tree_time;
1040 unsigned long time_diff;
1045 "This test program is not designed for leak detection mode\n");
1046 GC_printf("Expect lots of problems.\n");
1049 # ifndef DBG_HDRS_ALL
1050 collectable_count += 3;
1051 if ((GC_size(GC_malloc(7)) != 8 &&
1052 GC_size(GC_malloc(7)) != MIN_WORDS * sizeof(GC_word))
1053 || GC_size(GC_malloc(15)) != 16) {
1054 GC_printf("GC_size produced unexpected results\n");
1057 collectable_count += 1;
1058 if (GC_size(GC_malloc(0)) != MIN_WORDS * sizeof(GC_word)) {
1059 GC_printf("GC_malloc(0) failed: GC_size returns %ld\n",
1060 (unsigned long)GC_size(GC_malloc(0)));
1063 collectable_count += 1;
1064 if (GC_size(GC_malloc_uncollectable(0)) != MIN_WORDS * sizeof(GC_word)) {
1065 GC_printf("GC_malloc_uncollectable(0) failed\n");
1068 GC_is_valid_displacement_print_proc = fail_proc1;
1069 GC_is_visible_print_proc = fail_proc1;
1070 collectable_count += 1;
1072 if (GC_base(x + 13) != x) {
1073 GC_printf("GC_base(heap ptr) produced incorrect result\n");
1077 if (GC_base(y) != 0) {
1078 GC_printf("GC_base(fn_ptr) produced incorrect result\n");
1082 if (GC_same_obj(x+5, x) != x + 5) {
1083 GC_printf("GC_same_obj produced incorrect result\n");
1086 if (GC_is_visible(y) != y || GC_is_visible(x) != x) {
1087 GC_printf("GC_is_visible produced incorrect result\n");
1093 GC_printf("GC_PTR_STORE failed: %p != %p\n", *z, x);
1096 if (!TEST_FAIL_COUNT(1)) {
1097 # if!(defined(POWERPC) || defined(IA64)) || defined(M68K)
1098 /* On POWERPCs function pointers point to a descriptor in the */
1099 /* data segment, so there should have been no failures. */
1100 /* The same applies to IA64. Something similar seems to */
1101 /* be going on with NetBSD/M68K. */
1102 GC_printf("GC_is_visible produced wrong failure indication\n");
1106 if (GC_is_valid_displacement(y) != y
1107 || GC_is_valid_displacement(x) != x
1108 || GC_is_valid_displacement(x + 3) != x + 3) {
1110 "GC_is_valid_displacement produced incorrect result\n");
1113 # if defined(__STDC__) && !defined(MSWIN32) && !defined(MSWINCE)
1114 /* Harder to test under Windows without a gc.h declaration. */
1117 extern void *GC_memalign();
1120 for (i = sizeof(GC_word); i < 512; i *= 2) {
1121 GC_word result = (GC_word) GC_memalign(i, 17);
1122 if (result % i != 0 || result == 0 || *(int *)result != 0) FAIL;
1126 # ifndef ALL_INTERIOR_POINTERS
1127 # if defined(RS6000) || defined(POWERPC)
1128 if (!TEST_FAIL_COUNT(1)) {
1130 if (GC_all_interior_pointers && !TEST_FAIL_COUNT(1)
1131 || !GC_all_interior_pointers && !TEST_FAIL_COUNT(2)) {
1133 GC_printf("GC_is_valid_displacement produced wrong failure indication\n");
1137 # endif /* DBG_HDRS_ALL */
1138 /* Test floating point alignment */
1139 collectable_count += 2;
1140 *(double *)GC_MALLOC(sizeof(double)) = 1.0;
1141 *(double *)GC_MALLOC(sizeof(double)) = 1.0;
1142 /* Test size 0 allocation a bit more */
1145 for (i = 0; i < 10000; ++i) {
1147 GC_FREE(GC_MALLOC(0));
1148 GC_MALLOC_ATOMIC(0);
1149 GC_FREE(GC_MALLOC_ATOMIC(0));
1152 # ifdef GC_GCJ_SUPPORT
1153 GC_REGISTER_DISPLACEMENT(sizeof(struct fake_vtable *));
1154 GC_init_gcj_malloc(0, (void *)fake_gcj_mark_proc);
1156 /* Make sure that fn arguments are visible to the collector. */
1159 GC_malloc(12), GC_malloc(12), GC_malloc(12),
1160 (GC_gcollect(),GC_malloc(12)),
1161 GC_malloc(12), GC_malloc(12), GC_malloc(12),
1162 (GC_gcollect(),GC_malloc(12)),
1163 GC_malloc(12), GC_malloc(12), GC_malloc(12),
1164 (GC_gcollect(),GC_malloc(12)),
1165 GC_malloc(12), GC_malloc(12), GC_malloc(12),
1166 (GC_gcollect(),GC_malloc(12)),
1167 GC_malloc(12), GC_malloc(12), GC_malloc(12),
1168 (GC_gcollect(),GC_malloc(12)),
1171 /* GC_malloc(0) must return NULL or something we can deallocate. */
1172 GC_free(GC_malloc(0));
1173 GC_free(GC_malloc_atomic(0));
1174 GC_free(GC_malloc(0));
1175 GC_free(GC_malloc_atomic(0));
1176 /* Repeated list reversal test. */
1177 GET_TIME(start_time);
1179 if (GC_print_stats) {
1180 GET_TIME(reverse_time);
1181 time_diff = MS_TIME_DIFF(reverse_time, start_time);
1182 GC_log_printf("-------------Finished reverse_test at time %u (%p)\n",
1183 (unsigned) time_diff, &start_time);
1185 # ifndef DBG_HDRS_ALL
1187 if (GC_print_stats) {
1188 GET_TIME(typed_time);
1189 time_diff = MS_TIME_DIFF(typed_time, start_time);
1190 GC_log_printf("-------------Finished typed_test at time %u (%p)\n",
1191 (unsigned) time_diff, &start_time);
1193 # endif /* DBG_HDRS_ALL */
1195 if (GC_print_stats) {
1196 GET_TIME(tree_time);
1197 time_diff = MS_TIME_DIFF(tree_time, start_time);
1198 GC_log_printf("-------------Finished tree_test at time %u (%p)\n",
1199 (unsigned) time_diff, &start_time);
1204 # if defined(THREADS) && defined(HANDLE_FORK)
1207 tiny_reverse_test(0);
1210 GC_log_printf("Finished a child process\n");
1215 GC_log_printf("Finished %p\n", &start_time);
1218 void check_heap_stats()
1223 int late_finalize_count = 0;
1225 # ifdef VERY_SMALL_CONFIG
1226 /* these are something of a guess */
1227 if (sizeof(char *) > 4) {
1228 max_heap_sz = 4500000;
1230 max_heap_sz = 2800000;
1233 if (sizeof(char *) > 4) {
1234 max_heap_sz = 19000000;
1236 max_heap_sz = 11000000;
1241 # ifdef SAVE_CALL_CHAIN
1243 # ifdef SAVE_CALL_COUNT
1244 max_heap_sz += max_heap_sz * SAVE_CALL_COUNT/4;
1248 /* Garbage collect repeatedly so that all inaccessible objects */
1249 /* can be finalized. */
1250 while (GC_collect_a_little()) { }
1251 for (i = 0; i < 16; i++) {
1253 late_finalize_count += GC_invoke_finalizers();
1255 (void)GC_printf("Completed %u tests\n", n_tests);
1256 (void)GC_printf("Allocated %d collectable objects\n", collectable_count);
1257 (void)GC_printf("Allocated %d uncollectable objects\n",
1258 uncollectable_count);
1259 (void)GC_printf("Allocated %d atomic objects\n", atomic_count);
1260 (void)GC_printf("Allocated %d stubborn objects\n", stubborn_count);
1261 (void)GC_printf("Finalized %d/%d objects - ",
1262 finalized_count, finalizable_count);
1263 # ifdef FINALIZE_ON_DEMAND
1264 if (finalized_count != late_finalize_count) {
1265 (void)GC_printf("Demand finalization error\n");
1269 if (finalized_count > finalizable_count
1270 || finalized_count < finalizable_count/2) {
1271 (void)GC_printf("finalization is probably broken\n");
1274 (void)GC_printf("finalization is probably ok\n");
1277 for (i = 0; i < MAX_FINALIZED; i++) {
1278 if (live_indicators[i] != 0) {
1282 i = finalizable_count - finalized_count - still_live;
1284 GC_printf("%d disappearing links remain and %d more objects "
1285 "were not finalized\n", still_live, i);
1287 GC_printf("\tVery suspicious!\n");
1289 GC_printf("\tSlightly suspicious, but probably OK.\n");
1292 (void)GC_printf("Total number of bytes allocated is %lu\n",
1294 (GC_bytes_allocd + GC_bytes_allocd_before_gc));
1295 (void)GC_printf("Final heap size is %lu bytes\n",
1296 (unsigned long)GC_get_heap_size());
1297 if (GC_bytes_allocd + GC_bytes_allocd_before_gc
1298 # ifdef VERY_SMALL_CONFIG
1299 < 2700000*n_tests) {
1301 < 33500000*n_tests) {
1303 (void)GC_printf("Incorrect execution - missed some allocations\n");
1306 if (GC_get_heap_size() > max_heap_sz*n_tests) {
1307 (void)GC_printf("Unexpected heap growth - collector may be broken\n");
1310 (void)GC_printf("Collector appears to work\n");
1314 void SetMinimumStack(long minSize)
1318 if (minSize > LMGetDefltStack())
1320 newApplLimit = (long) GetApplLimit()
1321 - (minSize - LMGetDefltStack());
1322 SetApplLimit((Ptr) newApplLimit);
1327 #define cMinStackSpace (512L * 1024L)
1332 void warn_proc(char *msg, GC_word p)
1334 void warn_proc(msg, p)
1339 GC_printf(msg, (unsigned long)p);
1345 && !defined(GC_WIN32_THREADS) && !defined(GC_PTHREADS) \
1347 #if defined(MSWIN32) && !defined(__MINGW32__)
1348 int APIENTRY WinMain(HINSTANCE instance, HINSTANCE prev, LPTSTR cmd, int n)
1359 /* No good way to determine stack base from library; do it */
1360 /* manually on this platform. */
1361 GC_stackbottom = (void *)(&dummy);
1364 /* Make sure we have lots and lots of stack space. */
1365 SetMinimumStack(cMinStackSpace);
1366 /* Cheat and let stdio initialize toolbox for us. */
1367 printf("Testing GC Macintosh port.\n");
1370 GC_set_warn_proc(warn_proc);
1371 # if (defined(MPROTECT_VDB) || defined(PROC_VDB) || defined(GWW_VDB)) \
1372 && !defined(MAKE_BACK_GRAPH) && !defined(NO_INCREMENTAL)
1373 GC_enable_incremental();
1374 GC_printf("Switched to incremental mode\n");
1375 # if defined(MPROTECT_VDB)
1376 GC_printf("Emulating dirty bits with mprotect/signals\n");
1379 GC_printf("Reading dirty bits from /proc\n");
1381 GC_printf("Using DEFAULT_VDB dirty bit implementation\n");
1391 /* Entry points we should be testing, but aren't. */
1392 /* Some can be tested by defining GC_DEBUG at the top of this file */
1393 /* This is a bit SunOS4 specific. */
1394 GC_noop(GC_expand_hp, GC_add_roots, GC_clear_roots,
1395 GC_register_disappearing_link,
1396 GC_register_finalizer_ignore_self,
1397 GC_debug_register_displacement,
1398 GC_print_obj, GC_debug_change_stubborn,
1399 GC_debug_end_stubborn_change, GC_debug_malloc_uncollectable,
1400 GC_debug_free, GC_debug_realloc, GC_generic_malloc_words_small,
1401 GC_init, GC_make_closure, GC_debug_invoke_finalizer,
1402 GC_page_was_ever_dirty, GC_is_fresh,
1403 GC_malloc_ignore_off_page, GC_malloc_atomic_ignore_off_page,
1404 GC_set_max_heap_size, GC_get_bytes_since_gc,
1405 GC_get_total_bytes, GC_pre_incr, GC_post_incr);
1408 GC_win32_free_heap();
1414 #if defined(GC_WIN32_THREADS) && !defined(GC_PTHREADS)
1416 DWORD __stdcall thr_run_one_test(void *arg)
1423 HANDLE win_created_h;
1426 LRESULT CALLBACK window_proc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1431 GC_printf("Received WM_HIBERNATE, calling GC_gcollect\n");
1435 GC_printf("Received WM_CLOSE, closing window\n");
1436 DestroyWindow(hwnd);
1442 ret = DefWindowProc(hwnd, uMsg, wParam, lParam);
1448 DWORD __stdcall thr_window(void *arg)
1450 WNDCLASS win_class = {
1455 GetModuleHandle(NULL),
1458 (HBRUSH)(COLOR_APPWORKSPACE+1),
1464 if (!RegisterClass(&win_class))
1467 win_handle = CreateWindowEx(
1472 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
1475 GetModuleHandle(NULL),
1478 if (win_handle == NULL)
1481 SetEvent(win_created_h);
1483 ShowWindow(win_handle, SW_SHOW);
1484 UpdateWindow(win_handle);
1486 while (GetMessage(&msg, NULL, 0, 0)) {
1487 TranslateMessage(&msg);
1488 DispatchMessage(&msg);
1498 int APIENTRY GC_WinMain(HINSTANCE instance, HINSTANCE prev, LPWSTR cmd, int n)
1500 int APIENTRY WinMain(HINSTANCE instance, HINSTANCE prev, LPSTR cmd, int n)
1513 GC_use_DllMain(); /* Test with implicit thread registration if possible. */
1514 GC_printf("Using DllMain to track threads\n");
1517 # ifndef NO_INCREMENTAL
1518 GC_enable_incremental();
1520 InitializeCriticalSection(&incr_cs);
1521 (void) GC_set_warn_proc(warn_proc);
1523 win_created_h = CreateEvent(NULL, FALSE, FALSE, NULL);
1524 if (win_created_h == (HANDLE)NULL) {
1525 (void)GC_printf("Event creation failed %\n", GetLastError());
1528 win_thr_h = GC_CreateThread(NULL, 0, thr_window, 0, 0, &thread_id);
1529 if (win_thr_h == (HANDLE)NULL) {
1530 (void)GC_printf("Thread creation failed %d\n", GetLastError());
1533 if (WaitForSingleObject(win_created_h, INFINITE) != WAIT_OBJECT_0)
1535 CloseHandle(win_created_h);
1538 for (i = 0; i < NTEST; i++) {
1539 h[i] = GC_CreateThread(NULL, 0, thr_run_one_test, 0, 0, &thread_id);
1540 if (h[i] == (HANDLE)NULL) {
1541 (void)GC_printf("Thread creation failed %d\n", GetLastError());
1545 # endif /* NTEST > 0 */
1548 for (i = 0; i < NTEST; i++) {
1549 if (WaitForSingleObject(h[i], INFINITE) != WAIT_OBJECT_0) {
1550 (void)GC_printf("Thread wait failed %d\n", GetLastError());
1554 # endif /* NTEST > 0 */
1556 PostMessage(win_handle, WM_CLOSE, 0, 0);
1557 if (WaitForSingleObject(win_thr_h, INFINITE) != WAIT_OBJECT_0)
1564 #endif /* GC_WIN32_THREADS */
1575 /* GC_enable_incremental(); */
1576 (void) GC_set_warn_proc(warn_proc);
1577 th1 = PCR_Th_Fork(run_one_test, 0);
1578 th2 = PCR_Th_Fork(run_one_test, 0);
1580 if (PCR_Th_T_Join(th1, &code, NIL, PCR_allSigsBlocked, PCR_waitForever)
1581 != PCR_ERes_okay || code != 0) {
1582 (void)GC_printf("Thread 1 failed\n");
1584 if (PCR_Th_T_Join(th2, &code, NIL, PCR_allSigsBlocked, PCR_waitForever)
1585 != PCR_ERes_okay || code != 0) {
1586 (void)GC_printf("Thread 2 failed\n");
1593 #if defined(GC_PTHREADS)
1594 void * thr_run_one_test(void * arg)
1601 # define GC_free GC_debug_free
1608 pthread_attr_t attr;
1610 # ifdef GC_IRIX_THREADS
1611 /* Force a larger stack to be preallocated */
1612 /* Since the initial cant always grow later. */
1613 *((volatile char *)&code - 1024*1024) = 0; /* Require 1 Mb */
1614 # endif /* GC_IRIX_THREADS */
1615 # if defined(GC_HPUX_THREADS)
1616 /* Default stack size is too small, especially with the 64 bit ABI */
1618 if (pthread_default_stacksize_np(1024*1024, 0) != 0) {
1619 (void)GC_printf("pthread_default_stacksize_np failed.\n");
1621 # endif /* GC_HPUX_THREADS */
1622 # ifdef PTW32_STATIC_LIB
1623 pthread_win32_process_attach_np ();
1624 pthread_win32_thread_attach_np ();
1628 pthread_attr_init(&attr);
1629 # if defined(GC_IRIX_THREADS) || defined(GC_FREEBSD_THREADS) \
1630 || defined(GC_DARWIN_THREADS) || defined(GC_AIX_THREADS)
1631 pthread_attr_setstacksize(&attr, 1000000);
1634 # if (defined(MPROTECT_VDB)) \
1635 && !defined(PARALLEL_MARK) &&!defined(REDIRECT_MALLOC) \
1636 && !defined(MAKE_BACK_GRAPH) && !defined(USE_PROC_FOR_LIBRARIES) \
1637 && !defined(NO_INCREMENTAL)
1638 GC_enable_incremental();
1639 (void) GC_printf("Switched to incremental mode\n");
1640 # if defined(MPROTECT_VDB)
1641 (void)GC_printf("Emulating dirty bits with mprotect/signals\n");
1644 (void)GC_printf("Reading dirty bits from /proc\n");
1646 (void)GC_printf("Using DEFAULT_VDB dirty bit implementation\n");
1650 (void) GC_set_warn_proc(warn_proc);
1651 if ((code = pthread_key_create(&fl_key, 0)) != 0) {
1652 (void)GC_printf("Key creation failed %d\n", code);
1655 if ((code = pthread_create(&th1, &attr, thr_run_one_test, 0)) != 0) {
1656 (void)GC_printf("Thread 1 creation failed %d\n", code);
1659 if ((code = pthread_create(&th2, &attr, thr_run_one_test, 0)) != 0) {
1660 (void)GC_printf("Thread 2 creation failed %d\n", code);
1664 if ((code = pthread_join(th1, 0)) != 0) {
1665 (void)GC_printf("Thread 1 failed %d\n", code);
1668 if (pthread_join(th2, 0) != 0) {
1669 (void)GC_printf("Thread 2 failed %d\n", code);
1673 (void)fflush(stdout);
1674 pthread_attr_destroy(&attr);
1675 GC_printf("Completed %d collections\n", GC_gc_no);
1676 # ifdef PTW32_STATIC_LIB
1677 pthread_win32_thread_detach_np ();
1678 pthread_win32_process_detach_np ();
1682 #endif /* GC_PTHREADS */