sparse_array.c 5.9 KB

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