123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593 |
- #include "private/gc_priv.h"
- #include <stdio.h>
- #if !defined(MACOS) && !defined(MSWINCE)
- # include <signal.h>
- # if !defined(GC_NO_TYPES) && !defined(SN_TARGET_PSP2) \
- && !defined(__CC_ARM)
- # include <sys/types.h>
- # endif
- #endif
- #if VERIFY_UNITY_DEFINES
- #if ! (ALL_INTERIOR_POINTERS \
- && GC_GCJ_SUPPORT \
- && JAVA_FINALIZATION \
- && NO_EXECUTE_PERMISSION \
- && GC_VERSION_MAJOR == 7 \
- && GC_VERSION_MINOR == 7 \
- && GC_VERSION_MICRO == 0 \
- && GC_NO_THREADS_DISCOVERY \
- && IGNORE_DYNAMIC_LOADING \
- && GC_DONT_REGISTER_MAIN_STATIC_DATA \
- && GC_THREADS \
- && USE_MMAP \
- && USE_MUNMAP)
- #error Unity Defines Incorrect
- #endif
- #endif
- word GC_non_gc_bytes = 0;
- word GC_gc_no = 0;
- #ifndef NO_CLOCK
- static unsigned long full_gc_total_time = 0;
- static GC_bool measure_performance = FALSE;
-
-
- GC_API void GC_CALL GC_start_performance_measurement(void)
- {
- measure_performance = TRUE;
- }
- GC_API unsigned long GC_CALL GC_get_full_gc_total_time(void)
- {
- return full_gc_total_time;
- }
- #endif
- #ifndef GC_DISABLE_INCREMENTAL
- GC_INNER GC_bool GC_incremental = FALSE;
- #endif
- GC_API int GC_CALL GC_is_incremental_mode(void)
- {
- return (int)GC_incremental;
- }
- #ifdef THREADS
- int GC_parallel = FALSE;
- #endif
- #if defined(GC_FULL_FREQ) && !defined(CPPCHECK)
- int GC_full_freq = GC_FULL_FREQ;
- #else
- int GC_full_freq = 19;
-
-
- #endif
- STATIC GC_bool GC_need_full_gc = FALSE;
-
- #ifdef THREAD_LOCAL_ALLOC
- GC_INNER GC_bool GC_world_stopped = FALSE;
- #endif
- STATIC word GC_used_heap_size_after_full = 0;
- EXTERN_C_BEGIN
- extern const char * const GC_copyright[];
- EXTERN_C_END
- const char * const GC_copyright[] =
- {"Copyright 1988,1989 Hans-J. Boehm and Alan J. Demers ",
- "Copyright (c) 1991-1995 by Xerox Corporation. All rights reserved. ",
- "Copyright (c) 1996-1998 by Silicon Graphics. All rights reserved. ",
- "Copyright (c) 1999-2009 by Hewlett-Packard Company. All rights reserved. ",
- "THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY",
- " EXPRESSED OR IMPLIED. ANY USE IS AT YOUR OWN RISK.",
- "See source code for details." };
- #ifndef GC_NO_VERSION_VAR
- EXTERN_C_BEGIN
- extern const unsigned GC_version;
- EXTERN_C_END
- const unsigned GC_version = ((GC_VERSION_MAJOR << 16) |
- (GC_VERSION_MINOR << 8) | GC_VERSION_MICRO);
- #endif
- GC_API unsigned GC_CALL GC_get_version(void)
- {
- return (GC_VERSION_MAJOR << 16) | (GC_VERSION_MINOR << 8) |
- GC_VERSION_MICRO;
- }
- #ifdef GC_DONT_EXPAND
- int GC_dont_expand = TRUE;
- #else
- int GC_dont_expand = FALSE;
- #endif
- #if defined(GC_FREE_SPACE_DIVISOR) && !defined(CPPCHECK)
- word GC_free_space_divisor = GC_FREE_SPACE_DIVISOR;
- #else
- word GC_free_space_divisor = 3;
- #endif
- GC_INNER int GC_CALLBACK GC_never_stop_func(void)
- {
- return(0);
- }
- #if defined(GC_TIME_LIMIT) && !defined(CPPCHECK)
- unsigned long long GC_time_limit = GC_TIME_LIMIT * 1000000;
-
-
- #else
- unsigned long long GC_time_limit = 50000000;
- #endif
- #ifndef NO_CLOCK
- STATIC CLOCK_TYPE GC_start_time = 0;
-
-
- #endif
- STATIC int GC_n_attempts = 0;
-
- STATIC GC_stop_func GC_default_stop_func = GC_never_stop_func;
-
- GC_API void GC_CALL GC_set_stop_func(GC_stop_func stop_func)
- {
- DCL_LOCK_STATE;
- GC_ASSERT(NONNULL_ARG_NOT_NULL(stop_func));
- LOCK();
- GC_default_stop_func = stop_func;
- UNLOCK();
- }
- GC_API GC_stop_func GC_CALL GC_get_stop_func(void)
- {
- GC_stop_func stop_func;
- DCL_LOCK_STATE;
- LOCK();
- stop_func = GC_default_stop_func;
- UNLOCK();
- return stop_func;
- }
- #if defined(GC_DISABLE_INCREMENTAL) || defined(NO_CLOCK)
- # define GC_timeout_stop_func GC_default_stop_func
- #else
- STATIC int GC_CALLBACK GC_timeout_stop_func (void)
- {
- CLOCK_TYPE current_time;
- static unsigned count = 0;
- unsigned long long time_diff;
- if ((*GC_default_stop_func)())
- return(1);
- if ((count++ & 3) != 0) return(0);
- GET_TIME(current_time);
- time_diff = NS_TIME_DIFF(current_time,GC_start_time);
- if (time_diff >= GC_time_limit) {
- GC_COND_LOG_PRINTF(
- "Abandoning stopped marking after %llu nanoseconds (attempt %d)\n",
- time_diff, GC_n_attempts);
- return(1);
- }
- return(0);
- }
- #endif
- #ifdef THREADS
- GC_INNER word GC_total_stacksize = 0;
- #endif
- static size_t min_bytes_allocd_minimum = 1;
-
- GC_API void GC_CALL GC_set_min_bytes_allocd(size_t value)
- {
- GC_ASSERT(value > 0);
- min_bytes_allocd_minimum = value;
- }
- GC_API size_t GC_CALL GC_get_min_bytes_allocd(void)
- {
- return min_bytes_allocd_minimum;
- }
- static word min_bytes_allocd(void)
- {
- word result;
- word stack_size;
- word total_root_size;
-
-
- word scan_size;
-
- # ifdef THREADS
- if (GC_need_to_lock) {
-
- stack_size = GC_total_stacksize;
-
- # ifdef DEBUG_THREADS
- GC_log_printf("Total stacks size: %lu\n",
- (unsigned long)stack_size);
- # endif
- } else
- # endif
- {
- # ifdef STACK_NOT_SCANNED
- stack_size = 0;
- # elif defined(STACK_GROWS_UP)
- stack_size = GC_approx_sp() - GC_stackbottom;
- # else
- stack_size = GC_stackbottom - GC_approx_sp();
- # endif
- }
- total_root_size = 2 * stack_size + GC_root_size;
- scan_size = 2 * GC_composite_in_use + GC_atomic_in_use / 4
- + total_root_size;
- result = scan_size / GC_free_space_divisor;
- if (GC_incremental) {
- result /= 2;
- }
- return result > min_bytes_allocd_minimum
- ? result : min_bytes_allocd_minimum;
- }
- STATIC word GC_non_gc_bytes_at_gc = 0;
-
-
- STATIC word GC_adj_bytes_allocd(void)
- {
- signed_word result;
- signed_word expl_managed = (signed_word)GC_non_gc_bytes
- - (signed_word)GC_non_gc_bytes_at_gc;
-
-
-
-
- result = (signed_word)GC_bytes_allocd
- + (signed_word)GC_bytes_dropped
- - (signed_word)GC_bytes_freed
- + (signed_word)GC_finalizer_bytes_freed
- - expl_managed;
- if (result > (signed_word)GC_bytes_allocd) {
- result = GC_bytes_allocd;
-
- }
- result += GC_bytes_finalized;
-
-
-
-
- if (result < (signed_word)(GC_bytes_allocd >> 3)) {
-
-
-
-
- return(GC_bytes_allocd >> 3);
- } else {
- return(result);
- }
- }
- STATIC void GC_clear_a_few_frames(void)
- {
- # ifndef CLEAR_NWORDS
- # define CLEAR_NWORDS 64
- # endif
- volatile word frames[CLEAR_NWORDS];
- BZERO((word *)frames, CLEAR_NWORDS * sizeof(word));
- }
- STATIC word GC_collect_at_heapsize = (word)(-1);
- STATIC GC_bool GC_should_start_incremental_collection = FALSE;
- STATIC GC_bool GC_disable_automatic_collection = FALSE;
- GC_API void GC_set_disable_automatic_collection(GC_bool disable)
- {
- GC_disable_automatic_collection = disable;
- }
- GC_API void GC_start_incremental_collection()
- {
- if (GC_incremental)
- {
- GC_should_start_incremental_collection = TRUE;
- GC_collect_a_little();
- }
- }
- GC_INNER GC_bool GC_should_collect(void)
- {
- static word last_min_bytes_allocd;
- static word last_gc_no;
- if (last_gc_no != GC_gc_no) {
- last_gc_no = GC_gc_no;
- last_min_bytes_allocd = min_bytes_allocd();
- }
- if (GC_should_start_incremental_collection)
- {
- GC_should_start_incremental_collection = FALSE;
- return TRUE;
- }
- if (GC_disable_automatic_collection)
- return FALSE;
- return(GC_adj_bytes_allocd() >= last_min_bytes_allocd
- || GC_heapsize >= GC_collect_at_heapsize);
- }
- GC_start_callback_proc GC_start_call_back = 0;
-
-
-
- GC_API void GC_CALL GC_set_start_callback(GC_start_callback_proc fn)
- {
- DCL_LOCK_STATE;
- LOCK();
- GC_start_call_back = fn;
- UNLOCK();
- }
- GC_API GC_start_callback_proc GC_CALL GC_get_start_callback(void)
- {
- GC_start_callback_proc fn;
- DCL_LOCK_STATE;
- LOCK();
- fn = GC_start_call_back;
- UNLOCK();
- return fn;
- }
- GC_INLINE void GC_notify_full_gc(void)
- {
- if (GC_start_call_back != 0) {
- (*GC_start_call_back)();
- }
- }
- STATIC GC_bool GC_is_full_gc = FALSE;
- STATIC GC_bool GC_stopped_mark(GC_stop_func stop_func);
- STATIC void GC_finish_collection(void);
- STATIC void GC_maybe_gc(void)
- {
- GC_ASSERT(I_HOLD_LOCK());
- ASSERT_CANCEL_DISABLED();
- if (GC_should_collect()) {
- static int n_partial_gcs = 0;
- if (!GC_incremental) {
-
- GC_try_to_collect_inner(GC_never_stop_func);
- n_partial_gcs = 0;
- return;
- } else {
- # ifdef PARALLEL_MARK
- if (GC_parallel)
- GC_wait_for_reclaim();
- # endif
- if (GC_need_full_gc || n_partial_gcs >= GC_full_freq) {
- GC_COND_LOG_PRINTF(
- "***>Full mark for collection #%lu after %lu allocd bytes\n",
- (unsigned long)GC_gc_no + 1, (unsigned long)GC_bytes_allocd);
- GC_promote_black_lists();
- (void)GC_reclaim_all((GC_stop_func)0, TRUE);
- GC_notify_full_gc();
- GC_clear_marks();
- n_partial_gcs = 0;
- GC_is_full_gc = TRUE;
- } else {
- n_partial_gcs++;
- }
- }
-
-
-
- # ifndef NO_CLOCK
- if (GC_time_limit != GC_TIME_UNLIMITED) { GET_TIME(GC_start_time); }
- # endif
-
-
- if (GC_stopped_mark(GC_time_limit == GC_TIME_UNLIMITED?
- GC_never_stop_func : GC_timeout_stop_func)) {
- # ifdef SAVE_CALL_CHAIN
- GC_save_callers(GC_last_stack);
- # endif
- GC_finish_collection();
- } else {
- if (!GC_is_full_gc) {
-
- GC_n_attempts++;
- }
- }
- }
- }
- STATIC GC_on_collection_event_proc GC_on_collection_event = 0;
- GC_API void GC_CALL GC_set_on_collection_event(GC_on_collection_event_proc fn)
- {
-
- DCL_LOCK_STATE;
- LOCK();
- GC_on_collection_event = fn;
- UNLOCK();
- }
- GC_API GC_on_collection_event_proc GC_CALL GC_get_on_collection_event(void)
- {
- GC_on_collection_event_proc fn;
- DCL_LOCK_STATE;
- LOCK();
- fn = GC_on_collection_event;
- UNLOCK();
- return fn;
- }
- GC_INNER GC_bool GC_try_to_collect_inner(GC_stop_func stop_func)
- {
- # ifndef NO_CLOCK
- CLOCK_TYPE start_time = 0;
- GC_bool start_time_valid;
- # endif
- ASSERT_CANCEL_DISABLED();
- GC_ASSERT(I_HOLD_LOCK());
- if (GC_dont_gc || (*stop_func)()) return FALSE;
- if (GC_on_collection_event)
- GC_on_collection_event(GC_EVENT_START);
- if (GC_incremental && GC_collection_in_progress()) {
- GC_COND_LOG_PRINTF(
- "GC_try_to_collect_inner: finishing collection in progress\n");
-
- while(GC_collection_in_progress()) {
- if ((*stop_func)()) {
-
- return(FALSE);
- }
- GC_collect_a_little_inner(1);
- }
- }
- GC_notify_full_gc();
- # ifndef NO_CLOCK
- start_time_valid = FALSE;
- if ((GC_print_stats | (int)measure_performance) != 0) {
- if (GC_print_stats)
- GC_log_printf("Initiating full world-stop collection!\n");
- start_time_valid = TRUE;
- GET_TIME(start_time);
- }
- # endif
- GC_promote_black_lists();
-
-
-
-
-
- # ifdef PARALLEL_MARK
- if (GC_parallel)
- GC_wait_for_reclaim();
- # endif
- if ((GC_find_leak || stop_func != GC_never_stop_func)
- && !GC_reclaim_all(stop_func, FALSE)) {
-
-
- return(FALSE);
- }
- GC_invalidate_mark_state();
- GC_clear_marks();
- # ifdef SAVE_CALL_CHAIN
- GC_save_callers(GC_last_stack);
- # endif
- GC_is_full_gc = TRUE;
- if (!GC_stopped_mark(stop_func)) {
- if (!GC_incremental) {
-
-
-
- GC_invalidate_mark_state();
- GC_unpromote_black_lists();
- }
-
-
- return(FALSE);
- }
- GC_finish_collection();
- # ifndef NO_CLOCK
- if (start_time_valid) {
- CLOCK_TYPE current_time;
- unsigned long time_diff;
- GET_TIME(current_time);
- time_diff = MS_TIME_DIFF(current_time, start_time);
- if (measure_performance)
- full_gc_total_time += time_diff;
- if (GC_print_stats)
- GC_log_printf("Complete collection took %lu msecs\n", time_diff);
- }
- # endif
- if (GC_on_collection_event)
- GC_on_collection_event(GC_EVENT_END);
- return(TRUE);
- }
- #ifndef GC_RATE
- # define GC_RATE 10
- #endif
- #ifndef MAX_PRIOR_ATTEMPTS
- # define MAX_PRIOR_ATTEMPTS 1
- #endif
-
-
-
-
- STATIC int GC_deficit = 0;
-
- STATIC int GC_rate = GC_RATE;
- GC_API void GC_CALL GC_set_rate(int value)
- {
- GC_ASSERT(value > 0);
- GC_rate = value;
- }
- GC_API int GC_CALL GC_get_rate(void)
- {
- return GC_rate;
- }
- static int max_prior_attempts = MAX_PRIOR_ATTEMPTS;
- GC_API void GC_CALL GC_set_max_prior_attempts(int value)
- {
- GC_ASSERT(value >= 0);
- max_prior_attempts = value;
- }
- GC_API int GC_CALL GC_get_max_prior_attempts(void)
- {
- return max_prior_attempts;
- }
- GC_INNER void GC_collect_a_little_inner(int n)
- {
- IF_CANCEL(int cancel_state;)
- GC_ASSERT(I_HOLD_LOCK());
- if (GC_dont_gc) return;
- DISABLE_CANCEL(cancel_state);
- if (GC_incremental && GC_collection_in_progress()) {
- int i;
- int max_deficit = GC_rate * n;
- for (i = GC_deficit; i < max_deficit; i++) {
- if (GC_mark_some((ptr_t)0)) {
-
- # ifdef SAVE_CALL_CHAIN
- GC_save_callers(GC_last_stack);
- # endif
- # ifdef PARALLEL_MARK
- if (GC_parallel)
- GC_wait_for_reclaim();
- # endif
- if (GC_n_attempts < max_prior_attempts
- && GC_time_limit != GC_TIME_UNLIMITED) {
- # ifndef NO_CLOCK
- GET_TIME(GC_start_time);
- # endif
- if (!GC_stopped_mark(GC_timeout_stop_func)) {
- GC_n_attempts++;
- break;
- }
- } else {
-
-
- (void)GC_stopped_mark(GC_never_stop_func);
- }
- GC_finish_collection();
- break;
- }
- }
- if (GC_deficit > 0) {
- GC_deficit -= max_deficit;
- if (GC_deficit < 0)
- GC_deficit = 0;
- }
- } else {
- GC_maybe_gc();
- }
- RESTORE_CANCEL(cancel_state);
- }
- GC_INNER void (*GC_check_heap)(void) = 0;
- GC_INNER void (*GC_print_all_smashed)(void) = 0;
- GC_API int GC_CALL GC_collect_a_little(void)
- {
- int result;
- DCL_LOCK_STATE;
- LOCK();
- GC_collect_a_little_inner(1);
- result = (int)GC_collection_in_progress();
- UNLOCK();
- if (!result && GC_debugging_started) GC_print_all_smashed();
- return(result);
- }
- #ifndef NO_CLOCK
-
-
-
- static unsigned world_stopped_total_time = 0;
- static unsigned world_stopped_total_divisor = 0;
- # ifndef MAX_TOTAL_TIME_DIVISOR
-
-
-
- # define MAX_TOTAL_TIME_DIVISOR 1000
- # endif
- #endif
- #ifdef USE_MUNMAP
- # define IF_USE_MUNMAP(x) x
- # define COMMA_IF_USE_MUNMAP(x) , x
- #else
- # define IF_USE_MUNMAP(x)
- # define COMMA_IF_USE_MUNMAP(x)
- #endif
- STATIC GC_bool GC_stopped_mark(GC_stop_func stop_func)
- {
- unsigned i;
- # ifndef NO_CLOCK
- CLOCK_TYPE start_time = 0;
- # endif
- GC_ASSERT(I_HOLD_LOCK());
- # if !defined(REDIRECT_MALLOC) && defined(USE_WINALLOC)
- GC_add_current_malloc_heap();
- # endif
- # if defined(REGISTER_LIBRARIES_EARLY)
- GC_cond_register_dynamic_libraries();
- # endif
- # ifndef NO_CLOCK
- if (GC_PRINT_STATS_FLAG)
- GET_TIME(start_time);
- # endif
- # if !defined(GC_NO_FINALIZATION) && !defined(GC_TOGGLE_REFS_NOT_NEEDED)
- GC_process_togglerefs();
- # endif
- # ifdef THREADS
- if (GC_on_collection_event)
- GC_on_collection_event(GC_EVENT_PRE_STOP_WORLD);
- # endif
- STOP_WORLD();
- # ifdef THREADS
- if (GC_on_collection_event)
- GC_on_collection_event(GC_EVENT_POST_STOP_WORLD);
- # endif
- # ifdef THREAD_LOCAL_ALLOC
- GC_world_stopped = TRUE;
- # endif
-
- GC_COND_LOG_PRINTF(
- "\n--> Marking for collection #%lu after %lu allocated bytes\n",
- (unsigned long)GC_gc_no + 1, (unsigned long) GC_bytes_allocd);
- # ifdef MAKE_BACK_GRAPH
- if (GC_print_back_height) {
- GC_build_back_graph();
- }
- # endif
-
- if (GC_on_collection_event)
- GC_on_collection_event(GC_EVENT_MARK_START);
-
- GC_clear_a_few_frames();
- GC_noop6(0,0,0,0,0,0);
- GC_initiate_gc();
- for (i = 0;;i++) {
- if ((*stop_func)()) {
- GC_COND_LOG_PRINTF("Abandoned stopped marking after"
- " %u iterations\n", i);
- GC_deficit = i;
- # ifdef THREAD_LOCAL_ALLOC
- GC_world_stopped = FALSE;
- # endif
- # ifdef THREADS
- if (GC_on_collection_event)
- GC_on_collection_event(GC_EVENT_PRE_START_WORLD);
- # endif
- START_WORLD();
- # ifdef THREADS
- if (GC_on_collection_event)
- GC_on_collection_event(GC_EVENT_POST_START_WORLD);
- # endif
-
- return(FALSE);
- }
- if (GC_mark_some(GC_approx_sp())) break;
- }
- GC_gc_no++;
- GC_DBGLOG_PRINTF("GC #%lu freed %ld bytes, heap %lu KiB"
- IF_USE_MUNMAP(" (+ %lu KiB unmapped)") "\n",
- (unsigned long)GC_gc_no, (long)GC_bytes_found,
- TO_KiB_UL(GC_heapsize - GC_unmapped_bytes)
- COMMA_IF_USE_MUNMAP(TO_KiB_UL(GC_unmapped_bytes)));
-
- if (GC_debugging_started) {
- (*GC_check_heap)();
- }
- if (GC_on_collection_event)
- GC_on_collection_event(GC_EVENT_MARK_END);
- # ifdef THREAD_LOCAL_ALLOC
- GC_world_stopped = FALSE;
- # endif
- # ifdef THREADS
- if (GC_on_collection_event)
- GC_on_collection_event(GC_EVENT_PRE_START_WORLD);
- # endif
- START_WORLD();
- # ifdef THREADS
- if (GC_on_collection_event)
- GC_on_collection_event(GC_EVENT_POST_START_WORLD);
- # endif
- # ifndef NO_CLOCK
- if (GC_PRINT_STATS_FLAG) {
- unsigned long time_diff;
- unsigned total_time, divisor;
- CLOCK_TYPE current_time;
- GET_TIME(current_time);
- time_diff = MS_TIME_DIFF(current_time,start_time);
-
- total_time = world_stopped_total_time;
- divisor = world_stopped_total_divisor;
- if ((int)total_time < 0 || divisor >= MAX_TOTAL_TIME_DIVISOR) {
-
- total_time >>= 1;
- divisor >>= 1;
- }
- total_time += time_diff < (((unsigned)-1) >> 1) ?
- (unsigned)time_diff : ((unsigned)-1) >> 1;
-
- world_stopped_total_time = total_time;
- world_stopped_total_divisor = ++divisor;
- GC_ASSERT(divisor != 0);
- GC_log_printf(
- "World-stopped marking took %lu msecs (%u in average)\n",
- time_diff, total_time / divisor);
- }
- # endif
- return(TRUE);
- }
- GC_INNER void GC_set_fl_marks(ptr_t q)
- {
- if (q != NULL) {
- struct hblk *h = HBLKPTR(q);
- struct hblk *last_h = h;
- hdr *hhdr = HDR(h);
- IF_PER_OBJ(word sz = hhdr->hb_sz;)
- for (;;) {
- word bit_no = MARK_BIT_NO((ptr_t)q - (ptr_t)h, sz);
- if (!mark_bit_from_hdr(hhdr, bit_no)) {
- set_mark_bit_from_hdr(hhdr, bit_no);
- ++hhdr -> hb_n_marks;
- }
- q = (ptr_t)obj_link(q);
- if (q == NULL)
- break;
- h = HBLKPTR(q);
- if (h != last_h) {
- last_h = h;
- hhdr = HDR(h);
- IF_PER_OBJ(sz = hhdr->hb_sz;)
- }
- }
- }
- }
- #if defined(GC_ASSERTIONS) && defined(THREAD_LOCAL_ALLOC)
-
-
- void GC_check_fl_marks(void **pfreelist)
- {
-
-
-
- # if defined(AO_HAVE_load_acquire_read) && !defined(THREAD_SANITIZER)
- AO_t *list = (AO_t *)AO_load_acquire_read((AO_t *)pfreelist);
-
- AO_t *prev;
- AO_t *p;
- if ((word)list <= HBLKSIZE) return;
- prev = (AO_t *)pfreelist;
- for (p = list; p != NULL;) {
- AO_t *next;
- if (!GC_is_marked(p)) {
- ABORT_ARG2("Unmarked local free list entry",
- ": object %p on list %p", (void *)p, (void *)list);
- }
-
-
-
-
-
-
-
- next = (AO_t *)AO_load_acquire_read(p);
- if (AO_load(prev) != (AO_t)p)
- break;
- prev = p;
- p = next;
- }
- # else
-
- (void)pfreelist;
- # endif
- }
- #endif
- STATIC void GC_clear_fl_marks(ptr_t q)
- {
- struct hblk *h = HBLKPTR(q);
- struct hblk *last_h = h;
- hdr *hhdr = HDR(h);
- word sz = hhdr->hb_sz;
- for (;;) {
- word bit_no = MARK_BIT_NO((ptr_t)q - (ptr_t)h, sz);
- if (mark_bit_from_hdr(hhdr, bit_no)) {
- size_t n_marks = hhdr -> hb_n_marks;
- GC_ASSERT(n_marks != 0);
- clear_mark_bit_from_hdr(hhdr, bit_no);
- n_marks--;
- # ifdef PARALLEL_MARK
-
- if (0 != n_marks || !GC_parallel) {
- hhdr -> hb_n_marks = n_marks;
- }
- # else
- hhdr -> hb_n_marks = n_marks;
- # endif
- }
- GC_bytes_found -= sz;
- q = (ptr_t)obj_link(q);
- if (q == NULL)
- break;
- h = HBLKPTR(q);
- if (h != last_h) {
- last_h = h;
- hhdr = HDR(h);
- sz = hhdr->hb_sz;
- }
- }
- }
- #if defined(GC_ASSERTIONS) && defined(THREAD_LOCAL_ALLOC)
- void GC_check_tls(void);
- #endif
- GC_on_heap_resize_proc GC_on_heap_resize = 0;
- GC_INLINE int GC_compute_heap_usage_percent(void)
- {
- word used = GC_composite_in_use + GC_atomic_in_use;
- word heap_sz = GC_heapsize - GC_unmapped_bytes;
- return used >= heap_sz ? 0 : used < ((word)-1) / 100 ?
- (int)((used * 100) / heap_sz) : (int)(used / (heap_sz / 100));
- }
- STATIC void GC_finish_collection(void)
- {
- # ifndef NO_CLOCK
- CLOCK_TYPE start_time = 0;
- CLOCK_TYPE finalize_time = 0;
- # endif
- GC_ASSERT(I_HOLD_LOCK());
- # if defined(GC_ASSERTIONS) \
- && defined(THREAD_LOCAL_ALLOC) && !defined(DBG_HDRS_ALL)
-
-
- GC_check_tls();
- # endif
- # ifndef NO_CLOCK
- if (GC_print_stats)
- GET_TIME(start_time);
- # endif
- if (GC_on_collection_event)
- GC_on_collection_event(GC_EVENT_RECLAIM_START);
- # ifndef GC_GET_HEAP_USAGE_NOT_NEEDED
- if (GC_bytes_found > 0)
- GC_reclaimed_bytes_before_gc += (word)GC_bytes_found;
- # endif
- GC_bytes_found = 0;
- # if defined(LINUX) && defined(__ELF__) && !defined(SMALL_CONFIG)
- if (GETENV("GC_PRINT_ADDRESS_MAP") != 0) {
- GC_print_address_map();
- }
- # endif
- COND_DUMP;
- if (GC_find_leak) {
-
-
- word size;
- unsigned kind;
- ptr_t q;
- for (kind = 0; kind < GC_n_kinds; kind++) {
- for (size = 1; size <= MAXOBJGRANULES; size++) {
- q = (ptr_t)GC_obj_kinds[kind].ok_freelist[size];
- if (q != NULL)
- GC_set_fl_marks(q);
- }
- }
- GC_start_reclaim(TRUE);
-
- }
- # ifndef GC_NO_FINALIZATION
- GC_finalize();
- # endif
- # ifndef NO_CLOCK
- if (GC_print_stats)
- GET_TIME(finalize_time);
- # endif
- if (GC_print_back_height) {
- # ifdef MAKE_BACK_GRAPH
- GC_traverse_back_graph();
- # elif !defined(SMALL_CONFIG)
- GC_err_printf("Back height not available: "
- "Rebuild collector with -DMAKE_BACK_GRAPH\n");
- # endif
- }
-
-
-
-
-
-
- {
- word size;
- ptr_t q;
- unsigned kind;
- for (kind = 0; kind < GC_n_kinds; kind++) {
- for (size = 1; size <= MAXOBJGRANULES; size++) {
- q = (ptr_t)GC_obj_kinds[kind].ok_freelist[size];
- if (q != NULL)
- GC_clear_fl_marks(q);
- }
- }
- }
- GC_VERBOSE_LOG_PRINTF("Bytes recovered before sweep - f.l. count = %ld\n",
- (long)GC_bytes_found);
-
- GC_start_reclaim(FALSE);
- GC_DBGLOG_PRINTF("In-use heap: %d%% (%lu KiB pointers + %lu KiB other)\n",
- GC_compute_heap_usage_percent(),
- TO_KiB_UL(GC_composite_in_use),
- TO_KiB_UL(GC_atomic_in_use));
- if (GC_is_full_gc) {
- GC_used_heap_size_after_full = USED_HEAP_SIZE;
- GC_need_full_gc = FALSE;
- } else {
- GC_need_full_gc = USED_HEAP_SIZE - GC_used_heap_size_after_full
- > min_bytes_allocd();
- }
- GC_VERBOSE_LOG_PRINTF("Immediately reclaimed %ld bytes, heapsize:"
- " %lu bytes" IF_USE_MUNMAP(" (%lu unmapped)") "\n",
- (long)GC_bytes_found,
- (unsigned long)GC_heapsize
- COMMA_IF_USE_MUNMAP((unsigned long)
- GC_unmapped_bytes));
-
- GC_n_attempts = 0;
- GC_is_full_gc = FALSE;
- GC_bytes_allocd_before_gc += GC_bytes_allocd;
- GC_non_gc_bytes_at_gc = GC_non_gc_bytes;
- GC_bytes_allocd = 0;
- GC_bytes_dropped = 0;
- GC_bytes_freed = 0;
- GC_finalizer_bytes_freed = 0;
- IF_USE_MUNMAP(GC_unmap_old());
- if (GC_on_collection_event)
- GC_on_collection_event(GC_EVENT_RECLAIM_END);
- # ifndef NO_CLOCK
- if (GC_print_stats) {
- CLOCK_TYPE done_time;
- GET_TIME(done_time);
- # if !defined(SMALL_CONFIG) && !defined(GC_NO_FINALIZATION)
-
- GC_print_finalization_stats();
- # endif
- GC_log_printf("Finalize plus initiate sweep took %lu + %lu msecs\n",
- MS_TIME_DIFF(finalize_time,start_time),
- MS_TIME_DIFF(done_time,finalize_time));
- }
- # elif !defined(SMALL_CONFIG) && !defined(GC_NO_FINALIZATION)
- if (GC_print_stats)
- GC_print_finalization_stats();
- # endif
- }
- STATIC GC_bool GC_try_to_collect_general(GC_stop_func stop_func,
- GC_bool force_unmap GC_ATTR_UNUSED)
- {
- GC_bool result;
- IF_USE_MUNMAP(int old_unmap_threshold;)
- IF_CANCEL(int cancel_state;)
- DCL_LOCK_STATE;
- if (!EXPECT(GC_is_initialized, TRUE)) GC_init();
- if (GC_debugging_started) GC_print_all_smashed();
- GC_INVOKE_FINALIZERS();
- LOCK();
- DISABLE_CANCEL(cancel_state);
- # ifdef USE_MUNMAP
- old_unmap_threshold = GC_unmap_threshold;
- if (force_unmap ||
- (GC_force_unmap_on_gcollect && old_unmap_threshold > 0))
- GC_unmap_threshold = 1;
- # endif
- ENTER_GC();
-
- GC_noop6(0,0,0,0,0,0);
- result = GC_try_to_collect_inner(stop_func != 0 ? stop_func :
- GC_default_stop_func);
- EXIT_GC();
- IF_USE_MUNMAP(GC_unmap_threshold = old_unmap_threshold);
- RESTORE_CANCEL(cancel_state);
- UNLOCK();
- if (result) {
- if (GC_debugging_started) GC_print_all_smashed();
- GC_INVOKE_FINALIZERS();
- }
- return(result);
- }
- GC_API int GC_CALL GC_try_to_collect(GC_stop_func stop_func)
- {
- GC_ASSERT(NONNULL_ARG_NOT_NULL(stop_func));
- return (int)GC_try_to_collect_general(stop_func, FALSE);
- }
- GC_API void GC_CALL GC_gcollect(void)
- {
-
-
- (void)GC_try_to_collect_general(0, FALSE);
- if (GC_have_errors) GC_print_all_errors();
- }
- STATIC word GC_heapsize_at_forced_unmap = 0;
- GC_API void GC_CALL GC_gcollect_and_unmap(void)
- {
-
-
- GC_heapsize_at_forced_unmap = GC_heapsize;
-
- (void)GC_try_to_collect_general(GC_never_stop_func, TRUE);
- }
- GC_INNER word GC_n_heap_sects = 0;
-
- #ifdef USE_PROC_FOR_LIBRARIES
- GC_INNER word GC_n_memory = 0;
-
- #endif
- #ifdef USE_PROC_FOR_LIBRARIES
-
-
- GC_INNER void GC_add_to_our_memory(ptr_t p, size_t bytes)
- {
- if (0 == p) return;
- if (GC_n_memory >= MAX_HEAP_SECTS)
- ABORT("Too many GC-allocated memory sections: Increase MAX_HEAP_SECTS");
- GC_our_memory[GC_n_memory].hs_start = p;
- GC_our_memory[GC_n_memory].hs_bytes = bytes;
- GC_n_memory++;
- }
- #endif
- GC_INNER void GC_add_to_heap(struct hblk *p, size_t bytes)
- {
- hdr * phdr;
- word endp;
- if (GC_n_heap_sects >= MAX_HEAP_SECTS) {
- ABORT("Too many heap sections: Increase MAXHINCR or MAX_HEAP_SECTS");
- }
- while ((word)p <= HBLKSIZE) {
-
- ++p;
- bytes -= HBLKSIZE;
- if (0 == bytes) return;
- }
- endp = (word)p + bytes;
- if (endp <= (word)p) {
-
- bytes -= HBLKSIZE;
- if (0 == bytes) return;
- endp -= HBLKSIZE;
- }
- phdr = GC_install_header(p);
- if (0 == phdr) {
-
-
-
- return;
- }
- GC_ASSERT(endp > (word)p && endp == (word)p + bytes);
- GC_heap_sects[GC_n_heap_sects].hs_start = (ptr_t)p;
- GC_heap_sects[GC_n_heap_sects].hs_bytes = bytes;
- GC_n_heap_sects++;
- phdr -> hb_sz = bytes;
- phdr -> hb_flags = 0;
- GC_freehblk(p);
- GC_heapsize += bytes;
-
- GC_collect_at_heapsize += bytes;
- if (GC_collect_at_heapsize < GC_heapsize )
- GC_collect_at_heapsize = (word)(-1);
- if ((word)p <= (word)GC_least_plausible_heap_addr
- || GC_least_plausible_heap_addr == 0) {
- GC_least_plausible_heap_addr = (void *)((ptr_t)p - sizeof(word));
-
-
-
-
- }
- if ((word)p + bytes >= (word)GC_greatest_plausible_heap_addr) {
- GC_greatest_plausible_heap_addr = (void *)endp;
- }
- }
- #if !defined(NO_DEBUGGING)
- void GC_print_heap_sects(void)
- {
- unsigned i;
- GC_printf("Total heap size: %lu" IF_USE_MUNMAP(" (%lu unmapped)") "\n",
- (unsigned long)GC_heapsize
- COMMA_IF_USE_MUNMAP((unsigned long)GC_unmapped_bytes));
- for (i = 0; i < GC_n_heap_sects; i++) {
- ptr_t start = GC_heap_sects[i].hs_start;
- size_t len = GC_heap_sects[i].hs_bytes;
- struct hblk *h;
- unsigned nbl = 0;
- for (h = (struct hblk *)start; (word)h < (word)(start + len); h++) {
- if (GC_is_black_listed(h, HBLKSIZE)) nbl++;
- }
- GC_printf("Section %d from %p to %p %u/%lu blacklisted\n",
- i, (void *)start, (void *)&start[len],
- nbl, (unsigned long)divHBLKSZ(len));
- }
- }
- #endif
- void * GC_least_plausible_heap_addr = (void *)ONES;
- void * GC_greatest_plausible_heap_addr = 0;
- GC_INLINE word GC_max(word x, word y)
- {
- return(x > y? x : y);
- }
- GC_INLINE word GC_min(word x, word y)
- {
- return(x < y? x : y);
- }
- STATIC word GC_max_heapsize = 0;
- GC_API void GC_CALL GC_set_max_heap_size(GC_word n)
- {
- GC_max_heapsize = n;
- }
- GC_word GC_max_retries = 0;
- GC_INNER GC_bool GC_expand_hp_inner(word n)
- {
- size_t bytes;
- struct hblk * space;
- word expansion_slop;
-
- if (n < MINHINCR) n = MINHINCR;
- bytes = ROUNDUP_PAGESIZE((size_t)n * HBLKSIZE);
- if (GC_max_heapsize != 0
- && (GC_max_heapsize < (word)bytes
- || GC_heapsize > GC_max_heapsize - (word)bytes)) {
-
- return(FALSE);
- }
- space = GET_MEM(bytes);
- GC_add_to_our_memory((ptr_t)space, bytes);
- if (space == 0) {
- WARN("Failed to expand heap by %" WARN_PRIdPTR " bytes\n",
- (word)bytes);
- return(FALSE);
- }
- GC_INFOLOG_PRINTF("Grow heap to %lu KiB after %lu bytes allocated\n",
- TO_KiB_UL(GC_heapsize + (word)bytes),
- (unsigned long)GC_bytes_allocd);
-
-
-
- expansion_slop = min_bytes_allocd() + 4*MAXHINCR*HBLKSIZE;
- if ((GC_last_heap_addr == 0 && !((word)space & SIGNB))
- || (GC_last_heap_addr != 0
- && (word)GC_last_heap_addr < (word)space)) {
-
- word new_limit = (word)space + (word)bytes + expansion_slop;
- if (new_limit > (word)space) {
- GC_greatest_plausible_heap_addr =
- (void *)GC_max((word)GC_greatest_plausible_heap_addr,
- (word)new_limit);
- }
- } else {
-
- word new_limit = (word)space - expansion_slop;
- if (new_limit < (word)space) {
- GC_least_plausible_heap_addr =
- (void *)GC_min((word)GC_least_plausible_heap_addr,
- (word)space - expansion_slop);
- }
- }
- GC_prev_heap_addr = GC_last_heap_addr;
- GC_last_heap_addr = (ptr_t)space;
- GC_add_to_heap(space, bytes);
-
- GC_collect_at_heapsize =
- GC_heapsize + expansion_slop - 2*MAXHINCR*HBLKSIZE;
- if (GC_collect_at_heapsize < GC_heapsize )
- GC_collect_at_heapsize = (word)(-1);
- if (GC_on_heap_resize)
- (*GC_on_heap_resize)(GC_heapsize);
- return(TRUE);
- }
- GC_API int GC_CALL GC_expand_hp(size_t bytes)
- {
- int result;
- DCL_LOCK_STATE;
- if (!EXPECT(GC_is_initialized, TRUE)) GC_init();
- LOCK();
- result = (int)GC_expand_hp_inner(divHBLKSZ((word)bytes));
- if (result) GC_requested_heapsize += bytes;
- UNLOCK();
- return(result);
- }
- word GC_fo_entries = 0;
- GC_INNER unsigned GC_fail_count = 0;
-
-
- static word last_fo_entries = 0;
- static word last_bytes_finalized = 0;
- #define GC_WORD_MAX (~(word)0)
- GC_INNER GC_bool GC_collect_or_expand(word needed_blocks,
- GC_bool ignore_off_page,
- GC_bool retry)
- {
- GC_bool gc_not_stopped = TRUE;
- word blocks_to_get;
- IF_CANCEL(int cancel_state;)
- DISABLE_CANCEL(cancel_state);
- if (!GC_incremental && !GC_dont_gc &&
- ((GC_dont_expand && GC_bytes_allocd > 0)
- || (GC_fo_entries > (last_fo_entries + 500)
- && (last_bytes_finalized | GC_bytes_finalized) != 0)
- || GC_should_collect())) {
-
-
-
- gc_not_stopped = GC_try_to_collect_inner(
- GC_bytes_allocd > 0 && (!GC_dont_expand || !retry) ?
- GC_default_stop_func : GC_never_stop_func);
- if (gc_not_stopped == TRUE || !retry) {
-
-
- last_fo_entries = GC_fo_entries;
- last_bytes_finalized = GC_bytes_finalized;
- RESTORE_CANCEL(cancel_state);
- return(TRUE);
- }
- }
- blocks_to_get = (GC_heapsize - GC_heapsize_at_forced_unmap)
- / (HBLKSIZE * GC_free_space_divisor)
- + needed_blocks;
- if (blocks_to_get > MAXHINCR) {
- word slop;
-
-
-
- if (ignore_off_page) {
- slop = 4;
- } else {
- slop = 2 * divHBLKSZ(BL_LIMIT);
- if (slop > needed_blocks) slop = needed_blocks;
- }
- if (needed_blocks + slop > MAXHINCR) {
- blocks_to_get = needed_blocks + slop;
- } else {
- blocks_to_get = MAXHINCR;
- }
- if (blocks_to_get > divHBLKSZ(GC_WORD_MAX))
- blocks_to_get = divHBLKSZ(GC_WORD_MAX);
- }
- if (!GC_expand_hp_inner(blocks_to_get)
- && (blocks_to_get == needed_blocks
- || !GC_expand_hp_inner(needed_blocks))) {
- if (gc_not_stopped == FALSE) {
-
- GC_gcollect_inner();
- GC_ASSERT(GC_bytes_allocd == 0);
- } else if (GC_fail_count++ < GC_max_retries) {
- WARN("Out of Memory! Trying to continue...\n", 0);
- GC_gcollect_inner();
- } else {
- # if !defined(AMIGA) || !defined(GC_AMIGA_FASTALLOC)
- WARN("Out of Memory! Heap size: %" WARN_PRIdPTR " MiB."
- " Returning NULL!\n", (GC_heapsize - GC_unmapped_bytes) >> 20);
- # endif
- RESTORE_CANCEL(cancel_state);
- return(FALSE);
- }
- } else if (GC_fail_count) {
- GC_COND_LOG_PRINTF("Memory available again...\n");
- }
- RESTORE_CANCEL(cancel_state);
- return(TRUE);
- }
- GC_INNER ptr_t GC_allocobj(size_t gran, int kind)
- {
- void ** flh = &(GC_obj_kinds[kind].ok_freelist[gran]);
- GC_bool tried_minor = FALSE;
- GC_bool retry = FALSE;
- GC_ASSERT(I_HOLD_LOCK());
- if (gran == 0) return(0);
- while (*flh == 0) {
- ENTER_GC();
-
- if(TRUE_INCREMENTAL) GC_collect_a_little_inner(1);
-
- GC_ASSERT(!GC_is_full_gc
- || NULL == GC_obj_kinds[kind].ok_reclaim_list
- || NULL == GC_obj_kinds[kind].ok_reclaim_list[gran]);
- GC_continue_reclaim(gran, kind);
- EXIT_GC();
- if (*flh == 0) {
- GC_new_hblk(gran, kind);
- if (*flh == 0) {
- ENTER_GC();
- if (GC_incremental && GC_time_limit == GC_TIME_UNLIMITED
- && !tried_minor) {
- GC_collect_a_little_inner(1);
- tried_minor = TRUE;
- } else {
- if (!GC_collect_or_expand(1, FALSE, retry)) {
- EXIT_GC();
- return(0);
- }
- retry = TRUE;
- }
- EXIT_GC();
- }
- }
- }
-
- GC_fail_count = 0;
- return (ptr_t)(*flh);
- }
|