sql_list.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. #ifndef INCLUDES_MYSQL_SQL_LIST_H
  2. #define INCLUDES_MYSQL_SQL_LIST_H
  3. /* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License, version 2.0,
  6. as published by the Free Software Foundation.
  7. This program is also distributed with certain software (including
  8. but not limited to OpenSSL) that is licensed under separate terms,
  9. as designated in a particular file or component or in included license
  10. documentation. The authors of MySQL hereby grant you an additional
  11. permission to link the program and your derivative works with the
  12. separately licensed software that they have included with MySQL.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License, version 2.0, for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  20. #include <stddef.h>
  21. #include <sys/types.h>
  22. #include <algorithm>
  23. #include <iterator>
  24. #include <type_traits>
  25. #include "my_alloc.h"
  26. #include "my_compiler.h"
  27. #include "my_dbug.h"
  28. #include "my_sharedlib.h"
  29. #include "sql/thr_malloc.h"
  30. /**
  31. Simple intrusive linked list.
  32. @remark Similar in nature to base_list, but intrusive. It keeps a
  33. a pointer to the first element in the list and a indirect
  34. reference to the last element.
  35. */
  36. template <typename T>
  37. class SQL_I_List {
  38. public:
  39. uint elements;
  40. /** The first element in the list. */
  41. T *first;
  42. /** A reference to the next element in the list. */
  43. T **next;
  44. SQL_I_List() { empty(); }
  45. SQL_I_List(const SQL_I_List &tmp)
  46. : elements(tmp.elements),
  47. first(tmp.first),
  48. next(elements ? tmp.next : &first) {}
  49. SQL_I_List(SQL_I_List &&) = default;
  50. inline void empty() {
  51. elements = 0;
  52. first = NULL;
  53. next = &first;
  54. }
  55. inline void link_in_list(T *element, T **next_ptr) {
  56. elements++;
  57. (*next) = element;
  58. next = next_ptr;
  59. *next = NULL;
  60. }
  61. inline void save_and_clear(SQL_I_List<T> *save) {
  62. *save = *this;
  63. empty();
  64. }
  65. inline void push_front(SQL_I_List<T> *save) {
  66. /* link current list last */
  67. *save->next = first;
  68. first = save->first;
  69. elements += save->elements;
  70. }
  71. inline void push_back(SQL_I_List<T> *save) {
  72. if (save->first) {
  73. *next = save->first;
  74. next = save->next;
  75. elements += save->elements;
  76. }
  77. }
  78. inline uint size() const { return elements; }
  79. SQL_I_List &operator=(SQL_I_List &) = default;
  80. SQL_I_List &operator=(SQL_I_List &&) = default;
  81. };
  82. /*
  83. Basic single linked list
  84. Used for item and item_buffs.
  85. All list ends with a pointer to the 'end_of_list' element, which
  86. data pointer is a null pointer and the next pointer points to itself.
  87. This makes it very fast to traverse lists as we don't have to
  88. test for a specialend condition for list that can't contain a null
  89. pointer.
  90. */
  91. /**
  92. list_node - a node of a single-linked list.
  93. @note We never call a destructor for instances of this class.
  94. */
  95. struct list_node {
  96. list_node *next;
  97. void *info;
  98. list_node(void *info_par, list_node *next_par)
  99. : next(next_par), info(info_par) {}
  100. list_node() /* For end_of_list */
  101. {
  102. info = 0;
  103. next = this;
  104. }
  105. };
  106. extern MYSQL_PLUGIN_IMPORT list_node end_of_list;
  107. class base_list {
  108. protected:
  109. list_node *first, **last;
  110. public:
  111. uint elements;
  112. bool operator==(const base_list &rhs) const {
  113. return elements == rhs.elements && first == rhs.first && last == rhs.last;
  114. }
  115. inline void empty() {
  116. elements = 0;
  117. first = &end_of_list;
  118. last = &first;
  119. }
  120. inline base_list() { empty(); }
  121. /**
  122. This is a shallow copy constructor that implicitly passes the ownership
  123. from the source list to the new instance. The old instance is not
  124. updated, so both objects end up sharing the same nodes. If one of
  125. the instances then adds or removes a node, the other becomes out of
  126. sync ('last' pointer), while still operational. Some old code uses and
  127. relies on this behaviour. This logic is quite tricky: please do not use
  128. it in any new code.
  129. */
  130. base_list(const base_list &tmp)
  131. : first(tmp.first),
  132. last(tmp.elements ? tmp.last : &first),
  133. elements(tmp.elements) {}
  134. base_list &operator=(const base_list &tmp) {
  135. elements = tmp.elements;
  136. first = tmp.first;
  137. last = elements ? tmp.last : &first;
  138. return *this;
  139. }
  140. /**
  141. Construct a deep copy of the argument in memory root mem_root.
  142. The elements themselves are copied by pointer.
  143. */
  144. base_list(const base_list &rhs, MEM_ROOT *mem_root);
  145. inline bool push_back(void *info) {
  146. if (((*last) = new (*THR_MALLOC) list_node(info, &end_of_list))) {
  147. last = &(*last)->next;
  148. elements++;
  149. return 0;
  150. }
  151. return 1;
  152. }
  153. inline bool push_back(void *info, MEM_ROOT *mem_root) {
  154. if (((*last) = new (mem_root) list_node(info, &end_of_list))) {
  155. last = &(*last)->next;
  156. elements++;
  157. return 0;
  158. }
  159. return 1;
  160. }
  161. inline bool push_front(void *info) {
  162. list_node *node = new (*THR_MALLOC) list_node(info, first);
  163. if (node) {
  164. if (last == &first) last = &node->next;
  165. first = node;
  166. elements++;
  167. return 0;
  168. }
  169. return 1;
  170. }
  171. inline bool push_front(void *info, MEM_ROOT *mem_root) {
  172. list_node *node = new (mem_root) list_node(info, first);
  173. if (node) {
  174. if (last == &first) last = &node->next;
  175. first = node;
  176. elements++;
  177. return false;
  178. }
  179. return true;
  180. }
  181. void remove(list_node **prev) {
  182. list_node *node = (*prev)->next;
  183. if (!--elements)
  184. last = &first;
  185. else if (last == &(*prev)->next)
  186. last = prev;
  187. destroy(*prev);
  188. *prev = node;
  189. }
  190. inline void concat(base_list *list) {
  191. if (!list->is_empty()) {
  192. *last = list->first;
  193. last = list->last;
  194. elements += list->elements;
  195. }
  196. }
  197. inline void *pop(void) {
  198. if (first == &end_of_list) return 0;
  199. list_node *tmp = first;
  200. first = first->next;
  201. if (!--elements) last = &first;
  202. return tmp->info;
  203. }
  204. inline void disjoin(base_list *list) {
  205. list_node **prev = &first;
  206. list_node *node = first;
  207. list_node *list_first = list->first;
  208. elements = 0;
  209. while (node && node != list_first) {
  210. prev = &node->next;
  211. node = node->next;
  212. elements++;
  213. }
  214. *prev = *last;
  215. last = prev;
  216. }
  217. inline void prepend(base_list *list) {
  218. if (!list->is_empty()) {
  219. *list->last = first;
  220. if (is_empty()) last = list->last;
  221. first = list->first;
  222. elements += list->elements;
  223. }
  224. }
  225. /**
  226. Swap two lists.
  227. */
  228. inline void swap(base_list &rhs) {
  229. std::swap(first, rhs.first);
  230. std::swap(last, rhs.last);
  231. std::swap(elements, rhs.elements);
  232. }
  233. inline list_node *last_node() { return *last; }
  234. inline list_node *first_node() { return first; }
  235. inline void *head() { return first->info; }
  236. inline const void *head() const { return first->info; }
  237. inline void **head_ref() { return first != &end_of_list ? &first->info : 0; }
  238. inline bool is_empty() const { return first == &end_of_list; }
  239. inline list_node *last_ref() { return &end_of_list; }
  240. inline uint size() const { return elements; }
  241. friend class base_list_iterator;
  242. friend class error_list;
  243. friend class error_list_iterator;
  244. #ifdef LIST_EXTRA_DEBUG
  245. /*
  246. Check list invariants and print results into trace. Invariants are:
  247. - (*last) points to end_of_list
  248. - There are no NULLs in the list.
  249. - base_list::elements is the number of elements in the list.
  250. SYNOPSIS
  251. check_list()
  252. name Name to print to trace file
  253. RETURN
  254. 1 The list is Ok.
  255. 0 List invariants are not met.
  256. */
  257. bool check_list(const char *name) {
  258. base_list *list = this;
  259. list_node *node = first;
  260. uint cnt = 0;
  261. while (node->next != &end_of_list) {
  262. if (!node->info) {
  263. DBUG_PRINT("list_invariants",
  264. ("%s: error: NULL element in the list", name));
  265. return false;
  266. }
  267. node = node->next;
  268. cnt++;
  269. }
  270. if (last != &(node->next)) {
  271. DBUG_PRINT("list_invariants", ("%s: error: wrong last pointer", name));
  272. return false;
  273. }
  274. if (cnt + 1 != elements) {
  275. DBUG_PRINT("list_invariants", ("%s: error: wrong element count", name));
  276. return false;
  277. }
  278. DBUG_PRINT("list_invariants", ("%s: list is ok", name));
  279. return true;
  280. }
  281. #endif // LIST_EXTRA_DEBUG
  282. protected:
  283. void after(void *info, list_node *node) {
  284. list_node *new_node = new (*THR_MALLOC) list_node(info, node->next);
  285. node->next = new_node;
  286. elements++;
  287. if (last == &(node->next)) last = &new_node->next;
  288. }
  289. bool after(void *info, list_node *node, MEM_ROOT *mem_root) {
  290. list_node *new_node = new (mem_root) list_node(info, node->next);
  291. if (!new_node) return true; // OOM
  292. node->next = new_node;
  293. elements++;
  294. if (last == &(node->next)) last = &new_node->next;
  295. return false;
  296. }
  297. };
  298. class base_list_iterator {
  299. protected:
  300. base_list *list;
  301. list_node **el, **prev, *current;
  302. void sublist(base_list &ls, uint elm) {
  303. ls.first = *el;
  304. ls.last = list->last;
  305. ls.elements = elm;
  306. }
  307. public:
  308. base_list_iterator() : list(0), el(0), prev(0), current(0) {}
  309. base_list_iterator(base_list &list_par) { init(list_par); }
  310. inline void init(base_list &list_par) {
  311. list = &list_par;
  312. el = &list_par.first;
  313. prev = 0;
  314. current = 0;
  315. }
  316. inline void *next(void) {
  317. prev = el;
  318. current = *el;
  319. el = &current->next;
  320. return current->info;
  321. }
  322. inline void *next_fast(void) {
  323. list_node *tmp;
  324. tmp = *el;
  325. el = &tmp->next;
  326. return tmp->info;
  327. }
  328. inline void rewind(void) { el = &list->first; }
  329. inline void *replace(void *element) { // Return old element
  330. void *tmp = current->info;
  331. DBUG_ASSERT(current->info != 0);
  332. current->info = element;
  333. return tmp;
  334. }
  335. void *replace(base_list &new_list) {
  336. void *ret_value = current->info;
  337. if (!new_list.is_empty()) {
  338. *new_list.last = current->next;
  339. current->info = new_list.first->info;
  340. current->next = new_list.first->next;
  341. if ((list->last == &current->next) && (new_list.elements > 1))
  342. list->last = new_list.last;
  343. list->elements += new_list.elements - 1;
  344. }
  345. return ret_value; // return old element
  346. }
  347. inline void remove(void) // Remove current
  348. {
  349. list->remove(prev);
  350. el = prev;
  351. current = 0; // Safeguard
  352. }
  353. void after(void *element) // Insert element after current
  354. {
  355. list->after(element, current);
  356. current = current->next;
  357. el = &current->next;
  358. }
  359. bool after(void *a, MEM_ROOT *mem_root) {
  360. if (list->after(a, current, mem_root)) return true;
  361. current = current->next;
  362. el = &current->next;
  363. return false;
  364. }
  365. inline void **ref(void) // Get reference pointer
  366. {
  367. return &current->info;
  368. }
  369. inline bool is_last(void) { return el == list->last; }
  370. inline bool is_before_first() const { return current == NULL; }
  371. bool prepend(void *a, MEM_ROOT *mem_root) {
  372. if (list->push_front(a, mem_root)) return true;
  373. el = &list->first;
  374. prev = el;
  375. el = &(*el)->next;
  376. return false;
  377. }
  378. friend class error_list_iterator;
  379. };
  380. template <class T>
  381. class List_STL_Iterator;
  382. template <class T>
  383. class List : public base_list {
  384. public:
  385. List() : base_list() {}
  386. inline List(const List<T> &tmp) : base_list(tmp) {}
  387. List &operator=(const List &tmp) {
  388. return static_cast<List &>(base_list::operator=(tmp));
  389. }
  390. inline List(const List<T> &tmp, MEM_ROOT *mem_root)
  391. : base_list(tmp, mem_root) {}
  392. /*
  393. Typecasting to (void *) it's necessary if we want to declare List<T> with
  394. constant T parameter (like List<const char>), since the untyped storage
  395. is "void *", and assignment of const pointer to "void *" is a syntax error.
  396. */
  397. inline bool push_back(T *a) { return base_list::push_back((void *)a); }
  398. inline bool push_back(T *a, MEM_ROOT *mem_root) {
  399. return base_list::push_back((void *)a, mem_root);
  400. }
  401. inline bool push_front(T *a) { return base_list::push_front((void *)a); }
  402. inline bool push_front(T *a, MEM_ROOT *mem_root) {
  403. return base_list::push_front((void *)a, mem_root);
  404. }
  405. inline T *head() { return static_cast<T *>(base_list::head()); }
  406. inline const T *head() const {
  407. return static_cast<const T *>(base_list::head());
  408. }
  409. inline T **head_ref() { return (T **)base_list::head_ref(); }
  410. inline T *pop() { return (T *)base_list::pop(); }
  411. inline void concat(List<T> *list) { base_list::concat(list); }
  412. inline void disjoin(List<T> *list) { base_list::disjoin(list); }
  413. inline void prepend(List<T> *list) { base_list::prepend(list); }
  414. void delete_elements(void) {
  415. list_node *element, *next;
  416. for (element = first; element != &end_of_list; element = next) {
  417. next = element->next;
  418. delete (T *)element->info;
  419. }
  420. empty();
  421. }
  422. void destroy_elements(void) {
  423. list_node *element, *next;
  424. for (element = first; element != &end_of_list; element = next) {
  425. next = element->next;
  426. destroy((T *)element->info);
  427. }
  428. empty();
  429. }
  430. T *operator[](uint index) const {
  431. DBUG_ASSERT(index < elements);
  432. list_node *current = first;
  433. for (uint i = 0; i < index; ++i) current = current->next;
  434. return static_cast<T *>(current->info);
  435. }
  436. void replace(uint index, T *new_value) {
  437. DBUG_ASSERT(index < elements);
  438. list_node *current = first;
  439. for (uint i = 0; i < index; ++i) current = current->next;
  440. current->info = new_value;
  441. }
  442. bool swap_elts(uint index1, uint index2) {
  443. if (index1 == index2) return false;
  444. if (index1 >= elements || index2 >= elements) return true; // error
  445. if (index2 < index1) std::swap(index1, index2);
  446. list_node *current1 = first;
  447. for (uint i = 0; i < index1; ++i) current1 = current1->next;
  448. list_node *current2 = current1;
  449. for (uint i = 0; i < index2 - index1; ++i) current2 = current2->next;
  450. std::swap(current1->info, current2->info);
  451. return false;
  452. }
  453. /**
  454. @brief
  455. Sort the list
  456. @param cmp node comparison function
  457. @details
  458. The function sorts list nodes by an exchange sort algorithm.
  459. The order of list nodes isn't changed, values of info fields are
  460. swapped instead. Due to this, list iterators that are initialized before
  461. sort could be safely used after sort, i.e they wouldn't cause a crash.
  462. As this isn't an effective algorithm the list to be sorted is supposed to
  463. be short.
  464. */
  465. template <typename Node_cmp_func>
  466. void sort(Node_cmp_func cmp) {
  467. if (elements < 2) return;
  468. for (list_node *n1 = first; n1 && n1 != &end_of_list; n1 = n1->next) {
  469. for (list_node *n2 = n1->next; n2 && n2 != &end_of_list; n2 = n2->next) {
  470. if (cmp(static_cast<T *>(n1->info), static_cast<T *>(n2->info)) > 0) {
  471. void *tmp = n1->info;
  472. n1->info = n2->info;
  473. n2->info = tmp;
  474. }
  475. }
  476. }
  477. }
  478. // For C++11 range-based for loops.
  479. using iterator = List_STL_Iterator<T>;
  480. iterator begin() { return iterator(first); }
  481. iterator end() {
  482. // If the list overlaps another list, last isn't actually
  483. // the last element, and if so, we'd give a different result from
  484. // List_iterator_fast.
  485. DBUG_ASSERT((*last)->next == &end_of_list);
  486. return iterator(*last);
  487. }
  488. using const_iterator = List_STL_Iterator<const T>;
  489. const_iterator begin() const { return const_iterator(first); }
  490. const_iterator end() const {
  491. DBUG_ASSERT((*last)->next == &end_of_list);
  492. return const_iterator(*last);
  493. }
  494. const_iterator cbegin() const { return const_iterator(first); }
  495. const_iterator cend() const {
  496. DBUG_ASSERT((*last)->next == &end_of_list);
  497. return const_iterator(*last);
  498. }
  499. };
  500. template <class T>
  501. class List_iterator : public base_list_iterator {
  502. public:
  503. List_iterator(List<T> &a) : base_list_iterator(a) {}
  504. List_iterator() : base_list_iterator() {}
  505. inline void init(List<T> &a) { base_list_iterator::init(a); }
  506. inline T *operator++(int) { return (T *)base_list_iterator::next(); }
  507. inline T *replace(T *a) { return (T *)base_list_iterator::replace(a); }
  508. inline T *replace(List<T> &a) { return (T *)base_list_iterator::replace(a); }
  509. inline void rewind(void) { base_list_iterator::rewind(); }
  510. inline void remove() { base_list_iterator::remove(); }
  511. inline void after(T *a) { base_list_iterator::after(a); }
  512. inline bool after(T *a, MEM_ROOT *mem_root) {
  513. return base_list_iterator::after(a, mem_root);
  514. }
  515. inline T **ref(void) { return (T **)base_list_iterator::ref(); }
  516. };
  517. template <class T>
  518. class List_iterator_fast : public base_list_iterator {
  519. protected:
  520. inline T *replace(T *) { return (T *)0; }
  521. inline T *replace(List<T> &) { return (T *)0; }
  522. inline void remove(void) {}
  523. inline void after(T *) {}
  524. inline T **ref(void) { return (T **)0; }
  525. public:
  526. inline List_iterator_fast(List<T> &a) : base_list_iterator(a) {}
  527. inline List_iterator_fast() : base_list_iterator() {}
  528. inline void init(List<T> &a) { base_list_iterator::init(a); }
  529. inline T *operator++(int) { return (T *)base_list_iterator::next_fast(); }
  530. inline void rewind(void) { base_list_iterator::rewind(); }
  531. void sublist(List<T> &list_arg, uint el_arg) {
  532. base_list_iterator::sublist(list_arg, el_arg);
  533. }
  534. };
  535. /*
  536. Like List_iterator<T>, but with an STL-compatible interface
  537. (ForwardIterator), so that you can use it in range-based for loops.
  538. Prefer this to List_iterator<T> wherever possible, but also prefer
  539. std::vector<T> or std::list<T> to List<T> wherever possible.
  540. */
  541. template <class T>
  542. class List_STL_Iterator {
  543. public:
  544. explicit List_STL_Iterator(list_node *node) : m_current(node) {}
  545. // Iterator (required for InputIterator).
  546. T &operator*() const { return *static_cast<T *>(m_current->info); }
  547. List_STL_Iterator &operator++() {
  548. m_current = m_current->next;
  549. return *this;
  550. }
  551. using difference_type = ptrdiff_t;
  552. using value_type = T; // NOTE: std::remove_cv_t<T> from C++20.
  553. using pointer = T *;
  554. using reference = T &;
  555. using iterator_category = std::forward_iterator_tag;
  556. // EqualityComparable (required for InputIterator).
  557. bool operator==(const List_STL_Iterator &other) const {
  558. return m_current == other.m_current;
  559. }
  560. // InputIterator (required for ForwardIterator).
  561. bool operator!=(const List_STL_Iterator &other) const {
  562. return !(*this == other);
  563. }
  564. T *operator->() const { return static_cast<T *>(m_current->info); }
  565. // DefaultConstructible (required for ForwardIterator).
  566. List_STL_Iterator() {}
  567. // ForwardIterator.
  568. List_STL_Iterator operator++(int) {
  569. List_STL_Iterator copy = *this;
  570. m_current = m_current->next;
  571. return copy;
  572. }
  573. private:
  574. list_node *m_current;
  575. };
  576. template <typename T>
  577. class base_ilist;
  578. template <typename T>
  579. class base_ilist_iterator;
  580. /*
  581. A simple intrusive list.
  582. NOTE: this inherently unsafe, since we rely on <T> to have
  583. the same layout as ilink<T> (see base_ilist::sentinel).
  584. Please consider using a different strategy for linking objects.
  585. */
  586. template <typename T>
  587. class ilink {
  588. T **prev, *next;
  589. public:
  590. ilink() : prev(NULL), next(NULL) {}
  591. void unlink() {
  592. /* Extra tests because element doesn't have to be linked */
  593. if (prev) *prev = next;
  594. if (next) next->prev = prev;
  595. prev = NULL;
  596. next = NULL;
  597. }
  598. friend class base_ilist<T>;
  599. friend class base_ilist_iterator<T>;
  600. };
  601. /* Needed to be able to have an I_List of char* strings in mysqld.cc. */
  602. class i_string : public ilink<i_string> {
  603. public:
  604. const char *ptr;
  605. i_string() : ptr(0) {}
  606. i_string(const char *s) : ptr(s) {}
  607. };
  608. /* needed for linked list of two strings for replicate-rewrite-db */
  609. class i_string_pair : public ilink<i_string_pair> {
  610. public:
  611. const char *key;
  612. const char *val;
  613. i_string_pair() : key(0), val(0) {}
  614. i_string_pair(const char *key_arg, const char *val_arg)
  615. : key(key_arg), val(val_arg) {}
  616. };
  617. template <class T>
  618. class I_List_iterator;
  619. template <typename T>
  620. class base_ilist {
  621. T *first;
  622. ilink<T> sentinel;
  623. static_assert(!std::is_polymorphic<T>::value,
  624. "Do not use this for classes with virtual members");
  625. public:
  626. // The sentinel is not a T, but at least it is a POD
  627. void empty() SUPPRESS_UBSAN {
  628. first = static_cast<T *>(&sentinel);
  629. sentinel.prev = &first;
  630. }
  631. base_ilist() { empty(); }
  632. // The sentinel is not a T, but at least it is a POD
  633. bool is_empty() const SUPPRESS_UBSAN {
  634. return first == static_cast<const T *>(&sentinel);
  635. }
  636. /// Pushes new element in front of list.
  637. void push_front(T *a) {
  638. first->prev = &a->next;
  639. a->next = first;
  640. a->prev = &first;
  641. first = a;
  642. }
  643. /// Pushes new element to the end of the list, i.e. in front of the sentinel.
  644. void push_back(T *a) {
  645. *sentinel.prev = a;
  646. a->next = static_cast<T *>(&sentinel);
  647. a->prev = sentinel.prev;
  648. sentinel.prev = &a->next;
  649. }
  650. // Unlink first element, and return it.
  651. T *get() {
  652. if (is_empty()) return NULL;
  653. T *first_link = first;
  654. first_link->unlink();
  655. return first_link;
  656. }
  657. T *head() { return is_empty() ? NULL : first; }
  658. /**
  659. Moves list elements to new owner, and empties current owner (i.e. this).
  660. @param[in,out] new_owner The new owner of the list elements.
  661. Should be empty in input.
  662. */
  663. void move_elements_to(base_ilist *new_owner) {
  664. DBUG_ASSERT(new_owner->is_empty());
  665. new_owner->first = first;
  666. new_owner->sentinel = sentinel;
  667. empty();
  668. }
  669. friend class base_ilist_iterator<T>;
  670. private:
  671. /*
  672. We don't want to allow copying of this class, as that would give us
  673. two list heads containing the same elements.
  674. So we declare, but don't define copy CTOR and assignment operator.
  675. */
  676. base_ilist(const base_ilist &);
  677. void operator=(const base_ilist &);
  678. };
  679. template <typename T>
  680. class base_ilist_iterator {
  681. base_ilist<T> *list;
  682. T **el, *current;
  683. public:
  684. base_ilist_iterator(base_ilist<T> &list_par)
  685. : list(&list_par), el(&list_par.first), current(NULL) {}
  686. // The sentinel is not a T, but at least it is a POD
  687. T *next(void) SUPPRESS_UBSAN {
  688. /* This is coded to allow push_back() while iterating */
  689. current = *el;
  690. if (current == static_cast<T *>(&list->sentinel)) return NULL;
  691. el = &current->next;
  692. return current;
  693. }
  694. };
  695. template <class T>
  696. class I_List : private base_ilist<T> {
  697. public:
  698. using base_ilist<T>::empty;
  699. using base_ilist<T>::is_empty;
  700. using base_ilist<T>::get;
  701. using base_ilist<T>::push_front;
  702. using base_ilist<T>::push_back;
  703. using base_ilist<T>::head;
  704. void move_elements_to(I_List<T> *new_owner) {
  705. base_ilist<T>::move_elements_to(new_owner);
  706. }
  707. friend class I_List_iterator<T>;
  708. };
  709. template <class T>
  710. class I_List_iterator : public base_ilist_iterator<T> {
  711. public:
  712. I_List_iterator(I_List<T> &a) : base_ilist_iterator<T>(a) {}
  713. inline T *operator++(int) { return base_ilist_iterator<T>::next(); }
  714. };
  715. void free_list(I_List<i_string_pair> *list);
  716. void free_list(I_List<i_string> *list);
  717. template <class T>
  718. List<T> *List_merge(T *head, List<T> *tail) {
  719. tail->push_front(head);
  720. return tail;
  721. }
  722. #endif // INCLUDES_MYSQL_SQL_LIST_H