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