1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/core/lib/priority_queue.c
Frederik Haxel cb83a2ea8a core: 64 bit compatibility
Only minor changes are required to make the kernel 64 bit compatible.
Most of the changes are either DEBUG/printf formatting or different types for void pointer casting.

The only other change is the type of the `data` member in priority_queue_node_t, as `data` must be able to store a pointer.
For current architectures, the assumption `sizeof(unsigned int) == sizeof(void *)` holds, but not for 64 bit.
Therefore, the type is changed to `uintptr_t', which has the same size for the current architectures, but can also store a pointer in 64 bits.
2024-01-15 10:16:30 +01:00

92 lines
2.1 KiB
C

/*
* Copyright (C) 2013, 2014 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup core_util
* @{
*
* @file
* @brief A simple priority queue
*
* @author Kaspar Schleiser <kaspar@schleiser.de>
* @}
*/
#include <inttypes.h>
#include <assert.h>
#include "priority_queue.h"
#define ENABLE_DEBUG 0
#include "debug.h"
void priority_queue_remove(priority_queue_t *root_, priority_queue_node_t *node)
{
/* The strict aliasing rules allow this assignment. */
priority_queue_node_t *root = (priority_queue_node_t *)root_;
while (root->next != NULL) {
if (root->next == node) {
root->next = node->next;
node->next = NULL;
return;
}
root = root->next;
}
}
priority_queue_node_t *priority_queue_remove_head(priority_queue_t *root)
{
priority_queue_node_t *head = root->first;
if (head) {
root->first = head->next;
}
return head;
}
void priority_queue_add(priority_queue_t *root, priority_queue_node_t *new_obj)
{
/* The strict aliasing rules allow this assignment. */
priority_queue_node_t *node = (priority_queue_node_t *)root;
while (node->next != NULL) {
/* not trying to add the same node twice */
assert(node->next != new_obj);
if (node->next->priority > new_obj->priority) {
new_obj->next = node->next;
node->next = new_obj;
return;
}
node = node->next;
}
node->next = new_obj;
new_obj->next = NULL;
}
#if IS_ACTIVE(ENABLE_DEBUG)
void priority_queue_print(priority_queue_t *root)
{
printf("queue:\n");
for (priority_queue_node_t *node = root->first; node; node = node->next) {
printf("Data: %" PRIuPTR " Priority: %" PRIu32 "\n", node->data,
node->priority);
}
}
void priority_queue_print_node(priority_queue_node_t *node)
{
printf("Data: %" PRIuPTR " Priority: %" PRIu32 " Next: %p\n", node->data,
node->priority, (void *)node->next);
}
#endif