Skip to content

Commit 8c6ec13

Browse files
committed
Normalized send memory allocation: added user context associated with sent buffers; fixed ownership over buffer array
1 parent 9d0a455 commit 8c6ec13

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

include/wtf.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ typedef struct {
260260
typedef struct {
261261
wtf_stream_event_type_t type; //! Type of stream event
262262
wtf_stream_t* stream; //! Stream that generated the event
263-
void* user_context; //! User-provided context data
263+
void* user_context; //! User-provided context data (wtf_stream_set_context)
264264

265265
union {
266266
struct {
@@ -272,6 +272,7 @@ typedef struct {
272272
struct {
273273
wtf_buffer_t* buffers; //! Array of sent data buffers
274274
uint32_t buffer_count; //! Number of buffers sent
275+
void* user_context; //! User-provided context data (wtf_stream_send)
275276
bool cancelled; //! True if send was cancelled
276277
} send_complete;
277278

@@ -556,6 +557,7 @@ WTF_API void* wtf_session_get_context(wtf_session_t* session);
556557
//! @param buffers array of data buffers to send
557558
//! @param buffer_count number of buffers in array
558559
//! @param fin true if this is the final data
560+
//! @param user_context an opaque context to be passed back to the caller on completion
559561
//! @return WTF_SUCCESS on success, error code on failure
560562
//!
561563
//! @note Memory ownership:
@@ -575,7 +577,7 @@ WTF_API void* wtf_session_get_context(wtf_session_t* session);
575577
//! @note The function passes the original buffers directly to the QUIC layer without
576578
//! modification or copying. No protocol headers are added for stream data.
577579
WTF_API wtf_result_t wtf_stream_send(wtf_stream_t* stream, const wtf_buffer_t* buffers,
578-
uint32_t buffer_count, bool fin);
580+
uint32_t buffer_count, bool fin, void* user_context);
579581

580582
//! Close a stream gracefully (send FIN)
581583
//! @param stream stream to close

src/stream.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ static void wtf_stream_cleanup_send_context(wtf_stream* stream, wtf_internal_sen
284284
.user_context = stream->user_context,
285285
.send_complete = {.buffers = send_ctx->buffers,
286286
.buffer_count = send_ctx->count,
287+
.user_context = send_ctx->user_context,
287288
.cancelled = cancelled}};
288289
stream->callback(&event);
289290
} else {
@@ -565,7 +566,7 @@ static wtf_result_t wtf_stream_validate_send_params(wtf_stream* stream, const wt
565566
}
566567

567568
wtf_result_t wtf_stream_send(wtf_stream* stream, const wtf_buffer_t* buffers, uint32_t buffer_count,
568-
bool fin)
569+
bool fin, void* user_context)
569570
{
570571
wtf_result_t validation_result = wtf_stream_validate_send_params(stream, buffers, buffer_count);
571572
if (validation_result != WTF_SUCCESS) {
@@ -576,9 +577,16 @@ wtf_result_t wtf_stream_send(wtf_stream* stream, const wtf_buffer_t* buffers, ui
576577
if (!send_ctx) {
577578
return WTF_ERROR_OUT_OF_MEMORY;
578579
}
580+
wtf_buffer_t* internal_buffers = malloc(sizeof *buffers * buffer_count);
581+
if (!internal_buffers) {
582+
free(send_ctx);
583+
return WTF_ERROR_OUT_OF_MEMORY;
584+
}
585+
memcpy(internal_buffers, buffers, sizeof *buffers * buffer_count);
579586

580-
send_ctx->buffers = (wtf_buffer_t*)buffers;
587+
send_ctx->buffers = internal_buffers;
581588
send_ctx->count = buffer_count;
589+
send_ctx->user_context = user_context;
582590
send_ctx->internal_send = false;
583591

584592
QUIC_SEND_FLAGS flags = QUIC_SEND_FLAG_NONE;
@@ -598,6 +606,7 @@ wtf_result_t wtf_stream_send(wtf_stream* stream, const wtf_buffer_t* buffers, ui
598606
return WTF_SUCCESS;
599607
}
600608

609+
free(send_ctx->buffers);
601610
free(send_ctx);
602611
return wtf_quic_status_to_result(status);
603612
}
@@ -656,7 +665,7 @@ wtf_result_t wtf_stream_close(wtf_stream_t* stream)
656665
mtx_unlock(&strm->mutex);
657666

658667
wtf_buffer_t empty_buffer = {0, NULL};
659-
return wtf_stream_send(strm, &empty_buffer, 1, true);
668+
return wtf_stream_send(strm, &empty_buffer, 1, true, NULL);
660669
}
661670

662671
wtf_result_t wtf_stream_get_id(wtf_stream_t* stream, uint64_t* stream_id)

src/types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ typedef struct wtf_context {
322322
typedef struct {
323323
wtf_buffer_t* buffers;
324324
uint32_t count;
325+
void* user_context;
325326
wtf_session* session;
326327
bool internal_send;
327328
} wtf_internal_send_context;

0 commit comments

Comments
 (0)