60695266a76d5f074e79010b134aa55fb66dddff
[openssl.git] / doc / internal / man3 / DEFINE_SPARSE_ARRAY_OF.pod
1 =pod
2
3 =head1 NAME
4
5 DEFINE_SPARSE_ARRAY_OF, ossl_sa_TYPE_new, ossl_sa_TYPE_free,
6 ossl_sa_TYPE_free_leaves, ossl_sa_TYPE_num, ossl_sa_TYPE_doall,
7 ossl_sa_TYPE_doall_arg, ossl_sa_TYPE_get, ossl_sa_TYPE_set
8 - sparse array container
9
10 =head1 SYNOPSIS
11
12 =for comment generic
13
14  #include "internal/sparse_array.h"
15
16  typedef struct sparse_array_st OPENSSL_SA;
17
18  SPARSE_ARRAY_OF(TYPE)
19  DEFINE_SPARSE_ARRAY_OF(TYPE)
20
21  SPARSE_ARRAY_OF(TYPE) *ossl_sa_TYPE_new(void);
22  void ossl_sa_TYPE_free(const SPARSE_ARRAY_OF(TYPE) *sa);
23  void ossl_sa_TYPE_free_leaves(const SPARSE_ARRAY_OF(TYPE) *sa);
24  size_t ossl_sa_TYPE_num(const SPARSE_ARRAY_OF(TYPE) *sa);
25  void ossl_sa_TYPE_doall(const OPENSSL_SA *sa, void (*leaf)(ossl_uintmax_t,
26                                                             void *));
27  void ossl_sa_TYPE_doall_arg(const OPENSSL_SA *sa,
28                              void (*leaf)(ossl_uintmax_t, void *, void *),
29                              void *arg);
30  TYPE *ossl_sa_TYPE_get(const SPARSE_ARRAY_OF(TYPE) *sa, ossl_uintmax_t idx);
31  int ossl_sa_TYPE_set(SPARSE_ARRAY_OF(TYPE) *sa, ossl_uintmax_t idx,
32                       TYPE *value);
33
34 =head1 DESCRIPTION
35
36 =begin comment
37
38 POD is pretty good at recognising function names and making them appropriately
39 bold...  however, when part of the function name is variable, we have to help
40 the processor along
41
42 =end comment
43
44 SPARSE_ARRAY_OF() returns the name for a sparse array of the specified
45 I<TYPE>.  DEFINE_STACK_OF() creates set of functions for a sparse array of
46 I<TYPE>. This will mean that a pointer to type I<TYPE> is stored in each
47 element of a sparse array, the type is referenced by B<SPARSE_ARRAY_OF>(I<TYPE>)
48 and each function name begins with B<ossl_sa_I<TYPE>_>. For example:
49
50  TYPE *ossl_sa_TYPE_get(SPARSE_ARRAY_OF(TYPE) *sa, ossl_uintmax_t idx);
51
52 B<ossl_sa_I<TYPE>_num>() returns the number of elements in I<sa> or 0 if I<sa>
53 is NULL.
54
55 B<ossl_sa_I<TYPE>_get>() returns element I<idx> in I<sa>, where I<idx> starts
56 at zero. If I<idx> refers to a value that has not been set then NULL is
57 returned.
58
59 B<ossl_sa_I<TYPE>_set>() sets element I<idx> in I<sa> to I<value>, where I<idx>
60 starts at zero. The sparse array will be resized as required.
61
62 B<ossl_sa_I<TYPE>_new>() allocates a new empty sparse array.
63
64 B<ossl_sa_I<TYPE>_free>() frees up the I<sa> structure. It does I<not> free up any
65 elements of I<sa>. After this call I<sa> is no longer valid.
66
67 B<ossl_sa_I<TYPE>_free_leaves>() frees up the I<sa> structure and all of its
68 elements.  After this call I<sa> is no longer valid.
69
70 B<ossl_sa_I<TYPE>_doall>() calls the function I<leaf> for each element in I<sa>
71 in ascending index order. The index position, within the sparse array,
72 of each item is passed as the first argument to the leaf function and a
73 pointer to the associated value is is passed as the second argument.
74
75 B<ossl_sa_I<TYPE>_doall_arg>() calls the function I<leaf> for each element in
76 I<sa> in ascending index order. The index position, within the sparse
77 array, of each item is passed as the first argument to the leaf function,
78 a pointer to the associated value is passed as the second argument and
79 the third argument is the user supplied I<arg>.
80
81
82 =head1 NOTES
83
84 Sparse arrays are an internal data structure and should B<not> be used by user
85 applications.
86
87 Care should be taken when accessing sparse arrays in multi-threaded
88 environments.  The B<ossl_sa_I<TYPE>_set>() operation can cause the internal
89 structure of the sparse array to change which causes race conditions if the
90 sparse array is accessed in a different thread.
91
92 SPARSE_ARRAY_OF() and DEFINE_SPARSE_ARRAY_OF() are implemented as macros.
93
94 The underlying utility B<OPENSSL_SA_> API should not be used directly.  It
95 defines these functions: OPENSSL_SA_doall, OPENSSL_SA_doall_arg,
96 OPENSSL_SA_free, OPENSSL_SA_free_leaves, OPENSSL_SA_get, OPENSSL_SA_new,
97 OPENSSL_SA_num and OPENSSL_SA_set.
98
99 =head1 RETURN VALUES
100
101 B<ossl_sa_I<TYPE>_num>() returns the number of elements in the sparse array or
102 B<0> if the passed sparse array is NULL.
103
104 B<ossl_sa_I<TYPE>_get>() returns a pointer to a sparse array element or NULL if
105 the element has not be set.
106
107 B<ossl_sa_I<TYPE>_set>() return B<1> on success and B<0> on error. In the latter
108 case, the elements of the sparse array remain unchanged, although the internal
109 structures might have.
110
111 B<ossl_sa_I<TYPE>_new>() returns an empty sparse array or NULL if an error
112 occurs.
113
114 B<ossl_sa_I<TYPE>_doall>(), B<ossl_sa_I<TYPE>_doall_arg>(),
115 B<ossl_sa_I<TYPE>_free>() and B<ossl_sa_I<TYPE>_free_leaves>()
116 do not return values.
117
118 =head1 HISTORY
119
120 This functionality was added to OpenSSL 3.0.
121
122 =head1 COPYRIGHT
123
124 Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.  Copyright
125 (c) 2019, Oracle and/or its affiliates.  All rights reserved.
126
127 Licensed under the Apache License 2.0 (the "License").  You may not use this
128 file except in compliance with the License.  You can obtain a copy in the file
129 LICENSE in the source distribution or at
130 L<https://www.openssl.org/source/license.html>.
131
132 =cut