| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | .\" $NetBSD: thmap.9,v 1.2 2019/08/28 22:11:25 wiz Exp $ | | 1 | .\" $NetBSD: thmap.9,v 1.3 2020/08/28 07:03:41 riastradh Exp $ |
2 | .\" | | 2 | .\" |
3 | .\" Copyright (c) 2018 Mindaugas Rasiukevicius <rmind at noxt eu> | | 3 | .\" Copyright (c) 2018 Mindaugas Rasiukevicius <rmind at noxt eu> |
4 | .\" All rights reserved. | | 4 | .\" All rights reserved. |
5 | .\" | | 5 | .\" |
6 | .\" Redistribution and use in source and binary forms, with or without | | 6 | .\" Redistribution and use in source and binary forms, with or without |
7 | .\" modification, are permitted provided that the following conditions | | 7 | .\" modification, are permitted provided that the following conditions |
8 | .\" are met: | | 8 | .\" are met: |
9 | .\" 1. Redistributions of source code must retain the above copyright | | 9 | .\" 1. Redistributions of source code must retain the above copyright |
10 | .\" notice, this list of conditions and the following disclaimer. | | 10 | .\" notice, this list of conditions and the following disclaimer. |
11 | .\" 2. Redistributions in binary form must reproduce the above copyright | | 11 | .\" 2. Redistributions in binary form must reproduce the above copyright |
12 | .\" notice, this list of conditions and the following disclaimer in the | | 12 | .\" notice, this list of conditions and the following disclaimer in the |
13 | .\" documentation and/or other materials provided with the distribution. | | 13 | .\" documentation and/or other materials provided with the distribution. |
14 | .\" | | 14 | .\" |
| @@ -26,173 +26,173 @@ | | | @@ -26,173 +26,173 @@ |
26 | .\" | | 26 | .\" |
27 | .Dd December 11, 2018 | | 27 | .Dd December 11, 2018 |
28 | .Dt THMAP 9 | | 28 | .Dt THMAP 9 |
29 | .Os | | 29 | .Os |
30 | .Sh NAME | | 30 | .Sh NAME |
31 | .Nm thmap | | 31 | .Nm thmap |
32 | .Nd concurrent trie-hash map | | 32 | .Nd concurrent trie-hash map |
33 | .Sh SYNOPSIS | | 33 | .Sh SYNOPSIS |
34 | .In thmap.h | | 34 | .In thmap.h |
35 | .\" ----- | | 35 | .\" ----- |
36 | .Ft thmap_t * | | 36 | .Ft thmap_t * |
37 | .Fn thmap_create "uintptr_t baseptr" "const thmap_ops_t *ops" "unsigned flags" | | 37 | .Fn thmap_create "uintptr_t baseptr" "const thmap_ops_t *ops" "unsigned flags" |
38 | .Ft void | | 38 | .Ft void |
39 | .Fn thmap_destroy "thmap_t *hmap" | | 39 | .Fn thmap_destroy "thmap_t *thmap" |
40 | .Ft void * | | 40 | .Ft void * |
41 | .Fn thmap_get "thmap_t *hmap" "const void *key" "size_t len" | | 41 | .Fn thmap_get "thmap_t *thmap" "const void *key" "size_t len" |
42 | .Ft void * | | 42 | .Ft void * |
43 | .Fn thmap_put "thmap_t *hmap" "const void *key" "size_t len" "void *val" | | 43 | .Fn thmap_put "thmap_t *thmap" "const void *key" "size_t len" "void *val" |
44 | .Ft void * | | 44 | .Ft void * |
45 | .Fn thmap_del "thmap_t *hmap" "const void *key" "size_t len" | | 45 | .Fn thmap_del "thmap_t *thmap" "const void *key" "size_t len" |
46 | .Ft void * | | 46 | .Ft void * |
47 | .Fn thmap_stage_gc "thmap_t *hmap" | | 47 | .Fn thmap_stage_gc "thmap_t *thmap" |
48 | .Ft void | | 48 | .Ft void |
49 | .Fn thmap_gc "thmap_t *hmap" "void *ref" | | 49 | .Fn thmap_gc "thmap_t *thmap" "void *ref" |
50 | .Ft void | | 50 | .Ft void |
51 | .Fn thmap_setroot "thmap_t *thmap" "uintptr_t root_offset" | | 51 | .Fn thmap_setroot "thmap_t *thmap" "uintptr_t root_offset" |
52 | .Ft uintptr_t | | 52 | .Ft uintptr_t |
53 | .Fn thmap_getroot "const thmap_t *thmap" | | 53 | .Fn thmap_getroot "const thmap_t *thmap" |
54 | .\" ----- | | 54 | .\" ----- |
55 | .Sh DESCRIPTION | | 55 | .Sh DESCRIPTION |
56 | Concurrent trie-hash map \(em a general purpose associative array, | | 56 | Concurrent trie-hash map \(em a general purpose associative array, |
57 | combining the elements of hashing and radix trie. | | 57 | combining the elements of hashing and radix trie. |
58 | Highlights: | | 58 | Highlights: |
59 | .Pp | | 59 | .Pp |
60 | .Bl -hyphen -compact | | 60 | .Bl -hyphen -compact |
61 | .It | | 61 | .It |
62 | Very competitive performance, with logarithmic time complexity on average. | | 62 | Very competitive performance, with logarithmic time complexity on average. |
63 | .It | | 63 | .It |
64 | Lookups are lock-free and inserts/deletes are using fine-grained locking. | | 64 | Lookups are lock-free and inserts/deletes are using fine-grained locking. |
65 | .It | | 65 | .It |
66 | Incremental growth of the data structure (no large resizing/rehashing). | | 66 | Incremental growth of the data structure (no large resizing/rehashing). |
67 | .It | | 67 | .It |
68 | Optional support for use with shared memory, e.g. memory-mapped file. | | 68 | Optional support for use with shared memory, e.g. memory-mapped file. |
69 | .El | | 69 | .El |
70 | .Pp | | 70 | .Pp |
71 | Delete operations (the key/data destruction) must be synchronized with | | 71 | Delete operations (the key/data destruction) must be synchronized with |
72 | the readers using some reclamation mechanism. | | 72 | the readers using some reclamation mechanism. |
73 | .\" ----- | | 73 | .\" ----- |
74 | .Sh FUNCTIONS | | 74 | .Sh FUNCTIONS |
75 | .Bl -tag -width thmap_create | | 75 | .Bl -tag -width abcd |
76 | .It Fn thmap_create | | 76 | .It Fn thmap_create baseptr ops flags |
77 | Construct a new trie-hash map. | | 77 | Construct a new trie-hash map. |
78 | The optional | | 78 | The optional |
79 | .Fa ops | | 79 | .Fa ops |
80 | parameter can | | 80 | parameter can |
81 | used to set the custom allocate/free operations (see the description of | | 81 | used to set the custom allocate/free operations (see the description of |
82 | .Vt thmap_ops_t | | 82 | .Vt thmap_ops_t |
83 | below). | | 83 | below). |
84 | In such case, the | | 84 | In such case, the |
85 | .Fa baseptr | | 85 | .Fa baseptr |
86 | is the base (start) address of the address space mapping (it must be | | 86 | is the base (start) address of the address space mapping (it must be |
87 | word-aligned). | | 87 | word-aligned). |
88 | If | | 88 | If |
89 | .Fa ops | | 89 | .Fa ops |
90 | is set to | | 90 | is set to |
91 | .Dv NULL , | | 91 | .Dv NULL , |
92 | then | | 92 | then |
93 | .Xr malloc 3 | | 93 | .Xr malloc 3 |
94 | and | | 94 | and |
95 | .Xr free 3 | | 95 | .Xr free 3 |
96 | will be used as the default operations and | | 96 | will be used as the default operations and |
97 | .Fa baseptr | | 97 | .Fa baseptr |
98 | should be set to zero. | | 98 | should be set to zero. |
99 | Currently, the supported | | 99 | Currently, the supported |
100 | .Fa flags | | 100 | .Fa flags |
101 | are: | | 101 | are: |
102 | .Bl -tag -width THMAP_NOCOPY | | 102 | .Bl -tag -width THMAP_SETROOT |
103 | .It Dv THMAP_NOCOPY | | 103 | .It Dv THMAP_NOCOPY |
104 | The keys on insert will not be copied and the given pointers to them will | | 104 | The keys on insert will not be copied and the given pointers to them will |
105 | be expected to be valid and the values constant until the key is deleted; | | 105 | be expected to be valid and the values constant until the key is deleted; |
106 | by default, the put operation will make a copy of the key. | | 106 | by default, the put operation will make a copy of the key. |
107 | .It Dv THMAP_SETROOT | | 107 | .It Dv THMAP_SETROOT |
108 | Indicate that the root of the map will be manually set using the | | 108 | Indicate that the root of the map will be manually set using the |
109 | .Fn thmap_setroot | | 109 | .Fn thmap_setroot |
110 | routine; | | 110 | routine; |
111 | by default, the map is initialized and the root node is set on | | 111 | by default, the map is initialized and the root node is set on |
112 | .Fn thmap_create . | | 112 | .Fn thmap_create . |
113 | .El | | 113 | .El |
114 | .\" --- | | 114 | .\" --- |
115 | .It Fn thmap_destroy | | 115 | .It Fn thmap_destroy thmap |
116 | Destroy the map, freeing the memory it uses. | | 116 | Destroy the map, freeing the memory it uses. |
117 | .\" --- | | 117 | .\" --- |
118 | .It Fn thmap_get | | 118 | .It Fn thmap_get thmap key len |
119 | Lookup the key (of a given length) and return the value associated with it. | | 119 | Lookup the key (of a given length) and return the value associated with it. |
120 | Return | | 120 | Return |
121 | .Dv NULL | | 121 | .Dv NULL |
122 | if the key is not found (see the | | 122 | if the key is not found (see the |
123 | .Sx CAVEATS | | 123 | .Sx CAVEATS |
124 | section). | | 124 | section). |
125 | .\" --- | | 125 | .\" --- |
126 | .It Fn thmap_put | | 126 | .It Fn thmap_put thmap key len val |
127 | Insert the key with an arbitrary value. | | 127 | Insert the key with an arbitrary value. |
128 | If the key is already present, return the already existing associated value | | 128 | If the key is already present, return the already existing associated value |
129 | without changing it. | | 129 | without changing it. |
130 | Otherwise, on a successful insert, return the given value. | | 130 | Otherwise, on a successful insert, return the given value. |
131 | Just compare the result against | | 131 | Just compare the result against |
132 | .Fa val | | 132 | .Fa val |
133 | to test whether the insert was successful. | | 133 | to test whether the insert was successful. |
134 | .\" --- | | 134 | .\" --- |
135 | .It Fn thmap_del | | 135 | .It Fn thmap_del thmap key len |
136 | Remove the given key. | | 136 | Remove the given key. |
137 | If the key was present, return the associated value; | | 137 | If the key was present, return the associated value; |
138 | otherwise return | | 138 | otherwise return |
139 | .Dv NULL . | | 139 | .Dv NULL . |
140 | The memory associated with the entry is not released immediately, because | | 140 | The memory associated with the entry is not released immediately, because |
141 | in the concurrent environment (e.g., multi-threaded application) the caller | | 141 | in the concurrent environment (e.g., multi-threaded application) the caller |
142 | may need to ensure it is safe to do so. | | 142 | may need to ensure it is safe to do so. |
143 | It is managed using the | | 143 | It is managed using the |
144 | .Fn thmap_stage_gc | | 144 | .Fn thmap_stage_gc |
145 | and | | 145 | and |
146 | .Fn thmap_gc | | 146 | .Fn thmap_gc |
147 | routines. | | 147 | routines. |
148 | .\" --- | | 148 | .\" --- |
149 | .It Fn thmap_stage_gc | | 149 | .It Fn thmap_stage_gc thmap |
150 | Stage the currently pending entries (the memory not yet released after | | 150 | Stage the currently pending entries (the memory not yet released after |
151 | the deletion) for reclamation (G/C). | | 151 | the deletion) for reclamation (G/C). |
152 | This operation should be called | | 152 | This operation should be called |
153 | .Em before | | 153 | .Em before |
154 | the synchronization barrier. | | 154 | the synchronization barrier. |
155 | .Pp | | 155 | .Pp |
156 | Returns a reference which must be passed to | | 156 | Returns a reference which must be passed to |
157 | .Fn thmap_gc . | | 157 | .Fn thmap_gc . |
158 | Not calling the G/C function for the returned reference would result in | | 158 | Not calling the G/C function for the returned reference would result in |
159 | a memory leak. | | 159 | a memory leak. |
160 | .\" --- | | 160 | .\" --- |
161 | .It Fn thmap_gc | | 161 | .It Fn thmap_gc thmap ref |
162 | Reclaim (G/C) the staged entries i.e. release any memory associated | | 162 | Reclaim (G/C) the staged entries i.e. release any memory associated |
163 | with the deleted keys. | | 163 | with the deleted keys. |
164 | The reference must be the value returned by the call to | | 164 | The reference must be the value returned by the call to |
165 | .Fn thmap_stage_gc . | | 165 | .Fn thmap_stage_gc . |
166 | .Pp | | 166 | .Pp |
167 | This function must be called | | 167 | This function must be called |
168 | .Em after | | 168 | .Em after |
169 | the synchronization barrier which guarantees that there are no active | | 169 | the synchronization barrier which guarantees that there are no active |
170 | readers referencing the staged entries. | | 170 | readers referencing the staged entries. |
171 | .\" --- | | 171 | .\" --- |
172 | .El | | 172 | .El |
173 | .Pp | | 173 | .Pp |
174 | If the map is created using the | | 174 | If the map is created using the |
175 | .Fa THMAP_SETROOT | | 175 | .Fa THMAP_SETROOT |
176 | flag, then the following functions are applicable: | | 176 | flag, then the following functions are applicable: |
177 | .\" --- | | 177 | .\" --- |
178 | .Bl -tag -width thmap_setroot | | 178 | .Bl -tag -width abcd |
179 | .It Fn thmap_setroot | | 179 | .It Fn thmap_setroot thmap root_offset |
180 | Set the root node. | | 180 | Set the root node. |
181 | The address must be relative to the base address, as if allocated by the | | 181 | The address must be relative to the base address, as if allocated by the |
182 | .Fn thmap_ops_t::alloc | | 182 | .Fn thmap_ops_t::alloc |
183 | routine. | | 183 | routine. |
184 | Return 0 on success and \-1 on failure (if already set). | | 184 | Return 0 on success and \-1 on failure (if already set). |
185 | .It Fn thmap_getroot | | 185 | .It Fn thmap_getroot thmap |
186 | Get the root node address. | | 186 | Get the root node address. |
187 | The returned address will be relative to the base address. | | 187 | The returned address will be relative to the base address. |
188 | .El | | 188 | .El |
189 | .\" --- | | 189 | .\" --- |
190 | .Pp | | 190 | .Pp |
191 | Members of | | 191 | Members of |
192 | .Vt thmap_ops_t | | 192 | .Vt thmap_ops_t |
193 | are | | 193 | are |
194 | .Bd -literal | | 194 | .Bd -literal |
195 | uintptr_t (*alloc)(size_t len); | | 195 | uintptr_t (*alloc)(size_t len); |
196 | void (*free)(uintptr_t addr, size_t len); | | 196 | void (*free)(uintptr_t addr, size_t len); |
197 | .Ed | | 197 | .Ed |
198 | .\" ----- | | 198 | .\" ----- |