Home | History | Annotate | Line # | Download | only in internal
      1      1.1  christos /*
      2      1.1  christos  * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
      3      1.1  christos  *
      4      1.1  christos  * Licensed under the Apache License 2.0 (the "License").  You may not use
      5      1.1  christos  * this file except in compliance with the License.  You can obtain a copy
      6      1.1  christos  * in the file LICENSE in the source distribution or at
      7      1.1  christos  * https://www.openssl.org/source/license.html
      8      1.1  christos  */
      9      1.1  christos 
     10      1.1  christos #ifndef OSSL_INTERNAL_PRIORITY_QUEUE_H
     11  1.1.1.2  christos #define OSSL_INTERNAL_PRIORITY_QUEUE_H
     12  1.1.1.2  christos #pragma once
     13      1.1  christos 
     14  1.1.1.2  christos #include <stdlib.h>
     15  1.1.1.2  christos #include <openssl/e_os2.h>
     16      1.1  christos 
     17  1.1.1.2  christos #define PRIORITY_QUEUE_OF(type) OSSL_PRIORITY_QUEUE_##type
     18      1.1  christos 
     19  1.1.1.2  christos #define DEFINE_PRIORITY_QUEUE_OF_INTERNAL(type, ctype)                                                                              \
     20  1.1.1.2  christos     typedef struct ossl_priority_queue_st_##type PRIORITY_QUEUE_OF(type);                                                           \
     21  1.1.1.2  christos     static ossl_unused ossl_inline PRIORITY_QUEUE_OF(type) * ossl_pqueue_##type##_new(int (*compare)(const ctype *, const ctype *)) \
     22  1.1.1.2  christos     {                                                                                                                               \
     23  1.1.1.2  christos         return (PRIORITY_QUEUE_OF(type) *)ossl_pqueue_new(                                                                          \
     24  1.1.1.2  christos             (int (*)(const void *, const void *))compare);                                                                          \
     25  1.1.1.2  christos     }                                                                                                                               \
     26  1.1.1.2  christos     static ossl_unused ossl_inline void                                                                                             \
     27  1.1.1.2  christos     ossl_pqueue_##type##_free(PRIORITY_QUEUE_OF(type) * pq)                                                                         \
     28  1.1.1.2  christos     {                                                                                                                               \
     29  1.1.1.2  christos         ossl_pqueue_free((OSSL_PQUEUE *)pq);                                                                                        \
     30  1.1.1.2  christos     }                                                                                                                               \
     31  1.1.1.2  christos     static ossl_unused ossl_inline void                                                                                             \
     32  1.1.1.2  christos     ossl_pqueue_##type##_pop_free(PRIORITY_QUEUE_OF(type) * pq,                                                                     \
     33  1.1.1.2  christos         void (*freefunc)(ctype *))                                                                                                  \
     34  1.1.1.2  christos     {                                                                                                                               \
     35  1.1.1.2  christos         ossl_pqueue_pop_free((OSSL_PQUEUE *)pq, (void (*)(void *))freefunc);                                                        \
     36  1.1.1.2  christos     }                                                                                                                               \
     37  1.1.1.2  christos     static ossl_unused ossl_inline int                                                                                              \
     38  1.1.1.2  christos     ossl_pqueue_##type##_reserve(PRIORITY_QUEUE_OF(type) * pq, size_t n)                                                            \
     39  1.1.1.2  christos     {                                                                                                                               \
     40  1.1.1.2  christos         return ossl_pqueue_reserve((OSSL_PQUEUE *)pq, n);                                                                           \
     41  1.1.1.2  christos     }                                                                                                                               \
     42  1.1.1.2  christos     static ossl_unused ossl_inline size_t                                                                                           \
     43  1.1.1.2  christos     ossl_pqueue_##type##_num(const PRIORITY_QUEUE_OF(type) * pq)                                                                    \
     44  1.1.1.2  christos     {                                                                                                                               \
     45  1.1.1.2  christos         return ossl_pqueue_num((OSSL_PQUEUE *)pq);                                                                                  \
     46  1.1.1.2  christos     }                                                                                                                               \
     47  1.1.1.2  christos     static ossl_unused ossl_inline int                                                                                              \
     48  1.1.1.2  christos     ossl_pqueue_##type##_push(PRIORITY_QUEUE_OF(type) * pq,                                                                         \
     49  1.1.1.2  christos         ctype * data, size_t *elem)                                                                                                 \
     50  1.1.1.2  christos     {                                                                                                                               \
     51  1.1.1.2  christos         return ossl_pqueue_push((OSSL_PQUEUE *)pq, (void *)data, elem);                                                             \
     52  1.1.1.2  christos     }                                                                                                                               \
     53  1.1.1.2  christos     static ossl_unused ossl_inline ctype *                                                                                          \
     54  1.1.1.2  christos     ossl_pqueue_##type##_peek(const PRIORITY_QUEUE_OF(type) * pq)                                                                   \
     55  1.1.1.2  christos     {                                                                                                                               \
     56  1.1.1.2  christos         return (type *)ossl_pqueue_peek((OSSL_PQUEUE *)pq);                                                                         \
     57  1.1.1.2  christos     }                                                                                                                               \
     58  1.1.1.2  christos     static ossl_unused ossl_inline ctype *                                                                                          \
     59  1.1.1.2  christos     ossl_pqueue_##type##_pop(PRIORITY_QUEUE_OF(type) * pq)                                                                          \
     60  1.1.1.2  christos     {                                                                                                                               \
     61  1.1.1.2  christos         return (type *)ossl_pqueue_pop((OSSL_PQUEUE *)pq);                                                                          \
     62  1.1.1.2  christos     }                                                                                                                               \
     63  1.1.1.2  christos     static ossl_unused ossl_inline ctype *                                                                                          \
     64  1.1.1.2  christos     ossl_pqueue_##type##_remove(PRIORITY_QUEUE_OF(type) * pq,                                                                       \
     65  1.1.1.2  christos         size_t elem)                                                                                                                \
     66  1.1.1.2  christos     {                                                                                                                               \
     67  1.1.1.2  christos         return (type *)ossl_pqueue_remove((OSSL_PQUEUE *)pq, elem);                                                                 \
     68  1.1.1.2  christos     }                                                                                                                               \
     69  1.1.1.2  christos     struct ossl_priority_queue_st_##type
     70      1.1  christos 
     71  1.1.1.2  christos #define DEFINE_PRIORITY_QUEUE_OF(type) \
     72      1.1  christos     DEFINE_PRIORITY_QUEUE_OF_INTERNAL(type, type)
     73      1.1  christos 
     74      1.1  christos typedef struct ossl_pqueue_st OSSL_PQUEUE;
     75      1.1  christos 
     76      1.1  christos OSSL_PQUEUE *ossl_pqueue_new(int (*compare)(const void *, const void *));
     77      1.1  christos void ossl_pqueue_free(OSSL_PQUEUE *pq);
     78      1.1  christos void ossl_pqueue_pop_free(OSSL_PQUEUE *pq, void (*freefunc)(void *));
     79      1.1  christos int ossl_pqueue_reserve(OSSL_PQUEUE *pq, size_t n);
     80      1.1  christos 
     81      1.1  christos size_t ossl_pqueue_num(const OSSL_PQUEUE *pq);
     82      1.1  christos int ossl_pqueue_push(OSSL_PQUEUE *pq, void *data, size_t *elem);
     83      1.1  christos void *ossl_pqueue_peek(const OSSL_PQUEUE *pq);
     84      1.1  christos void *ossl_pqueue_pop(OSSL_PQUEUE *pq);
     85      1.1  christos void *ossl_pqueue_remove(OSSL_PQUEUE *pq, size_t elem);
     86      1.1  christos 
     87      1.1  christos #endif
     88