sparse_array.h 6.6 KB

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