diff --git a/include/uv.h b/include/uv.h index e36e1ac536090aae072cf6854cf0ade43e658cc4..4399b5b4032f5bf6e3bbb006eaab53573b72991f 100644 --- a/include/uv.h +++ b/include/uv.h @@ -1953,7 +1953,7 @@ UV_EXTERN void* uv_loop_get_data(const uv_loop_t*); UV_EXTERN void uv_loop_set_data(uv_loop_t*, void* data); UV_EXTERN int uv_register_task_to_event(struct uv_loop_s* loop, uv_post_task func, void* handler); UV_EXTERN int uv_unregister_task_to_event(struct uv_loop_s* loop); -UV_EXTERN int uv_check_data_valid(struct uv_loop_data* data); +UV_EXTERN int uv_check_data_valid(uv_loop_t* loop); /* String utilities needed internally for dealing with Windows. */ size_t uv_utf16_length_as_wtf8(const uint16_t* utf16, diff --git a/src/threadpool.c b/src/threadpool.c index 3f058d77228728fce0b41db496709738ae95eb67..a449dbf249810440c95e54a8e3adcd23c68ce73c 100644 --- a/src/threadpool.c +++ b/src/threadpool.c @@ -653,11 +653,10 @@ static int uv__work_cancel(uv_loop_t* loop, uv_req_t* req, struct uv__work* w) { uv__loop_internal_fields_t* lfields = uv__get_internal_fields(w->loop); int qos = (ffrt_qos_t)(intptr_t)req->reserved[0]; - if (uv_check_data_valid((struct uv_loop_data*)(w->loop->data)) == 0) { + if (uv_check_data_valid(w->loop) == 0) { int status = (w->work == uv__cancelled) ? UV_ECANCELED : 0; - struct uv_loop_data* addr = (struct uv_loop_data*)((uint64_t)w->loop->data - - (UV_EVENT_MAGIC_OFFSET << UV_EVENT_MAGIC_OFFSETBITS)); - addr->post_task_func(addr->event_handler, uv__task_done_wrapper, (void*)w, status, qos); + struct uv_loop_data* data = (struct uv_loop_data*)w->loop->data; + data->post_task_func(data->event_handler, uv__task_done_wrapper, (void*)w, status, qos); } else { uv__queue_insert_tail(&(lfields->wq_sub[qos]), &w->wq); uv_async_send(&loop->wq_async); @@ -680,7 +679,7 @@ void uv__work_done(uv_async_t* handle) { loop = container_of(handle, uv_loop_t, wq_async); #ifdef USE_OHOS_DFX - if (uv_check_data_valid((struct uv_loop_data*)(loop->data)) != 0) + if (uv_check_data_valid(loop) != 0) uv__print_active_reqs(loop, "complete"); #endif rdlock_closed_uv_loop_rwlock(); @@ -805,11 +804,10 @@ void uv__ffrt_work(ffrt_executor_task_t* data, ffrt_qos_t qos) uv_mutex_lock(&loop->wq_mutex); w->work = NULL; /* Signal uv_cancel() that the work req is done executing. */ - if (uv_check_data_valid((struct uv_loop_data*)(loop->data)) == 0) { + if (uv_check_data_valid(loop) == 0) { int status = (w->work == uv__cancelled) ? UV_ECANCELED : 0; - struct uv_loop_data* addr = (struct uv_loop_data*)((uint64_t)loop->data - - (UV_EVENT_MAGIC_OFFSET << UV_EVENT_MAGIC_OFFSETBITS)); - addr->post_task_func(addr->event_handler, uv__task_done_wrapper, (void*)w, status, qos); + struct uv_loop_data* data = (struct uv_loop_data*)loop->data; + data->post_task_func(data->event_handler, uv__task_done_wrapper, (void*)w, status, qos); } else { uv__loop_internal_fields_t* lfields = uv__get_internal_fields(loop); uv__queue_insert_tail(&(lfields->wq_sub[qos]), &w->wq); diff --git a/src/timer.c b/src/timer.c index 35548b421ca56f39ece914b2aafc17e540b47746..124c4e6e1547991d80021b1bed9fc76db4385af2 100644 --- a/src/timer.c +++ b/src/timer.c @@ -98,7 +98,7 @@ int uv_timer_start(uv_timer_t* handle, timer_less_than); uv__handle_start(handle); #ifdef __linux__ - if (uv_check_data_valid((struct uv_loop_data*)handle->loop->data) == 0) { + if (uv_check_data_valid(handle->loop) == 0) { uv_async_send(&handle->loop->wq_async); } #endif diff --git a/src/unix/core.c b/src/unix/core.c index ba7c8cef32d667947ad6dface7a70faf147ea355..bf7658dd9cc0cf4a5c67f5052cf957c6287681aa 100644 --- a/src/unix/core.c +++ b/src/unix/core.c @@ -1937,17 +1937,13 @@ int uv_register_task_to_event(struct uv_loop_s* loop, uv_post_task func, void* h struct uv_loop_data* data = (struct uv_loop_data*)malloc(sizeof(struct uv_loop_data)); if (data == NULL) return -1; - if ((uint64_t)data >> UV_EVENT_MAGIC_OFFSETBITS != 0x0) { - UV_LOGE("malloc address error"); - free(data); - return -1; - } (void)memset(data, 0, sizeof(struct uv_loop_data)); data->post_task_func = func; data->event_handler = handler; - data = (struct uv_loop_data*)((uint64_t)data | (UV_EVENT_MAGIC_OFFSET << UV_EVENT_MAGIC_OFFSETBITS)); loop->data = (void *)data; + uv__loop_internal_fields_t* lfields_flag = uv__get_internal_fields(loop); + lfields_flag->register_flag = 1; return 0; #else return -1; @@ -1958,13 +1954,12 @@ int uv_register_task_to_event(struct uv_loop_s* loop, uv_post_task func, void* h int uv_unregister_task_to_event(struct uv_loop_s* loop) { #if defined(__aarch64__) - if (loop == NULL || loop->data == NULL || - ((uint64_t)loop->data >> UV_EVENT_MAGIC_OFFSETBITS) != (uint64_t)(UV_EVENT_MAGIC_OFFSET)) + uv__loop_internal_fields_t* lfields_flag = uv__get_internal_fields(loop); + if (loop == NULL || loop->data == NULL || lfields_flag->register_flag == 0) return -1; - loop->data = (struct uv_loop_data*)((uint64_t)loop->data - - (UV_EVENT_MAGIC_OFFSET << UV_EVENT_MAGIC_OFFSETBITS)); free(loop->data); loop->data = NULL; + lfields_flag->register_flag = 0; return 0; #else return -1; @@ -1972,14 +1967,13 @@ int uv_unregister_task_to_event(struct uv_loop_s* loop) } -int uv_check_data_valid(struct uv_loop_data* data) { +int uv_check_data_valid(uv_loop_t* loop) { #if defined(__aarch64__) - if (data == NULL || ((uint64_t)data >> UV_EVENT_MAGIC_OFFSETBITS) != (uint64_t)(UV_EVENT_MAGIC_OFFSET)) { + uv__loop_internal_fields_t* lfields_flag = uv__get_internal_fields(loop); + if (loop == NULL || loop->data == NULL || lfields_flag->register_flag == 0) { return -1; } - struct uv_loop_data* addr = (struct uv_loop_data*)((uint64_t)data - - (UV_EVENT_MAGIC_OFFSET << UV_EVENT_MAGIC_OFFSETBITS)); - if (addr->post_task_func == NULL) { + if (((struct uv_loop_data*)loop->data)->post_task_func == NULL) { UV_LOGE("post_task_func NULL"); return -1; } diff --git a/src/unix/loop.c b/src/unix/loop.c index 2cb896aa40319469ca619159f1e4371a26675868..8db6aa5eea5c13107c8bca42888af91196723e60 100644 --- a/src/unix/loop.c +++ b/src/unix/loop.c @@ -67,6 +67,10 @@ int uv_loop_init(uv_loop_t* loop) { uv__queue_init(&loop->handle_queue); loop->active_handles = 0; +#ifdef __aarch64__ + uv__loop_internal_fields_t* lfields_flag = uv__get_internal_fields(loop); + lfields_flag->register_flag = 0; +#endif loop->active_reqs.count = 0; loop->nfds = 0; loop->watchers = NULL; diff --git a/src/uv-common.h b/src/uv-common.h index bc2e3009814a9f5183579c46628ddf39f5c542c5..2df434de5033effbf1179bf81ef1a870fb741dac 100644 --- a/src/uv-common.h +++ b/src/uv-common.h @@ -450,6 +450,9 @@ struct uv__loop_internal_fields_s { #ifdef USE_FFRT struct uv__queue wq_sub[4]; #endif +#ifdef __aarch64__ + unsigned int register_flag; +#endif }; uint64_t uv__get_addr_tag(void* addr);