sparse_array.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright 2019-2022 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 default is to use four bits which means that the are 16
  21. * pointers in each tree node.
  22. *
  23. * The library builder is also permitted to define other sizes in the closed
  24. * interval [2, sizeof(ossl_uintmax_t) * 8]. Space use generally scales
  25. * exponentially with the block size, although the implementation only
  26. * creates enough blocks to support the largest used index. The depth is:
  27. * ceil(log_2(largest index) / 2^{block size})
  28. * E.g. with a block size of 4, and a largest index of 1000, the depth
  29. * will be three.
  30. */
  31. #ifndef OPENSSL_SA_BLOCK_BITS
  32. # define OPENSSL_SA_BLOCK_BITS 4
  33. #elif OPENSSL_SA_BLOCK_BITS < 2 || OPENSSL_SA_BLOCK_BITS > (BN_BITS2 - 1)
  34. # error OPENSSL_SA_BLOCK_BITS is out of range
  35. #endif
  36. /*
  37. * From the number of bits, work out:
  38. * the number of pointers in a tree node;
  39. * a bit mask to quickly extract an index and
  40. * the maximum depth of the tree structure.
  41. */
  42. #define SA_BLOCK_MAX (1 << OPENSSL_SA_BLOCK_BITS)
  43. #define SA_BLOCK_MASK (SA_BLOCK_MAX - 1)
  44. #define SA_BLOCK_MAX_LEVELS (((int)sizeof(ossl_uintmax_t) * 8 \
  45. + OPENSSL_SA_BLOCK_BITS - 1) \
  46. / OPENSSL_SA_BLOCK_BITS)
  47. struct sparse_array_st {
  48. int levels;
  49. ossl_uintmax_t top;
  50. size_t nelem;
  51. void **nodes;
  52. };
  53. OPENSSL_SA *ossl_sa_new(void)
  54. {
  55. OPENSSL_SA *res = OPENSSL_zalloc(sizeof(*res));
  56. return res;
  57. }
  58. static void sa_doall(const OPENSSL_SA *sa, void (*node)(void **),
  59. void (*leaf)(ossl_uintmax_t, void *, void *), void *arg)
  60. {
  61. int i[SA_BLOCK_MAX_LEVELS];
  62. void *nodes[SA_BLOCK_MAX_LEVELS];
  63. ossl_uintmax_t idx = 0;
  64. int l = 0;
  65. i[0] = 0;
  66. nodes[0] = sa->nodes;
  67. while (l >= 0) {
  68. const int n = i[l];
  69. void ** const p = nodes[l];
  70. if (n >= SA_BLOCK_MAX) {
  71. if (p != NULL && node != NULL)
  72. (*node)(p);
  73. l--;
  74. idx >>= OPENSSL_SA_BLOCK_BITS;
  75. } else {
  76. i[l] = n + 1;
  77. if (p != NULL && p[n] != NULL) {
  78. idx = (idx & ~SA_BLOCK_MASK) | n;
  79. if (l < sa->levels - 1) {
  80. i[++l] = 0;
  81. nodes[l] = p[n];
  82. idx <<= OPENSSL_SA_BLOCK_BITS;
  83. } else if (leaf != NULL) {
  84. (*leaf)(idx, p[n], arg);
  85. }
  86. }
  87. }
  88. }
  89. }
  90. static void sa_free_node(void **p)
  91. {
  92. OPENSSL_free(p);
  93. }
  94. static void sa_free_leaf(ossl_uintmax_t n, void *p, void *arg)
  95. {
  96. OPENSSL_free(p);
  97. }
  98. void ossl_sa_free(OPENSSL_SA *sa)
  99. {
  100. if (sa != NULL) {
  101. sa_doall(sa, &sa_free_node, NULL, NULL);
  102. OPENSSL_free(sa);
  103. }
  104. }
  105. void ossl_sa_free_leaves(OPENSSL_SA *sa)
  106. {
  107. sa_doall(sa, &sa_free_node, &sa_free_leaf, NULL);
  108. OPENSSL_free(sa);
  109. }
  110. /* Wrap this in a structure to avoid compiler warnings */
  111. struct trampoline_st {
  112. void (*func)(ossl_uintmax_t, void *);
  113. };
  114. static void trampoline(ossl_uintmax_t n, void *l, void *arg)
  115. {
  116. ((const struct trampoline_st *)arg)->func(n, l);
  117. }
  118. void ossl_sa_doall(const OPENSSL_SA *sa, void (*leaf)(ossl_uintmax_t, void *))
  119. {
  120. struct trampoline_st tramp;
  121. tramp.func = leaf;
  122. if (sa != NULL)
  123. sa_doall(sa, NULL, &trampoline, &tramp);
  124. }
  125. void ossl_sa_doall_arg(const OPENSSL_SA *sa,
  126. void (*leaf)(ossl_uintmax_t, void *, void *),
  127. void *arg)
  128. {
  129. if (sa != NULL)
  130. sa_doall(sa, NULL, leaf, arg);
  131. }
  132. size_t ossl_sa_num(const OPENSSL_SA *sa)
  133. {
  134. return sa == NULL ? 0 : sa->nelem;
  135. }
  136. void *ossl_sa_get(const OPENSSL_SA *sa, ossl_uintmax_t n)
  137. {
  138. int level;
  139. void **p, *r = NULL;
  140. if (sa == NULL || sa->nelem == 0)
  141. return NULL;
  142. if (n <= sa->top) {
  143. p = sa->nodes;
  144. for (level = sa->levels - 1; p != NULL && level > 0; level--)
  145. p = (void **)p[(n >> (OPENSSL_SA_BLOCK_BITS * level))
  146. & SA_BLOCK_MASK];
  147. r = p == NULL ? NULL : p[n & SA_BLOCK_MASK];
  148. }
  149. return r;
  150. }
  151. static ossl_inline void **alloc_node(void)
  152. {
  153. return OPENSSL_zalloc(SA_BLOCK_MAX * sizeof(void *));
  154. }
  155. int ossl_sa_set(OPENSSL_SA *sa, ossl_uintmax_t posn, void *val)
  156. {
  157. int i, level = 1;
  158. ossl_uintmax_t n = posn;
  159. void **p;
  160. if (sa == NULL)
  161. return 0;
  162. for (level = 1; level < SA_BLOCK_MAX_LEVELS; level++)
  163. if ((n >>= OPENSSL_SA_BLOCK_BITS) == 0)
  164. break;
  165. for (;sa->levels < level; sa->levels++) {
  166. p = alloc_node();
  167. if (p == NULL)
  168. return 0;
  169. p[0] = sa->nodes;
  170. sa->nodes = p;
  171. }
  172. if (sa->top < posn)
  173. sa->top = posn;
  174. p = sa->nodes;
  175. for (level = sa->levels - 1; level > 0; level--) {
  176. i = (posn >> (OPENSSL_SA_BLOCK_BITS * level)) & SA_BLOCK_MASK;
  177. if (p[i] == NULL && (p[i] = alloc_node()) == NULL)
  178. return 0;
  179. p = p[i];
  180. }
  181. p += posn & SA_BLOCK_MASK;
  182. if (val == NULL && *p != NULL)
  183. sa->nelem--;
  184. else if (val != NULL && *p == NULL)
  185. sa->nelem++;
  186. *p = val;
  187. return 1;
  188. }