sparse_array.h 3.2 KB

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