📄 test.c
字号:
(void)GC_printf1("Completed %lu tests\n", (unsigned long)n_tests); (void)GC_printf1("Allocated %lu collectable objects\n", (unsigned long)collectable_count); (void)GC_printf1("Allocated %lu uncollectable objects\n", (unsigned long)uncollectable_count); (void)GC_printf1("Allocated %lu atomic objects\n", (unsigned long)atomic_count); (void)GC_printf1("Allocated %lu stubborn objects\n", (unsigned long)stubborn_count); (void)GC_printf2("Finalized %lu/%lu objects - ", (unsigned long)finalized_count, (unsigned long)finalizable_count);# ifdef FINALIZE_ON_DEMAND if (finalized_count != late_finalize_count) { (void)GC_printf0("Demand finalization error\n"); FAIL; }# endif if (finalized_count > finalizable_count || finalized_count < finalizable_count/2) { (void)GC_printf0("finalization is probably broken\n"); FAIL; } else { (void)GC_printf0("finalization is probably ok\n"); } still_live = 0; for (i = 0; i < MAX_FINALIZED; i++) { if (live_indicators[i] != 0) { still_live++; } } i = finalizable_count - finalized_count - still_live; if (0 != i) { (void)GC_printf2 ("%lu disappearing links remain and %ld more objects were not finalized\n", (unsigned long) still_live, (long)i); if (i > 10) { GC_printf0("\tVery suspicious!\n"); } else { GC_printf0("\tSlightly suspicious, but probably OK.\n"); } } (void)GC_printf1("Total number of bytes allocated is %lu\n", (unsigned long) WORDS_TO_BYTES(GC_words_allocd + GC_words_allocd_before_gc)); (void)GC_printf1("Final heap size is %lu bytes\n", (unsigned long)GC_get_heap_size()); if (WORDS_TO_BYTES(GC_words_allocd + GC_words_allocd_before_gc)# ifdef VERY_SMALL_CONFIG < 2700000*n_tests) {# else < 33500000*n_tests) {# endif (void)GC_printf0("Incorrect execution - missed some allocations\n"); FAIL; } if (GC_get_heap_size() > max_heap_sz*n_tests) { (void)GC_printf0("Unexpected heap growth - collector may be broken\n"); FAIL; } (void)GC_printf0("Collector appears to work\n");}#if defined(MACOS)void SetMinimumStack(long minSize){ long newApplLimit; if (minSize > LMGetDefltStack()) { newApplLimit = (long) GetApplLimit() - (minSize - LMGetDefltStack()); SetApplLimit((Ptr) newApplLimit); MaxApplZone(); }}#define cMinStackSpace (512L * 1024L)#endif#ifdef __STDC__ void warn_proc(char *msg, GC_word p)#else void warn_proc(msg, p) char *msg; GC_word p;#endif{ GC_printf1(msg, (unsigned long)p); /*FAIL;*/}#if !defined(PCR) && !defined(GC_SOLARIS_THREADS) \ && !defined(GC_WIN32_THREADS) && !defined(GC_PTHREADS) \ || defined(LINT)#if defined(MSWIN32) && !defined(__MINGW32__) int APIENTRY WinMain(HINSTANCE instance, HINSTANCE prev, LPTSTR cmd, int n)#else int main()#endif{# if defined(DJGPP) int dummy;# endif n_tests = 0; # if defined(DJGPP) /* No good way to determine stack base from library; do it */ /* manually on this platform. */ GC_stackbottom = (GC_PTR)(&dummy);# endif# if defined(MACOS) /* Make sure we have lots and lots of stack space. */ SetMinimumStack(cMinStackSpace); /* Cheat and let stdio initialize toolbox for us. */ printf("Testing GC Macintosh port.\n");# endif GC_INIT(); /* Only needed on a few platforms. */ (void) GC_set_warn_proc(warn_proc);# if (defined(MPROTECT_VDB) || defined(PROC_VDB)) \ && !defined(MAKE_BACK_GRAPH) GC_enable_incremental(); (void) GC_printf0("Switched to incremental mode\n");# if defined(MPROTECT_VDB) (void)GC_printf0("Emulating dirty bits with mprotect/signals\n");# else# ifdef PROC_VDB (void)GC_printf0("Reading dirty bits from /proc\n");# else (void)GC_printf0("Using DEFAULT_VDB dirty bit implementation\n");# endif# endif# endif run_one_test(); check_heap_stats();# ifndef MSWINCE (void)fflush(stdout);# endif# ifdef LINT /* Entry points we should be testing, but aren't. */ /* Some can be tested by defining GC_DEBUG at the top of this file */ /* This is a bit SunOS4 specific. */ GC_noop(GC_expand_hp, GC_add_roots, GC_clear_roots, GC_register_disappearing_link, GC_register_finalizer_ignore_self, GC_debug_register_displacement, GC_print_obj, GC_debug_change_stubborn, GC_debug_end_stubborn_change, GC_debug_malloc_uncollectable, GC_debug_free, GC_debug_realloc, GC_generic_malloc_words_small, GC_init, GC_make_closure, GC_debug_invoke_finalizer, GC_page_was_ever_dirty, GC_is_fresh, GC_malloc_ignore_off_page, GC_malloc_atomic_ignore_off_page, GC_set_max_heap_size, GC_get_bytes_since_gc, GC_get_total_bytes, GC_pre_incr, GC_post_incr);# endif# ifdef MSWIN32 GC_win32_free_heap();# endif return(0);}# endif#if defined(GC_WIN32_THREADS) && !defined(CYGWIN32)DWORD __stdcall thr_run_one_test(void *arg){ run_one_test(); return 0;}#ifdef MSWINCEHANDLE win_created_h;HWND win_handle;LRESULT CALLBACK window_proc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam){ LRESULT ret = 0; switch (uMsg) { case WM_HIBERNATE: GC_printf0("Received WM_HIBERNATE, calling GC_gcollect\n"); GC_gcollect(); break; case WM_CLOSE: GC_printf0("Received WM_CLOSE, closing window\n"); DestroyWindow(hwnd); break; case WM_DESTROY: PostQuitMessage(0); break; default: ret = DefWindowProc(hwnd, uMsg, wParam, lParam); break; } return ret;}DWORD __stdcall thr_window(void *arg){ WNDCLASS win_class = { CS_NOCLOSE, window_proc, 0, 0, GetModuleHandle(NULL), NULL, NULL, (HBRUSH)(COLOR_APPWORKSPACE+1), NULL, L"GCtestWindow" }; MSG msg; if (!RegisterClass(&win_class)) FAIL; win_handle = CreateWindowEx( 0, L"GCtestWindow", L"GCtest", 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, GetModuleHandle(NULL), NULL); if (win_handle == NULL) FAIL; SetEvent(win_created_h); ShowWindow(win_handle, SW_SHOW); UpdateWindow(win_handle); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return 0;}#endif#define NTEST 2# ifdef MSWINCEint APIENTRY GC_WinMain(HINSTANCE instance, HINSTANCE prev, LPWSTR cmd, int n)# elseint APIENTRY WinMain(HINSTANCE instance, HINSTANCE prev, LPSTR cmd, int n)# endif{# if NTEST > 0 HANDLE h[NTEST]; int i;# endif# ifdef MSWINCE HANDLE win_thr_h;# endif DWORD thread_id;# if 0 GC_enable_incremental();# endif GC_init(); InitializeCriticalSection(&incr_cs); (void) GC_set_warn_proc(warn_proc);# ifdef MSWINCE win_created_h = CreateEvent(NULL, FALSE, FALSE, NULL); if (win_created_h == (HANDLE)NULL) { (void)GC_printf1("Event creation failed %lu\n", (unsigned long)GetLastError()); FAIL; } win_thr_h = GC_CreateThread(NULL, 0, thr_window, 0, 0, &thread_id); if (win_thr_h == (HANDLE)NULL) { (void)GC_printf1("Thread creation failed %lu\n", (unsigned long)GetLastError()); FAIL; } if (WaitForSingleObject(win_created_h, INFINITE) != WAIT_OBJECT_0) FAIL; CloseHandle(win_created_h);# endif# if NTEST > 0 for (i = 0; i < NTEST; i++) { h[i] = GC_CreateThread(NULL, 0, thr_run_one_test, 0, 0, &thread_id); if (h[i] == (HANDLE)NULL) { (void)GC_printf1("Thread creation failed %lu\n", (unsigned long)GetLastError()); FAIL; } }# endif /* NTEST > 0 */ run_one_test();# if NTEST > 0 for (i = 0; i < NTEST; i++) { if (WaitForSingleObject(h[i], INFINITE) != WAIT_OBJECT_0) { (void)GC_printf1("Thread wait failed %lu\n", (unsigned long)GetLastError()); FAIL; } }# endif /* NTEST > 0 */# ifdef MSWINCE PostMessage(win_handle, WM_CLOSE, 0, 0); if (WaitForSingleObject(win_thr_h, INFINITE) != WAIT_OBJECT_0) FAIL;# endif check_heap_stats(); return(0);}#endif /* GC_WIN32_THREADS */#ifdef PCRtest(){ PCR_Th_T * th1; PCR_Th_T * th2; int code; n_tests = 0; /* GC_enable_incremental(); */ (void) GC_set_warn_proc(warn_proc); th1 = PCR_Th_Fork(run_one_test, 0); th2 = PCR_Th_Fork(run_one_test, 0); run_one_test(); if (PCR_Th_T_Join(th1, &code, NIL, PCR_allSigsBlocked, PCR_waitForever) != PCR_ERes_okay || code != 0) { (void)GC_printf0("Thread 1 failed\n"); } if (PCR_Th_T_Join(th2, &code, NIL, PCR_allSigsBlocked, PCR_waitForever) != PCR_ERes_okay || code != 0) { (void)GC_printf0("Thread 2 failed\n"); } check_heap_stats(); return(0);}#endif#if defined(GC_SOLARIS_THREADS) || defined(GC_PTHREADS)void * thr_run_one_test(void * arg){ run_one_test(); return(0);}#ifdef GC_DEBUG# define GC_free GC_debug_free#endif#if defined(GC_SOLARIS_THREADS) && !defined(GC_SOLARIS_PTHREADS)main(){ thread_t th1; thread_t th2; int code; n_tests = 0; GC_INIT(); /* Only needed if gc is dynamic library. */# ifndef MAKE_BACK_GRAPH GC_enable_incremental();# endif (void) GC_set_warn_proc(warn_proc); if (thr_keycreate(&fl_key, GC_free) != 0) { (void)GC_printf1("Key creation failed %lu\n", (unsigned long)code); FAIL; } if ((code = thr_create(0, 1024*1024, thr_run_one_test, 0, 0, &th1)) != 0) { (void)GC_printf1("Thread 1 creation failed %lu\n", (unsigned long)code); FAIL; } if ((code = thr_create(0, 1024*1024, thr_run_one_test, 0, THR_NEW_LWP, &th2)) != 0) { (void)GC_printf1("Thread 2 creation failed %lu\n", (unsigned long)code); FAIL; } run_one_test(); if ((code = thr_join(th1, 0, 0)) != 0) { (void)GC_printf1("Thread 1 failed %lu\n", (unsigned long)code); FAIL; } if (thr_join(th2, 0, 0) != 0) { (void)GC_printf1("Thread 2 failed %lu\n", (unsigned long)code); FAIL; } check_heap_stats(); (void)fflush(stdout); return(0);}#else /* pthreads */#ifndef GC_PTHREADS --> bad news#endifmain(){ pthread_t th1; pthread_t th2; pthread_attr_t attr; int code;# ifdef GC_IRIX_THREADS /* Force a larger stack to be preallocated */ /* Since the initial cant always grow later. */ *((volatile char *)&code - 1024*1024) = 0; /* Require 1 Mb */# endif /* GC_IRIX_THREADS */# if defined(GC_HPUX_THREADS) /* Default stack size is too small, especially with the 64 bit ABI */ /* Increase it. */ if (pthread_default_stacksize_np(1024*1024, 0) != 0) { (void)GC_printf0("pthread_default_stacksize_np failed.\n"); }# endif /* GC_HPUX_THREADS */ GC_INIT(); pthread_attr_init(&attr);# if defined(GC_IRIX_THREADS) || defined(GC_FREEBSD_THREADS) \ || defined(GC_DARWIN_THREADS) || defined(GC_AIX_THREADS) pthread_attr_setstacksize(&attr, 1000000);# endif n_tests = 0;# if (defined(MPROTECT_VDB)) \ && !defined(PARALLEL_MARK) &&!defined(REDIRECT_MALLOC) \ && !defined(MAKE_BACK_GRAPH) GC_enable_incremental(); (void) GC_printf0("Switched to incremental mode\n");# if defined(MPROTECT_VDB) (void)GC_printf0("Emulating dirty bits with mprotect/signals\n");# else# ifdef PROC_VDB (void)GC_printf0("Reading dirty bits from /proc\n");# else (void)GC_printf0("Using DEFAULT_VDB dirty bit implementation\n");# endif# endif# endif (void) GC_set_warn_proc(warn_proc); if ((code = pthread_key_create(&fl_key, 0)) != 0) { (void)GC_printf1("Key creation failed %lu\n", (unsigned long)code); FAIL; } if ((code = pthread_create(&th1, &attr, thr_run_one_test, 0)) != 0) { (void)GC_printf1("Thread 1 creation failed %lu\n", (unsigned long)code); FAIL; } if ((code = pthread_create(&th2, &attr, thr_run_one_test, 0)) != 0) { (void)GC_printf1("Thread 2 creation failed %lu\n", (unsigned long)code); FAIL; } run_one_test(); if ((code = pthread_join(th1, 0)) != 0) { (void)GC_printf1("Thread 1 failed %lu\n", (unsigned long)code); FAIL; } if (pthread_join(th2, 0) != 0) { (void)GC_printf1("Thread 2 failed %lu\n", (unsigned long)code); FAIL; } check_heap_stats(); (void)fflush(stdout); pthread_attr_destroy(&attr); GC_printf1("Completed %d collections\n", GC_gc_no); return(0);}#endif /* GC_PTHREADS */#endif /* GC_SOLARIS_THREADS || GC_PTHREADS */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -