pqueue.c 2.8 KB
Newer Older
1
/*
R
Rich Salz 已提交
2
 * Copyright 2005-2016 The OpenSSL Project Authors. All Rights Reserved.
B
Ben Laurie 已提交
3
 *
R
Rich Salz 已提交
4 5 6 7
 * Licensed under the OpenSSL license (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
B
Ben Laurie 已提交
8 9
 */

R
Rich Salz 已提交
10
#include "ssl_locl.h"
11
#include <openssl/bn.h>
B
Ben Laurie 已提交
12

R
Rich Salz 已提交
13
struct pqueue_st {
14 15
    pitem *items;
    int count;
R
Rich Salz 已提交
16
};
17 18 19

pitem *pitem_new(unsigned char *prio64be, void *data)
{
R
Rich Salz 已提交
20
    pitem *item = OPENSSL_malloc(sizeof(*item));
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
    if (item == NULL)
        return NULL;

    memcpy(item->priority, prio64be, sizeof(item->priority));

    item->data = data;
    item->next = NULL;

    return item;
}

void pitem_free(pitem *item)
{
    OPENSSL_free(item);
}

R
Rich Salz 已提交
37
pqueue *pqueue_new()
38
{
R
Rich Salz 已提交
39
    pqueue *pq = OPENSSL_zalloc(sizeof(*pq));
40 41 42 43

    return pq;
}

R
Rich Salz 已提交
44
void pqueue_free(pqueue *pq)
45 46 47 48
{
    OPENSSL_free(pq);
}

R
Rich Salz 已提交
49
pitem *pqueue_insert(pqueue *pq, pitem *item)
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
{
    pitem *curr, *next;

    if (pq->items == NULL) {
        pq->items = item;
        return item;
    }

    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 */
            item->next = next;

            if (curr == NULL)
                pq->items = item;
            else
                curr->next = item;

            return item;
        }

        else if (cmp == 0)      /* duplicates not allowed */
            return NULL;
    }

    item->next = NULL;
    curr->next = item;

    return item;
}

R
Rich Salz 已提交
85
pitem *pqueue_peek(pqueue *pq)
86 87 88 89
{
    return pq->items;
}

R
Rich Salz 已提交
90
pitem *pqueue_pop(pqueue *pq)
91 92 93 94 95 96 97 98 99
{
    pitem *item = pq->items;

    if (pq->items != NULL)
        pq->items = pq->items->next;

    return item;
}

R
Rich Salz 已提交
100
pitem *pqueue_find(pqueue *pq, unsigned char *prio64be)
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
{
    pitem *next;
    pitem *found = NULL;

    if (pq->items == NULL)
        return NULL;

    for (next = pq->items; next->next != NULL; next = next->next) {
        if (memcmp(next->priority, prio64be, 8) == 0) {
            found = next;
            break;
        }
    }

    /* check the one last node */
    if (memcmp(next->priority, prio64be, 8) == 0)
        found = next;

    if (!found)
        return NULL;

    return found;
}

R
Rich Salz 已提交
125
pitem *pqueue_iterator(pqueue *pq)
126 127 128 129
{
    return pqueue_peek(pq);
}

130
pitem *pqueue_next(piterator *item)
131 132 133 134 135 136 137 138 139 140 141 142 143
{
    pitem *ret;

    if (item == NULL || *item == NULL)
        return NULL;

    /* *item != NULL */
    ret = *item;
    *item = (*item)->next;

    return ret;
}

M
Matt Caswell 已提交
144
size_t pqueue_size(pqueue *pq)
D
Dr. Stephen Henson 已提交
145
{
146
    pitem *item = pq->items;
147
    size_t count = 0;
148 149 150 151 152 153

    while (item != NULL) {
        count++;
        item = item->next;
    }
    return count;
D
Dr. Stephen Henson 已提交
154
}