Neil Horman a552c23c65 Harden asn1 oid loader to invalid inputs | 11 months ago | |
---|---|---|
.. | ||
aes | 11 months ago | |
aria | 2 years ago | |
asn1 | 11 months ago | |
async | 2 years ago | |
bf | 1 year ago | |
bio | 11 months ago | |
bn | 11 months ago | |
buffer | 2 years ago | |
camellia | 2 years ago | |
cast | 1 year ago | |
chacha | 1 year ago | |
cmac | 1 year ago | |
cmp | 1 year ago | |
cms | 11 months ago | |
comp | 1 year ago | |
conf | 1 year ago | |
crmf | 1 year ago | |
ct | 2 years ago | |
des | 1 year ago | |
dh | 1 year ago | |
dsa | 1 year ago | |
dso | 1 year ago | |
ec | 1 year ago | |
encode_decode | 11 months ago | |
engine | 1 year ago | |
err | 1 year ago | |
ess | 2 years ago | |
evp | 1 year ago | |
ffc | 1 year ago | |
hmac | 3 years ago | |
hpke | 1 year ago | |
http | 11 months ago | |
idea | 1 year ago | |
kdf | 5 years ago | |
lhash | 1 year ago | |
md2 | 1 year ago | |
md4 | 1 year ago | |
md5 | 11 months ago | |
mdc2 | 1 year ago | |
modes | 1 year ago | |
objects | 1 year ago | |
ocsp | 1 year ago | |
pem | 1 year ago | |
perlasm | 11 months ago | |
pkcs12 | 1 year ago | |
pkcs7 | 11 months ago | |
poly1305 | 1 year ago | |
property | 11 months ago | |
rand | 1 year ago | |
rc2 | 1 year ago | |
rc4 | 1 year ago | |
rc5 | 1 year ago | |
ripemd | 1 year ago | |
rsa | 1 year ago | |
seed | 1 year ago | |
sha | 11 months ago | |
siphash | 2 years ago | |
sm2 | 1 year ago | |
sm3 | 1 year ago | |
sm4 | 1 year ago | |
srp | 1 year ago | |
stack | 1 year ago | |
store | 1 year ago | |
thread | 1 year ago | |
ts | 1 year ago | |
txt_db | 1 year ago | |
ui | 1 year ago | |
whrlpool | 1 year ago | |
x509 | 11 months ago | |
LPdir_nyi.c | 6 years ago | |
LPdir_unix.c | 1 year ago | |
LPdir_vms.c | 6 years ago | |
LPdir_win.c | 6 years ago | |
LPdir_win32.c | 6 years ago | |
LPdir_wince.c | 6 years ago | |
README-sparse_array.md | 4 years ago | |
alphacpuid.pl | 4 years ago | |
arm64cpuid.pl | 2 years ago | |
arm_arch.h | 11 months ago | |
armcap.c | 11 months ago | |
armv4cpuid.pl | 1 year ago | |
asn1_dsa.c | 3 years ago | |
bsearch.c | 5 years ago | |
build.info | 1 year ago | |
c64xpluscpuid.pl | 4 years ago | |
context.c | 1 year ago | |
core_algorithm.c | 2 years ago | |
core_fetch.c | 2 years ago | |
core_namemap.c | 1 year ago | |
cpt_err.c | 2 years ago | |
cpuid.c | 1 year ago | |
cryptlib.c | 1 year ago | |
ctype.c | 1 year ago | |
cversion.c | 5 years ago | |
der_writer.c | 2 years ago | |
deterministic_nonce.c | 1 year ago | |
dllmain.c | 2 years ago | |
ebcdic.c | 6 years ago | |
ex_data.c | 1 year ago | |
getenv.c | 2 years ago | |
ia64cpuid.S | 6 years ago | |
info.c | 1 year ago | |
init.c | 1 year ago | |
initthread.c | 1 year ago | |
loongarch64cpuid.pl | 2 years ago | |
loongarch_arch.h | 1 year ago | |
loongarchcap.c | 1 year ago | |
mem.c | 1 year ago | |
mem_clr.c | 6 years ago | |
mem_sec.c | 11 months ago | |
mips_arch.h | 5 years ago | |
o_dir.c | 2 years ago | |
o_fopen.c | 2 years ago | |
o_init.c | 2 years ago | |
o_str.c | 1 year ago | |
o_time.c | 3 years ago | |
packet.c | 1 year ago | |
param_build.c | 11 months ago | |
param_build_set.c | 1 year ago | |
params.c | 1 year ago | |
params_dup.c | 2 years ago | |
params_from_text.c | 2 years ago | |
params_idx.c.in | 1 year ago | |
pariscid.pl | 4 years ago | |
passphrase.c | 2 years ago | |
ppccap.c | 2 years ago | |
ppccpuid.pl | 2 years ago | |
provider.c | 1 year ago | |
provider_child.c | 1 year ago | |
provider_conf.c | 1 year ago | |
provider_core.c | 11 months ago | |
provider_local.h | 3 years ago | |
provider_predefined.c | 3 years ago | |
punycode.c | 1 year ago | |
quic_vlint.c | 1 year ago | |
riscv32cpuid.pl | 2 years ago | |
riscv64cpuid.pl | 1 year ago | |
riscvcap.c | 1 year ago | |
s390x_arch.h | 1 year ago | |
s390xcap.c | 1 year ago | |
s390xcpuid.pl | 4 years ago | |
self_test_core.c | 2 years ago | |
sleep.c | 1 year ago | |
sparccpuid.S | 6 years ago | |
sparcv9cap.c | 3 years ago | |
sparse_array.c | 2 years ago | |
threads_lib.c | 2 years ago | |
threads_none.c | 1 year ago | |
threads_pthread.c | 1 year ago | |
threads_win.c | 1 year ago | |
time.c | 1 year ago | |
trace.c | 1 year ago | |
uid.c | 1 year ago | |
vms_rms.h | 6 years ago | |
x86_64cpuid.pl | 3 years ago | |
x86cpuid.pl | 4 years ago |
The sparse_array.c
file contains an implementation of a sparse array that
attempts to be both space and time efficient.
The sparse array is represented using a tree structure. Each node in the tree contains a block of pointers to either the user supplied leaf values or to another node.
There are a number of parameters used to define the block size:
OPENSSL_SA_BLOCK_BITS Specifies the number of bits covered by each block
SA_BLOCK_MAX Specifies the number of pointers in each block
SA_BLOCK_MASK Specifies a bit mask to perform modulo block size
SA_BLOCK_MAX_LEVELS Indicates the maximum possible height of the tree
These constants are inter-related:
SA_BLOCK_MAX = 2 ^ OPENSSL_SA_BLOCK_BITS
SA_BLOCK_MASK = SA_BLOCK_MAX - 1
SA_BLOCK_MAX_LEVELS = number of bits in size_t divided by
OPENSSL_SA_BLOCK_BITS rounded up to the next multiple
of OPENSSL_SA_BLOCK_BITS
OPENSSL_SA_BLOCK_BITS
can be defined at compile time and this overrides the
built in setting.
As a space and performance optimisation, the height of the tree is usually less than the maximum possible height. Only sufficient height is allocated to accommodate the largest index added to the data structure.
The largest index used to add a value to the array determines the tree height:
+----------------------+---------------------+
| Largest Added Index | Height of Tree |
+----------------------+---------------------+
| SA_BLOCK_MAX - 1 | 1 |
| SA_BLOCK_MAX ^ 2 - 1 | 2 |
| SA_BLOCK_MAX ^ 3 - 1 | 3 |
| ... | ... |
| size_t max | SA_BLOCK_MAX_LEVELS |
+----------------------+---------------------+
The tree height is dynamically increased as needed based on additions.
An empty tree is represented by a NULL root pointer. Inserting a value at index 0 results in the allocation of a top level node full of null pointers except for the single pointer to the user's data (N = SA_BLOCK_MAX for brevity):
+----+
|Root|
|Node|
+-+--+
|
|
|
v
+-+-+---+---+---+---+
| 0 | 1 | 2 |...|N-1|
| |nil|nil|...|nil|
+-+-+---+---+---+---+
|
|
|
v
+-+--+
|User|
|Data|
+----+
Index 0
Inserting at element 2N+1 creates a new root node and pushes down the old root node. It then creates a second second level node to hold the pointer to the user's new data:
+----+
|Root|
|Node|
+-+--+
|
|
|
v
+-+-+---+---+---+---+
| 0 | 1 | 2 |...|N-1|
| |nil| |...|nil|
+-+-+---+-+-+---+---+
| |
| +------------------+
| |
v v
+-+-+---+---+---+---+ +-+-+---+---+---+---+
| 0 | 1 | 2 |...|N-1| | 0 | 1 | 2 |...|N-1|
|nil| |nil|...|nil| |nil| |nil|...|nil|
+-+-+---+---+---+---+ +---+-+-+---+---+---+
| |
| |
| |
v v
+-+--+ +-+--+
|User| |User|
|Data| |Data|
+----+ +----+
Index 0 Index 2N+1
The nodes themselves are allocated in a sparse manner. Only nodes which exist along a path from the root of the tree to an added leaf will be allocated. The complexity is hidden and nodes are allocated on an as needed basis. Because the data is expected to be sparse this doesn't result in a large waste of space.
Values can be removed from the sparse array by setting their index position to NULL. The data structure does not attempt to reclaim nodes or reduce the height of the tree on removal. For example, now setting index 0 to NULL would result in:
+----+
|Root|
|Node|
+-+--+
|
|
|
v
+-+-+---+---+---+---+
| 0 | 1 | 2 |...|N-1|
| |nil| |...|nil|
+-+-+---+-+-+---+---+
| |
| +------------------+
| |
v v
+-+-+---+---+---+---+ +-+-+---+---+---+---+
| 0 | 1 | 2 |...|N-1| | 0 | 1 | 2 |...|N-1|
|nil|nil|nil|...|nil| |nil| |nil|...|nil|
+---+---+---+---+---+ +---+-+-+---+---+---+
|
|
|
v
+-+--+
|User|
|Data|
+----+
Index 2N+1
Accesses to elements in the sparse array take O(log n) time where n is the
largest element. The base of the logarithm is SA_BLOCK_MAX
, so for moderately
small indices (e.g. NIDs), single level (constant time) access is achievable.
Space usage is O(minimum(m, n log(n)) where m is the number of elements in the
array.
Note: sparse arrays only include pointers to types.
Thus, SPARSE_ARRAY_OF(char)
can be used to store a string.