Skip to content

Commit

Permalink
Add an init function for priority queue node structures so that we're…
Browse files Browse the repository at this point in the history
… not adding magic SIZE_MAXs everyone it's used
  • Loading branch information
Bret Ambrose committed Oct 25, 2023
1 parent ec64760 commit c5d5e70
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
11 changes: 11 additions & 0 deletions include/aws/common/priority_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions source/priority_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
4 changes: 2 additions & 2 deletions source/task_scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)) {
Expand Down

0 comments on commit c5d5e70

Please sign in to comment.