1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci * Copyright (c) 2019, Oracle and/or its affiliates.  All rights reserved.
4e1051a39Sopenharmony_ci *
5e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
6e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
7e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
8e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
9e1051a39Sopenharmony_ci */
10e1051a39Sopenharmony_ci
11e1051a39Sopenharmony_ci#ifndef OSSL_CRYPTO_SPARSE_ARRAY_H
12e1051a39Sopenharmony_ci# define OSSL_CRYPTO_SPARSE_ARRAY_H
13e1051a39Sopenharmony_ci# pragma once
14e1051a39Sopenharmony_ci
15e1051a39Sopenharmony_ci# include <openssl/e_os2.h>
16e1051a39Sopenharmony_ci
17e1051a39Sopenharmony_ci# ifdef __cplusplus
18e1051a39Sopenharmony_ciextern "C" {
19e1051a39Sopenharmony_ci# endif
20e1051a39Sopenharmony_ci
21e1051a39Sopenharmony_ci# define SPARSE_ARRAY_OF(type) struct sparse_array_st_ ## type
22e1051a39Sopenharmony_ci
23e1051a39Sopenharmony_ci# define DEFINE_SPARSE_ARRAY_OF_INTERNAL(type, ctype) \
24e1051a39Sopenharmony_ci    SPARSE_ARRAY_OF(type); \
25e1051a39Sopenharmony_ci    static ossl_unused ossl_inline SPARSE_ARRAY_OF(type) * \
26e1051a39Sopenharmony_ci        ossl_sa_##type##_new(void) \
27e1051a39Sopenharmony_ci    { \
28e1051a39Sopenharmony_ci        return (SPARSE_ARRAY_OF(type) *)ossl_sa_new(); \
29e1051a39Sopenharmony_ci    } \
30e1051a39Sopenharmony_ci    static ossl_unused ossl_inline void \
31e1051a39Sopenharmony_ci    ossl_sa_##type##_free(SPARSE_ARRAY_OF(type) *sa) \
32e1051a39Sopenharmony_ci    { \
33e1051a39Sopenharmony_ci        ossl_sa_free((OPENSSL_SA *)sa); \
34e1051a39Sopenharmony_ci    } \
35e1051a39Sopenharmony_ci    static ossl_unused ossl_inline void \
36e1051a39Sopenharmony_ci    ossl_sa_##type##_free_leaves(SPARSE_ARRAY_OF(type) *sa) \
37e1051a39Sopenharmony_ci    { \
38e1051a39Sopenharmony_ci        ossl_sa_free_leaves((OPENSSL_SA *)sa); \
39e1051a39Sopenharmony_ci    } \
40e1051a39Sopenharmony_ci    static ossl_unused ossl_inline size_t \
41e1051a39Sopenharmony_ci    ossl_sa_##type##_num(const SPARSE_ARRAY_OF(type) *sa) \
42e1051a39Sopenharmony_ci    { \
43e1051a39Sopenharmony_ci        return ossl_sa_num((OPENSSL_SA *)sa); \
44e1051a39Sopenharmony_ci    } \
45e1051a39Sopenharmony_ci    static ossl_unused ossl_inline void \
46e1051a39Sopenharmony_ci    ossl_sa_##type##_doall(const SPARSE_ARRAY_OF(type) *sa, \
47e1051a39Sopenharmony_ci                           void (*leaf)(ossl_uintmax_t, type *)) \
48e1051a39Sopenharmony_ci    { \
49e1051a39Sopenharmony_ci        ossl_sa_doall((OPENSSL_SA *)sa, \
50e1051a39Sopenharmony_ci                      (void (*)(ossl_uintmax_t, void *))leaf); \
51e1051a39Sopenharmony_ci    } \
52e1051a39Sopenharmony_ci    static ossl_unused ossl_inline void \
53e1051a39Sopenharmony_ci    ossl_sa_##type##_doall_arg(const SPARSE_ARRAY_OF(type) *sa, \
54e1051a39Sopenharmony_ci                               void (*leaf)(ossl_uintmax_t, type *, void *), \
55e1051a39Sopenharmony_ci                               void *arg) \
56e1051a39Sopenharmony_ci    { \
57e1051a39Sopenharmony_ci        ossl_sa_doall_arg((OPENSSL_SA *)sa, \
58e1051a39Sopenharmony_ci                          (void (*)(ossl_uintmax_t, void *, void *))leaf, arg); \
59e1051a39Sopenharmony_ci    } \
60e1051a39Sopenharmony_ci    static ossl_unused ossl_inline ctype \
61e1051a39Sopenharmony_ci    *ossl_sa_##type##_get(const SPARSE_ARRAY_OF(type) *sa, ossl_uintmax_t n) \
62e1051a39Sopenharmony_ci    { \
63e1051a39Sopenharmony_ci        return (type *)ossl_sa_get((OPENSSL_SA *)sa, n); \
64e1051a39Sopenharmony_ci    } \
65e1051a39Sopenharmony_ci    static ossl_unused ossl_inline int \
66e1051a39Sopenharmony_ci    ossl_sa_##type##_set(SPARSE_ARRAY_OF(type) *sa, \
67e1051a39Sopenharmony_ci                         ossl_uintmax_t n, ctype *val) \
68e1051a39Sopenharmony_ci    { \
69e1051a39Sopenharmony_ci        return ossl_sa_set((OPENSSL_SA *)sa, n, (void *)val); \
70e1051a39Sopenharmony_ci    } \
71e1051a39Sopenharmony_ci    SPARSE_ARRAY_OF(type)
72e1051a39Sopenharmony_ci
73e1051a39Sopenharmony_ci# define DEFINE_SPARSE_ARRAY_OF(type) \
74e1051a39Sopenharmony_ci    DEFINE_SPARSE_ARRAY_OF_INTERNAL(type, type)
75e1051a39Sopenharmony_ci# define DEFINE_SPARSE_ARRAY_OF_CONST(type) \
76e1051a39Sopenharmony_ci    DEFINE_SPARSE_ARRAY_OF_INTERNAL(type, const type)
77e1051a39Sopenharmony_ci
78e1051a39Sopenharmony_citypedef struct sparse_array_st OPENSSL_SA;
79e1051a39Sopenharmony_ciOPENSSL_SA *ossl_sa_new(void);
80e1051a39Sopenharmony_civoid ossl_sa_free(OPENSSL_SA *sa);
81e1051a39Sopenharmony_civoid ossl_sa_free_leaves(OPENSSL_SA *sa);
82e1051a39Sopenharmony_cisize_t ossl_sa_num(const OPENSSL_SA *sa);
83e1051a39Sopenharmony_civoid ossl_sa_doall(const OPENSSL_SA *sa, void (*leaf)(ossl_uintmax_t, void *));
84e1051a39Sopenharmony_civoid ossl_sa_doall_arg(const OPENSSL_SA *sa,
85e1051a39Sopenharmony_ci                       void (*leaf)(ossl_uintmax_t, void *, void *), void *);
86e1051a39Sopenharmony_civoid *ossl_sa_get(const OPENSSL_SA *sa, ossl_uintmax_t n);
87e1051a39Sopenharmony_ciint ossl_sa_set(OPENSSL_SA *sa, ossl_uintmax_t n, void *val);
88e1051a39Sopenharmony_ci
89e1051a39Sopenharmony_ci# ifdef  __cplusplus
90e1051a39Sopenharmony_ci}
91e1051a39Sopenharmony_ci# endif
92e1051a39Sopenharmony_ci#endif
93