openssl/ssl/pqueue.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

154 lines
2.8 KiB
C
Raw Normal View History

2005-04-27 00:02:40 +08:00
/*
* Copyright 2005-2020 The OpenSSL Project Authors. All Rights Reserved.
2005-04-27 00:02:40 +08:00
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
2005-04-27 00:02:40 +08:00
*/
#include "ssl_local.h"
#include <openssl/bn.h>
2005-04-27 00:02:40 +08:00
struct pqueue_st {
2005-04-27 00:02:40 +08:00
pitem *items;
int count;
};
pitem *pitem_new(unsigned char *prio64be, void *data)
2005-04-27 00:02:40 +08:00
{
pitem *item = OPENSSL_malloc(sizeof(*item));
if (item == NULL)
2005-04-27 00:02:40 +08:00
return NULL;
memcpy(item->priority, prio64be, sizeof(item->priority));
2005-04-27 00:02:40 +08:00
item->data = data;
item->next = NULL;
return item;
}
2005-04-27 00:02:40 +08:00
void pitem_free(pitem *item)
{
OPENSSL_free(item);
}
pqueue *pqueue_new(void)
2005-04-27 00:02:40 +08:00
{
pqueue *pq = OPENSSL_zalloc(sizeof(*pq));
2005-04-27 00:02:40 +08:00
return pq;
}
void pqueue_free(pqueue *pq)
2005-04-27 00:02:40 +08:00
{
OPENSSL_free(pq);
}
pitem *pqueue_insert(pqueue *pq, pitem *item)
2005-04-27 00:02:40 +08:00
{
pitem *curr, *next;
2005-04-27 00:02:40 +08:00
if (pq->items == NULL) {
pq->items = item;
return item;
}
2005-04-27 00:02:40 +08:00
for (curr = NULL, next = pq->items;
next != NULL; curr = next, next = next->next) {
/*
* we can compare 64-bit value in big-endian encoding with memcmp:-)
*/
int cmp = memcmp(next->priority, item->priority, 8);
if (cmp > 0) { /* next > item */
2005-04-27 00:02:40 +08:00
item->next = next;
2005-04-27 00:02:40 +08:00
if (curr == NULL)
pq->items = item;
else
curr->next = item;
2005-04-27 00:02:40 +08:00
return item;
}
else if (cmp == 0) /* duplicates not allowed */
2005-04-27 00:02:40 +08:00
return NULL;
}
2005-04-27 00:02:40 +08:00
item->next = NULL;
curr->next = item;
2005-04-27 00:02:40 +08:00
return item;
}
pitem *pqueue_peek(pqueue *pq)
2005-04-27 00:02:40 +08:00
{
return pq->items;
}
pitem *pqueue_pop(pqueue *pq)
2005-04-27 00:02:40 +08:00
{
pitem *item = pq->items;
2005-04-27 00:02:40 +08:00
if (pq->items != NULL)
pq->items = pq->items->next;
2005-04-27 00:02:40 +08:00
return item;
}
pitem *pqueue_find(pqueue *pq, unsigned char *prio64be)
2005-04-27 00:02:40 +08:00
{
2010-06-12 22:13:23 +08:00
pitem *next;
2005-04-27 00:02:40 +08:00
pitem *found = NULL;
2005-04-27 00:02:40 +08:00
if (pq->items == NULL)
return NULL;
2010-06-12 22:13:23 +08:00
for (next = pq->items; next->next != NULL; next = next->next) {
if (memcmp(next->priority, prio64be, 8) == 0) {
2005-04-27 00:02:40 +08:00
found = next;
break;
}
2005-04-27 00:02:40 +08:00
}
2005-04-27 00:02:40 +08:00
/* check the one last node */
if (memcmp(next->priority, prio64be, 8) == 0)
2005-04-27 00:02:40 +08:00
found = next;
2005-04-27 00:02:40 +08:00
if (!found)
return NULL;
2005-04-27 00:02:40 +08:00
return found;
}
pitem *pqueue_iterator(pqueue *pq)
2005-04-27 00:02:40 +08:00
{
return pqueue_peek(pq);
}
pitem *pqueue_next(piterator *item)
2005-04-27 00:02:40 +08:00
{
pitem *ret;
2005-04-27 00:02:40 +08:00
if (item == NULL || *item == NULL)
return NULL;
2005-04-27 00:02:40 +08:00
/* *item != NULL */
ret = *item;
*item = (*item)->next;
2005-04-27 00:02:40 +08:00
return ret;
}
size_t pqueue_size(pqueue *pq)
2009-05-17 00:18:19 +08:00
{
pitem *item = pq->items;
size_t count = 0;
2009-05-17 00:18:19 +08:00
while (item != NULL) {
count++;
item = item->next;
}
return count;
}