You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

btrie.c 78KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644
  1. /* Level-Compressed Tree Bitmap (LC-TBM) Trie implementation
  2. *
  3. * Contributed by Geoffrey T. Dairiki <dairiki@dairiki.org>
  4. *
  5. * This file is released under a "Three-clause BSD License".
  6. *
  7. * Copyright (c) 2013, Geoffrey T. Dairiki
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. *
  17. * * Redistributions in binary form must reproduce the above
  18. * copyright notice, this list of conditions and the following
  19. * disclaimer in the documentation and/or other materials provided
  20. * with the distribution.
  21. *
  22. * * Neither the name of Geoffrey T. Dairiki nor the names of other
  23. * contributors may be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  29. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GEOFFREY
  30. * T. DAIRIKI BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  31. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  32. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  33. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  34. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  35. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  36. * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  37. * DAMAGE.
  38. */
  39. /*****************************************************************
  40. *
  41. * This code implements a routing table conceptually based on a binary
  42. * trie structure. Internally, the trie is represented by two types
  43. * of compound nodes: "multibit nodes", which contain the top few
  44. * levels of an entire binary subtree; and "level compression" (LC)
  45. * nodes which represent a (potentially long) chain of out-degree one
  46. * (single child) binary nodes (possibly ending at a terminal node).
  47. *
  48. * The multibit nodes are represented using a "Tree Bitmap" structure
  49. * (more on this below), which is very efficient --- both in terms of
  50. * memory usage and lookup speed --- at representing densely branching
  51. * parts of the trie. The LC nodes can efficiently represent long
  52. * non-branching chains of binary trie nodes. Using both node types
  53. * together results in efficient representation of both the sparse and
  54. * dense parts of a binary trie.
  55. *
  56. * Graphically, here's the rough idea:
  57. *
  58. * ........
  59. * .LC o .
  60. * . / . LC nodes can
  61. * . o . <= represent long chains
  62. * . \ . of (non-branching) binary
  63. * . o . trie nodes
  64. * . / .
  65. * . o .
  66. * ......../.....
  67. * .TBM o .
  68. * . / \ . TBM nodes can represent
  69. * . o * . <= several levels of densely
  70. * . / \ . branching binary trie nodes
  71. * . o o .
  72. * ......./.....\.......
  73. * .TBM o .. o LC.
  74. * . / \ .. \ .
  75. * . o o .. o .
  76. * . / / \ .. \ .
  77. * . * o *.. o .
  78. * ...../....... / .
  79. * . o LC. . o .
  80. * . \ . .....\......
  81. * . * . . o TBM.
  82. * ........ . / \ .
  83. * . o o .
  84. * . / \ \ .
  85. * .* * *.
  86. * ...........
  87. *
  88. * Terminology
  89. * -----------
  90. *
  91. * node
  92. * Usually, in the comments below, "node" will be used to refer to
  93. * a compound node: either a multibit (TBM) node or an LC node.
  94. *
  95. * "internal node" or "prefix"
  96. * The terms "prefix" or "internal node" are used to refer to
  97. * a node in the binary trie which is internal to a multibit (TBM)
  98. * node.
  99. *
  100. * ----------------------------------------------------------------
  101. *
  102. * Internal Representation of the Nodes
  103. * ====================================
  104. *
  105. * Multibit (TBM) Nodes
  106. * ~~~~~~~~~~~~~~~~~~~~
  107. *
  108. * The multibit nodes are represented using a "Tree Bitmap" (TBM)
  109. * structure as described by Eatherton, Dittia and Varghese[1]. See
  110. * the paper referenced below for basic details.
  111. *
  112. * A multibit node, represents several levels of a binary trie.
  113. * For example, here is a multibit node of stride 2 (which represent
  114. * two levels of a binary trie.
  115. *
  116. * +------- | ------+
  117. * | multi o |
  118. * | bit / \ |
  119. * | node / \ |
  120. * | o * |
  121. * +--- / \ - / \ --+
  122. * O
  123. *
  124. * Note that, for a multibit node of stride S, there are 2^S - 1 internal
  125. * nodes, each of which may have data (or not) associated with them, and
  126. * 2^S "external paths" leading to other (possibly compound nodes).
  127. * (In the diagram above, one of three internal node (the one denoted by "*")
  128. * has data, and one of four extending paths leads to an external node
  129. * (denoted by the 'O').)
  130. *
  131. * The TBM structure can represent these bitmaps in a very memory-efficient
  132. * manner.
  133. *
  134. * Each TBM node consists of two bitmaps --- the "internal bitmap" and the
  135. * "extending paths bitmap" --- and a pointer which points to an array
  136. * which contains both the extending path ("child") nodes and any
  137. * internal prefix data for the TBM node.
  138. *
  139. * +--------+--------+
  140. * TBM | ext bm | int bm |
  141. * Node +--------+--------+
  142. * | pointer |----+
  143. * +-----------------+ |
  144. * |
  145. * |
  146. * +-----------------+ |
  147. * | extending path | |
  148. * | node[N-1] | |
  149. * +-----------------+ |
  150. * / ... / |
  151. * / ... / |
  152. * +-----------------+ |
  153. * | extending path | |
  154. * | node[0] | |
  155. * +-----------------+<---+
  156. * | int. data[M-1] |
  157. * +-----------------+
  158. * / ... /
  159. * +-----------------+
  160. * | int. data[0] |
  161. * +-----------------+
  162. *
  163. * The extending paths bitmap (or "ext bitmap") has one bit for each
  164. * possible "extending path" from the bottom of the multibit node. To
  165. * check if a particular extending path is present, one checks to see if
  166. * the corresponding bit is set in the ext bitmap. The index into the
  167. * array of children for that path can be found by counting the number
  168. * of set bits to the left of that bit.
  169. *
  170. * Similarly, the internal bitmap has one bit for each binary node
  171. * which is internal to the multibit node. To determine whether there
  172. * is data stored for an internal prefix, one checks the corresponding
  173. * bit in the internal bitmap. As for extending paths, the index into
  174. * the array of internal data is found by counting the number of set
  175. * bits to the left of that bit.
  176. *
  177. * To save space in the node structure, the node data array is stored
  178. * contiguously with the node extending path array. The single
  179. * ("children") pointer in the TBM structure points to the beginning
  180. * of the array of extending path nodes and to (one past) the end of
  181. * the the internal data array.
  182. *
  183. * The multibit stride is chosen so that the entire TBM node structure fits
  184. * in the space of two pointers. On 32 bit machines this means the stride
  185. * is four (each of the two bitmaps is 16 bits); on 32 bit machines the
  186. * stride is five.
  187. *
  188. * Note that there are only 2^stride - 1 internal prefixes in a TBM
  189. * node. That means there is one unused bit in the internal bitmap.
  190. * We require that that bit must always be clear for a TBM node. (If
  191. * set, it indicates that the structure represents, instead, an LC
  192. * node. See below.)
  193. *
  194. * ----------------------------------------------------------------
  195. *
  196. * Level Compression (LC) Nodes
  197. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  198. *
  199. * LC nodes are used to represent a chain of out-degree-one (single
  200. * child) prefixes in the binary trie. The are represented by a bit
  201. * string (the "relative prefix") along with its length and a pointer
  202. * to the extending path (the next node past the LC node.)
  203. *
  204. *
  205. * Non-Terminal LC Node:
  206. *
  207. * +------------------+-------+
  208. * | relative prefix |1|0|len|
  209. * +------------------+-------+
  210. * | ptr.child |--+
  211. * +--------------------------+ |
  212. * |
  213. * |
  214. * +--------------------------+ |
  215. * | Next node - | |
  216. * | either LC or TBM | |
  217. * | | |
  218. * +--------------------------+<-+
  219. *
  220. * The Relative Prefix
  221. * -------------------
  222. *
  223. * The maximum relative prefix per LC node is selected so that (again)
  224. * the entire node structure fits in the space of two pointers. On 32 bit
  225. * machines, the maximum relative prefix is 24 bits; on 62 bit machines
  226. * the limit is 56 bits.
  227. *
  228. * In the LC node structure, the relative prefix is stored as an array
  229. * of bytes. To avoid some bit-shifting during tree searches, these
  230. * bytes are byte-aligned with the global prefix. In other words, in
  231. * general there are (pos % 8) "pad" bits at the beginning of the
  232. * relative prefix --- where pos "starting bit" (or depth in the
  233. * binary tree) of the LC node --- which really belong to the parent
  234. * node(s) of the LC node. For efficiency (so that we don't have to
  235. * mask them out when matching) we require that these pad bits be
  236. * correct --- they must match the path which leads to the LC node.
  237. *
  238. * The relative prefix length stored in the LC node structure does not
  239. * count the pad bits.
  240. *
  241. * Terminal Node Compression
  242. * -------------------------
  243. *
  244. * For memory efficiency, we also support "terminal LC" nodes. When
  245. * the extension path from an LC node consists a single terminal node,
  246. * we store that terminal nodes data directly in the parent LC node.
  247. *
  248. * Instead of this:
  249. *
  250. * +------------------+-------+
  251. * | relative prefix |1|0|len|
  252. * +------------------+-------+
  253. * | ptr.child |--+
  254. * +--------------------------+ |
  255. * |
  256. * +--------------------------+ |
  257. * | Terminal Node (TBM node, | |
  258. * | empty except for the | |
  259. * +--| root internal node.) | |
  260. * | +--------------------------+<-+
  261. * |
  262. * +->+--------------------------+
  263. * | terminal node data |
  264. * +--------------------------+
  265. *
  266. * We can do this:
  267. *
  268. * +------------------+-------+
  269. * | relative prefix |1|1|len|
  270. * +------------------+-------+
  271. * | terminal node data |
  272. * +--------------------------+
  273. *
  274. * Terminal LC nodes are differentiated from non-terminal LC nodes
  275. * by the setting of the is_terminal flag.
  276. *
  277. * Node Structure Packing Details
  278. * ------------------------------
  279. *
  280. * The LC and TBM node structures are carefully packed so that the
  281. * "is_lc" flag (which indicates that a node is an LC node)
  282. * corresponds to the one unused bit in the internal bitmap of the TBM
  283. * node structure (which we require to be zero for TBM nodes).
  284. *
  285. * ----------------------------------------------------------------
  286. *
  287. * References
  288. * ==========
  289. *
  290. * [1] Will Eatherton, George Varghese, and Zubin Dittia. 2004. Tree
  291. * bitmap: hardware/software IP lookups with incremental
  292. * updates. SIGCOMM Comput. Commun. Rev. 34, 2 (April 2004),
  293. * 97-122. DOI=10.1145/997150.997160
  294. * http://doi.acm.org/10.1145/997150.997160
  295. * http://comnet.kaist.ac.kr/yhlee/CN_2008_Spring/readings/Eath-04-tree_bitmap.pdf
  296. *
  297. ****************************************************************/
  298. #include <stdio.h>
  299. #include <stdlib.h>
  300. #include <string.h>
  301. #include <setjmp.h>
  302. #if defined(TEST) && defined(NDEBUG)
  303. # warning undefining NDEBUG for TEST build
  304. # undef NDEBUG
  305. #endif
  306. #include <assert.h>
  307. #include "btrie.h"
  308. #include "libutil/mem_pool.h"
  309. #ifdef __SIZEOF_POINTER__
  310. #define SIZEOF_VOID_P __SIZEOF_POINTER__
  311. #else
  312. #if defined(__ILP32__) || defined(__ILP32) || defined(_ILP32)
  313. # define SIZEOF_VOID_P 4
  314. #elif defined(__ILP64__) || defined(__ILP64) || defined(_ILP64)
  315. # define SIZEOF_VOID_P 8
  316. #elif defined(__LLP64__) || defined(__LLP64) || defined(_LLP64) || defined(_WIN64)
  317. # define SIZEOF_VOID_P 8
  318. #elif defined(__LP64__) || defined(__LP64) || defined(_LP64)
  319. # define SIZEOF_VOID_P 8
  320. #elif defined(UINTPTR_MAX) && defined(UINT64_MAX) && (UINTPTR_MAX == UINT64_MAX)
  321. # define SIZEOF_VOID_P 8
  322. #else
  323. # define SIZEOF_VOID_P 4
  324. #endif
  325. #endif
  326. #if SIZEOF_VOID_P == 4
  327. # define TBM_STRIDE 4
  328. #elif SIZEOF_VOID_P == 8
  329. # define TBM_STRIDE 5
  330. #else
  331. # error "Unsupported word size"
  332. #endif
  333. #ifndef NO_STDINT_H
  334. # if TBM_STRIDE == 4
  335. typedef uint16_t tbm_bitmap_t;
  336. # else
  337. typedef uint32_t tbm_bitmap_t;
  338. # endif
  339. #else /* NO_STDINT_H */
  340. # if TBM_STRIDE == 4
  341. # if SIZEOF_SHORT == 2
  342. typedef short unsigned tbm_bitmap_t;
  343. # else
  344. # error "can not determine type for 16 bit unsigned int"
  345. # endif
  346. # else /* TBM_STRIDE == 5 */
  347. # if SIZEOF_INT == 4
  348. typedef unsigned tbm_bitmap_t;
  349. # elif SIZEOF_LONG == 4
  350. typedef long unsigned tbm_bitmap_t;
  351. # else
  352. # error "can not determine type for 32 bit unsigned int"
  353. # endif
  354. # endif
  355. #endif
  356. #define TBM_FANOUT (1U << TBM_STRIDE)
  357. #define LC_BYTES_PER_NODE (SIZEOF_VOID_P - 1)
  358. typedef union node_u node_t;
  359. /* The tbm_node and lc_node structs must be packed so that the the
  360. * high bit (LC_FLAGS_IS_LC) of lc_flags in the the lc_node struct
  361. * coincides with bit zero (the most significant bit) of tbm_node's
  362. * int_bm. (This bit is how we differentiate between the two node
  363. * types. It is always clear for a tbm_node and always set for an
  364. * lc_node.)
  365. */
  366. struct tbm_node
  367. {
  368. #ifdef WORDS_BIGENDIAN
  369. tbm_bitmap_t int_bm; /* the internal bitmap */
  370. tbm_bitmap_t ext_bm; /* extending path ("external") bitmap */
  371. #else
  372. tbm_bitmap_t ext_bm; /* extending path ("external") bitmap */
  373. tbm_bitmap_t int_bm; /* the internal bitmap */
  374. #endif
  375. union
  376. {
  377. node_t *children; /* pointer to array of children */
  378. const void **data_end; /* one past end of internal prefix data array */
  379. } ptr;
  380. };
  381. struct lc_node
  382. {
  383. /* lc_flags contains the LC prefix length and a couple of bit flags
  384. * (apparently char-sized bit fields are a gcc extension)
  385. */
  386. # define LC_FLAGS_IS_LC 0x80
  387. # define LC_FLAGS_IS_TERMINAL 0x40
  388. # define LC_FLAGS_LEN_MASK 0x3f
  389. #ifdef WORDS_BIGENDIAN
  390. btrie_oct_t lc_flags;
  391. btrie_oct_t prefix[LC_BYTES_PER_NODE];
  392. #else
  393. btrie_oct_t prefix[LC_BYTES_PER_NODE];
  394. btrie_oct_t lc_flags;
  395. #endif
  396. union
  397. {
  398. node_t *child; /* pointer to child (if !is_terminal) */
  399. const void *data; /* the prefix data (if is_terminal) */
  400. } ptr;
  401. };
  402. union node_u
  403. {
  404. struct tbm_node tbm_node;
  405. struct lc_node lc_node;
  406. };
  407. struct free_hunk
  408. {
  409. struct free_hunk *next;
  410. };
  411. #define MAX_CHILD_ARRAY_LEN (TBM_FANOUT + TBM_FANOUT / 2)
  412. struct btrie
  413. {
  414. node_t root;
  415. rspamd_mempool_t *mp;
  416. struct free_hunk *free_list[MAX_CHILD_ARRAY_LEN];
  417. jmp_buf exception;
  418. /* mem mgmt stats */
  419. size_t alloc_total; /* total bytes allocated from mempool */
  420. size_t alloc_data; /* bytes allocated for TBM node int. prefix data */
  421. size_t alloc_waste; /* bytes wasted by rounding of data array size */
  422. #ifdef BTRIE_DEBUG_ALLOC
  423. size_t alloc_hist[MAX_CHILD_ARRAY_LEN * 2]; /* histogram of alloc sizes */
  424. #endif
  425. /* trie stats */
  426. size_t n_entries; /* number of entries */
  427. size_t n_tbm_nodes; /* total number of TBM nodes in tree */
  428. size_t n_lc_nodes; /* total number of LC nodes in tree */
  429. };
  430. /****************************************************************
  431. *
  432. * Memory management
  433. *
  434. * We will need to frequently resize child/data arrays. The current
  435. * mempool implementation does not support resizing/freeing, so here
  436. * we roll our own.
  437. */
  438. static inline void _free_hunk(struct btrie *btrie, void *buf, unsigned n_nodes)
  439. {
  440. struct free_hunk *hunk = buf;
  441. hunk->next = btrie->free_list[n_nodes - 1];
  442. btrie->free_list[n_nodes - 1] = hunk;
  443. }
  444. static inline void *
  445. _get_hunk(struct btrie *btrie, unsigned n_nodes)
  446. {
  447. struct free_hunk *hunk = btrie->free_list[n_nodes - 1];
  448. if (hunk != NULL)
  449. btrie->free_list[n_nodes - 1] = hunk->next;
  450. return hunk;
  451. }
  452. /* Get pointer to uninitialized child/data array.
  453. *
  454. * Allocates memory for an array of NDATA (void *)s followed by an
  455. * array of NCHILDREN (node_t)s. The returned pointer points to to
  456. * beginning of the children array (i.e. it points to (one past) the
  457. * end of the data array.)
  458. */
  459. static node_t *
  460. alloc_nodes(struct btrie *btrie, unsigned nchildren, unsigned ndata)
  461. {
  462. size_t n_nodes = nchildren + (ndata + 1) / 2;
  463. node_t *hunk;
  464. assert(n_nodes > 0 && n_nodes <= MAX_CHILD_ARRAY_LEN);
  465. hunk = _get_hunk (btrie, n_nodes);
  466. if (hunk == NULL) {
  467. /* Do not have free hunk of exactly the requested size, look for a
  468. * larger hunk. (The funny order in which we scan the buckets is
  469. * heuristically selected in an attempt to minimize unnecessary
  470. * creation of small fragments)
  471. */
  472. size_t n, skip = n_nodes > 4 ? 4 : n_nodes;
  473. for (n = n_nodes + skip; n <= MAX_CHILD_ARRAY_LEN; n++) {
  474. if ((hunk = _get_hunk (btrie, n)) != NULL) {
  475. _free_hunk (btrie, hunk + n_nodes, n - n_nodes);
  476. goto DONE;
  477. }
  478. }
  479. for (n = n_nodes + 1; n < n_nodes + skip && n <= MAX_CHILD_ARRAY_LEN;
  480. n++) {
  481. if ((hunk = _get_hunk (btrie, n)) != NULL) {
  482. _free_hunk (btrie, hunk + n_nodes, n - n_nodes);
  483. goto DONE;
  484. }
  485. }
  486. /* failed to find free hunk, allocate a fresh one */
  487. hunk = rspamd_mempool_alloc0 (btrie->mp, n_nodes * sizeof(node_t));
  488. if (hunk == NULL)
  489. longjmp (btrie->exception, BTRIE_ALLOC_FAILED);
  490. btrie->alloc_total += n_nodes * sizeof(node_t);
  491. }
  492. DONE: btrie->alloc_data += ndata * sizeof(void *);
  493. btrie->alloc_waste += (ndata % 2) * sizeof(void *);
  494. #ifdef BTRIE_DEBUG_ALLOC
  495. btrie->alloc_hist[2 * nchildren + ndata]++;
  496. #endif
  497. /* adjust pointer to allow room for data array before child array */
  498. return hunk + (ndata + 1) / 2;
  499. }
  500. /* Free memory allocated by alloc_nodes */
  501. static void free_nodes(struct btrie *btrie, node_t *buf, unsigned nchildren,
  502. unsigned ndata)
  503. {
  504. size_t n_nodes = nchildren + (ndata + 1) / 2;
  505. assert(n_nodes > 0 && n_nodes <= MAX_CHILD_ARRAY_LEN);
  506. _free_hunk (btrie, buf - (ndata + 1) / 2, n_nodes);
  507. btrie->alloc_data -= ndata * sizeof(void *);
  508. btrie->alloc_waste -= (ndata % 2) * sizeof(void *);
  509. #ifdef BTRIE_DEBUG_ALLOC
  510. btrie->alloc_hist[2 * nchildren + ndata]--;
  511. #endif
  512. }
  513. /* Debugging/development only: */
  514. #ifdef BTRIE_DEBUG_ALLOC
  515. static void
  516. dump_alloc_hist(const struct btrie *btrie)
  517. {
  518. unsigned bin;
  519. size_t total_alloc = 0;
  520. size_t total_free = 0;
  521. size_t total_bytes = 0;
  522. size_t total_waste = 0;
  523. size_t total_free_bytes = 0;
  524. puts("hunk alloc free alloc wasted free");
  525. puts("size hunks hunks bytes bytes bytes");
  526. puts("==== ====== ====== ======== ======== ========");
  527. for (bin = 1; bin < 2 * MAX_CHILD_ARRAY_LEN; bin++) {
  528. size_t n_alloc = btrie->alloc_hist[bin];
  529. size_t bytes = n_alloc * bin * sizeof(void *);
  530. size_t waste_bytes = (bin % 2) * n_alloc * sizeof(void *);
  531. size_t n_free = 0, free_bytes;
  532. if (bin % 2 == 0) {
  533. const struct free_hunk *hunk;
  534. for (hunk = btrie->free_list[bin / 2 - 1]; hunk; hunk = hunk->next)
  535. n_free++;
  536. }
  537. free_bytes = n_free * bin * sizeof(void *);
  538. printf("%3zu: %6zu %6zu %8zu %8zu %8zu\n", bin * sizeof(void *),
  539. n_alloc, n_free, bytes, waste_bytes, free_bytes);
  540. total_alloc += n_alloc;
  541. total_free += n_free;
  542. total_bytes += bytes;
  543. total_waste += waste_bytes;
  544. total_free_bytes += free_bytes;
  545. }
  546. puts("---- ------ ------ -------- -------- --------");
  547. printf("SUM: %6zu %6zu %8zu %8zu %8zu\n",
  548. total_alloc, total_free, total_bytes, total_waste, total_free_bytes);
  549. }
  550. #endif
  551. /****************************************************************
  552. *
  553. * Bit twiddling
  554. *
  555. */
  556. static inline tbm_bitmap_t bit(unsigned b)
  557. {
  558. return 1U << ((1 << TBM_STRIDE) - 1 - b);
  559. }
  560. /* count the number of set bits in bitmap
  561. *
  562. * algorithm from
  563. * http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
  564. */
  565. static inline unsigned count_bits(tbm_bitmap_t v)
  566. {
  567. /* Count set bits in parallel. */
  568. /* v = (v & 0x5555...) + ((v >> 1) & 0x5555...); */
  569. v -= (v >> 1) & (tbm_bitmap_t) ~0UL / 3;
  570. /* v = (v & 0x3333...) + ((v >> 2) & 0x3333...); */
  571. v = (v & (tbm_bitmap_t) ~0UL / 5) + ((v >> 2) & (tbm_bitmap_t) ~0UL / 5);
  572. /* v = (v & 0x0f0f...) + ((v >> 4) & 0x0f0f...); */
  573. v = (v + (v >> 4)) & (tbm_bitmap_t) ~0UL / 17;
  574. /* v = v % 255; */
  575. #if TBM_STRIDE == 4
  576. /* tbm_bitmap_t is uint16_t, avoid the multiply */
  577. return (v + (v >> 8)) & 0x0ff;
  578. #else
  579. return (v * (tbm_bitmap_t) (~0UL / 255)) >> ((sizeof(tbm_bitmap_t) - 1) * 8);
  580. #endif
  581. }
  582. static inline unsigned count_bits_before(tbm_bitmap_t bm, int b)
  583. {
  584. return b ? count_bits (bm >> ((1 << TBM_STRIDE) - b)) : 0;
  585. }
  586. static inline unsigned count_bits_from(tbm_bitmap_t bm, int b)
  587. {
  588. return count_bits (bm << b);
  589. }
  590. /* extracts a few bits from bitstring, returning them as an integer */
  591. static inline btrie_oct_t RSPAMD_NO_SANITIZE extract_bits(const btrie_oct_t *prefix, unsigned pos,
  592. unsigned nbits)
  593. {
  594. if (nbits == 0)
  595. return 0;
  596. else {
  597. unsigned v = (prefix[pos / 8] << 8) + prefix[pos / 8 + 1];
  598. return (v >> (16 - nbits - pos % 8)) & ((1U << nbits) - 1);
  599. }
  600. }
  601. static inline unsigned extract_bit(const btrie_oct_t *prefix, int pos)
  602. {
  603. return (prefix[pos / 8] >> (7 - pos % 8)) & 0x01;
  604. }
  605. /* get mask for high n bits of a byte */
  606. static inline btrie_oct_t high_bits(unsigned n)
  607. {
  608. return (btrie_oct_t) -(1U << (8 - n));
  609. }
  610. /* determine whether two prefixes are equal */
  611. static inline int prefixes_equal(const btrie_oct_t *pfx1,
  612. const btrie_oct_t *pfx2, unsigned len)
  613. {
  614. return (memcmp (pfx1, pfx2, len / 8) == 0
  615. && (len % 8 == 0 ||
  616. ((pfx1[len / 8] ^ pfx2[len / 8]) & high_bits (len % 8)) == 0));
  617. }
  618. /* determine length of longest common subprefix */
  619. static inline unsigned common_prefix(const btrie_oct_t *pfx1,
  620. const btrie_oct_t *pfx2, unsigned len)
  621. {
  622. /* algorithm adapted from
  623. * http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogLookup
  624. */
  625. static btrie_oct_t leading_zeros[] =
  626. { 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  627. 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  628. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1,
  629. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  630. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  631. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  632. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  633. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  634. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  635. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  636. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  637. 0, };
  638. unsigned nb;
  639. for (nb = 0; nb < len / 8; nb++) {
  640. unsigned diff = *pfx1++ ^ *pfx2++;
  641. if (diff != 0)
  642. return 8 * nb + leading_zeros[diff];
  643. }
  644. if (len % 8) {
  645. unsigned n = leading_zeros[*pfx1 ^ *pfx2];
  646. if (n < len % 8)
  647. return 8 * nb + n;
  648. }
  649. return len;
  650. }
  651. /****************************************************************
  652. */
  653. static inline int is_empty_node(const node_t *node)
  654. {
  655. return node->tbm_node.ext_bm == 0 && node->tbm_node.int_bm == 0;
  656. }
  657. static inline int is_lc_node(const node_t *node)
  658. {
  659. return (node->lc_node.lc_flags & LC_FLAGS_IS_LC) != 0;
  660. }
  661. static inline int is_tbm_node(const node_t *node)
  662. {
  663. return !is_lc_node (node);
  664. }
  665. /* is node a TBM node with internal data? */
  666. static inline int has_data(const node_t *node)
  667. {
  668. return is_tbm_node (node) && node->tbm_node.int_bm != 0;
  669. }
  670. static inline unsigned base_index(unsigned pfx, unsigned plen)
  671. {
  672. assert(plen < TBM_STRIDE);
  673. assert(pfx < (1U << plen));
  674. return pfx | (1U << plen);
  675. }
  676. /* initialize node to an empty TBM node */
  677. static inline void init_empty_node(struct btrie *btrie, node_t *node)
  678. {
  679. memset(node, 0, sizeof(*node));
  680. btrie->n_tbm_nodes++;
  681. }
  682. /* get pointer to TBM internal prefix data */
  683. static inline const void **
  684. tbm_data_p(const struct tbm_node *node, unsigned pfx, unsigned plen)
  685. {
  686. unsigned bi = base_index (pfx, plen);
  687. if ((node->int_bm & bit (bi)) == 0)
  688. return NULL; /* no data */
  689. else {
  690. return &node->ptr.data_end[-(int) count_bits_from (node->int_bm, bi)];
  691. }
  692. }
  693. /* add an element to the internal data array */
  694. static void tbm_insert_data(struct btrie *btrie, struct tbm_node *node,
  695. unsigned pfx, unsigned plen, const void *data)
  696. {
  697. /* XXX: don't realloc if already big enough? */
  698. unsigned bi = base_index (pfx, plen);
  699. unsigned nchildren = count_bits (node->ext_bm);
  700. int ndata = count_bits (node->int_bm);
  701. unsigned di = count_bits_before (node->int_bm, bi);
  702. node_t *old_children = node->ptr.children;
  703. const void **old_data_beg = node->ptr.data_end - ndata;
  704. const void **data_beg;
  705. assert((node->int_bm & bit (bi)) == 0);
  706. node->ptr.children = alloc_nodes (btrie, nchildren, ndata + 1);
  707. data_beg = node->ptr.data_end - (ndata + 1);
  708. data_beg[di] = data;
  709. node->int_bm |= bit (bi);
  710. if (nchildren != 0 || ndata != 0) {
  711. memcpy(data_beg, old_data_beg, di * sizeof(data_beg[0]));
  712. memcpy(&data_beg[di + 1], &old_data_beg[di],
  713. (ndata - di) * sizeof(data_beg[0])
  714. + nchildren * sizeof(node_t));
  715. free_nodes (btrie, old_children, nchildren, ndata);
  716. }
  717. }
  718. /* determine whether TBM has internal prefix data for pfx/plen or ancestors */
  719. static inline int has_internal_data(const struct tbm_node *node, unsigned pfx,
  720. unsigned plen)
  721. {
  722. # define BIT(n) (1U << ((1 << TBM_STRIDE) - 1 - (n)))
  723. # define B0() BIT(1) /* the bit for 0/0 */
  724. # define B1(n) (BIT((n) + 2) | B0()) /* the bits for n/1 and its ancestors */
  725. # define B2(n) (BIT((n) + 4) | B1(n >> 1)) /* the bits for n/2 and ancestors */
  726. # define B3(n) (BIT((n) + 8) | B2(n >> 1)) /* the bits for n/3 and ancestors */
  727. # define B4(n) (BIT((n) + 16) | B3(n >> 1)) /* the bits for n/4 and ancestors */
  728. static tbm_bitmap_t ancestors[] =
  729. { 0, B0(), B1(0), B1(1), B2(0), B2(1), B2(2), B2(3), B3(0), B3(1), B3(2),
  730. B3(3), B3(4), B3(5), B3(6), B3(7),
  731. # if TBM_STRIDE == 5
  732. B4(0), B4(1), B4(2), B4(3), B4(4), B4(5), B4(6), B4(7), B4(8), B4(
  733. 9), B4(10), B4(11), B4(12), B4(13), B4(14), B4(15),
  734. # elif TBM_STRIDE != 4
  735. # error "unsupported TBM_STRIDE"
  736. # endif
  737. };
  738. # undef B4
  739. # undef B3
  740. # undef B2
  741. # undef B1
  742. # undef B0
  743. # undef BIT
  744. return (node->int_bm & ancestors[base_index (pfx, plen)]) != 0;
  745. }
  746. /* get pointer to TBM extending path */
  747. static inline node_t *
  748. tbm_ext_path(const struct tbm_node *node, unsigned pfx)
  749. {
  750. if ((node->ext_bm & bit (pfx)) == 0)
  751. return NULL;
  752. else
  753. return &node->ptr.children[count_bits_before (node->ext_bm, pfx)];
  754. }
  755. /* resize TBM node child array to make space for new child node */
  756. static node_t *
  757. tbm_insert_ext_path(struct btrie *btrie, struct tbm_node *node, unsigned pfx)
  758. {
  759. unsigned nchildren = count_bits (node->ext_bm);
  760. unsigned ci = count_bits_before (node->ext_bm, pfx);
  761. int ndata = count_bits (node->int_bm);
  762. node_t *old_children = node->ptr.children;
  763. const void **old_data_beg = node->ptr.data_end - ndata;
  764. assert((node->ext_bm & bit (pfx)) == 0);
  765. node->ptr.children = alloc_nodes (btrie, nchildren + 1, ndata);
  766. init_empty_node (btrie, &node->ptr.children[ci]);
  767. node->ext_bm |= bit (pfx);
  768. if (nchildren != 0 || ndata != 0) {
  769. const void **data_beg = node->ptr.data_end - ndata;
  770. memcpy(data_beg, old_data_beg,
  771. ndata * sizeof(data_beg[0]) + ci * sizeof(node_t));
  772. memcpy(&node->ptr.children[ci + 1], &old_children[ci],
  773. (nchildren - ci) * sizeof(old_children[0]));
  774. free_nodes (btrie, old_children, nchildren, ndata);
  775. }
  776. return &node->ptr.children[ci];
  777. }
  778. static inline int lc_is_terminal(const struct lc_node *node)
  779. {
  780. return (node->lc_flags & LC_FLAGS_IS_TERMINAL) != 0;
  781. }
  782. static inline unsigned lc_len(const struct lc_node *node)
  783. {
  784. return node->lc_flags & LC_FLAGS_LEN_MASK;
  785. }
  786. static inline void lc_init_flags(struct lc_node *node, int is_terminal,
  787. unsigned len)
  788. {
  789. assert((len & ~LC_FLAGS_LEN_MASK) == 0);
  790. node->lc_flags = LC_FLAGS_IS_LC | len;
  791. if (is_terminal)
  792. node->lc_flags |= LC_FLAGS_IS_TERMINAL;
  793. }
  794. static inline void lc_add_to_len(struct lc_node *node, int increment)
  795. {
  796. unsigned new_len = lc_len (node) + increment;
  797. assert((new_len & ~LC_FLAGS_LEN_MASK) == 0);
  798. node->lc_flags = (node->lc_flags & ~LC_FLAGS_LEN_MASK) | new_len;
  799. }
  800. static inline unsigned lc_shift(unsigned pos)
  801. {
  802. return pos / 8;
  803. }
  804. static inline unsigned lc_base(unsigned pos)
  805. {
  806. return 8 * lc_shift (pos);
  807. }
  808. static inline unsigned lc_bits(const struct lc_node *node, unsigned pos)
  809. {
  810. return pos % 8 + lc_len (node);
  811. }
  812. static inline unsigned lc_bytes(const struct lc_node *node, unsigned pos)
  813. {
  814. return (lc_bits (node, pos) + 7) / 8;
  815. }
  816. static inline unsigned lc_leading_bits(const struct lc_node *node, unsigned pos,
  817. unsigned nbits)
  818. {
  819. return extract_bits (node->prefix, pos % 8, nbits);
  820. }
  821. /* Initialize a new terminal LC node
  822. *
  823. * If prefix is too long to fit in a single LC node, then a chain
  824. * of LC nodes will be created.
  825. */
  826. static void init_terminal_node(struct btrie *btrie, node_t *dst, unsigned pos,
  827. const btrie_oct_t *prefix, unsigned len, const void *data)
  828. {
  829. struct lc_node *node = &dst->lc_node;
  830. unsigned nbytes = (len + 7) / 8;
  831. while (nbytes - lc_shift (pos) > LC_BYTES_PER_NODE) {
  832. memcpy(node->prefix, prefix + lc_shift (pos), LC_BYTES_PER_NODE);
  833. lc_init_flags (node, 0, 8 * LC_BYTES_PER_NODE - pos % 8);
  834. node->ptr.child = alloc_nodes (btrie, 1, 0);
  835. pos += lc_len (node);
  836. node = &node->ptr.child->lc_node;
  837. btrie->n_lc_nodes++;
  838. }
  839. memcpy(node->prefix, prefix + lc_shift (pos), nbytes - lc_shift (pos));
  840. lc_init_flags (node, 1, len - pos);
  841. node->ptr.data = data;
  842. btrie->n_lc_nodes++;
  843. }
  844. /* merge chains of multiple LC nodes into a single LC node, if possible.
  845. *
  846. * also ensure that the leading nodes in the LC chain have maximum length.
  847. */
  848. static void coalesce_lc_node(struct btrie *btrie, struct lc_node *node,
  849. unsigned pos)
  850. {
  851. while (!lc_is_terminal (node) && lc_bits (node, pos) < 8 * LC_BYTES_PER_NODE
  852. && is_lc_node (node->ptr.child)) {
  853. struct lc_node *child = &node->ptr.child->lc_node;
  854. unsigned spare_bits = 8 * LC_BYTES_PER_NODE - lc_bits (node, pos);
  855. unsigned end = pos + lc_len (node);
  856. unsigned shift = lc_shift (end) - lc_shift (pos);
  857. if (lc_len (child) <= spare_bits) {
  858. /* node plus child will fit in single node - merge */
  859. memcpy(node->prefix + shift, child->prefix, lc_bytes (child, end));
  860. lc_init_flags (node, lc_is_terminal (child),
  861. lc_len (node) + lc_len (child));
  862. node->ptr = child->ptr;
  863. free_nodes (btrie, (node_t *) child, 1, 0);
  864. btrie->n_lc_nodes--;
  865. }
  866. else {
  867. /* can't merge, but can take some of children bits */
  868. unsigned cshift = lc_shift (end + spare_bits) - lc_shift (end);
  869. memcpy(node->prefix + shift, child->prefix,
  870. LC_BYTES_PER_NODE - shift);
  871. lc_add_to_len (node, spare_bits);
  872. if (cshift)
  873. memmove(child->prefix, child->prefix + cshift,
  874. lc_bytes (child, end) - cshift);
  875. assert(lc_len (child) > spare_bits);
  876. lc_add_to_len (child, -spare_bits);
  877. pos += lc_len (node);
  878. node = child;
  879. }
  880. }
  881. }
  882. static void init_tbm_node(struct btrie *btrie, node_t *node, unsigned pos,
  883. const btrie_oct_t pbyte, const void **root_data_p, node_t *left,
  884. node_t *right);
  885. /* given an LC node at orig_pos, create a new (shorter) node at pos */
  886. static void shorten_lc_node(struct btrie *btrie, node_t *dst, unsigned pos,
  887. struct lc_node *src, unsigned orig_pos)
  888. {
  889. assert(orig_pos < pos);
  890. assert(lc_len (src) >= pos - orig_pos);
  891. assert(dst != (node_t * )src);
  892. if (lc_len (src) == pos - orig_pos && !lc_is_terminal (src)) {
  893. /* just steal the child */
  894. node_t *child = src->ptr.child;
  895. *dst = *child;
  896. free_nodes (btrie, child, 1, 0);
  897. btrie->n_lc_nodes--;
  898. }
  899. else {
  900. struct lc_node *node = &dst->lc_node;
  901. unsigned shift = lc_shift (pos) - lc_shift (orig_pos);
  902. if (shift) {
  903. memmove(node->prefix, src->prefix + shift,
  904. lc_bytes (src, orig_pos) - shift);
  905. node->lc_flags = src->lc_flags;
  906. node->ptr = src->ptr;
  907. }
  908. else {
  909. *node = *src;
  910. }
  911. lc_add_to_len (node, -(pos - orig_pos));
  912. coalesce_lc_node (btrie, node, pos);
  913. }
  914. }
  915. /* convert LC node to non-terminal LC node of length len *in place*
  916. *
  917. * on entry, node must have length at least len
  918. */
  919. static void split_lc_node(struct btrie *btrie, struct lc_node *node,
  920. unsigned pos, unsigned len)
  921. {
  922. node_t *child = alloc_nodes (btrie, 1, 0);
  923. assert(lc_len (node) >= len);
  924. shorten_lc_node (btrie, child, pos + len, node, pos);
  925. lc_init_flags (node, 0, len);
  926. node->ptr.child = child;
  927. btrie->n_lc_nodes++;
  928. }
  929. /* convert non-terminal LC node of length one to a TBM node *in place* */
  930. static void convert_lc_node_1(struct btrie *btrie, struct lc_node *node,
  931. unsigned pos)
  932. {
  933. btrie_oct_t pbyte = node->prefix[0];
  934. node_t *child = node->ptr.child;
  935. node_t *left, *right;
  936. assert(lc_len (node) == 1);
  937. assert(!lc_is_terminal (node));
  938. if (extract_bit (node->prefix, pos % 8))
  939. left = NULL, right = child;
  940. else
  941. left = child, right = NULL;
  942. init_tbm_node (btrie, (node_t *) node, pos, pbyte, NULL, left, right);
  943. free_nodes (btrie, child, 1, 0);
  944. btrie->n_lc_nodes--;
  945. }
  946. /* convert an LC node to TBM node *in place* */
  947. static void convert_lc_node(struct btrie *btrie, struct lc_node *node,
  948. unsigned pos)
  949. {
  950. unsigned len = lc_len (node);
  951. if (len >= TBM_STRIDE) {
  952. unsigned pfx = lc_leading_bits (node, pos, TBM_STRIDE);
  953. struct tbm_node *result = (struct tbm_node *) node;
  954. /* split to LC of len TBM_STRIDE followed by child (extending path) */
  955. split_lc_node (btrie, node, pos, TBM_STRIDE);
  956. /* then convert leading LC node to TBM node */
  957. result->int_bm = 0;
  958. result->ext_bm = bit (pfx);
  959. btrie->n_lc_nodes--;
  960. btrie->n_tbm_nodes++;
  961. }
  962. else if (lc_is_terminal (node)) {
  963. /* convert short terminal LC to TBM (with internal data) */
  964. unsigned pfx = lc_leading_bits (node, pos, len);
  965. const void *data = node->ptr.data;
  966. node_t *result = (node_t *) node;
  967. init_empty_node (btrie, result);
  968. tbm_insert_data (btrie, &result->tbm_node, pfx, len, data);
  969. btrie->n_lc_nodes--;
  970. }
  971. else {
  972. assert(len > 0);
  973. for (; len > 1; len--) {
  974. split_lc_node (btrie, node, pos, len - 1);
  975. convert_lc_node_1 (btrie, &node->ptr.child->lc_node, pos + len - 1);
  976. }
  977. convert_lc_node_1 (btrie, node, pos);
  978. }
  979. }
  980. static void insert_lc_node(struct btrie *btrie, node_t *dst, unsigned pos,
  981. btrie_oct_t pbyte, unsigned last_bit, node_t *tail)
  982. {
  983. struct lc_node *node = &dst->lc_node;
  984. btrie_oct_t mask = 1 << (7 - (pos % 8));
  985. btrie_oct_t bit = last_bit ? mask : 0;
  986. if (mask != 0x01 && is_lc_node (tail)) {
  987. /* optimization: LC tail has room for the extra bit (without shifting) */
  988. assert((tail->lc_node.prefix[0] & mask) == bit);
  989. *node = tail->lc_node;
  990. lc_add_to_len (node, 1);
  991. return;
  992. }
  993. /* add new leading LC node of len 1 */
  994. node->prefix[0] = pbyte | bit;
  995. lc_init_flags (node, 0, 1);
  996. node->ptr.child = alloc_nodes (btrie, 1, 0);
  997. node->ptr.child[0] = *tail;
  998. btrie->n_lc_nodes++;
  999. if (is_lc_node (tail))
  1000. coalesce_lc_node (btrie, node, pos);
  1001. }
  1002. /* given:
  1003. * pbyte: the bits in the prefix between lc_base(pos) and pos
  1004. * pfx: the next TBM_STRIDE bits in the prefix starting at pos
  1005. * returns:
  1006. * the bits in the prefix between lc_base(pos + plen) and pos + plen
  1007. */
  1008. static inline btrie_oct_t next_pbyte(btrie_oct_t pbyte, unsigned pos,
  1009. unsigned pfx)
  1010. {
  1011. unsigned end = pos + TBM_STRIDE;
  1012. if (end % 8 != 0) {
  1013. btrie_oct_t nbyte = (btrie_oct_t) pfx << (8 - end % 8);
  1014. if (end % 8 > TBM_STRIDE)
  1015. nbyte |= pbyte & high_bits (pos % 8);
  1016. return nbyte;
  1017. }
  1018. return 0;
  1019. }
  1020. /* construct a new TBM node, given the data and children of the
  1021. * root prefix of the new node.
  1022. */
  1023. static void init_tbm_node(struct btrie *btrie, node_t *dst, unsigned pos,
  1024. const btrie_oct_t pbyte, const void **root_data_p, node_t *left,
  1025. node_t *right)
  1026. {
  1027. struct tbm_node *node = &dst->tbm_node;
  1028. unsigned nchildren = 0;
  1029. unsigned ndata = 0;
  1030. node_t children[TBM_FANOUT];
  1031. const void *data[TBM_FANOUT - 1];
  1032. tbm_bitmap_t ext_bm = 0;
  1033. tbm_bitmap_t int_bm = 0;
  1034. unsigned i, d, pfx_base;
  1035. if (left && is_lc_node (left) && lc_len (&left->lc_node) < TBM_STRIDE)
  1036. convert_lc_node (btrie, &left->lc_node, pos + 1);
  1037. if (right && is_lc_node (right) && lc_len (&right->lc_node) < TBM_STRIDE)
  1038. convert_lc_node (btrie, &right->lc_node, pos + 1);
  1039. /* set internal data for root prefix */
  1040. if (root_data_p) {
  1041. data[ndata++] = *root_data_p;
  1042. int_bm |= bit (base_index (0, 0));
  1043. }
  1044. /* copy internal data from children */
  1045. for (d = 0; d < TBM_STRIDE - 1; d++) {
  1046. if (left && has_data (left)) {
  1047. for (i = 0; i < 1U << d; i++) {
  1048. const void **data_p = tbm_data_p (&left->tbm_node, i, d);
  1049. if (data_p) {
  1050. data[ndata++] = *data_p;
  1051. int_bm |= bit (base_index (i, d + 1));
  1052. }
  1053. }
  1054. }
  1055. if (right && has_data (right)) {
  1056. for (i = 0; i < 1U << d; i++) {
  1057. const void **data_p = tbm_data_p (&right->tbm_node, i, d);
  1058. if (data_p) {
  1059. data[ndata++] = *data_p;
  1060. int_bm |= bit (base_index (i + (1 << d), d + 1));
  1061. }
  1062. }
  1063. }
  1064. }
  1065. /* copy extending paths */
  1066. for (pfx_base = 0; pfx_base < TBM_FANOUT; pfx_base += TBM_FANOUT / 2) {
  1067. node_t *child = pfx_base ? right : left;
  1068. if (child == NULL) {
  1069. continue;
  1070. }
  1071. else if (is_lc_node (child)) {
  1072. unsigned pfx = pfx_base + lc_leading_bits (&child->lc_node, pos + 1,
  1073. TBM_STRIDE - 1);
  1074. /* child is LC node, just shorten it by TBM_STRIDE - 1 */
  1075. shorten_lc_node (btrie, &children[nchildren++], pos + TBM_STRIDE,
  1076. &child->lc_node, pos + 1);
  1077. ext_bm |= bit (pfx);
  1078. }
  1079. else if (!is_empty_node (child)) {
  1080. /* convert deepest internal prefixes of child to extending paths
  1081. * of the new node
  1082. */
  1083. for (i = 0; i < TBM_FANOUT / 2; i++) {
  1084. const void **data_p = tbm_data_p (&child->tbm_node, i,
  1085. TBM_STRIDE - 1);
  1086. node_t *left_ext = tbm_ext_path (&child->tbm_node, 2 * i);
  1087. node_t *right_ext = tbm_ext_path (&child->tbm_node, 2 * i + 1);
  1088. if (data_p || left_ext || right_ext) {
  1089. node_t *ext_path = &children[nchildren++];
  1090. unsigned pfx = pfx_base + i;
  1091. btrie_oct_t npbyte = next_pbyte (pbyte, pos, pfx);
  1092. ext_bm |= bit (pfx);
  1093. if (left_ext == NULL && right_ext == NULL) {
  1094. /* only have data - set ext_path to zero-length terminal LC node */
  1095. lc_init_flags (&ext_path->lc_node, 1, 0);
  1096. ext_path->lc_node.prefix[0] = npbyte;
  1097. ext_path->lc_node.ptr.data = *data_p;
  1098. btrie->n_lc_nodes++;
  1099. }
  1100. else if (data_p || (left_ext && right_ext)) {
  1101. /* have at least two of data, left_ext, right_ext
  1102. * ext_path must be a full TBM node */
  1103. init_tbm_node (btrie, ext_path, pos + TBM_STRIDE,
  1104. npbyte, data_p, left_ext, right_ext);
  1105. }
  1106. else if (left_ext) {
  1107. /* have only left_ext, insert length-one LC node */
  1108. insert_lc_node (btrie, ext_path, pos + TBM_STRIDE,
  1109. npbyte, 0, left_ext);
  1110. }
  1111. else {
  1112. /* have only right_ext, insert length-one LC node */
  1113. insert_lc_node (btrie, ext_path, pos + TBM_STRIDE,
  1114. npbyte, 1, right_ext);
  1115. }
  1116. }
  1117. }
  1118. btrie->n_tbm_nodes--;
  1119. free_nodes (btrie, child->tbm_node.ptr.children,
  1120. count_bits (child->tbm_node.ext_bm),
  1121. count_bits (child->tbm_node.int_bm));
  1122. }
  1123. }
  1124. assert(count_bits (int_bm) == ndata);
  1125. assert(count_bits (ext_bm) == nchildren);
  1126. node->ptr.children = alloc_nodes (btrie, nchildren, ndata);
  1127. memcpy(node->ptr.data_end - (int )ndata, data, ndata * sizeof(data[0]));
  1128. memcpy(node->ptr.children, children, nchildren * sizeof(children[0]));
  1129. node->ext_bm = ext_bm;
  1130. node->int_bm = int_bm;
  1131. btrie->n_tbm_nodes++;
  1132. }
  1133. static enum btrie_result add_to_trie(struct btrie *btrie, node_t *node,
  1134. unsigned pos, const btrie_oct_t *prefix, unsigned len, const void *data)
  1135. {
  1136. for (;;) {
  1137. if (is_lc_node (node)) {
  1138. struct lc_node *lc_node = &node->lc_node;
  1139. unsigned end = pos + lc_len (lc_node);
  1140. unsigned cbits = common_prefix (prefix + lc_shift (pos),
  1141. lc_node->prefix, (len < end ? len : end) - lc_base (pos));
  1142. unsigned clen = lc_base (pos) + cbits; /* position of first mismatch */
  1143. if (clen == end && !lc_is_terminal (lc_node)) {
  1144. /* matched entire prefix of LC node, proceed to child */
  1145. assert(lc_len (lc_node) > 0);
  1146. node = lc_node->ptr.child;
  1147. pos = end;
  1148. }
  1149. else if (clen == end && len == end && lc_is_terminal (lc_node)) {
  1150. /* exact match for terminal node - already have data for prefix */
  1151. return BTRIE_DUPLICATE_PREFIX;
  1152. }
  1153. else {
  1154. assert(clen < end || (lc_is_terminal (lc_node) && len > end));
  1155. /* Need to insert new TBM node at clen */
  1156. if (clen > pos) {
  1157. split_lc_node (btrie, lc_node, pos, clen - pos);
  1158. node = lc_node->ptr.child;
  1159. assert(is_lc_node (node));
  1160. pos = clen;
  1161. }
  1162. convert_lc_node (btrie, &node->lc_node, pos);
  1163. }
  1164. }
  1165. else if (is_empty_node (node)) {
  1166. /* at empty TBM node - just replace with terminal LC node */
  1167. init_terminal_node (btrie, node, pos, prefix, len, data);
  1168. btrie->n_entries++;
  1169. btrie->n_tbm_nodes--;
  1170. return BTRIE_OKAY;
  1171. }
  1172. else {
  1173. struct tbm_node *tbm_node = &node->tbm_node;
  1174. unsigned end = pos + TBM_STRIDE;
  1175. if (len < end) {
  1176. unsigned plen = len - pos;
  1177. unsigned pfx = extract_bits (prefix, pos, plen);
  1178. if (tbm_data_p (tbm_node, pfx, plen) != NULL)
  1179. return BTRIE_DUPLICATE_PREFIX; /* prefix already has data */
  1180. else {
  1181. tbm_insert_data (btrie, tbm_node, pfx, plen, data);
  1182. btrie->n_entries++;
  1183. return BTRIE_OKAY;
  1184. }
  1185. }
  1186. else {
  1187. unsigned pfx = extract_bits (prefix, pos, TBM_STRIDE);
  1188. /* follow extending path */
  1189. node = tbm_ext_path (tbm_node, pfx);
  1190. if (node == NULL)
  1191. node = tbm_insert_ext_path (btrie, tbm_node, pfx);
  1192. pos = end;
  1193. }
  1194. }
  1195. }
  1196. }
  1197. static const void *
  1198. search_trie(const node_t *node, unsigned pos, const btrie_oct_t *prefix,
  1199. unsigned len)
  1200. {
  1201. /* remember last TBM node seen with internal data */
  1202. const struct tbm_node *int_node = 0;
  1203. unsigned int_pfx = 0, int_plen = 0;
  1204. while (node) {
  1205. if (is_lc_node (node)) {
  1206. const struct lc_node *lc_node = &node->lc_node;
  1207. unsigned end = pos + lc_len (lc_node);
  1208. if (len < end)
  1209. break;
  1210. if (!prefixes_equal (prefix + lc_shift (pos), lc_node->prefix,
  1211. end - lc_base (pos)))
  1212. break;
  1213. if (lc_is_terminal (lc_node))
  1214. return lc_node->ptr.data; /* found terminal node */
  1215. pos = end;
  1216. node = lc_node->ptr.child;
  1217. }
  1218. else {
  1219. const struct tbm_node *tbm_node = &node->tbm_node;
  1220. unsigned end = pos + TBM_STRIDE;
  1221. if (len < end) {
  1222. unsigned plen = len - pos;
  1223. unsigned pfx = extract_bits (prefix, pos, plen);
  1224. if (has_internal_data (tbm_node, pfx, plen)) {
  1225. int_node = tbm_node;
  1226. int_pfx = pfx;
  1227. int_plen = plen;
  1228. }
  1229. break;
  1230. }
  1231. else {
  1232. unsigned pfx = extract_bits (prefix, pos, TBM_STRIDE);
  1233. if (has_internal_data (tbm_node, pfx >> 1, TBM_STRIDE - 1)) {
  1234. int_node = tbm_node;
  1235. int_pfx = pfx >> 1;
  1236. int_plen = TBM_STRIDE - 1;
  1237. }
  1238. pos = end;
  1239. node = tbm_ext_path (tbm_node, pfx);
  1240. }
  1241. }
  1242. }
  1243. if (int_node) {
  1244. const void **data_p = tbm_data_p (int_node, int_pfx, int_plen);
  1245. while (data_p == NULL) {
  1246. assert(int_plen > 0);
  1247. int_pfx >>= 1;
  1248. int_plen--;
  1249. data_p = tbm_data_p (int_node, int_pfx, int_plen);
  1250. }
  1251. return *data_p;
  1252. }
  1253. return NULL;
  1254. }
  1255. struct btrie *
  1256. btrie_init(rspamd_mempool_t *mp)
  1257. {
  1258. struct btrie *btrie;
  1259. if (!(btrie = rspamd_mempool_alloc0 (mp, sizeof(*btrie)))) {
  1260. return NULL;
  1261. }
  1262. btrie->mp = mp;
  1263. btrie->alloc_total = sizeof(*btrie);
  1264. /* count the empty root node */
  1265. btrie->n_tbm_nodes = 1;
  1266. return btrie;
  1267. }
  1268. enum btrie_result btrie_add_prefix(struct btrie *btrie,
  1269. const btrie_oct_t *prefix, unsigned len, const void *data)
  1270. {
  1271. enum btrie_result rv;
  1272. if ((rv = setjmp (btrie->exception)) != 0)
  1273. return rv; /* out of memory */
  1274. return add_to_trie (btrie, &btrie->root, 0, prefix, len, data);
  1275. }
  1276. const void *
  1277. btrie_lookup(const struct btrie *btrie, const btrie_oct_t *prefix, unsigned len)
  1278. {
  1279. return search_trie (&btrie->root, 0, prefix, len);
  1280. }
  1281. /****************************************************************
  1282. *
  1283. * btrie_stats() - statistics reporting
  1284. */
  1285. #ifdef BTRIE_EXTENDED_STATS
  1286. /* Define BTRIE_EXTENDED_STATS to get extra statistics (including
  1287. * trie depth). This statistics require a traversal of the entire trie
  1288. * to compute, and so are disabled by default.
  1289. */
  1290. struct stats {
  1291. size_t max_depth;
  1292. size_t total_depth;
  1293. #ifndef NDEBUG
  1294. size_t n_lc_nodes;
  1295. size_t n_tbm_nodes;
  1296. size_t n_entries;
  1297. size_t alloc_data;
  1298. size_t alloc_waste;
  1299. #endif
  1300. };
  1301. static void
  1302. node_stats(const node_t *node, size_t depth, struct stats *stats)
  1303. {
  1304. if (depth > stats->max_depth)
  1305. stats->max_depth = depth;
  1306. stats->total_depth += depth;
  1307. if (is_lc_node(node)) {
  1308. #ifndef NDEBUG
  1309. stats->n_lc_nodes++;
  1310. #endif
  1311. if (!lc_is_terminal(&node->lc_node))
  1312. node_stats(node->lc_node.ptr.child, depth + 1, stats);
  1313. #ifndef NDEBUG
  1314. else
  1315. stats->n_entries++;
  1316. #endif
  1317. }
  1318. else {
  1319. unsigned i;
  1320. unsigned nchildren = count_bits(node->tbm_node.ext_bm);
  1321. #ifndef NDEBUG
  1322. unsigned ndata = count_bits(node->tbm_node.int_bm);
  1323. stats->n_tbm_nodes++;
  1324. stats->n_entries += ndata;
  1325. stats->alloc_data += ndata * sizeof(void *);
  1326. stats->alloc_waste += (ndata % 2) * sizeof(void *);
  1327. #endif
  1328. for (i = 0; i < nchildren; i++)
  1329. node_stats(&node->tbm_node.ptr.children[i], depth + 1, stats);
  1330. }
  1331. }
  1332. #endif /* BTRIE_EXTENDED_STATS */
  1333. #ifndef NDEBUG
  1334. static size_t count_free(const struct btrie *btrie)
  1335. {
  1336. size_t total = 0;
  1337. unsigned sz;
  1338. for (sz = 1; sz <= MAX_CHILD_ARRAY_LEN; sz++) {
  1339. const struct free_hunk *free = btrie->free_list[sz - 1];
  1340. size_t n;
  1341. for (n = 0; free; n++)
  1342. free = free->next;
  1343. total += sz * n;
  1344. }
  1345. return total * sizeof(node_t);
  1346. }
  1347. #endif /* not NDEBUG */
  1348. const char *
  1349. btrie_stats(const struct btrie *btrie, guint duplicates)
  1350. {
  1351. static char buf[128];
  1352. size_t n_nodes = btrie->n_lc_nodes + btrie->n_tbm_nodes;
  1353. size_t alloc_free = (btrie->alloc_total + sizeof(node_t) /* do not double-count the root node */
  1354. - n_nodes * sizeof(node_t) - btrie->alloc_data - btrie->alloc_waste
  1355. - sizeof(*btrie));
  1356. #ifdef BTRIE_EXTENDED_STATS
  1357. struct stats stats;
  1358. double average_depth;
  1359. memset(&stats, 0, sizeof(stats));
  1360. node_stats(&btrie->root, 0, &stats);
  1361. average_depth = (double)stats.total_depth / n_nodes;
  1362. #ifndef NDEBUG
  1363. /* check the node counts */
  1364. assert(stats.n_lc_nodes == btrie->n_lc_nodes);
  1365. assert(stats.n_tbm_nodes == btrie->n_tbm_nodes);
  1366. assert(stats.n_entries == btrie->n_entries);
  1367. assert(stats.alloc_data == btrie->alloc_data);
  1368. assert(stats.alloc_waste == btrie->alloc_waste);
  1369. #endif /* not NDEBUG */
  1370. #endif /* BTRIE_EXTENDED_STATS */
  1371. #ifndef NDEBUG
  1372. /* check that we haven't lost any memory */
  1373. assert(alloc_free == count_free (btrie));
  1374. #endif
  1375. #ifdef BTRIE_DEBUG_ALLOC
  1376. dump_alloc_hist(btrie);
  1377. #endif
  1378. #ifdef BTRIE_EXTENDED_STATS
  1379. snprintf(buf, sizeof(buf),
  1380. "ents=%lu tbm=%lu lc=%lu mem=%.0fk free=%lu waste=%lu"
  1381. " depth=%.1f/%lu"
  1382. ,(long unsigned)btrie->n_entries, (long unsigned)btrie->n_tbm_nodes,
  1383. (long unsigned)btrie->n_lc_nodes, (double)btrie->alloc_total / 1024,
  1384. (long unsigned)alloc_free, (long unsigned)btrie->alloc_waste
  1385. , average_depth, (long unsigned)stats.max_depth);
  1386. #else
  1387. snprintf(buf, sizeof(buf),
  1388. "ents=%lu dup=%u tbm=%lu lc=%lu mem=%.0fk free=%lu waste=%lu",
  1389. (long unsigned)btrie->n_entries,
  1390. duplicates,
  1391. (long unsigned)btrie->n_tbm_nodes,
  1392. (long unsigned)btrie->n_lc_nodes, (double)btrie->alloc_total / 1024,
  1393. (long unsigned)alloc_free, (long unsigned)btrie->alloc_waste
  1394. );
  1395. #endif
  1396. buf[sizeof(buf) - 1] = '\0';
  1397. return buf;
  1398. }
  1399. /****************************************************************/
  1400. #ifndef NO_MASTER_DUMP
  1401. struct walk_context
  1402. {
  1403. btrie_walk_cb_t *callback;
  1404. void *user_data;
  1405. btrie_oct_t prefix[(BTRIE_MAX_PREFIX + 7) / 8];
  1406. };
  1407. static void
  1408. walk_node(const node_t *node, unsigned pos, struct walk_context *ctx);
  1409. static void walk_tbm_node(const struct tbm_node *node, unsigned pos,
  1410. unsigned pfx, unsigned plen, struct walk_context *ctx)
  1411. {
  1412. btrie_oct_t *prefix = ctx->prefix;
  1413. int pbyte = pos / 8;
  1414. btrie_oct_t pbit = 0x80 >> (pos % 8);
  1415. const void **data_p = tbm_data_p (node, pfx, plen);
  1416. if (pos >= BTRIE_MAX_PREFIX) {
  1417. /* This can/should not happen, but don't overwrite buffers if it does. */
  1418. return;
  1419. }
  1420. if (data_p)
  1421. ctx->callback (prefix, pos, *data_p, 0, ctx->user_data);
  1422. /* walk children */
  1423. if (plen < TBM_STRIDE - 1) {
  1424. /* children are internal prefixes in same node */
  1425. walk_tbm_node (node, pos + 1, pfx << 1, plen + 1, ctx);
  1426. prefix[pbyte] |= pbit;
  1427. walk_tbm_node (node, pos + 1, (pfx << 1) + 1, plen + 1, ctx);
  1428. prefix[pbyte] &= ~pbit;
  1429. }
  1430. else {
  1431. /* children are extending paths */
  1432. const node_t *ext_path;
  1433. if ((ext_path = tbm_ext_path (node, pfx << 1)) != NULL)
  1434. walk_node (ext_path, pos + 1, ctx);
  1435. if ((ext_path = tbm_ext_path (node, (pfx << 1) + 1)) != NULL) {
  1436. prefix[pbyte] |= pbit;
  1437. walk_node (ext_path, pos + 1, ctx);
  1438. prefix[pbyte] &= ~pbit;
  1439. }
  1440. }
  1441. if (data_p)
  1442. ctx->callback (prefix, pos, *data_p, 1, ctx->user_data);
  1443. }
  1444. static void walk_lc_node(const struct lc_node *node, unsigned pos,
  1445. struct walk_context *ctx)
  1446. {
  1447. btrie_oct_t *prefix = ctx->prefix;
  1448. unsigned end = pos + lc_len (node);
  1449. btrie_oct_t save_prefix = prefix[lc_shift (pos)];
  1450. if (end > BTRIE_MAX_PREFIX) {
  1451. /* This can/should not happen, but don't overwrite buffers if it does. */
  1452. return;
  1453. }
  1454. /* construct full prefix to node */
  1455. memcpy(&prefix[lc_shift (pos)], node->prefix, lc_bytes (node, pos));
  1456. if (end % 8)
  1457. prefix[end / 8] &= high_bits (end % 8);
  1458. if (lc_is_terminal (node)) {
  1459. ctx->callback (prefix, end, node->ptr.data, 0, ctx->user_data);
  1460. ctx->callback (prefix, end, node->ptr.data, 1, ctx->user_data);
  1461. }
  1462. else
  1463. walk_node (node->ptr.child, end, ctx);
  1464. prefix[lc_shift (pos)] = save_prefix; /* restore parents prefix */
  1465. if (lc_bytes (node, pos) > 1)
  1466. memset(&prefix[lc_shift (pos) + 1], 0, lc_bytes (node, pos) - 1);
  1467. }
  1468. static void walk_node(const node_t *node, unsigned pos,
  1469. struct walk_context *ctx)
  1470. {
  1471. if (is_lc_node (node))
  1472. walk_lc_node (&node->lc_node, pos, ctx);
  1473. else
  1474. walk_tbm_node (&node->tbm_node, pos, 0, 0, ctx);
  1475. }
  1476. /* walk trie in lexicographical order
  1477. *
  1478. * calls callback twice (once preorder, once postorder) at each prefix
  1479. */
  1480. void btrie_walk(const struct btrie *btrie, btrie_walk_cb_t *callback,
  1481. void *user_data)
  1482. {
  1483. struct walk_context ctx;
  1484. memset(&ctx, 0, sizeof(ctx));
  1485. ctx.callback = callback;
  1486. ctx.user_data = user_data;
  1487. walk_node (&btrie->root, 0, &ctx);
  1488. }
  1489. #endif /* not NO_MASTER_DUMP */
  1490. #ifdef TEST
  1491. /*****************************************************************
  1492. *
  1493. * Unit tests
  1494. *
  1495. */
  1496. #include <stdio.h>
  1497. #ifndef UNUSED
  1498. # define UNUSED __attribute__((unused))
  1499. #endif
  1500. /* bogus replacements mp_alloc for running self-tests */
  1501. void *
  1502. mp_alloc(UNUSED struct mempool *mp, unsigned sz, UNUSED int align)
  1503. {
  1504. return malloc(sz);
  1505. }
  1506. #if 0
  1507. # define PASS(name) puts("OK " name)
  1508. #else
  1509. # define PASS(name) fputs(".", stdout); fflush(stdout)
  1510. #endif
  1511. const char * pgm_name = "???";
  1512. static void
  1513. test_struct_node_packing()
  1514. {
  1515. node_t node;
  1516. assert(sizeof(struct tbm_node) == 2 * sizeof(void *));
  1517. assert(sizeof(struct lc_node) == 2 * sizeof(void *));
  1518. assert(sizeof(node_t) == 2 * sizeof(void *));
  1519. /* The lc_node bit must be an alias for bit zero of int_bm, since
  1520. * that is the only unused bit in the TBM node structure.
  1521. */
  1522. memset(&node, 0, sizeof(node));
  1523. assert(node.tbm_node.int_bm == 0);
  1524. lc_init_flags(&node.lc_node, 0, 0);
  1525. assert(node.tbm_node.int_bm == bit(0));
  1526. PASS("test_struct_node_packing");
  1527. }
  1528. static void
  1529. test_bit()
  1530. {
  1531. tbm_bitmap_t ones = ~(tbm_bitmap_t)0;
  1532. tbm_bitmap_t high_bit = ones ^ (ones >> 1);
  1533. assert(bit(0) == high_bit);
  1534. assert(bit(1) == high_bit >> 1);
  1535. assert(bit(8 * sizeof(tbm_bitmap_t) - 1) == 1);
  1536. PASS("test_bit");
  1537. }
  1538. static void
  1539. test_count_bits()
  1540. {
  1541. unsigned max_bits = sizeof(tbm_bitmap_t) * 8;
  1542. tbm_bitmap_t ones = ~(tbm_bitmap_t)0;
  1543. assert(count_bits(0) == 0);
  1544. assert(count_bits(1) == 1);
  1545. assert(count_bits(2) == 1);
  1546. assert(count_bits(3) == 2);
  1547. assert(count_bits(ones) == max_bits);
  1548. assert(count_bits(~1) == max_bits - 1);
  1549. /* count_bits(0x5555....) */
  1550. assert(count_bits(ones / 3) == max_bits / 2);
  1551. /* count_bits(0x3333...) */
  1552. assert(count_bits(ones / 5) == max_bits / 2);
  1553. /* count_bits(0x0f0f...) */
  1554. assert(count_bits(ones / 17) == max_bits / 2);
  1555. /* count_bits(0x1010...) */
  1556. assert(count_bits(ones / 255) == max_bits / 8);
  1557. PASS("test_count_bits");
  1558. }
  1559. static void
  1560. test_count_bits_before()
  1561. {
  1562. unsigned max_bits = sizeof(tbm_bitmap_t) * 8;
  1563. tbm_bitmap_t ones = ~(tbm_bitmap_t)0;
  1564. unsigned i;
  1565. for (i = 0; i < max_bits; i++) {
  1566. assert(count_bits_before(0, i) == 0);
  1567. assert(count_bits_before(ones, i) == i);
  1568. }
  1569. PASS("test_count_bits_before");
  1570. }
  1571. static void
  1572. test_count_bits_from()
  1573. {
  1574. unsigned max_bits = sizeof(tbm_bitmap_t) * 8;
  1575. tbm_bitmap_t ones = ~(tbm_bitmap_t)0;
  1576. unsigned i;
  1577. for (i = 0; i < max_bits; i++) {
  1578. assert(count_bits_from(0, i) == 0);
  1579. assert(count_bits_from(ones, i) == max_bits - i);
  1580. }
  1581. PASS("test_count_bits_from");
  1582. }
  1583. static void
  1584. test_extract_bits()
  1585. {
  1586. static btrie_oct_t prefix[] = {0xff, 0x55, 0xaa, 0x00};
  1587. unsigned i;
  1588. for (i = 0; i < 32; i++)
  1589. assert(extract_bits(prefix, i, 0) == 0);
  1590. for (i = 0; i < 8; i++)
  1591. assert(extract_bits(prefix, i, 1) == 1);
  1592. for (i = 8; i < 16; i++)
  1593. assert(extract_bits(prefix, i, 1) == i % 2);
  1594. for (i = 16; i < 24; i++)
  1595. assert(extract_bits(prefix, i, 1) == (i + 1) % 2);
  1596. for (i = 24; i < 32; i++)
  1597. assert(extract_bits(prefix, i, 1) == 0);
  1598. assert(extract_bits(prefix, 2, 6) == 0x3f);
  1599. assert(extract_bits(prefix, 3, 6) == 0x3e);
  1600. assert(extract_bits(prefix, 4, 6) == 0x3d);
  1601. assert(extract_bits(prefix, 5, 6) == 0x3a);
  1602. assert(extract_bits(prefix, 6, 6) == 0x35);
  1603. assert(extract_bits(prefix, 7, 6) == 0x2a);
  1604. assert(extract_bits(prefix, 8, 6) == 0x15);
  1605. PASS("test_extract_bits");
  1606. }
  1607. static void
  1608. test_high_bits()
  1609. {
  1610. assert(high_bits(0) == 0x00);
  1611. assert(high_bits(1) == 0x80);
  1612. assert(high_bits(2) == 0xc0);
  1613. assert(high_bits(3) == 0xe0);
  1614. assert(high_bits(4) == 0xf0);
  1615. assert(high_bits(5) == 0xf8);
  1616. assert(high_bits(6) == 0xfc);
  1617. assert(high_bits(7) == 0xfe);
  1618. assert(high_bits(8) == 0xff);
  1619. PASS("test_high_bits");
  1620. }
  1621. static void
  1622. test_prefixes_equal()
  1623. {
  1624. btrie_oct_t prefix1[LC_BYTES_PER_NODE];
  1625. btrie_oct_t prefix2[LC_BYTES_PER_NODE];
  1626. unsigned i;
  1627. memset(prefix1, 0xaa, LC_BYTES_PER_NODE);
  1628. memset(prefix2, 0xaa, LC_BYTES_PER_NODE);
  1629. for (i = 0; i < 8 * LC_BYTES_PER_NODE; i++) {
  1630. assert(prefixes_equal(prefix1, prefix2, i));
  1631. prefix1[i / 8] ^= 1 << (7 - i % 8);
  1632. assert(!prefixes_equal(prefix1, prefix2, 8 * LC_BYTES_PER_NODE));
  1633. assert(prefixes_equal(prefix1, prefix2, i));
  1634. if (i + 1 < 8 * LC_BYTES_PER_NODE)
  1635. assert(!prefixes_equal(prefix1, prefix2, i + 1));
  1636. prefix1[i / 8] ^= 1 << (7 - i % 8);
  1637. }
  1638. PASS("test_prefixes_equal");
  1639. }
  1640. static void
  1641. test_common_prefix()
  1642. {
  1643. btrie_oct_t prefix1[LC_BYTES_PER_NODE];
  1644. btrie_oct_t prefix2[LC_BYTES_PER_NODE];
  1645. unsigned i;
  1646. memset(prefix1, 0x55, LC_BYTES_PER_NODE);
  1647. memset(prefix2, 0x55, LC_BYTES_PER_NODE);
  1648. for (i = 0; i < 8 * LC_BYTES_PER_NODE; i++) {
  1649. assert(common_prefix(prefix1, prefix2, i) == i);
  1650. prefix1[i / 8] ^= 1 << (7 - i % 8);
  1651. assert(common_prefix(prefix1, prefix2, 8 * LC_BYTES_PER_NODE) == i);
  1652. if (i + 1 < 8 * LC_BYTES_PER_NODE)
  1653. assert(common_prefix(prefix1, prefix2, i+1) == i);
  1654. prefix1[i / 8] ^= 1 << (7 - i % 8);
  1655. }
  1656. PASS("test_common_prefix");
  1657. }
  1658. static void
  1659. test_base_index()
  1660. {
  1661. assert(base_index(0,0) == 1);
  1662. assert(base_index(0,1) == 2);
  1663. assert(base_index(1,1) == 3);
  1664. assert(base_index(0,2) == 4);
  1665. assert(base_index(1,2) == 5);
  1666. assert(base_index(2,2) == 6);
  1667. assert(base_index(3,2) == 7);
  1668. PASS("test_base_index");
  1669. }
  1670. static void
  1671. test_has_internal_data()
  1672. {
  1673. struct tbm_node node;
  1674. unsigned plen, pfx, bi;
  1675. for (plen = 0; plen < TBM_STRIDE; plen++) {
  1676. for (pfx = 0; pfx < 1U << plen; pfx++) {
  1677. tbm_bitmap_t ancestor_mask = 0;
  1678. for (bi = base_index(pfx, plen); bi; bi >>= 1) {
  1679. node.int_bm = bit(bi);
  1680. ancestor_mask |= bit(bi);
  1681. assert(has_internal_data(&node, pfx, plen));
  1682. }
  1683. node.int_bm = ~ancestor_mask;
  1684. assert(!has_internal_data(&node, pfx, plen));
  1685. }
  1686. }
  1687. PASS("test_has_internal_data");
  1688. }
  1689. /****************************************************************/
  1690. static const btrie_oct_t numbered_bytes[] = {
  1691. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  1692. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  1693. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  1694. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  1695. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  1696. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  1697. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  1698. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  1699. };
  1700. static void
  1701. check_non_terminal_lc_node(struct lc_node *node, unsigned len)
  1702. {
  1703. assert(is_lc_node((node_t *)node));
  1704. assert(!lc_is_terminal(node));
  1705. assert(lc_len(node) == len);
  1706. }
  1707. static void
  1708. check_terminal_lc_node(struct lc_node *node, unsigned len, const void *data)
  1709. {
  1710. assert(is_lc_node((node_t *)node));
  1711. assert(lc_is_terminal(node));
  1712. assert(lc_len(node) == len);
  1713. assert(node->ptr.data == data);
  1714. }
  1715. static void
  1716. test_init_terminal_node()
  1717. {
  1718. struct btrie *btrie = btrie_init(NULL);
  1719. const void *data = (void *)0xdeadbeef;
  1720. node_t node;
  1721. struct lc_node *head = &node.lc_node;
  1722. init_terminal_node(btrie, &node, 0,
  1723. numbered_bytes, 8 * LC_BYTES_PER_NODE, data);
  1724. check_terminal_lc_node(head, 8 * LC_BYTES_PER_NODE, data);
  1725. assert(memcmp(head->prefix, numbered_bytes, LC_BYTES_PER_NODE) == 0);
  1726. init_terminal_node(btrie, &node, 7,
  1727. numbered_bytes, 8 * LC_BYTES_PER_NODE, data);
  1728. check_terminal_lc_node(head, 8 * LC_BYTES_PER_NODE - 7, data);
  1729. assert(memcmp(head->prefix, numbered_bytes, LC_BYTES_PER_NODE) == 0);
  1730. init_terminal_node(btrie, &node, 0,
  1731. numbered_bytes, 2 * 8 * LC_BYTES_PER_NODE, data);
  1732. check_non_terminal_lc_node(head, 8 * LC_BYTES_PER_NODE);
  1733. assert(memcmp(head->prefix, numbered_bytes, LC_BYTES_PER_NODE) == 0);
  1734. {
  1735. struct lc_node *child = &head->ptr.child->lc_node;
  1736. check_terminal_lc_node(child, 8 * LC_BYTES_PER_NODE, data);
  1737. assert(memcmp(child->prefix, &numbered_bytes[LC_BYTES_PER_NODE],
  1738. LC_BYTES_PER_NODE) == 0);
  1739. }
  1740. init_terminal_node(btrie, &node, 15,
  1741. numbered_bytes, 8 * LC_BYTES_PER_NODE + 15, data);
  1742. check_non_terminal_lc_node(head, 8 * LC_BYTES_PER_NODE - 7);
  1743. assert(memcmp(head->prefix, &numbered_bytes[1], LC_BYTES_PER_NODE) == 0);
  1744. {
  1745. struct lc_node *child = &head->ptr.child->lc_node;
  1746. check_terminal_lc_node(child, 7, data);
  1747. assert(child->prefix[0] == numbered_bytes[LC_BYTES_PER_NODE + 1]);
  1748. }
  1749. PASS("test_init_terminal_node");
  1750. }
  1751. static void
  1752. test_coalesce_lc_node()
  1753. {
  1754. struct btrie *btrie = btrie_init(NULL);
  1755. const void *data = (void *)0xdeadbeef;
  1756. node_t node;
  1757. struct lc_node *head = &node.lc_node;
  1758. /* test merging */
  1759. init_terminal_node(btrie, &node, 0,
  1760. numbered_bytes, 8 * (LC_BYTES_PER_NODE + 1), data);
  1761. check_non_terminal_lc_node(head, LC_BYTES_PER_NODE * 8);
  1762. lc_add_to_len(head, -8);
  1763. coalesce_lc_node(btrie, head, 8);
  1764. check_terminal_lc_node(head, LC_BYTES_PER_NODE * 8, data);
  1765. assert(head->prefix[LC_BYTES_PER_NODE - 1]
  1766. == numbered_bytes[LC_BYTES_PER_NODE]);
  1767. /* test bit stealing */
  1768. init_terminal_node(btrie, &node, 0,
  1769. numbered_bytes, 8 * (2 * LC_BYTES_PER_NODE), data);
  1770. check_non_terminal_lc_node(head, LC_BYTES_PER_NODE * 8);
  1771. lc_add_to_len(head, -15);
  1772. coalesce_lc_node(btrie, head, 15);
  1773. check_non_terminal_lc_node(head, LC_BYTES_PER_NODE * 8 - 7);
  1774. assert(memcmp(head->prefix, numbered_bytes, LC_BYTES_PER_NODE - 1) == 0);
  1775. assert(head->prefix[LC_BYTES_PER_NODE - 1]
  1776. == numbered_bytes[LC_BYTES_PER_NODE]);
  1777. {
  1778. struct lc_node *child = &head->ptr.child->lc_node;
  1779. check_terminal_lc_node(child, 8 * (LC_BYTES_PER_NODE - 1), data);
  1780. assert(memcmp(child->prefix, &numbered_bytes[LC_BYTES_PER_NODE + 1],
  1781. LC_BYTES_PER_NODE - 1) == 0);
  1782. }
  1783. PASS("test_coalesce_lc_node");
  1784. }
  1785. static void
  1786. test_shorten_lc_node()
  1787. {
  1788. struct btrie *btrie = btrie_init(NULL);
  1789. const void *data = (void *)0xdeadbeef;
  1790. node_t node, shorter;
  1791. /* test shorten without shift */
  1792. init_terminal_node(btrie, &node, 0,
  1793. numbered_bytes, 8 * LC_BYTES_PER_NODE, data);
  1794. memset(shorter.lc_node.prefix, 0xff, LC_BYTES_PER_NODE);
  1795. shorten_lc_node(btrie, &shorter, 7, &node.lc_node, 0);
  1796. check_terminal_lc_node(&shorter.lc_node, LC_BYTES_PER_NODE * 8 - 7, data);
  1797. assert(memcmp(shorter.lc_node.prefix, numbered_bytes, LC_BYTES_PER_NODE)
  1798. == 0);
  1799. /* test shorten with shift */
  1800. init_terminal_node(btrie, &node, 7,
  1801. numbered_bytes, 8 * LC_BYTES_PER_NODE, data);
  1802. memset(shorter.lc_node.prefix, 0xff, LC_BYTES_PER_NODE);
  1803. shorten_lc_node(btrie, &shorter, 9, &node.lc_node, 7);
  1804. check_terminal_lc_node(&shorter.lc_node, LC_BYTES_PER_NODE * 8 - 9, data);
  1805. assert(memcmp(shorter.lc_node.prefix, &numbered_bytes[1],
  1806. LC_BYTES_PER_NODE - 1) == 0);
  1807. {
  1808. /* test child stealing */
  1809. struct lc_node head;
  1810. node_t tail, shorter;
  1811. lc_init_flags(&head, 0, 7);
  1812. head.ptr.child = &tail;
  1813. init_empty_node(btrie, &tail);
  1814. shorten_lc_node(btrie, &shorter, 7, &head, 0);
  1815. assert(is_empty_node(&shorter));
  1816. }
  1817. PASS("test_shorten_lc_node");
  1818. }
  1819. static void
  1820. test_split_lc_node()
  1821. {
  1822. struct btrie *btrie = btrie_init(NULL);
  1823. const void *data = (void *)0xdeadbeef;
  1824. struct lc_node node;
  1825. init_terminal_node(btrie, (node_t *)&node, 1, numbered_bytes, 25, data);
  1826. split_lc_node(btrie, &node, 1, 8);
  1827. check_non_terminal_lc_node(&node, 8);
  1828. check_terminal_lc_node(&node.ptr.child->lc_node, 16, data);
  1829. /* test conversion of terminal to non-terminal */
  1830. init_terminal_node(btrie, (node_t *)&node, 7, numbered_bytes, 10, data);
  1831. split_lc_node(btrie, &node, 7, 3);
  1832. check_non_terminal_lc_node(&node, 3);
  1833. check_terminal_lc_node(&node.ptr.child->lc_node, 0, data);
  1834. PASS("test_split_lc_node");
  1835. }
  1836. static void
  1837. test_convert_lc_node_1()
  1838. {
  1839. struct btrie *btrie = btrie_init(NULL);
  1840. const void *data = (void *)0xdeadbeef;
  1841. struct lc_node head;
  1842. /* test tail is left */
  1843. lc_init_flags(&head, 0, 1);
  1844. head.prefix[0] = 0;
  1845. head.ptr.child = alloc_nodes(btrie, 1, 0);
  1846. init_terminal_node(btrie, head.ptr.child, 1, numbered_bytes, 1, data);
  1847. convert_lc_node_1(btrie, &head, 0);
  1848. {
  1849. node_t *result = (node_t *)&head;
  1850. assert(is_tbm_node(result));
  1851. assert(result->tbm_node.ext_bm == 0);
  1852. assert(result->tbm_node.int_bm == bit(base_index(0, 1)));
  1853. assert(*tbm_data_p(&result->tbm_node, 0, 1) == data);
  1854. }
  1855. /* test tail is right */
  1856. lc_init_flags(&head, 0, 1);
  1857. head.prefix[0] = 1;
  1858. head.ptr.child = alloc_nodes(btrie, 1, 0);
  1859. init_terminal_node(btrie, head.ptr.child, 8, numbered_bytes, 10, data);
  1860. convert_lc_node_1(btrie, &head, 7);
  1861. {
  1862. node_t *result = (node_t *)&head;
  1863. assert(is_tbm_node(result));
  1864. assert(result->tbm_node.ext_bm == 0);
  1865. assert(result->tbm_node.int_bm == bit(base_index(4, 3)));
  1866. assert(*tbm_data_p(&result->tbm_node, 4, 3) == data);
  1867. }
  1868. PASS("test_convert_lc_node_1");
  1869. }
  1870. static void
  1871. test_convert_lc_node()
  1872. {
  1873. struct btrie *btrie = btrie_init(NULL);
  1874. const void *data = (void *)0xdeadbeef;
  1875. node_t node;
  1876. /* if (len >= TBM_STRIDE) */
  1877. init_terminal_node(btrie, &node, 7, numbered_bytes, TBM_STRIDE + 7, data);
  1878. convert_lc_node(btrie, &node.lc_node, 7);
  1879. assert(is_tbm_node(&node));
  1880. assert(node.tbm_node.ext_bm == bit(0));
  1881. assert(node.tbm_node.int_bm == 0);
  1882. check_terminal_lc_node(&tbm_ext_path(&node.tbm_node, 0)->lc_node, 0, data);
  1883. /* if (lc_is_terminal(node)) */
  1884. init_terminal_node(btrie, &node, 0, numbered_bytes, 0, data);
  1885. convert_lc_node(btrie, &node.lc_node, 0);
  1886. assert(is_tbm_node(&node));
  1887. assert(node.tbm_node.ext_bm == 0);
  1888. assert(node.tbm_node.int_bm == bit(base_index(0, 0)));
  1889. assert(*tbm_data_p(&node.tbm_node, 0, 0) == data);
  1890. /* else */
  1891. lc_init_flags(&node.lc_node, 0, TBM_STRIDE - 1);
  1892. node.lc_node.prefix[0] = 0;
  1893. node.lc_node.ptr.child = alloc_nodes(btrie, 1, 0);
  1894. init_empty_node(btrie, node.lc_node.ptr.child);
  1895. tbm_insert_data(btrie, &node.lc_node.ptr.child->tbm_node, 0, 0, data);
  1896. convert_lc_node(btrie, &node.lc_node, 0);
  1897. assert(is_tbm_node(&node));
  1898. assert(node.tbm_node.ext_bm == 0);
  1899. assert(node.tbm_node.int_bm == bit(base_index(0, TBM_STRIDE - 1)));
  1900. assert(*tbm_data_p(&node.tbm_node, 0, TBM_STRIDE - 1) == data);
  1901. PASS("test_convert_lc_node");
  1902. }
  1903. static void
  1904. test_insert_lc_node()
  1905. {
  1906. struct btrie *btrie = btrie_init(NULL);
  1907. const void *data = (void *)0xdeadbeef;
  1908. node_t node, tail;
  1909. /* test optimized case, last_bit == 0 */
  1910. init_terminal_node(btrie, &tail, 9, numbered_bytes, 17, data);
  1911. insert_lc_node(btrie, &node, 8, 0, 0, &tail);
  1912. check_terminal_lc_node(&node.lc_node, 9, data);
  1913. assert(memcmp(node.lc_node.prefix, &numbered_bytes[1], 2) == 0);
  1914. /* test optimized case, last_bit == 1 */
  1915. init_terminal_node(btrie, &tail, 7, &numbered_bytes[0x12], 15, data);
  1916. insert_lc_node(btrie, &node, 6, 0x10, 1, &tail);
  1917. check_terminal_lc_node(&node.lc_node, 9, data);
  1918. assert(node.lc_node.prefix[0] == 0x12);
  1919. assert(node.lc_node.prefix[1] == 0x13);
  1920. /* test with shift */
  1921. init_terminal_node(btrie, &tail, 0, numbered_bytes, 8, data);
  1922. insert_lc_node(btrie, &node, 7, 0x40, 1, &tail);
  1923. check_terminal_lc_node(&node.lc_node, 9, data);
  1924. assert(node.lc_node.prefix[0] == 0x41);
  1925. assert(node.lc_node.prefix[1] == numbered_bytes[0]);
  1926. /* test with TBM node */
  1927. init_empty_node(btrie, &tail);
  1928. insert_lc_node(btrie, &node, 6, 0x40, 0, &tail);
  1929. check_non_terminal_lc_node(&node.lc_node, 1);
  1930. assert(is_tbm_node(node.lc_node.ptr.child));
  1931. PASS("test_insert_lc_node");
  1932. }
  1933. static void
  1934. test_next_pbyte()
  1935. {
  1936. assert(next_pbyte(0xff, 0, 1) == 0x80 >> (TBM_STRIDE - 1));
  1937. assert(next_pbyte(0xff, 1, 1) == (0x80 | (0x80 >> TBM_STRIDE)));
  1938. assert(next_pbyte(0xff, 2, 1) == (0xc0 | (0x80 >> (TBM_STRIDE + 1))));
  1939. assert(next_pbyte(0xff, 8 - TBM_STRIDE, 1) == 0);
  1940. assert(next_pbyte(0xff, 9 - TBM_STRIDE, 1) == 0x80);
  1941. PASS("test_next_pbyte");
  1942. }
  1943. static void
  1944. test_init_tbm_node()
  1945. {
  1946. struct btrie *btrie = btrie_init(NULL);
  1947. const void *data = (void *)0xdeadbeef;
  1948. unsigned lr;
  1949. node_t node;
  1950. /* test root data */
  1951. init_tbm_node(btrie, &node, 0, 0, &data, NULL, NULL);
  1952. assert(is_tbm_node(&node));
  1953. assert(node.tbm_node.ext_bm == 0);
  1954. assert(node.tbm_node.int_bm == bit(base_index(0, 0)));
  1955. assert(*tbm_data_p(&node.tbm_node, 0, 0) == data);
  1956. for (lr = 0; lr < 2; lr++) {
  1957. node_t child;
  1958. node_t *left = lr ? NULL : &child;
  1959. node_t *right = lr ? &child : NULL;
  1960. unsigned base = lr ? (1U << (TBM_STRIDE - 1)) : 0;
  1961. unsigned pfx;
  1962. /* test with long LC node child */
  1963. init_terminal_node(btrie, &child, 1, numbered_bytes, TBM_STRIDE + 1, data);
  1964. init_tbm_node(btrie, &node, 0, 0, NULL, left, right);
  1965. assert(is_tbm_node(&node));
  1966. assert(node.tbm_node.ext_bm == bit(base));
  1967. assert(node.tbm_node.int_bm == 0);
  1968. check_terminal_lc_node(&tbm_ext_path(&node.tbm_node, base)->lc_node,
  1969. 1, data);
  1970. /* test with short LC node children */
  1971. init_terminal_node(btrie, &child, 1, numbered_bytes, TBM_STRIDE - 1, data);
  1972. init_tbm_node(btrie, &node, 0, 0, NULL, left, right);
  1973. assert(is_tbm_node(&node));
  1974. assert(node.tbm_node.ext_bm == 0);
  1975. assert(node.tbm_node.int_bm == bit(base_index(base >> 1, TBM_STRIDE-1)));
  1976. assert(*tbm_data_p(&node.tbm_node, base >> 1, TBM_STRIDE-1) == data);
  1977. /* construct TBM node with all eight combinations of having data,
  1978. * left_ext and/or right_ext in its extending paths */
  1979. init_empty_node(btrie, &child);
  1980. for (pfx = 0; pfx < 8; pfx++) {
  1981. if (pfx & 1)
  1982. tbm_insert_data(btrie, &child.tbm_node, pfx, TBM_STRIDE - 1, data);
  1983. if (pfx & 2) {
  1984. btrie_oct_t prefix0 = 0;
  1985. init_terminal_node(btrie,
  1986. tbm_insert_ext_path(btrie, &child.tbm_node, 2*pfx),
  1987. TBM_STRIDE + 1,
  1988. &prefix0, TBM_STRIDE + 2, data);
  1989. }
  1990. if (pfx & 4) {
  1991. btrie_oct_t prefix0 = 0x80 >> TBM_STRIDE;
  1992. init_terminal_node(btrie,
  1993. tbm_insert_ext_path(btrie, &child.tbm_node, 2*pfx+1),
  1994. TBM_STRIDE + 1,
  1995. &prefix0, TBM_STRIDE + 3, data);
  1996. }
  1997. }
  1998. init_tbm_node(btrie, &node, 0, 0, NULL, left, right);
  1999. for (pfx = 0; pfx < 8; pfx++) {
  2000. unsigned base = lr ? (1U << (TBM_STRIDE - 1)) : 0;
  2001. node_t *ext_path = tbm_ext_path(&node.tbm_node, base + pfx);
  2002. if (pfx == 0)
  2003. assert(ext_path == NULL);
  2004. else if (pfx == 1)
  2005. check_terminal_lc_node(&ext_path->lc_node, 0, data);
  2006. else if (pfx == 2) {
  2007. check_terminal_lc_node(&ext_path->lc_node, 2, data);
  2008. assert(ext_path->lc_node.prefix[0] == 0);
  2009. }
  2010. else if (pfx == 4) {
  2011. check_terminal_lc_node(&ext_path->lc_node, 3, data);
  2012. assert(ext_path->lc_node.prefix[0] == (0x80 >> TBM_STRIDE));
  2013. }
  2014. else {
  2015. tbm_bitmap_t int_bm = 0;
  2016. assert(is_tbm_node(ext_path));
  2017. if (pfx & 1) {
  2018. int_bm |= bit(base_index(0, 0));
  2019. assert(*tbm_data_p(&ext_path->tbm_node, 0, 0) == data);
  2020. }
  2021. if (pfx & 2) {
  2022. int_bm |= bit(base_index(0, 2));
  2023. assert(*tbm_data_p(&ext_path->tbm_node, 0, 2) == data);
  2024. }
  2025. if (pfx & 4) {
  2026. int_bm |= bit(base_index(4, 3));
  2027. assert(*tbm_data_p(&ext_path->tbm_node, 4, 3) == data);
  2028. }
  2029. assert(ext_path->tbm_node.int_bm == int_bm);
  2030. }
  2031. }
  2032. }
  2033. PASS("test_init_tbm_node");
  2034. }
  2035. static void
  2036. test_add_to_trie()
  2037. {
  2038. struct btrie *btrie = btrie_init(NULL);
  2039. const void *data = (void *)0xdeadbeef;
  2040. enum btrie_result result;
  2041. unsigned pfx, plen;
  2042. node_t root;
  2043. /* test initial insertion */
  2044. init_empty_node(btrie, &root);
  2045. result = add_to_trie(btrie, &root, 0,
  2046. numbered_bytes, 8 * 2 * LC_BYTES_PER_NODE, data);
  2047. assert(result == BTRIE_OKAY);
  2048. check_non_terminal_lc_node(&root.lc_node, 8 * LC_BYTES_PER_NODE);
  2049. check_terminal_lc_node(&root.lc_node.ptr.child->lc_node,
  2050. 8 * LC_BYTES_PER_NODE, data);
  2051. /* test can follow LC node to tail, and then detect duplicate prefix */
  2052. result = add_to_trie(btrie, &root, 0,
  2053. numbered_bytes, 8 * 2 * LC_BYTES_PER_NODE, data);
  2054. assert(result == BTRIE_DUPLICATE_PREFIX);
  2055. /* test can insert new TBM node within existing LC node */
  2056. result = add_to_trie(btrie, &root, 0,
  2057. &numbered_bytes[1], 16, data);
  2058. assert(result == BTRIE_OKAY);
  2059. check_non_terminal_lc_node(&root.lc_node, 7);
  2060. assert(is_tbm_node(root.lc_node.ptr.child));
  2061. /* test can convert terminal LC node to TBM node */
  2062. init_terminal_node(btrie, &root, 0, numbered_bytes, 12, data);
  2063. result = add_to_trie(btrie, &root, 0, numbered_bytes, 24, data);
  2064. assert(result == BTRIE_OKAY);
  2065. check_non_terminal_lc_node(&root.lc_node, 12);
  2066. assert(is_tbm_node(root.lc_node.ptr.child));
  2067. /* test can insert internal prefix data in TBM node */
  2068. for (plen = 0; plen < TBM_STRIDE; plen++) {
  2069. for (pfx = 0; pfx < (1U << plen); pfx++) {
  2070. btrie_oct_t prefix0 = plen ? pfx << (8 - plen) : 0;
  2071. init_empty_node(btrie, &root);
  2072. init_terminal_node(btrie, tbm_insert_ext_path(btrie, &root.tbm_node, 0),
  2073. TBM_STRIDE,
  2074. numbered_bytes, 8, data);
  2075. result = add_to_trie(btrie, &root, 0, &prefix0, plen, data);
  2076. assert(result == BTRIE_OKAY);
  2077. assert(is_tbm_node(&root));
  2078. assert(root.tbm_node.ext_bm == bit(0));
  2079. assert(root.tbm_node.int_bm == bit(base_index(pfx, plen)));
  2080. assert(*tbm_data_p(&root.tbm_node, pfx, plen) == data);
  2081. result = add_to_trie(btrie, &root, 0, &prefix0, plen, data);
  2082. assert(result == BTRIE_DUPLICATE_PREFIX);
  2083. }
  2084. }
  2085. /* test can add extending paths to TBM node */
  2086. for (pfx = 0; pfx < (1U << TBM_STRIDE); pfx++) {
  2087. btrie_oct_t prefix0 = pfx << (8 - TBM_STRIDE);
  2088. init_empty_node(btrie, &root);
  2089. tbm_insert_data(btrie, &root.tbm_node, 0, 0, data);
  2090. result = add_to_trie(btrie, &root, 0, &prefix0, 8, data);
  2091. assert(result == BTRIE_OKAY);
  2092. assert(is_tbm_node(&root));
  2093. assert(root.tbm_node.ext_bm == bit(pfx));
  2094. assert(root.tbm_node.int_bm == bit(base_index(0, 0)));
  2095. check_terminal_lc_node(&tbm_ext_path(&root.tbm_node, pfx)->lc_node,
  2096. 8 - TBM_STRIDE, data);
  2097. result = add_to_trie(btrie, &root, 0, &prefix0, 8, data);
  2098. assert(result == BTRIE_DUPLICATE_PREFIX);
  2099. }
  2100. /* test can follow extending path */
  2101. init_empty_node(btrie, &root);
  2102. init_terminal_node(btrie,
  2103. tbm_insert_ext_path(btrie, &root.tbm_node, 0), TBM_STRIDE,
  2104. numbered_bytes, 8, data);
  2105. result = add_to_trie(btrie, &root, 0, numbered_bytes, 7, data);
  2106. assert(result == BTRIE_OKAY);
  2107. assert(root.tbm_node.ext_bm == bit(0));
  2108. assert(root.tbm_node.int_bm == 0);
  2109. check_non_terminal_lc_node(&root.tbm_node.ptr.children[0].lc_node,
  2110. 7 - TBM_STRIDE);
  2111. PASS("test_add_to_trie");
  2112. }
  2113. static void
  2114. test_search_trie()
  2115. {
  2116. struct btrie *btrie = btrie_init(NULL);
  2117. const void *data01 = (void *)0xdead0001;
  2118. const void *data11 = (void *)0xdead0101;
  2119. const void *data = (void *)0xdeadbeef;
  2120. unsigned plen, pfx;
  2121. node_t root;
  2122. /* test can follow chain of LC nodes to an exact match */
  2123. init_empty_node(btrie, &root);
  2124. add_to_trie(btrie, &root, 0,
  2125. numbered_bytes, 8 * 2 * LC_BYTES_PER_NODE, data);
  2126. assert(search_trie(&root, 0, numbered_bytes, 8 * 2 * LC_BYTES_PER_NODE)
  2127. == data);
  2128. assert(search_trie(&root, 0, numbered_bytes, 8 * 2 * LC_BYTES_PER_NODE + 1)
  2129. == data);
  2130. assert(search_trie(&root, 0, numbered_bytes, 8 * 2 * LC_BYTES_PER_NODE - 1)
  2131. == NULL);
  2132. assert(search_trie(&root, 0, &numbered_bytes[1], 8 * 2 * LC_BYTES_PER_NODE)
  2133. == NULL);
  2134. /* test can follow extending path to an exact match */
  2135. for (pfx = 0; pfx < (1U << TBM_STRIDE); pfx++) {
  2136. btrie_oct_t prefix0 = pfx << (8 - TBM_STRIDE);
  2137. init_empty_node(btrie, &root);
  2138. tbm_insert_data(btrie, &root.tbm_node, 0, 1, data01);
  2139. tbm_insert_data(btrie, &root.tbm_node, 1, 1, data11);
  2140. add_to_trie(btrie, &root, 0, &prefix0, 8, data);
  2141. assert(search_trie(&root, 0, &prefix0, 8) == data);
  2142. /* test that last matching TBM internal prefix gets picked up */
  2143. if (prefix0 & 0x80)
  2144. assert(search_trie(&root, 0, &prefix0, 7) == data11);
  2145. else
  2146. assert(search_trie(&root, 0, &prefix0, 7) == data01);
  2147. prefix0 ^= 1 << (8 - TBM_STRIDE);
  2148. if (prefix0 & 0x80)
  2149. assert(search_trie(&root, 0, &prefix0, 8) == data11);
  2150. else
  2151. assert(search_trie(&root, 0, &prefix0, 8) == data01);
  2152. }
  2153. /* test finding of TBM internal prefixes */
  2154. init_empty_node(btrie, &root);
  2155. tbm_insert_data(btrie, &root.tbm_node, 0, 1, data01);
  2156. tbm_insert_data(btrie, &root.tbm_node, 1, 1, data11);
  2157. assert(search_trie(&root, 0, numbered_bytes, 0) == NULL);
  2158. for (plen = 1; plen < TBM_STRIDE; plen++) {
  2159. for (pfx = 0; pfx < (1U << TBM_STRIDE); pfx++) {
  2160. btrie_oct_t prefix0 = pfx << (8 - plen);
  2161. if (prefix0 & 0x80)
  2162. assert(search_trie(&root, 0, &prefix0, plen) == data11);
  2163. else
  2164. assert(search_trie(&root, 0, &prefix0, plen) == data01);
  2165. }
  2166. }
  2167. PASS("test_search_trie");
  2168. }
  2169. static int
  2170. unit_tests()
  2171. {
  2172. test_struct_node_packing();
  2173. test_bit();
  2174. test_count_bits();
  2175. test_count_bits_before();
  2176. test_count_bits_from();
  2177. test_extract_bits();
  2178. test_high_bits();
  2179. test_prefixes_equal();
  2180. test_common_prefix();
  2181. test_base_index();
  2182. test_has_internal_data();
  2183. test_init_terminal_node();
  2184. test_coalesce_lc_node();
  2185. test_shorten_lc_node();
  2186. test_split_lc_node();
  2187. test_convert_lc_node_1();
  2188. test_convert_lc_node();
  2189. test_insert_lc_node();
  2190. test_next_pbyte();
  2191. test_init_tbm_node();
  2192. test_add_to_trie();
  2193. test_search_trie();
  2194. puts("\nOK");
  2195. return 0;
  2196. }
  2197. /*****************************************************************
  2198. *
  2199. * btrie_dump: print out the trie structure (for testing)
  2200. *
  2201. */
  2202. #define INDENT_FILL "....:....|....:....|....:....|....:....|"
  2203. static void dump_node(const node_t *node, unsigned pos, btrie_oct_t *prefix,
  2204. int indent);
  2205. static void
  2206. dump_prefix(btrie_oct_t *prefix, unsigned len, int indent, const char *tail)
  2207. {
  2208. unsigned i;
  2209. printf("%*.*s0x", indent, indent, INDENT_FILL);
  2210. for (i = 0; i < len / 8; i++)
  2211. printf("%02x", prefix[i]);
  2212. if (len % 8)
  2213. printf("%02x", prefix[len / 8] & high_bits(len % 8));
  2214. printf("/%u%s", len, tail);
  2215. }
  2216. /* the opposite of extract_bits, sets a short string of bits from integer */
  2217. static void
  2218. insert_bits(btrie_oct_t *prefix, unsigned pos, btrie_oct_t pfx, unsigned nbits)
  2219. {
  2220. if (nbits != 0) {
  2221. unsigned v = (prefix[pos / 8] << 8) + prefix[pos / 8 + 1];
  2222. unsigned mask = (1U << nbits) - 1;
  2223. unsigned shift = 16 - (pos % 8) - nbits;
  2224. v = (v & ~(mask << shift)) | (pfx << shift);
  2225. prefix[pos / 8] = v >> 8;
  2226. prefix[pos / 8 + 1] = (btrie_oct_t)v;
  2227. }
  2228. }
  2229. static void
  2230. dump_tbm_node(const struct tbm_node *node, unsigned pos,
  2231. btrie_oct_t *prefix, int indent)
  2232. {
  2233. unsigned pfx = 0, plen = 0;
  2234. dump_prefix(prefix, pos, indent, " [tbm]\n");
  2235. for (;;) {
  2236. if (plen < TBM_STRIDE) {
  2237. const void **data_p = tbm_data_p(node, pfx, plen);
  2238. if (data_p) {
  2239. insert_bits(prefix, pos, pfx, plen);
  2240. dump_prefix(prefix, pos + plen, indent, "");
  2241. printf(" [%u/%u] (%s)\n", pfx, plen, (const char *)*data_p);
  2242. }
  2243. plen++;
  2244. pfx <<= 1;
  2245. }
  2246. else {
  2247. const node_t *ext_path = tbm_ext_path(node, pfx);
  2248. if (ext_path) {
  2249. insert_bits(prefix, pos, pfx, TBM_STRIDE);
  2250. dump_node(ext_path, pos + TBM_STRIDE, prefix, indent + 1);
  2251. }
  2252. while (pfx & 1) {
  2253. if (--plen == 0)
  2254. return;
  2255. pfx >>= 1;
  2256. }
  2257. pfx++;
  2258. }
  2259. }
  2260. }
  2261. static void
  2262. dump_lc_node(const struct lc_node *node, unsigned pos,
  2263. btrie_oct_t *prefix, int indent)
  2264. {
  2265. unsigned end = pos + lc_len(node);
  2266. btrie_oct_t save_prefix = prefix[lc_shift(pos)];
  2267. memcpy(&prefix[lc_shift(pos)], node->prefix, lc_bytes(node, pos));
  2268. if (lc_is_terminal(node)) {
  2269. dump_prefix(prefix, end, indent, "");
  2270. printf(" (%s)\n", (const char *)node->ptr.data);
  2271. }
  2272. else {
  2273. dump_prefix(prefix, end, indent, "\n");
  2274. dump_node(node->ptr.child, end, prefix, indent + 1);
  2275. }
  2276. prefix[lc_shift(pos)] = save_prefix;
  2277. if (lc_bytes(node, pos) > 1)
  2278. memset(&prefix[lc_shift(pos) + 1], 0, lc_bytes(node, pos) - 1);
  2279. }
  2280. static void
  2281. dump_node(const node_t *node, unsigned pos, btrie_oct_t *prefix, int indent)
  2282. {
  2283. if (is_lc_node(node))
  2284. dump_lc_node(&node->lc_node, pos, prefix, indent);
  2285. else
  2286. dump_tbm_node(&node->tbm_node, pos, prefix, indent);
  2287. }
  2288. static void
  2289. btrie_dump(struct btrie *btrie)
  2290. {
  2291. btrie_oct_t prefix[(BTRIE_MAX_PREFIX + 7) / 8];
  2292. memset(prefix, 0, sizeof(prefix));
  2293. dump_node(&btrie->root, 0, prefix, 0);
  2294. puts(btrie_stats(btrie));
  2295. }
  2296. /****************************************************************
  2297. *
  2298. * test program - just enough to construct a trie and preform a lookup
  2299. *
  2300. */
  2301. #include <arpa/inet.h>
  2302. static int
  2303. parse_prefix(const char *arg, btrie_oct_t prefix[16], unsigned *len)
  2304. {
  2305. char addrbuf[128];
  2306. return sscanf(arg, "%127[0-9a-fA-F:]/%u", addrbuf, len) == 2
  2307. && inet_pton(AF_INET6, addrbuf, prefix) == 1;
  2308. }
  2309. static int
  2310. test_btrie(int argc, char *argv[])
  2311. {
  2312. struct btrie *btrie = btrie_init(NULL);
  2313. int i;
  2314. btrie_oct_t prefix[16];
  2315. unsigned len;
  2316. for (i = 1; i < argc-1; i++) {
  2317. if (!parse_prefix(argv[i], prefix, &len)) {
  2318. fprintf(stderr, "Can not parse arg '%s'\n", argv[i]);
  2319. return 1;
  2320. }
  2321. btrie_add_prefix(btrie, prefix, len, argv[i]);
  2322. }
  2323. btrie_dump(btrie);
  2324. if (argc > 1) {
  2325. const void *data;
  2326. if (!parse_prefix(argv[argc-1], prefix, &len)) {
  2327. fprintf(stderr, "Can not parse arg '%s'\n", argv[argc-1]);
  2328. return 1;
  2329. }
  2330. data = btrie_lookup(btrie, prefix, 128);
  2331. printf("lookup(%s) => %s\n", argv[argc-1], (const char *)data);
  2332. }
  2333. return 0;
  2334. }
  2335. int
  2336. main(int argc, char *argv[])
  2337. {
  2338. if ((pgm_name = strrchr(argv[0], '/')) != NULL)
  2339. pgm_name++;
  2340. else
  2341. pgm_name = argv[0];
  2342. if (argc > 1)
  2343. return test_btrie(argc, argv);
  2344. else
  2345. return unit_tests();
  2346. }
  2347. #endif /* TEST */