quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

SPLAY.md (3516B)


      1 <!--
      2 Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
      3 
      4 SPDX-License-Identifier: curl
      5 -->
      6 
      7 # `splay`
      8 
      9     #include "splay.h"
     10 
     11 This is an internal module for splay tree management. A splay tree is a binary
     12 search tree with the additional property that recently accessed elements are
     13 quick to access again. A self-balancing tree.
     14 
     15 Nodes are added to the tree, they are accessed and removed from the tree and
     16 it automatically rebalances itself in each operation.
     17 
     18 ## libcurl use
     19 
     20 libcurl adds fixed timeout expiry timestamps to the splay tree, and is meant
     21 to scale up to holding a huge amount of pending timeouts with decent
     22 performance.
     23 
     24 The splay tree is used to:
     25 
     26 1. figure out the next timeout expiry value closest in time
     27 2. iterate over timeouts that already have expired
     28 
     29 This splay tree rebalances itself based on the time value.
     30 
     31 Each node in the splay tree points to a `struct Curl_easy`. Each `Curl_easy`
     32 struct is represented only once in the tree. To still allow each easy handle
     33 to have a large number of timeouts per handle, each handle has a sorted linked
     34 list of pending timeouts. Only the handle's timeout that is closest to expire
     35 is the timestamp used for the splay tree node.
     36 
     37 When a specific easy handle's timeout expires, the node gets removed from the
     38 splay tree and from the handle's linked list of timeouts. The next timeout for
     39 that handle is then first in line and becomes the new timeout value as the
     40 node is re-added to the splay.
     41 
     42 ## `Curl_splay`
     43 
     44 ~~~c
     45 struct Curl_tree *Curl_splay(struct curltime i, struct Curl_tree *t);
     46 ~~~
     47 
     48 Rearranges the tree `t` after the provide time `i`.
     49 
     50 ## `Curl_splayinsert`
     51 
     52 ~~~c
     53 struct Curl_tree *Curl_splayinsert(struct curltime key,
     54                                    struct Curl_tree *t,
     55                                    struct Curl_tree *node);
     56 ~~~
     57 
     58 This function inserts a new `node` in the tree, using the given `key`
     59 timestamp. The `node` struct has a field called `->payload` that can be set to
     60 point to anything. libcurl sets this to the `struct Curl_easy` handle that is
     61 associated with the timeout value set in `key`.
     62 
     63 The splay insert function does not allocate any memory, it assumes the caller
     64 has that arranged.
     65 
     66 It returns a pointer to the new tree root.
     67 
     68 ## `Curl_splaygetbest`
     69 
     70 ~~~c
     71 struct Curl_tree *Curl_splaygetbest(struct curltime key,
     72                                     struct Curl_tree *tree,
     73                                     struct Curl_tree **removed);
     74 ~~~
     75 
     76 If there is a node in the `tree` that has a time value that is less than the
     77 provided `key`, this function removes that node from the tree and provides it
     78 in the `*removed` pointer (or NULL if there was no match).
     79 
     80 It returns a pointer to the new tree root.
     81 
     82 ## `Curl_splayremove`
     83 
     84 ~~~c
     85 int Curl_splayremove(struct Curl_tree *tree,
     86                      struct Curl_tree *node,
     87                      struct Curl_tree **newroot);
     88 ~~~
     89 
     90 Removes a given `node` from a splay `tree`, and returns the `newroot`
     91 identifying the new tree root.
     92 
     93 Note that a clean tree without any nodes present implies a NULL pointer.
     94 
     95 ## `Curl_splayset`
     96 
     97 ~~~c
     98 void Curl_splayset(struct Curl_tree *node, void *payload);
     99 ~~~
    100 
    101 Set a custom pointer to be stored in the splay node. This pointer is not used
    102 by the splay code itself and can be retrieved again with `Curl_splayget`.
    103 
    104 ## `Curl_splayget`
    105 
    106 ~~~c
    107 void *Curl_splayget(struct Curl_tree *node);
    108 ~~~
    109 
    110 Get the custom pointer from the splay node that was previously set with
    111 `Curl_splayset`. If no pointer was set before, it returns NULL.