diff --git a/include/aws/common/priority_queue.h b/include/aws/common/priority_queue.h index 4478b95d0..a3434091b 100644 --- a/include/aws/common/priority_queue.h +++ b/include/aws/common/priority_queue.h @@ -83,6 +83,17 @@ void aws_priority_queue_init_static( size_t item_size, aws_priority_queue_compare_fn *pred); +/** + * Initializes a queue node to a default value that indicates the node is not in the queue. If we used the + * classical indexing for array-based heaps (starting at 1) we wouldn't need this, but because zero is a valid node + * index and we use SIZE_MAX as "not-in-the-heap" we need an init so that people aren't hard-coding magic SIZE_MAXs + * around all over the place. + * + * @param node priority queue node to initialize with a default value + */ +AWS_COMMON_API +void aws_priority_queue_node_init(struct aws_priority_queue_node *node); + /** * Checks that the backpointer at a specific index of the queue is * NULL or points to a correctly allocated aws_priority_queue_node. diff --git a/source/priority_queue.c b/source/priority_queue.c index 86a91feb3..dfa071f75 100644 --- a/source/priority_queue.c +++ b/source/priority_queue.c @@ -400,3 +400,7 @@ size_t aws_priority_queue_size(const struct aws_priority_queue *queue) { size_t aws_priority_queue_capacity(const struct aws_priority_queue *queue) { return aws_array_list_capacity(&queue->container); } + +void aws_priority_queue_node_init(struct aws_priority_queue_node *node) { + node->current_index = SIZE_MAX; +} diff --git a/source/task_scheduler.c b/source/task_scheduler.c index 4467b1249..bca150e39 100644 --- a/source/task_scheduler.c +++ b/source/task_scheduler.c @@ -135,7 +135,7 @@ void aws_task_scheduler_schedule_now(struct aws_task_scheduler *scheduler, struc (void *)task, task->type_tag); - task->priority_queue_node.current_index = SIZE_MAX; + aws_priority_queue_node_init(&task->priority_queue_node); aws_linked_list_node_reset(&task->node); task->timestamp = 0; @@ -161,7 +161,7 @@ void aws_task_scheduler_schedule_future( task->timestamp = time_to_run; - task->priority_queue_node.current_index = SIZE_MAX; + aws_priority_queue_node_init(&task->priority_queue_node); aws_linked_list_node_reset(&task->node); int err = aws_priority_queue_push_ref(&scheduler->timed_queue, &task, &task->priority_queue_node); if (AWS_UNLIKELY(err)) {