sparse_array.h 3.4 KB

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