xref: /src/crypto/openssl/include/internal/priority_queue.h (revision f25b8c9fb4f58cf61adb47d7570abe7caa6d385d)
109a25192SPierre Pronchery /*
209a25192SPierre Pronchery  * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
309a25192SPierre Pronchery  *
409a25192SPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
509a25192SPierre Pronchery  * this file except in compliance with the License.  You can obtain a copy
609a25192SPierre Pronchery  * in the file LICENSE in the source distribution or at
709a25192SPierre Pronchery  * https://www.openssl.org/source/license.html
809a25192SPierre Pronchery  */
909a25192SPierre Pronchery 
1009a25192SPierre Pronchery #ifndef OSSL_INTERNAL_PRIORITY_QUEUE_H
1109a25192SPierre Pronchery #define OSSL_INTERNAL_PRIORITY_QUEUE_H
1209a25192SPierre Pronchery #pragma once
1309a25192SPierre Pronchery 
1409a25192SPierre Pronchery #include <stdlib.h>
1509a25192SPierre Pronchery #include <openssl/e_os2.h>
1609a25192SPierre Pronchery 
1709a25192SPierre Pronchery #define PRIORITY_QUEUE_OF(type) OSSL_PRIORITY_QUEUE_##type
1809a25192SPierre Pronchery 
1909a25192SPierre Pronchery #define DEFINE_PRIORITY_QUEUE_OF_INTERNAL(type, ctype)                                                                              \
2009a25192SPierre Pronchery     typedef struct ossl_priority_queue_st_##type PRIORITY_QUEUE_OF(type);                                                           \
21808413daSEnji Cooper     static ossl_unused ossl_inline PRIORITY_QUEUE_OF(type) * ossl_pqueue_##type##_new(int (*compare)(const ctype *, const ctype *)) \
2209a25192SPierre Pronchery     {                                                                                                                               \
2309a25192SPierre Pronchery         return (PRIORITY_QUEUE_OF(type) *)ossl_pqueue_new(                                                                          \
2409a25192SPierre Pronchery             (int (*)(const void *, const void *))compare);                                                                          \
2509a25192SPierre Pronchery     }                                                                                                                               \
2609a25192SPierre Pronchery     static ossl_unused ossl_inline void                                                                                             \
2709a25192SPierre Pronchery     ossl_pqueue_##type##_free(PRIORITY_QUEUE_OF(type) * pq)                                                                         \
2809a25192SPierre Pronchery     {                                                                                                                               \
2909a25192SPierre Pronchery         ossl_pqueue_free((OSSL_PQUEUE *)pq);                                                                                        \
3009a25192SPierre Pronchery     }                                                                                                                               \
3109a25192SPierre Pronchery     static ossl_unused ossl_inline void                                                                                             \
3209a25192SPierre Pronchery     ossl_pqueue_##type##_pop_free(PRIORITY_QUEUE_OF(type) * pq,                                                                     \
3309a25192SPierre Pronchery         void (*freefunc)(ctype *))                                                                                                  \
3409a25192SPierre Pronchery     {                                                                                                                               \
3509a25192SPierre Pronchery         ossl_pqueue_pop_free((OSSL_PQUEUE *)pq, (void (*)(void *))freefunc);                                                        \
3609a25192SPierre Pronchery     }                                                                                                                               \
3709a25192SPierre Pronchery     static ossl_unused ossl_inline int                                                                                              \
3809a25192SPierre Pronchery     ossl_pqueue_##type##_reserve(PRIORITY_QUEUE_OF(type) * pq, size_t n)                                                            \
3909a25192SPierre Pronchery     {                                                                                                                               \
4009a25192SPierre Pronchery         return ossl_pqueue_reserve((OSSL_PQUEUE *)pq, n);                                                                           \
4109a25192SPierre Pronchery     }                                                                                                                               \
4209a25192SPierre Pronchery     static ossl_unused ossl_inline size_t                                                                                           \
4309a25192SPierre Pronchery     ossl_pqueue_##type##_num(const PRIORITY_QUEUE_OF(type) * pq)                                                                    \
4409a25192SPierre Pronchery     {                                                                                                                               \
4509a25192SPierre Pronchery         return ossl_pqueue_num((OSSL_PQUEUE *)pq);                                                                                  \
4609a25192SPierre Pronchery     }                                                                                                                               \
4709a25192SPierre Pronchery     static ossl_unused ossl_inline int                                                                                              \
4809a25192SPierre Pronchery     ossl_pqueue_##type##_push(PRIORITY_QUEUE_OF(type) * pq,                                                                         \
4909a25192SPierre Pronchery         ctype * data, size_t *elem)                                                                                                 \
5009a25192SPierre Pronchery     {                                                                                                                               \
5109a25192SPierre Pronchery         return ossl_pqueue_push((OSSL_PQUEUE *)pq, (void *)data, elem);                                                             \
5209a25192SPierre Pronchery     }                                                                                                                               \
5309a25192SPierre Pronchery     static ossl_unused ossl_inline ctype *                                                                                          \
5409a25192SPierre Pronchery     ossl_pqueue_##type##_peek(const PRIORITY_QUEUE_OF(type) * pq)                                                                   \
5509a25192SPierre Pronchery     {                                                                                                                               \
5609a25192SPierre Pronchery         return (type *)ossl_pqueue_peek((OSSL_PQUEUE *)pq);                                                                         \
5709a25192SPierre Pronchery     }                                                                                                                               \
5809a25192SPierre Pronchery     static ossl_unused ossl_inline ctype *                                                                                          \
5909a25192SPierre Pronchery     ossl_pqueue_##type##_pop(PRIORITY_QUEUE_OF(type) * pq)                                                                          \
6009a25192SPierre Pronchery     {                                                                                                                               \
6109a25192SPierre Pronchery         return (type *)ossl_pqueue_pop((OSSL_PQUEUE *)pq);                                                                          \
6209a25192SPierre Pronchery     }                                                                                                                               \
6309a25192SPierre Pronchery     static ossl_unused ossl_inline ctype *                                                                                          \
6409a25192SPierre Pronchery     ossl_pqueue_##type##_remove(PRIORITY_QUEUE_OF(type) * pq,                                                                       \
6509a25192SPierre Pronchery         size_t elem)                                                                                                                \
6609a25192SPierre Pronchery     {                                                                                                                               \
6709a25192SPierre Pronchery         return (type *)ossl_pqueue_remove((OSSL_PQUEUE *)pq, elem);                                                                 \
6809a25192SPierre Pronchery     }                                                                                                                               \
6909a25192SPierre Pronchery     struct ossl_priority_queue_st_##type
7009a25192SPierre Pronchery 
7109a25192SPierre Pronchery #define DEFINE_PRIORITY_QUEUE_OF(type) \
7209a25192SPierre Pronchery     DEFINE_PRIORITY_QUEUE_OF_INTERNAL(type, type)
7309a25192SPierre Pronchery 
7409a25192SPierre Pronchery typedef struct ossl_pqueue_st OSSL_PQUEUE;
7509a25192SPierre Pronchery 
7609a25192SPierre Pronchery OSSL_PQUEUE *ossl_pqueue_new(int (*compare)(const void *, const void *));
7709a25192SPierre Pronchery void ossl_pqueue_free(OSSL_PQUEUE *pq);
7809a25192SPierre Pronchery void ossl_pqueue_pop_free(OSSL_PQUEUE *pq, void (*freefunc)(void *));
7909a25192SPierre Pronchery int ossl_pqueue_reserve(OSSL_PQUEUE *pq, size_t n);
8009a25192SPierre Pronchery 
8109a25192SPierre Pronchery size_t ossl_pqueue_num(const OSSL_PQUEUE *pq);
8209a25192SPierre Pronchery int ossl_pqueue_push(OSSL_PQUEUE *pq, void *data, size_t *elem);
8309a25192SPierre Pronchery void *ossl_pqueue_peek(const OSSL_PQUEUE *pq);
8409a25192SPierre Pronchery void *ossl_pqueue_pop(OSSL_PQUEUE *pq);
8509a25192SPierre Pronchery void *ossl_pqueue_remove(OSSL_PQUEUE *pq, size_t elem);
8609a25192SPierre Pronchery 
8709a25192SPierre Pronchery #endif
88