Don't bother resetting weak references if none have been allocated.

This commit is contained in:
Alex Shinn 2015-06-14 16:58:48 +09:00
parent d1c71adb40
commit b4c7a7081d
3 changed files with 15 additions and 5 deletions

17
gc.c
View file

@ -277,12 +277,16 @@ void sexp_conservative_mark (sexp ctx) {
#endif
#if SEXP_USE_WEAK_REFERENCES
void sexp_reset_weak_references(sexp ctx) {
int i, len, all_reset_p;
sexp_heap h = sexp_context_heap(ctx);
int sexp_reset_weak_references(sexp ctx) {
int i, len, broke, all_reset_p;
sexp_heap h;
sexp p, t, end, *v;
sexp_free_list q, r;
for ( ; h; h=h->next) { /* just scan the whole heap */
if (sexp_not(sexp_global(ctx, SEXP_G_WEAK_OBJECTS_PRESENT)))
return 0;
broke = 0;
/* just scan the whole heap */
for (h = sexp_context_heap(ctx) ; h; h=h->next) {
p = sexp_heap_first_block(h);
q = h->free_list;
end = sexp_heap_end(h);
@ -309,6 +313,7 @@ void sexp_reset_weak_references(sexp ctx) {
}
}
if (all_reset_p) { /* ephemerons */
broke++;
len += sexp_type_weak_len_extra(t);
for ( ; i<len; i++) v[i] = SEXP_FALSE;
}
@ -317,9 +322,11 @@ void sexp_reset_weak_references(sexp ctx) {
p = (sexp) (((char*)p)+sexp_heap_align(sexp_allocated_bytes(ctx, p)));
}
}
sexp_debug_printf("%p (broke %d weak references)", ctx, broke);
return broke;
}
#else
#define sexp_reset_weak_references(ctx)
#define sexp_reset_weak_references(ctx) 0
#endif
#if SEXP_USE_FINALIZERS

View file

@ -1261,6 +1261,7 @@ enum sexp_context_globals {
SEXP_G_FOLD_CASE_P,
#endif
#if SEXP_USE_WEAK_REFERENCES
SEXP_G_WEAK_OBJECTS_PRESENT,
SEXP_G_FILE_DESCRIPTORS,
SEXP_G_NUM_FILE_DESCRIPTORS,
#endif

2
sexp.c
View file

@ -403,6 +403,7 @@ void sexp_init_context_globals (sexp ctx) {
sexp_global(ctx, SEXP_G_PRESERVATIVES) = SEXP_NULL;
#endif
#if SEXP_USE_WEAK_REFERENCES
sexp_global(ctx, SEXP_G_WEAK_OBJECTS_PRESENT) = SEXP_FALSE;
sexp_global(ctx, SEXP_G_FILE_DESCRIPTORS) = SEXP_FALSE;
sexp_global(ctx, SEXP_G_NUM_FILE_DESCRIPTORS) = SEXP_ZERO;
#endif
@ -1673,6 +1674,7 @@ sexp sexp_open_output_file_descriptor (sexp ctx, sexp self, sexp_sint_t n, sexp
sexp sexp_make_ephemeron_op(sexp ctx, sexp self, sexp_sint_t n, sexp key, sexp value) {
sexp res = sexp_alloc_type(ctx, pair, SEXP_EPHEMERON);
if (!sexp_exceptionp(res)) {
sexp_global(ctx, SEXP_G_WEAK_OBJECTS_PRESENT) = SEXP_TRUE;
sexp_ephemeron_key(res) = key;
sexp_ephemeron_value(res) = value;
}