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