From: Gabriel Ivăncescu gabrielopcode@gmail.com
Signed-off-by: Gabriel Ivăncescu gabrielopcode@gmail.com --- dlls/jscript/dispex.c | 35 +++++++++++++---------- dlls/jscript/jscript.c | 9 ------ dlls/jscript/jscript.h | 15 ++++++---- dlls/jscript/jscript_main.c | 56 ++++++++++++++++++++++++++++++++++++- dlls/jscript/set.c | 6 ++-- 5 files changed, 88 insertions(+), 33 deletions(-)
diff --git a/dlls/jscript/dispex.c b/dlls/jscript/dispex.c index ef72860f627..31174ebedbd 100644 --- a/dlls/jscript/dispex.c +++ b/dlls/jscript/dispex.c @@ -783,6 +783,7 @@ HRESULT gc_run(script_ctx_t *ctx) struct chunk *next; LONG ref[1020]; } *head, *chunk; + struct thread_data *thread_data = get_thread_data(); jsdisp_t *obj, *obj2, *link, *link2; dispex_prop_t *prop, *props_end; struct gc_ctx gc_ctx = { 0 }; @@ -791,7 +792,7 @@ HRESULT gc_run(script_ctx_t *ctx) struct list *iter;
/* Prevent recursive calls from side-effects during unlinking (e.g. CollectGarbage from host object's Release) */ - if(ctx->gc_is_unlinking) + if(!thread_data || thread_data->gc_is_unlinking) return S_OK;
if(!(head = malloc(sizeof(*head)))) @@ -800,7 +801,7 @@ HRESULT gc_run(script_ctx_t *ctx) chunk = head;
/* 1. Save actual refcounts and decrease them speculatively as-if we unlinked the objects */ - LIST_FOR_EACH_ENTRY(obj, &ctx->objects, jsdisp_t, entry) { + LIST_FOR_EACH_ENTRY(obj, &thread_data->objects, jsdisp_t, entry) { if(chunk_idx == ARRAY_SIZE(chunk->ref)) { if(!(chunk->next = malloc(sizeof(*chunk)))) { do { @@ -815,7 +816,7 @@ HRESULT gc_run(script_ctx_t *ctx) } chunk->ref[chunk_idx++] = obj->ref; } - LIST_FOR_EACH_ENTRY(obj, &ctx->objects, jsdisp_t, entry) { + LIST_FOR_EACH_ENTRY(obj, &thread_data->objects, jsdisp_t, entry) { for(prop = obj->props, props_end = prop + obj->prop_cnt; prop < props_end; prop++) { switch(prop->type) { case PROP_JSVAL: @@ -841,7 +842,7 @@ HRESULT gc_run(script_ctx_t *ctx) }
/* 2. Clear mark on objects with non-zero "external refcount" and all objects accessible from them */ - LIST_FOR_EACH_ENTRY(obj, &ctx->objects, jsdisp_t, entry) { + LIST_FOR_EACH_ENTRY(obj, &thread_data->objects, jsdisp_t, entry) { if(!obj->ref || !obj->gc_marked) continue;
@@ -899,7 +900,7 @@ HRESULT gc_run(script_ctx_t *ctx) /* For weak refs, traverse paths accessible from it via the WeakMaps, if the WeakMaps are alive at this point. We need both the key and the WeakMap for the entry to actually be accessible (and thus traversed). */ if(obj2->has_weak_refs) { - struct list *list = &RB_ENTRY_VALUE(rb_get(&ctx->weak_refs, obj2), struct weak_refs_entry, entry)->list; + struct list *list = &RB_ENTRY_VALUE(rb_get(&thread_data->weak_refs, obj2), struct weak_refs_entry, entry)->list; struct weakmap_entry *entry;
LIST_FOR_EACH_ENTRY(entry, list, struct weakmap_entry, weak_refs_entry) { @@ -926,7 +927,7 @@ HRESULT gc_run(script_ctx_t *ctx)
/* Restore */ chunk = head; chunk_idx = 0; - LIST_FOR_EACH_ENTRY(obj, &ctx->objects, jsdisp_t, entry) { + LIST_FOR_EACH_ENTRY(obj, &thread_data->objects, jsdisp_t, entry) { obj->ref = chunk->ref[chunk_idx++]; if(chunk_idx == ARRAY_SIZE(chunk->ref)) { struct chunk *next = chunk->next; @@ -940,13 +941,13 @@ HRESULT gc_run(script_ctx_t *ctx) return hres;
/* 3. Remove all the links from the marked objects, since they are dangling */ - ctx->gc_is_unlinking = TRUE; + thread_data->gc_is_unlinking = TRUE;
- iter = list_head(&ctx->objects); + iter = list_head(&thread_data->objects); while(iter) { obj = LIST_ENTRY(iter, jsdisp_t, entry); if(!obj->gc_marked) { - iter = list_next(&ctx->objects, iter); + iter = list_next(&thread_data->objects, iter); continue; }
@@ -956,12 +957,12 @@ HRESULT gc_run(script_ctx_t *ctx)
/* Releasing unlinked object should not delete any other object, so we can safely obtain the next pointer now */ - iter = list_next(&ctx->objects, iter); + iter = list_next(&thread_data->objects, iter); jsdisp_release(obj); }
- ctx->gc_is_unlinking = FALSE; - ctx->gc_last_tick = GetTickCount(); + thread_data->gc_is_unlinking = FALSE; + thread_data->gc_last_tick = GetTickCount(); return S_OK; }
@@ -2171,10 +2172,14 @@ jsdisp_t *to_jsdisp(IDispatch *disp)
HRESULT init_dispex(jsdisp_t *dispex, script_ctx_t *ctx, const builtin_info_t *builtin_info, jsdisp_t *prototype) { + struct thread_data *thread_data = alloc_thread_data(); unsigned i;
+ if(!thread_data) + return E_OUTOFMEMORY; + /* FIXME: Use better heuristics to decide when to run the GC */ - if(GetTickCount() - ctx->gc_last_tick > 30000) + if(GetTickCount() - thread_data->gc_last_tick > 30000) gc_run(ctx);
TRACE("%p (%p)\n", dispex, prototype); @@ -2201,7 +2206,7 @@ HRESULT init_dispex(jsdisp_t *dispex, script_ctx_t *ctx, const builtin_info_t *b script_addref(ctx); dispex->ctx = ctx;
- list_add_tail(&ctx->objects, &dispex->entry); + list_add_tail(&thread_data->objects, &dispex->entry); return S_OK; }
@@ -2241,7 +2246,7 @@ void jsdisp_free(jsdisp_t *obj) TRACE("(%p)\n", obj);
if(obj->has_weak_refs) { - struct list *list = &RB_ENTRY_VALUE(rb_get(&obj->ctx->weak_refs, obj), struct weak_refs_entry, entry)->list; + struct list *list = &RB_ENTRY_VALUE(rb_get(&get_thread_data()->weak_refs, obj), struct weak_refs_entry, entry)->list; do { remove_weakmap_entry(LIST_ENTRY(list->next, struct weakmap_entry, weak_refs_entry)); } while(obj->has_weak_refs); diff --git a/dlls/jscript/jscript.c b/dlls/jscript/jscript.c index b1940b770d3..42ac8a5fcf7 100644 --- a/dlls/jscript/jscript.c +++ b/dlls/jscript/jscript.c @@ -715,13 +715,6 @@ static ULONG WINAPI JScript_Release(IActiveScript *iface) return ref; }
-static int weak_refs_compare(const void *key, const struct rb_entry *entry) -{ - const struct weak_refs_entry *weak_refs_entry = RB_ENTRY_VALUE(entry, const struct weak_refs_entry, entry); - ULONG_PTR a = (ULONG_PTR)key, b = (ULONG_PTR)LIST_ENTRY(weak_refs_entry->list.next, struct weakmap_entry, weak_refs_entry)->key; - return (a > b) - (a < b); -} - static HRESULT WINAPI JScript_SetScriptSite(IActiveScript *iface, IActiveScriptSite *pass) { @@ -754,8 +747,6 @@ static HRESULT WINAPI JScript_SetScriptSite(IActiveScript *iface, ctx->html_mode = This->html_mode; ctx->acc = jsval_undefined(); list_init(&ctx->named_items); - list_init(&ctx->objects); - rb_init(&ctx->weak_refs, weak_refs_compare); heap_pool_init(&ctx->tmp_heap);
hres = create_jscaller(ctx); diff --git a/dlls/jscript/jscript.h b/dlls/jscript/jscript.h index 650c9278793..717392c5af7 100644 --- a/dlls/jscript/jscript.h +++ b/dlls/jscript/jscript.h @@ -129,6 +129,16 @@ typedef HRESULT (*builtin_setter_t)(script_ctx_t*,jsdisp_t*,jsval_t);
HRESULT builtin_set_const(script_ctx_t*,jsdisp_t*,jsval_t);
+struct thread_data { + BOOL gc_is_unlinking; + DWORD gc_last_tick; + struct list objects; + struct rb_tree weak_refs; +}; + +struct thread_data *alloc_thread_data(void); +struct thread_data *get_thread_data(void); + typedef struct named_item_t { jsdisp_t *script_obj; IDispatch *disp; @@ -377,8 +387,6 @@ struct _script_ctx_t {
struct _call_frame_t *call_ctx; struct list named_items; - struct list objects; - struct rb_tree weak_refs; IActiveScriptSite *site; IInternetHostSecurityManager *secmgr; DWORD safeopt; @@ -391,9 +399,6 @@ struct _script_ctx_t {
heap_pool_t tmp_heap;
- BOOL gc_is_unlinking; - DWORD gc_last_tick; - jsval_t *stack; unsigned stack_top; jsval_t acc; diff --git a/dlls/jscript/jscript_main.c b/dlls/jscript/jscript_main.c index 882c419ff83..167c4ef5f65 100644 --- a/dlls/jscript/jscript_main.c +++ b/dlls/jscript/jscript_main.c @@ -37,8 +37,56 @@ LONG module_ref = 0; DEFINE_GUID(GUID_NULL,0,0,0,0,0,0,0,0,0,0,0);
HINSTANCE jscript_hinstance; +static DWORD jscript_tls; static ITypeInfo *dispatch_typeinfo;
+static int weak_refs_compare(const void *key, const struct rb_entry *entry) +{ + const struct weak_refs_entry *weak_refs_entry = RB_ENTRY_VALUE(entry, const struct weak_refs_entry, entry); + ULONG_PTR a = (ULONG_PTR)key, b = (ULONG_PTR)LIST_ENTRY(weak_refs_entry->list.next, struct weakmap_entry, weak_refs_entry)->key; + return (a > b) - (a < b); +} + +struct thread_data *get_thread_data(void) +{ + return TlsGetValue(jscript_tls); +} + +struct thread_data *alloc_thread_data(void) +{ + struct thread_data *thread_data = get_thread_data(); + + if(!thread_data) { + thread_data = calloc(1, sizeof(struct thread_data)); + if(!thread_data) + return NULL; + list_init(&thread_data->objects); + rb_init(&thread_data->weak_refs, weak_refs_compare); + TlsSetValue(jscript_tls, thread_data); + } + + return thread_data; +} + +static void free_thread_data(void) +{ + struct weak_refs_entry *iter, *iter2; + struct thread_data *thread_data; + jsdisp_t *obj, *obj2; + + if(jscript_tls == TLS_OUT_OF_INDEXES || !(thread_data = TlsGetValue(jscript_tls))) + return; + + LIST_FOR_EACH_ENTRY_SAFE(obj, obj2, &thread_data->objects, jsdisp_t, entry) { + obj->has_weak_refs = FALSE; + list_remove(&obj->entry); + list_init(&obj->entry); + } + RB_FOR_EACH_ENTRY_DESTRUCTOR(iter, iter2, &thread_data->weak_refs, struct weak_refs_entry, entry) + free(iter); + free(thread_data); +} + HRESULT get_dispatch_typeinfo(ITypeInfo **out) { ITypeInfo *typeinfo; @@ -164,13 +212,19 @@ BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv) case DLL_PROCESS_ATTACH: DisableThreadLibraryCalls(hInstDLL); jscript_hinstance = hInstDLL; - if(!init_strings()) + jscript_tls = TlsAlloc(); + if(jscript_tls == TLS_OUT_OF_INDEXES || !init_strings()) return FALSE; break; case DLL_PROCESS_DETACH: if (lpv) break; if (dispatch_typeinfo) ITypeInfo_Release(dispatch_typeinfo); + if(jscript_tls != TLS_OUT_OF_INDEXES) TlsFree(jscript_tls); free_strings(); + break; + case DLL_THREAD_DETACH: + free_thread_data(); + break; }
return TRUE; diff --git a/dlls/jscript/set.c b/dlls/jscript/set.c index ac9efbb4da0..fb33eef9d21 100644 --- a/dlls/jscript/set.c +++ b/dlls/jscript/set.c @@ -658,7 +658,7 @@ void remove_weakmap_entry(struct weakmap_entry *entry) else { struct weak_refs_entry *weak_refs_entry = LIST_ENTRY(next, struct weak_refs_entry, list); entry->key->has_weak_refs = FALSE; - rb_remove(&entry->key->ctx->weak_refs, &weak_refs_entry->entry); + rb_remove(&get_thread_data()->weak_refs, &weak_refs_entry->entry); free(weak_refs_entry); } rb_remove(&weakmap->map, &entry->entry); @@ -771,14 +771,14 @@ static HRESULT WeakMap_set(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigne }
if(key->has_weak_refs) - weak_refs_entry = RB_ENTRY_VALUE(rb_get(&ctx->weak_refs, key), struct weak_refs_entry, entry); + weak_refs_entry = RB_ENTRY_VALUE(rb_get(&get_thread_data()->weak_refs, key), struct weak_refs_entry, entry); else { if(!(weak_refs_entry = malloc(sizeof(*weak_refs_entry)))) { jsval_release(entry->value); free(entry); return E_OUTOFMEMORY; } - rb_put(&ctx->weak_refs, key, &weak_refs_entry->entry); + rb_put(&get_thread_data()->weak_refs, key, &weak_refs_entry->entry); list_init(&weak_refs_entry->list); key->has_weak_refs = TRUE; }