Fix typo in comment
[openssl.git] / crypto / sparse_array.c
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 #include <openssl/crypto.h>
12 #include <openssl/bn.h>
13 #include "internal/sparse_array.h"
14
15 /*
16  * How many bits are used to index each level in the tree structre?
17  * This setting determines the number of pointers stored in each node of the
18  * tree used to represent the sparse array.  Having more pointers reduces the
19  * depth of the tree but potentially wastes more memory.  That is, this is a
20  * direct space versus time tradeoff.
21  *
22  * The large memory model uses twelve bits which means that the are 4096
23  * pointers in each tree node.  This is more than sufficient to hold the
24  * largest defined NID (as of Feb 2019).  This means that using a NID to
25  * index a sparse array becomes a constant time single array look up.
26  *
27  * The small memory model uses four bits which means the tree nodes contain
28  * sixteen pointers.  This reduces the amount of unused space significantly
29  * at a cost in time.
30  *
31  * The library builder is also permitted to define other sizes in the closed
32  * interval [2, sizeof(size_t) * 8].
33  */
34 #ifndef OPENSSL_SA_BLOCK_BITS
35 # ifdef OPENSSL_SMALL_FOOTPRINT
36 #  define OPENSSL_SA_BLOCK_BITS           4
37 # else
38 #  define OPENSSL_SA_BLOCK_BITS           12
39 # endif
40 #elif OPENSSL_SA_BLOCK_BITS < 2 || OPENSSL_SA_BLOCK_BITS > BN_BITS2
41 # error OPENSSL_SA_BLOCK_BITS is out of range
42 #endif
43
44 /*
45  * From the number of bits, work out:
46  *    the number of pointers in a tree node;
47  *    a bit mask to quickly extract an index and
48  *    the maximum depth of the tree structure.
49   */
50 #define SA_BLOCK_MAX            (1 << OPENSSL_SA_BLOCK_BITS)
51 #define SA_BLOCK_MASK           (SA_BLOCK_MAX - 1)
52 #define SA_BLOCK_MAX_LEVELS     (((int)sizeof(size_t) * 8 \
53                                   + OPENSSL_SA_BLOCK_BITS - 1) \
54                                  / OPENSSL_SA_BLOCK_BITS)
55
56 struct sparse_array_st {
57     int levels;
58     size_t top;
59     size_t nelem;
60     void **nodes;
61 };
62
63 OPENSSL_SA *OPENSSL_SA_new(void)
64 {
65     OPENSSL_SA *res = OPENSSL_zalloc(sizeof(*res));
66
67     return res;
68 }
69
70 static void sa_doall(const OPENSSL_SA *sa, void (*node)(void **),
71                      void (*leaf)(void *, void *), void *arg)
72 {
73     int i[SA_BLOCK_MAX_LEVELS];
74     void *nodes[SA_BLOCK_MAX_LEVELS];
75     int l = 0;
76
77     i[0] = 0;
78     nodes[0] = sa->nodes;
79     while (l >= 0) {
80         const int n = i[l];
81         void ** const p = nodes[l];
82
83         if (n >= SA_BLOCK_MAX) {
84             if (p != NULL && node != NULL)
85                 (*node)(p);
86             l--;
87         } else {
88             i[l] = n + 1;
89             if (p != NULL && p[n] != NULL) {
90                 if (l < sa->levels - 1) {
91                     i[++l] = 0;
92                     nodes[l] = p[n];
93                 } else if (leaf != NULL) {
94                     (*leaf)(p[n], arg);
95                 }
96             }
97         }
98     }
99 }
100
101 static void sa_free_node(void **p)
102 {
103     OPENSSL_free(p);
104 }
105
106 static void sa_free_leaf(void *p, void *arg)
107 {
108     OPENSSL_free(p);
109 }
110
111 void OPENSSL_SA_free(OPENSSL_SA *sa)
112 {
113     sa_doall(sa, &sa_free_node, NULL, NULL);
114     OPENSSL_free(sa);
115 }
116
117 void OPENSSL_SA_free_leaves(OPENSSL_SA *sa)
118 {
119     sa_doall(sa, &sa_free_node, &sa_free_leaf, NULL);
120     OPENSSL_free(sa);
121 }
122
123 /* Wrap this in a structure to avoid compiler warnings */
124 struct trampoline_st {
125     void (*func)(void *);
126 };
127
128 static void trampoline(void *l, void *arg)
129 {
130     ((const struct trampoline_st *)arg)->func(l);
131 }
132
133 void OPENSSL_SA_doall(const OPENSSL_SA *sa, void (*leaf)(void *))
134 {
135     struct trampoline_st tramp;
136
137     tramp.func = leaf;
138     if (sa != NULL)
139         sa_doall(sa, NULL, &trampoline, &tramp);
140 }
141
142 void OPENSSL_SA_doall_arg(const OPENSSL_SA *sa, void (*leaf)(void *, void *),
143                           void *arg)
144 {
145     if (sa != NULL)
146         sa_doall(sa, NULL, leaf, arg);
147 }
148
149 size_t OPENSSL_SA_num(const OPENSSL_SA *sa)
150 {
151     return sa == NULL ? 0 : sa->nelem;
152 }
153
154 void *OPENSSL_SA_get(const OPENSSL_SA *sa, size_t n)
155 {
156     int level;
157     void **p, *r = NULL;
158
159     if (sa == NULL)
160         return NULL;
161
162     if (n <= sa->top) {
163         p = sa->nodes;
164         for (level = sa->levels - 1; p != NULL && level > 0; level--)
165             p = (void **)p[(n >> (OPENSSL_SA_BLOCK_BITS * level))
166                            & SA_BLOCK_MASK];
167         r = p == NULL ? NULL : p[n & SA_BLOCK_MASK];
168     }
169     return r;
170 }
171
172 static ossl_inline void **alloc_node(void)
173 {
174     return OPENSSL_zalloc(SA_BLOCK_MAX * sizeof(void *));
175 }
176
177 int OPENSSL_SA_set(OPENSSL_SA *sa, size_t posn, void *val)
178 {
179     int i, level = 1;
180     size_t n = posn;
181     void **p;
182
183     if (sa == NULL)
184         return 0;
185
186     for (level = 1; level <= SA_BLOCK_MAX_LEVELS; level++)
187         if ((n >>= OPENSSL_SA_BLOCK_BITS) == 0)
188             break;
189
190     for (;sa->levels < level; sa->levels++) {
191         p = alloc_node();
192         if (p == NULL)
193             return 0;
194         p[0] = sa->nodes;
195         sa->nodes = p;
196     }
197     if (sa->top < posn)
198         sa->top = posn;
199
200     p = sa->nodes;
201     for (level = sa->levels - 1; level > 0; level--) {
202         i = (posn >> (OPENSSL_SA_BLOCK_BITS * level)) & SA_BLOCK_MASK;
203         if (p[i] == NULL && (p[i] = alloc_node()) == NULL)
204             return 0;
205         p = p[i];
206     }
207     p += posn & SA_BLOCK_MASK;
208     if (val == NULL && *p != NULL)
209         sa->nelem--;
210     else if (val != NULL && *p == NULL)
211         sa->nelem++;
212     *p = val;
213     return 1;
214 }