generic_mtrie_impl.hpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /*
  2. Copyright (c) 2018 Contributors as noted in the AUTHORS file
  3. This file is part of libzmq, the ZeroMQ core engine in C++.
  4. libzmq is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU Lesser General Public License (LGPL) as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. As a special exception, the Contributors give you permission to link
  9. this library with independent modules to produce an executable,
  10. regardless of the license terms of these independent modules, and to
  11. copy and distribute the resulting executable under terms of your choice,
  12. provided that you also meet, for each linked independent module, the
  13. terms and conditions of the license of that module. An independent
  14. module is a module which is not derived from or based on this library.
  15. If you modify this library, you must extend this exception to your
  16. version of the library.
  17. libzmq is distributed in the hope that it will be useful, but WITHOUT
  18. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  20. License for more details.
  21. You should have received a copy of the GNU Lesser General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #ifndef __ZMQ_GENERIC_MTRIE_IMPL_HPP_INCLUDED__
  25. #define __ZMQ_GENERIC_MTRIE_IMPL_HPP_INCLUDED__
  26. #include <stdlib.h>
  27. #include <new>
  28. #include <algorithm>
  29. #include <list>
  30. #include "err.hpp"
  31. #include "macros.hpp"
  32. #include "generic_mtrie.hpp"
  33. namespace zmq
  34. {
  35. template <typename T>
  36. generic_mtrie_t<T>::generic_mtrie_t () :
  37. _pipes (0), _num_prefixes (0), _min (0), _count (0), _live_nodes (0)
  38. {
  39. }
  40. template <typename T> generic_mtrie_t<T>::~generic_mtrie_t ()
  41. {
  42. LIBZMQ_DELETE (_pipes);
  43. if (_count == 1) {
  44. zmq_assert (_next.node);
  45. LIBZMQ_DELETE (_next.node);
  46. } else if (_count > 1) {
  47. for (unsigned short i = 0; i != _count; ++i) {
  48. LIBZMQ_DELETE (_next.table[i]);
  49. }
  50. free (_next.table);
  51. }
  52. }
  53. template <typename T>
  54. bool generic_mtrie_t<T>::add (prefix_t prefix_, size_t size_, value_t *pipe_)
  55. {
  56. generic_mtrie_t<value_t> *it = this;
  57. while (size_) {
  58. const unsigned char c = *prefix_;
  59. if (c < it->_min || c >= it->_min + it->_count) {
  60. // The character is out of range of currently handled
  61. // characters. We have to extend the table.
  62. if (!it->_count) {
  63. it->_min = c;
  64. it->_count = 1;
  65. it->_next.node = NULL;
  66. } else if (it->_count == 1) {
  67. const unsigned char oldc = it->_min;
  68. generic_mtrie_t *oldp = it->_next.node;
  69. it->_count = (it->_min < c ? c - it->_min : it->_min - c) + 1;
  70. it->_next.table = static_cast<generic_mtrie_t **> (
  71. malloc (sizeof (generic_mtrie_t *) * it->_count));
  72. alloc_assert (it->_next.table);
  73. for (unsigned short i = 0; i != it->_count; ++i)
  74. it->_next.table[i] = 0;
  75. it->_min = std::min (it->_min, c);
  76. it->_next.table[oldc - it->_min] = oldp;
  77. } else if (it->_min < c) {
  78. // The new character is above the current character range.
  79. const unsigned short old_count = it->_count;
  80. it->_count = c - it->_min + 1;
  81. it->_next.table = static_cast<generic_mtrie_t **> (realloc (
  82. it->_next.table, sizeof (generic_mtrie_t *) * it->_count));
  83. alloc_assert (it->_next.table);
  84. for (unsigned short i = old_count; i != it->_count; i++)
  85. it->_next.table[i] = NULL;
  86. } else {
  87. // The new character is below the current character range.
  88. const unsigned short old_count = it->_count;
  89. it->_count = (it->_min + old_count) - c;
  90. it->_next.table = static_cast<generic_mtrie_t **> (realloc (
  91. it->_next.table, sizeof (generic_mtrie_t *) * it->_count));
  92. alloc_assert (it->_next.table);
  93. memmove (it->_next.table + it->_min - c, it->_next.table,
  94. old_count * sizeof (generic_mtrie_t *));
  95. for (unsigned short i = 0; i != it->_min - c; i++)
  96. it->_next.table[i] = NULL;
  97. it->_min = c;
  98. }
  99. }
  100. // If next node does not exist, create one.
  101. if (it->_count == 1) {
  102. if (!it->_next.node) {
  103. it->_next.node = new (std::nothrow) generic_mtrie_t;
  104. alloc_assert (it->_next.node);
  105. ++(it->_live_nodes);
  106. }
  107. ++prefix_;
  108. --size_;
  109. it = it->_next.node;
  110. } else {
  111. if (!it->_next.table[c - it->_min]) {
  112. it->_next.table[c - it->_min] =
  113. new (std::nothrow) generic_mtrie_t;
  114. alloc_assert (it->_next.table[c - it->_min]);
  115. ++(it->_live_nodes);
  116. }
  117. ++prefix_;
  118. --size_;
  119. it = it->_next.table[c - it->_min];
  120. }
  121. }
  122. // We are at the node corresponding to the prefix. We are done.
  123. const bool result = !it->_pipes;
  124. if (!it->_pipes) {
  125. it->_pipes = new (std::nothrow) pipes_t;
  126. alloc_assert (it->_pipes);
  127. _num_prefixes.add (1);
  128. }
  129. it->_pipes->insert (pipe_);
  130. return result;
  131. }
  132. template <typename T>
  133. template <typename Arg>
  134. void generic_mtrie_t<T>::rm (value_t *pipe_,
  135. void (*func_) (prefix_t data_,
  136. size_t size_,
  137. Arg arg_),
  138. Arg arg_,
  139. bool call_on_uniq_)
  140. {
  141. // This used to be implemented as a non-tail recursive traversal of the trie,
  142. // which means remote clients controlled the depth of the recursion and the
  143. // stack size.
  144. // To simulate the non-tail recursion, with post-recursion changes depending on
  145. // the result of the recursive call, a stack is used to re-visit the same node
  146. // and operate on it again after children have been visited.
  147. // A boolean is used to record whether the node had already been visited and to
  148. // determine if the pre- or post- children visit actions have to be taken.
  149. // In the case of a node with (N > 1) children, the node has to be re-visited
  150. // N times, in the correct order after each child visit.
  151. std::list<struct iter> stack;
  152. unsigned char *buff = NULL;
  153. size_t maxbuffsize = 0;
  154. struct iter it = {this, NULL, NULL, 0, 0, 0, 0, false};
  155. stack.push_back (it);
  156. while (!stack.empty ()) {
  157. it = stack.back ();
  158. stack.pop_back ();
  159. if (!it.processed_for_removal) {
  160. // Remove the subscription from this node.
  161. if (it.node->_pipes && it.node->_pipes->erase (pipe_)) {
  162. if (!call_on_uniq_ || it.node->_pipes->empty ()) {
  163. func_ (buff, it.size, arg_);
  164. }
  165. if (it.node->_pipes->empty ()) {
  166. LIBZMQ_DELETE (it.node->_pipes);
  167. }
  168. }
  169. // Adjust the buffer.
  170. if (it.size >= maxbuffsize) {
  171. maxbuffsize = it.size + 256;
  172. buff =
  173. static_cast<unsigned char *> (realloc (buff, maxbuffsize));
  174. alloc_assert (buff);
  175. }
  176. switch (it.node->_count) {
  177. case 0:
  178. // If there are no subnodes in the trie, we are done with this node
  179. // pre-processing.
  180. break;
  181. case 1: {
  182. // If there's one subnode (optimisation).
  183. buff[it.size] = it.node->_min;
  184. // Mark this node as pre-processed and push it, so that the next
  185. // visit after the operation on the child can do the removals.
  186. it.processed_for_removal = true;
  187. stack.push_back (it);
  188. struct iter next = {it.node->_next.node,
  189. NULL,
  190. NULL,
  191. ++it.size,
  192. 0,
  193. 0,
  194. 0,
  195. false};
  196. stack.push_back (next);
  197. break;
  198. }
  199. default: {
  200. // If there are multiple subnodes.
  201. // When first visiting this node, initialize the new_min/max parameters
  202. // which will then be used after each child has been processed, on the
  203. // post-children iterations.
  204. if (it.current_child == 0) {
  205. // New min non-null character in the node table after the removal
  206. it.new_min = it.node->_min + it.node->_count - 1;
  207. // New max non-null character in the node table after the removal
  208. it.new_max = it.node->_min;
  209. }
  210. // Mark this node as pre-processed and push it, so that the next
  211. // visit after the operation on the child can do the removals.
  212. buff[it.size] = it.node->_min + it.current_child;
  213. it.processed_for_removal = true;
  214. stack.push_back (it);
  215. if (it.node->_next.table[it.current_child]) {
  216. struct iter next = {
  217. it.node->_next.table[it.current_child],
  218. NULL,
  219. NULL,
  220. it.size + 1,
  221. 0,
  222. 0,
  223. 0,
  224. false};
  225. stack.push_back (next);
  226. }
  227. }
  228. }
  229. } else {
  230. // Reset back for the next time, in case this node doesn't get deleted.
  231. // This is done unconditionally, unlike when setting this variable to true.
  232. it.processed_for_removal = false;
  233. switch (it.node->_count) {
  234. case 0:
  235. // If there are no subnodes in the trie, we are done with this node
  236. // post-processing.
  237. break;
  238. case 1:
  239. // If there's one subnode (optimisation).
  240. // Prune the node if it was made redundant by the removal
  241. if (it.node->_next.node->is_redundant ()) {
  242. LIBZMQ_DELETE (it.node->_next.node);
  243. it.node->_count = 0;
  244. --it.node->_live_nodes;
  245. zmq_assert (it.node->_live_nodes == 0);
  246. }
  247. break;
  248. default:
  249. // If there are multiple subnodes.
  250. {
  251. if (it.node->_next.table[it.current_child]) {
  252. // Prune redundant nodes from the mtrie
  253. if (it.node->_next.table[it.current_child]
  254. ->is_redundant ()) {
  255. LIBZMQ_DELETE (
  256. it.node->_next.table[it.current_child]);
  257. zmq_assert (it.node->_live_nodes > 0);
  258. --it.node->_live_nodes;
  259. } else {
  260. // The node is not redundant, so it's a candidate for being
  261. // the new min/max node.
  262. //
  263. // We loop through the node array from left to right, so the
  264. // first non-null, non-redundant node encountered is the new
  265. // minimum index. Conversely, the last non-redundant, non-null
  266. // node encountered is the new maximum index.
  267. if (it.current_child + it.node->_min
  268. < it.new_min)
  269. it.new_min =
  270. it.current_child + it.node->_min;
  271. if (it.current_child + it.node->_min
  272. > it.new_max)
  273. it.new_max =
  274. it.current_child + it.node->_min;
  275. }
  276. }
  277. // If there are more children to visit, push again the current
  278. // node, so that pre-processing can happen on the next child.
  279. // If we are done, reset the child index so that the ::rm is
  280. // fully idempotent.
  281. ++it.current_child;
  282. if (it.current_child >= it.node->_count)
  283. it.current_child = 0;
  284. else {
  285. stack.push_back (it);
  286. continue;
  287. }
  288. // All children have been visited and removed if needed, and
  289. // all pre- and post-visit operations have been carried.
  290. // Resize/free the node table if needed.
  291. zmq_assert (it.node->_count > 1);
  292. // Free the node table if it's no longer used.
  293. switch (it.node->_live_nodes) {
  294. case 0:
  295. free (it.node->_next.table);
  296. it.node->_next.table = NULL;
  297. it.node->_count = 0;
  298. break;
  299. case 1:
  300. // Compact the node table if possible
  301. // If there's only one live node in the table we can
  302. // switch to using the more compact single-node
  303. // representation
  304. zmq_assert (it.new_min == it.new_max);
  305. zmq_assert (it.new_min >= it.node->_min);
  306. zmq_assert (it.new_min
  307. < it.node->_min + it.node->_count);
  308. {
  309. generic_mtrie_t *node =
  310. it.node->_next
  311. .table[it.new_min - it.node->_min];
  312. zmq_assert (node);
  313. free (it.node->_next.table);
  314. it.node->_next.node = node;
  315. }
  316. it.node->_count = 1;
  317. it.node->_min = it.new_min;
  318. break;
  319. default:
  320. if (it.new_min > it.node->_min
  321. || it.new_max < it.node->_min
  322. + it.node->_count - 1) {
  323. zmq_assert (it.new_max - it.new_min + 1
  324. > 1);
  325. generic_mtrie_t **old_table =
  326. it.node->_next.table;
  327. zmq_assert (it.new_min > it.node->_min
  328. || it.new_max
  329. < it.node->_min
  330. + it.node->_count - 1);
  331. zmq_assert (it.new_min >= it.node->_min);
  332. zmq_assert (it.new_max
  333. <= it.node->_min
  334. + it.node->_count - 1);
  335. zmq_assert (it.new_max - it.new_min + 1
  336. < it.node->_count);
  337. it.node->_count =
  338. it.new_max - it.new_min + 1;
  339. it.node->_next.table =
  340. static_cast<generic_mtrie_t **> (
  341. malloc (sizeof (generic_mtrie_t *)
  342. * it.node->_count));
  343. alloc_assert (it.node->_next.table);
  344. memmove (it.node->_next.table,
  345. old_table
  346. + (it.new_min - it.node->_min),
  347. sizeof (generic_mtrie_t *)
  348. * it.node->_count);
  349. free (old_table);
  350. it.node->_min = it.new_min;
  351. }
  352. }
  353. }
  354. }
  355. }
  356. }
  357. free (buff);
  358. }
  359. template <typename T>
  360. typename generic_mtrie_t<T>::rm_result
  361. generic_mtrie_t<T>::rm (prefix_t prefix_, size_t size_, value_t *pipe_)
  362. {
  363. // This used to be implemented as a non-tail recursive traversal of the trie,
  364. // which means remote clients controlled the depth of the recursion and the
  365. // stack size.
  366. // To simulate the non-tail recursion, with post-recursion changes depending on
  367. // the result of the recursive call, a stack is used to re-visit the same node
  368. // and operate on it again after children have been visited.
  369. // A boolean is used to record whether the node had already been visited and to
  370. // determine if the pre- or post- children visit actions have to be taken.
  371. rm_result ret = not_found;
  372. std::list<struct iter> stack;
  373. struct iter it = {this, NULL, prefix_, size_, 0, 0, 0, false};
  374. stack.push_back (it);
  375. while (!stack.empty ()) {
  376. it = stack.back ();
  377. stack.pop_back ();
  378. if (!it.processed_for_removal) {
  379. if (!it.size) {
  380. if (!it.node->_pipes) {
  381. ret = not_found;
  382. continue;
  383. }
  384. typename pipes_t::size_type erased =
  385. it.node->_pipes->erase (pipe_);
  386. if (it.node->_pipes->empty ()) {
  387. zmq_assert (erased == 1);
  388. LIBZMQ_DELETE (it.node->_pipes);
  389. ret = last_value_removed;
  390. continue;
  391. }
  392. ret = (erased == 1) ? values_remain : not_found;
  393. continue;
  394. }
  395. it.current_child = *it.prefix;
  396. if (!it.node->_count || it.current_child < it.node->_min
  397. || it.current_child >= it.node->_min + it.node->_count) {
  398. ret = not_found;
  399. continue;
  400. }
  401. it.next_node =
  402. it.node->_count == 1
  403. ? it.node->_next.node
  404. : it.node->_next.table[it.current_child - it.node->_min];
  405. if (!it.next_node) {
  406. ret = not_found;
  407. continue;
  408. }
  409. it.processed_for_removal = true;
  410. stack.push_back (it);
  411. struct iter next = {
  412. it.next_node, NULL, it.prefix + 1, it.size - 1, 0, 0, 0, false};
  413. stack.push_back (next);
  414. } else {
  415. it.processed_for_removal = false;
  416. if (it.next_node->is_redundant ()) {
  417. LIBZMQ_DELETE (it.next_node);
  418. zmq_assert (it.node->_count > 0);
  419. if (it.node->_count == 1) {
  420. it.node->_next.node = NULL;
  421. it.node->_count = 0;
  422. --it.node->_live_nodes;
  423. zmq_assert (it.node->_live_nodes == 0);
  424. } else {
  425. it.node->_next.table[it.current_child - it.node->_min] = 0;
  426. zmq_assert (it.node->_live_nodes > 1);
  427. --it.node->_live_nodes;
  428. // Compact the table if possible
  429. if (it.node->_live_nodes == 1) {
  430. // If there's only one live node in the table we can
  431. // switch to using the more compact single-node
  432. // representation
  433. unsigned short i;
  434. for (i = 0; i < it.node->_count; ++i)
  435. if (it.node->_next.table[i])
  436. break;
  437. zmq_assert (i < it.node->_count);
  438. it.node->_min += i;
  439. it.node->_count = 1;
  440. generic_mtrie_t *oldp = it.node->_next.table[i];
  441. free (it.node->_next.table);
  442. it.node->_next.table = NULL;
  443. it.node->_next.node = oldp;
  444. } else if (it.current_child == it.node->_min) {
  445. // We can compact the table "from the left"
  446. unsigned short i;
  447. for (i = 1; i < it.node->_count; ++i)
  448. if (it.node->_next.table[i])
  449. break;
  450. zmq_assert (i < it.node->_count);
  451. it.node->_min += i;
  452. it.node->_count -= i;
  453. generic_mtrie_t **old_table = it.node->_next.table;
  454. it.node->_next.table =
  455. static_cast<generic_mtrie_t **> (malloc (
  456. sizeof (generic_mtrie_t *) * it.node->_count));
  457. alloc_assert (it.node->_next.table);
  458. memmove (it.node->_next.table, old_table + i,
  459. sizeof (generic_mtrie_t *) * it.node->_count);
  460. free (old_table);
  461. } else if (it.current_child
  462. == it.node->_min + it.node->_count - 1) {
  463. // We can compact the table "from the right"
  464. unsigned short i;
  465. for (i = 1; i < it.node->_count; ++i)
  466. if (it.node->_next.table[it.node->_count - 1 - i])
  467. break;
  468. zmq_assert (i < it.node->_count);
  469. it.node->_count -= i;
  470. generic_mtrie_t **old_table = it.node->_next.table;
  471. it.node->_next.table =
  472. static_cast<generic_mtrie_t **> (malloc (
  473. sizeof (generic_mtrie_t *) * it.node->_count));
  474. alloc_assert (it.node->_next.table);
  475. memmove (it.node->_next.table, old_table,
  476. sizeof (generic_mtrie_t *) * it.node->_count);
  477. free (old_table);
  478. }
  479. }
  480. }
  481. }
  482. }
  483. if (ret == last_value_removed) {
  484. zmq_assert (_num_prefixes.get () > 0);
  485. _num_prefixes.sub (1);
  486. }
  487. return ret;
  488. }
  489. template <typename T>
  490. template <typename Arg>
  491. void generic_mtrie_t<T>::match (prefix_t data_,
  492. size_t size_,
  493. void (*func_) (value_t *pipe_, Arg arg_),
  494. Arg arg_)
  495. {
  496. for (generic_mtrie_t *current = this; current; data_++, size_--) {
  497. // Signal the pipes attached to this node.
  498. if (current->_pipes) {
  499. for (typename pipes_t::iterator it = current->_pipes->begin (),
  500. end = current->_pipes->end ();
  501. it != end; ++it) {
  502. func_ (*it, arg_);
  503. }
  504. }
  505. // If we are at the end of the message, there's nothing more to match.
  506. if (!size_)
  507. break;
  508. // If there are no subnodes in the trie, return.
  509. if (current->_count == 0)
  510. break;
  511. if (current->_count == 1) {
  512. // If there's one subnode (optimisation).
  513. if (data_[0] != current->_min) {
  514. break;
  515. }
  516. current = current->_next.node;
  517. } else {
  518. // If there are multiple subnodes.
  519. if (data_[0] < current->_min
  520. || data_[0] >= current->_min + current->_count) {
  521. break;
  522. }
  523. current = current->_next.table[data_[0] - current->_min];
  524. }
  525. }
  526. }
  527. template <typename T> bool generic_mtrie_t<T>::is_redundant () const
  528. {
  529. return !_pipes && _live_nodes == 0;
  530. }
  531. }
  532. #endif