From fea8242ea7635363f00823f9e24d689535825928 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Sat, 7 Oct 2023 11:25:20 -0700 Subject: [PATCH] Reuse overlapped overhead on windows for something useful. --- src/core/ev.c | 15 +++++++++------ src/core/fiber.c | 1 - src/core/marsh.c | 1 - src/include/janet.h | 1 - 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/core/ev.c b/src/core/ev.c index 39bff114..523a2724 100644 --- a/src/core/ev.c +++ b/src/core/ev.c @@ -1484,7 +1484,8 @@ void janet_loop1_impl(int has_timeout, JanetTimestamp to) { } if (fiber != NULL) { fiber->ev_in_flight = 0; - fiber->ev_bytes = num_bytes_transfered; + /* System is done with this, we can reused this data */ + overlapped->InternalHigh = (ULONG_PTR) num_bytes_transfered; fiber->ev_callback(fiber, result ? JANET_ASYNC_EVENT_COMPLETE : JANET_ASYNC_EVENT_FAILED); } else { janet_free((void *) overlapped); @@ -2163,17 +2164,18 @@ void ev_callback_read(JanetFiber *fiber, JanetAsyncEvent event) { case JANET_ASYNC_EVENT_FAILED: case JANET_ASYNC_EVENT_COMPLETE: { /* Called when read finished */ - state->bytes_read += fiber->ev_bytes; + uint32_t ev_bytes = state->overlapped.InternalHigh; + state->bytes_read += ev_bytes; if (state->bytes_read == 0 && (state->mode != JANET_ASYNC_READMODE_RECVFROM)) { janet_schedule(fiber, janet_wrap_nil()); janet_async_end(fiber); return; } - janet_buffer_push_bytes(state->buf, state->chunk_buf, fiber->ev_bytes); - state->bytes_left -= fiber->ev_bytes; + janet_buffer_push_bytes(state->buf, state->chunk_buf, ev_bytes); + state->bytes_left -= ev_bytes; - if (state->bytes_left == 0 || !state->is_chunk || fiber->ev_bytes == 0) { + if (state->bytes_left == 0 || !state->is_chunk || ev_bytes == 0) { Janet resume_val; #ifdef JANET_NET if (state->mode == JANET_ASYNC_READMODE_RECVFROM) { @@ -2412,7 +2414,8 @@ void ev_callback_write(JanetFiber *fiber, JanetAsyncEvent event) { case JANET_ASYNC_EVENT_FAILED: case JANET_ASYNC_EVENT_COMPLETE: { /* Called when write finished */ - if (fiber->ev_bytes == 0 && (state->mode != JANET_ASYNC_WRITEMODE_SENDTO)) { + uint32_t ev_bytes = state->overlapped.InternalHigh; + if (ev_bytes == 0 && (state->mode != JANET_ASYNC_WRITEMODE_SENDTO)) { janet_cancel(fiber, janet_cstringv("disconnect")); janet_async_end(fiber); return; diff --git a/src/core/fiber.c b/src/core/fiber.c index fe558fb0..f733e7f6 100644 --- a/src/core/fiber.c +++ b/src/core/fiber.c @@ -43,7 +43,6 @@ static void fiber_reset(JanetFiber *fiber) { fiber->ev_callback = NULL; fiber->ev_state = NULL; fiber->ev_stream = NULL; - fiber->ev_bytes = 0; fiber->supervisor_channel = NULL; fiber->ev_in_flight = 0; #endif diff --git a/src/core/marsh.c b/src/core/marsh.c index 4332c6f7..9e032a2c 100644 --- a/src/core/marsh.c +++ b/src/core/marsh.c @@ -1051,7 +1051,6 @@ static const uint8_t *unmarshal_one_fiber( fiber->sched_id = 0; fiber->supervisor_channel = NULL; fiber->ev_state = NULL; - fiber->ev_bytes = 0; fiber->ev_callback = NULL; fiber->ev_stream = NULL; fiber->ev_in_flight = 0; diff --git a/src/include/janet.h b/src/include/janet.h index 0e2f5790..7288938d 100644 --- a/src/include/janet.h +++ b/src/include/janet.h @@ -927,7 +927,6 @@ struct JanetFiber { JanetStream *ev_stream; /* which stream we are waiting on */ void *ev_state; /* Extra data for ev callback state. On windows, first element must be OVERLAPPED. */ void *supervisor_channel; /* Channel to push self to when complete */ - uint32_t ev_bytes; /* Number of bytes for completion event */ int ev_in_flight; /* If overlapped operation is in flight */ #endif };