Skip to content

Commit 8233d0d

Browse files
committed
test_memio_move_message: add docs
1 parent 13f8f66 commit 8233d0d

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

tests/utils.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ int test_memio_move_message(struct test_memio_ctx *ctx, int client,
461461
int total_size = 0;
462462
int msg_in_size;
463463

464+
/* Select buffer and message metadata for client or server */
464465
if (client) {
465466
buff = buff_in = buff_out = ctx->c_buff;
466467
msg_count = ctx->c_msg_count;
@@ -471,29 +472,51 @@ int test_memio_move_message(struct test_memio_ctx *ctx, int client,
471472
msg_count = ctx->s_msg_count;
472473
msg_sizes = ctx->s_msg_sizes;
473474
}
475+
476+
/* Validate input and output message positions */
474477
if (msg_pos_in < 0 || msg_pos_in >= msg_count)
475478
return -1;
476479
if (msg_pos_out < 0 || msg_pos_out >= msg_count)
477480
msg_pos_out = msg_count-1;
478481
if (msg_pos_in == msg_pos_out)
479482
return 0;
483+
484+
/* Get the size of the message to move */
480485
msg_in_size = msg_sizes[msg_pos_in];
486+
487+
/* Calculate the total size of all messages */
481488
for (i = 0; i < msg_count; i++)
482489
total_size += msg_sizes[i];
490+
491+
/* Check if buffer has enough space for the move */
483492
if (total_size + msg_in_size > TEST_MEMIO_BUF_SZ)
484493
return -1;
494+
495+
/* Find the start of the input message in the buffer */
485496
for (i = 0; i < msg_pos_in; i++)
486497
buff_in += msg_sizes[i];
498+
499+
/* Find the position to move the message to in the buffer */
487500
for (i = 0; i < msg_pos_out + (msg_pos_out > msg_pos_in ? 1 : 0); i++)
488501
buff_out += msg_sizes[i];
502+
503+
/* Make space for the moved message at the output position */
489504
XMEMMOVE(buff_out + msg_in_size, buff_out,
490505
total_size - (buff_out - buff));
491506
total_size += msg_in_size;
507+
508+
/* Adjust input pointer if it was after the output position */
492509
if (buff_in > buff_out)
493510
buff_in += msg_in_size;
511+
512+
/* Copy the message to its new position */
494513
XMEMCPY(buff_out, buff_in, msg_in_size);
514+
515+
/* Remove the original message from its old position */
495516
XMEMMOVE(buff_in, buff_in + msg_in_size,
496517
total_size - (buff_in - buff) - msg_in_size);
518+
519+
/* Update the message sizes array to reflect the move */
497520
if (msg_pos_in < msg_pos_out) {
498521
XMEMMOVE(msg_sizes + msg_pos_in, msg_sizes + msg_pos_in + 1,
499522
sizeof(*msg_sizes) *

0 commit comments

Comments
 (0)