phpdbg_btree.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Felipe Pena <felipe@php.net> |
  16. | Authors: Joe Watkins <joe.watkins@live.co.uk> |
  17. | Authors: Bob Weinand <bwoebi@php.net> |
  18. +----------------------------------------------------------------------+
  19. */
  20. #include "phpdbg_btree.h"
  21. #include "phpdbg.h"
  22. #define CHOOSE_BRANCH(n) \
  23. branch = branch->branches[!!(n)];
  24. #ifdef _Win32
  25. # define emalloc malloc
  26. # define efree free
  27. #endif
  28. /* depth in bits */
  29. void phpdbg_btree_init(phpdbg_btree *tree, zend_ulong depth) {
  30. tree->depth = depth;
  31. tree->branch = NULL;
  32. tree->count = 0;
  33. }
  34. phpdbg_btree_result *phpdbg_btree_find(phpdbg_btree *tree, zend_ulong idx) {
  35. phpdbg_btree_branch *branch = tree->branch;
  36. int i = tree->depth - 1;
  37. if (branch == NULL) {
  38. return NULL;
  39. }
  40. do {
  41. if ((idx >> i) % 2 == 1) {
  42. if (branch->branches[1]) {
  43. CHOOSE_BRANCH(1);
  44. } else {
  45. return NULL;
  46. }
  47. } else {
  48. if (branch->branches[0]) {
  49. CHOOSE_BRANCH(0);
  50. } else {
  51. return NULL;
  52. }
  53. }
  54. } while (i--);
  55. return &branch->result;
  56. }
  57. phpdbg_btree_result *phpdbg_btree_find_closest(phpdbg_btree *tree, zend_ulong idx) {
  58. phpdbg_btree_branch *branch = tree->branch;
  59. int i = tree->depth - 1, last_superior_i = -1;
  60. zend_bool had_alternative_branch = 0;
  61. if (branch == NULL) {
  62. return NULL;
  63. }
  64. /* find nearest watchpoint */
  65. do {
  66. /* an impossible branch was found if: */
  67. if (!had_alternative_branch && (idx >> i) % 2 == 0 && !branch->branches[0]) {
  68. /* there's no lower branch than idx */
  69. if (last_superior_i == -1) {
  70. /* failure */
  71. return NULL;
  72. }
  73. /* reset state */
  74. branch = tree->branch;
  75. i = tree->depth - 1;
  76. /* follow branch according to bits in idx until the last lower branch before the impossible branch */
  77. do {
  78. CHOOSE_BRANCH((idx >> i) % 2 == 1 && branch->branches[1]);
  79. } while (--i > last_superior_i);
  80. /* use now the lower branch of which we can be sure that it contains only branches lower than idx */
  81. CHOOSE_BRANCH(0);
  82. /* and choose the highest possible branch in the branch containing only branches lower than idx */
  83. while (i--) {
  84. CHOOSE_BRANCH(branch->branches[1]);
  85. }
  86. break;
  87. }
  88. /* follow branch according to bits in idx until having found an impossible branch */
  89. if (had_alternative_branch || (idx >> i) % 2 == 1) {
  90. if (branch->branches[1]) {
  91. if (branch->branches[0]) {
  92. last_superior_i = i;
  93. }
  94. CHOOSE_BRANCH(1);
  95. } else {
  96. CHOOSE_BRANCH(0);
  97. had_alternative_branch = 1;
  98. }
  99. } else {
  100. CHOOSE_BRANCH(0);
  101. }
  102. } while (i--);
  103. return &branch->result;
  104. }
  105. phpdbg_btree_position phpdbg_btree_find_between(phpdbg_btree *tree, zend_ulong lower_idx, zend_ulong higher_idx) {
  106. phpdbg_btree_position pos;
  107. pos.tree = tree;
  108. pos.end = lower_idx;
  109. pos.cur = higher_idx;
  110. return pos;
  111. }
  112. phpdbg_btree_result *phpdbg_btree_next(phpdbg_btree_position *pos) {
  113. phpdbg_btree_result *result = phpdbg_btree_find_closest(pos->tree, pos->cur);
  114. if (result == NULL || result->idx < pos->end) {
  115. return NULL;
  116. }
  117. pos->cur = result->idx - 1;
  118. return result;
  119. }
  120. int phpdbg_btree_insert_or_update(phpdbg_btree *tree, zend_ulong idx, void *ptr, int flags) {
  121. int i = tree->depth - 1;
  122. phpdbg_btree_branch **branch = &tree->branch;
  123. do {
  124. if (*branch == NULL) {
  125. break;
  126. }
  127. branch = &(*branch)->branches[(idx >> i) % 2];
  128. } while (i--);
  129. if (*branch == NULL) {
  130. if (!(flags & PHPDBG_BTREE_INSERT)) {
  131. return FAILURE;
  132. }
  133. {
  134. phpdbg_btree_branch *memory = *branch = emalloc((i + 2) * sizeof(phpdbg_btree_branch));
  135. do {
  136. (*branch)->branches[!((idx >> i) % 2)] = NULL;
  137. branch = &(*branch)->branches[(idx >> i) % 2];
  138. *branch = ++memory;
  139. } while (i--);
  140. tree->count++;
  141. }
  142. } else if (!(flags & PHPDBG_BTREE_UPDATE)) {
  143. return FAILURE;
  144. }
  145. (*branch)->result.idx = idx;
  146. (*branch)->result.ptr = ptr;
  147. return SUCCESS;
  148. }
  149. int phpdbg_btree_delete(phpdbg_btree *tree, zend_ulong idx) {
  150. int i = tree->depth;
  151. phpdbg_btree_branch *branch = tree->branch;
  152. int i_last_dual_branch = -1, last_dual_branch_branch;
  153. phpdbg_btree_branch *last_dual_branch = NULL;
  154. goto check_branch_existence;
  155. do {
  156. if (branch->branches[0] && branch->branches[1]) {
  157. last_dual_branch = branch;
  158. i_last_dual_branch = i;
  159. last_dual_branch_branch = (idx >> i) % 2;
  160. }
  161. branch = branch->branches[(idx >> i) % 2];
  162. check_branch_existence:
  163. if (branch == NULL) {
  164. return FAILURE;
  165. }
  166. } while (i--);
  167. tree->count--;
  168. if (i_last_dual_branch == -1) {
  169. efree(tree->branch);
  170. tree->branch = NULL;
  171. } else {
  172. if (last_dual_branch->branches[last_dual_branch_branch] == last_dual_branch + 1) {
  173. phpdbg_btree_branch *original_branch = last_dual_branch->branches[!last_dual_branch_branch];
  174. memcpy(last_dual_branch + 1, last_dual_branch->branches[!last_dual_branch_branch], (i_last_dual_branch + 1) * sizeof(phpdbg_btree_branch));
  175. efree(last_dual_branch->branches[!last_dual_branch_branch]);
  176. last_dual_branch->branches[!last_dual_branch_branch] = last_dual_branch + 1;
  177. branch = last_dual_branch->branches[!last_dual_branch_branch];
  178. for (i = i_last_dual_branch; i--;) {
  179. branch = (branch->branches[branch->branches[1] == ++original_branch] = last_dual_branch + i_last_dual_branch - i + 1);
  180. }
  181. } else {
  182. efree(last_dual_branch->branches[last_dual_branch_branch]);
  183. }
  184. last_dual_branch->branches[last_dual_branch_branch] = NULL;
  185. }
  186. return SUCCESS;
  187. }