bf0a9966af270d2f68dd6b11e50bf6f0b8b5c925
[openssl.git] / crypto / include / internal / 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 HEADER_SPARSE_ARRAY_H
12 # define HEADER_SPARSE_ARRAY_H
13
14 # ifdef __cplusplus
15 extern "C" {
16 # endif
17
18 # define SPARSE_ARRAY_OF(type) struct sparse_array_st_ ## type
19
20 # define DEFINE_SPARSE_ARRAY_OF(type) \
21     SPARSE_ARRAY_OF(type); \
22     static ossl_inline SPARSE_ARRAY_OF(type) * \
23         ossl_sa_##type##_new(void) \
24     { \
25         return (SPARSE_ARRAY_OF(type) *)OPENSSL_SA_new(); \
26     } \
27     static ossl_inline void ossl_sa_##type##_free(SPARSE_ARRAY_OF(type) *sa) \
28     { \
29         OPENSSL_SA_free((OPENSSL_SA *)sa); \
30     } \
31     static ossl_inline void ossl_sa_##type##_free_leaves(SPARSE_ARRAY_OF(type) *sa) \
32     { \
33         OPENSSL_SA_free_leaves((OPENSSL_SA *)sa); \
34     } \
35     static ossl_inline size_t ossl_sa_##type##_num(const SPARSE_ARRAY_OF(type) *sa) \
36     { \
37         return OPENSSL_SA_num((OPENSSL_SA *)sa); \
38     } \
39     static ossl_inline void ossl_sa_##type##_doall(const SPARSE_ARRAY_OF(type) *sa, \
40                                                    void (*leaf)(type *)) \
41     { \
42         OPENSSL_SA_doall((OPENSSL_SA *)sa, (void (*)(void *))leaf); \
43     } \
44     static ossl_inline void ossl_sa_##type##_doall_arg(const SPARSE_ARRAY_OF(type) *sa, \
45                                                        void (*leaf)(type *, \
46                                                                     void *),\
47                                                        void *arg) \
48     { \
49         OPENSSL_SA_doall_arg((OPENSSL_SA *)sa, (void (*)(void *, void *))leaf, \
50                              arg); \
51     } \
52     static ossl_inline type *ossl_sa_##type##_get(const SPARSE_ARRAY_OF(type) *sa, \
53                                                   size_t n) \
54     { \
55         return (type *)OPENSSL_SA_get((OPENSSL_SA *)sa, n); \
56     } \
57     static ossl_inline int ossl_sa_##type##_set(SPARSE_ARRAY_OF(type) *sa, \
58                                                 size_t n, type *val) \
59     { \
60         return OPENSSL_SA_set((OPENSSL_SA *)sa, n, (void *)val); \
61     } \
62     SPARSE_ARRAY_OF(type)
63
64 typedef struct sparse_array_st OPENSSL_SA;
65 OPENSSL_SA *OPENSSL_SA_new(void);
66 void OPENSSL_SA_free(OPENSSL_SA *sa);
67 void OPENSSL_SA_free_leaves(OPENSSL_SA *sa);
68 size_t OPENSSL_SA_num(const OPENSSL_SA *sa);
69 void OPENSSL_SA_doall(const OPENSSL_SA *sa, void (*leaf)(void *));
70 void OPENSSL_SA_doall_arg(const OPENSSL_SA *sa, void (*leaf)(void *, void *),
71                           void *);
72 void *OPENSSL_SA_get(const OPENSSL_SA *sa, size_t n);
73 int OPENSSL_SA_set(OPENSSL_SA *sa, size_t n, void *val);
74
75 # ifdef  __cplusplus
76 }
77 # endif
78 #endif