spt.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440
  1. /* GNU/Linux splay tree test program based on oct 2021 gcc version
  2. * this may use all ram and disk swap then the programs stops but GNU/Linux does not crash or crashe other programs
  3. * https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=libiberty/splay-tree.c;h=7c8973c63c8fead8e1363f4f42a5f686fb16ac8c;hb=HEAD
  4. * int stack_ptr in foreach() allows only splay tree with max size of 2G, should be size_t
  5. * splay_tree_xmalloc_allocate(int size, void *data ATTRIBUTE_UNUSED) should use size_t size
  6. * 1000*1000*280 are 280 million splay tree nodes hits already a limit on debian Linux
  7. * splay_tree_foreach4() reaches 423 million splay tree nodes on Fedora Linux on test machine
  8. * splay_tree_foreach() is the gcc original
  9. * splay_tree_foreach2() is the memory saving version
  10. * splay_tree_foreach3() does not use realloc()
  11. * splay_tree_foreach4() does not use realloc() and is slower
  12. * used compiler settings from airbus aerospace
  13. * see https://github.com/airbus-seclab/c-compiler-security
  14. * -Wtraditional-conversion [temp turned off -- too much]
  15. * AIRBUS_GCC_COMPILER_WARNING="$CFLAGS -O2 -Wall -Wextra -Wpedantic -Wformat=2 -Wformat-overflow=2 -Wformat-truncation=2 -Wformat-security -Wnull-dereference -Wstack-protector -Wtrampolines -Walloca -Wvla -Warray-bounds=2 -Wimplicit-fallthrough=3 -Wshift-overflow=2 -Wcast-qual -Wstringop-overflow=4 -Wconversion -Warith-conversion -Wlogical-op -Wduplicated-cond -Wduplicated-branches -Wformat-signedness -Wshadow -Wstrict-overflow=4 -Wundef -Wstrict-prototypes -Wswitch-default -Wswitch-enum -Wstack-usage=1000000 -Wcast-align=strict -D_FORTIFY_SOURCE=2 -fstack-protector-strong -fstack-clash-protection -fPIE -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -Wl,-z,separate-code"
  16. * for gcc -fanalyzer option newest gcc version 11.2 is needed
  17. * ./spt
  18. * testing old splay_tree_foreach()
  19. * status=0 10 tree nodes stack used max 100 entries using 0 megabyte 800 bytes 0 realloc()'s
  20. * testing old splay_tree_foreach()
  21. * status=0 1000 tree nodes stack used max 1600 entries using 0 megabyte 12800 bytes 4 realloc()'s
  22. * testing new splay_tree_foreach()
  23. * status=0 1000 tree nodes stack used max 1000 entries using 0 megabyte 8000 bytes 1 realloc()'s saved 0 Mb
  24. * testing old splay_tree_foreach()
  25. * status=0 100000000 tree nodes stack used max 104857600 entries using 800 megabyte 838860800 bytes 20 realloc()'s
  26. * testing new splay_tree_foreach()
  27. * status=0 100000000 tree nodes stack used max 100000000 entries using 762 megabyte 800000000 bytes 1 realloc()'s saved 37 Mb
  28. * testing old splay_tree_foreach()
  29. * status=0 280000000 tree nodes stack used max 419430400 entries using 3200 megabyte 3355443200 bytes 22 realloc()'s
  30. * testing new splay_tree_foreach()
  31. * status=0 280000000 tree nodes stack used max 280000000 entries using 2136 megabyte 2240000000 bytes 1 realloc()'s saved 1063 Mb
  32. * testing splay_tree_foreach() without realloc()
  33. * splay_tree_foreach3(): splay tree has 3000000 nodes
  34. * status=0 3000000 tree nodes stack used max 3000000 entries using 22 megabyte 24000000 bytes 0 realloc()'s
  35. * On debian Linux the limit is 280 million splay tree nodes
  36. * at more the program stops and is killed by some security software on debian Linux
  37. * testing old splay_tree_foreach()
  38. * status=0 350000000 tree nodes stack used max 419430400 entries using 3200 megabyte 3355443200 bytes 22 realloc()'s
  39. * testing new splay_tree_foreach()
  40. * status=0 350000000 tree nodes stack used max 350000000 entries using 2670 megabyte 2800000000 bytes 1 realloc()'s saved 529 Mb
  41. * On Fedora Linux the limit is 350 million splay tree nodes
  42. * at more the Fedora desktop causes a logout and the program stops
  43. * at using only the splay_tree_foreach2() ith less memory consumption
  44. * testing new splay_tree_foreach() with maximum test machine limit
  45. * status=0 370000000 tree nodes stack used max 370000000 entries using 2822 megabyte 2960000000 bytes 1 realloc()'s
  46. * At more all ram and all disk swap space is used on the test computer.
  47. * tested on a Linux computer with 128 GB memory it works with more splay tree nodes.
  48. * using slower splay_tree_foreach4() Linux server with 128 GB ram can handle 3384 million nodes.
  49. * Now try this on WSL
  50. * Need to measure how much slower splay_tree_foreach4() is compared to splay_tree_foreach()
  51. */
  52. /* A splay-tree datatype.
  53. Copyright (C) 1998-2021 Free Software Foundation, Inc.
  54. Contributed by Mark Mitchell (mark@markmitchell.com).
  55. This file is part of GNU CC.
  56. GNU CC is free software; you can redistribute it and/or modify it
  57. under the terms of the GNU General Public License as published by
  58. the Free Software Foundation; either version 2, or (at your option)
  59. any later version.
  60. GNU CC is distributed in the hope that it will be useful, but
  61. WITHOUT ANY WARRANTY; without even the implied warranty of
  62. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  63. General Public License for more details.
  64. You should have received a copy of the GNU General Public License
  65. along with GNU CC; see the file COPYING. If not, write to
  66. the Free Software Foundation, 51 Franklin Street - Fifth Floor,
  67. Boston, MA 02110-1301, USA. */
  68. /* For an easily readable description of splay-trees, see:
  69. Lewis, Harry R. and Denenberg, Larry. Data Structures and Their
  70. Algorithms. Harper-Collins, Inc. 1991. */
  71. /*
  72. SPDX-License-Identifier: GPL-3.0+
  73. */
  74. /* orig
  75. #ifdef HAVE_CONFIG_H
  76. #include "config.h"
  77. #endif
  78. #ifdef HAVE_STDLIB_H
  79. #include <stdlib.h>
  80. #endif
  81. #ifdef HAVE_STRING_H
  82. #include <string.h>
  83. #endif
  84. #include <stdio.h>
  85. #include "libiberty.h"
  86. #include "splay-tree.h"
  87. */
  88. #include <stdio.h>
  89. #include <stdlib.h>
  90. #include <string.h>
  91. #include <unistd.h> /* should define sync() and syncfs() */
  92. /* just in case if sync() is still not defined */
  93. extern void sync(void);
  94. /* needed for type unintptr_t or use long long int */
  95. #include <stdint.h>
  96. #define ATTRIBUTE_UNUSED /**/
  97. /* how many stack entries used max 4 Giga */
  98. static unsigned int maxstack = 0;
  99. /* how many realloc() done */
  100. static int nrealloc = 0;
  101. /* Use typedefs for the key and data types to facilitate changing
  102. these types, if necessary. These types should be sufficiently wide
  103. that any pointer or scalar can be cast to these types, and then
  104. cast back, without loss of precision. */
  105. typedef uintptr_t splay_tree_key; /* 64bits unsigned int */
  106. typedef uintptr_t splay_tree_value;
  107. /* Forward declaration for a node in the tree. */
  108. typedef struct splay_tree_node_s *splay_tree_node;
  109. /* The type of a function which compares two splay-tree keys. The
  110. function should return values as for qsort. */
  111. typedef int (*splay_tree_compare_fn)(splay_tree_key, splay_tree_key);
  112. /* The type of a function used to deallocate any resources associated
  113. with the key. If you provide this function, the splay tree
  114. will take the ownership of the memory of the splay_tree_key arg
  115. of splay_tree_insert. This function is called to release the keys
  116. present in the tree when calling splay_tree_delete or splay_tree_remove.
  117. If splay_tree_insert is called with a key equal to a key already
  118. present in the tree, the old key and old value will be released. */
  119. typedef void (*splay_tree_delete_key_fn)(splay_tree_key);
  120. /* The type of a function used to deallocate any resources associated
  121. with the value. If you provide this function, the memory of the
  122. splay_tree_value arg of splay_tree_insert is managed similarly to
  123. the splay_tree_key memory: see splay_tree_delete_key_fn. */
  124. typedef void (*splay_tree_delete_value_fn)(splay_tree_value);
  125. /* The type of a function used to iterate over the tree. */
  126. typedef int (*splay_tree_foreach_fn)(splay_tree_node, void *);
  127. /* The type of a function used to allocate memory for tree root and
  128. node structures. The first argument is the number of bytes needed;
  129. the second is a data pointer the splay tree functions pass through
  130. to the allocator. This function must never return zero. */
  131. /* old typedef void *(*splay_tree_allocate_fn)(int, void *); */
  132. typedef void *(*splay_tree_allocate_fn)(size_t, void *);
  133. /* The type of a function used to free memory allocated using the
  134. corresponding splay_tree_allocate_fn. The first argument is the
  135. memory to be freed; the latter is a data pointer the splay tree
  136. functions pass through to the freer. */
  137. typedef void (*splay_tree_deallocate_fn)(void *, void *);
  138. /* The nodes in the splay tree. */
  139. struct splay_tree_node_s {
  140. /* The key. */
  141. splay_tree_key key;
  142. /* The value. */
  143. splay_tree_value value;
  144. /* The left and right children, respectively. */
  145. splay_tree_node left;
  146. splay_tree_node right;
  147. };
  148. /* The splay tree itself. */
  149. struct splay_tree_s {
  150. /* The root of the tree. */
  151. splay_tree_node root;
  152. /* The comparision function. */
  153. splay_tree_compare_fn comp;
  154. /* The deallocate-key function. NULL if no cleanup is necessary. */
  155. splay_tree_delete_key_fn delete_key;
  156. /* The deallocate-value function. NULL if no cleanup is necessary. */
  157. splay_tree_delete_value_fn delete_value;
  158. /* Node allocate function. Takes allocate_data as a parameter. */
  159. splay_tree_allocate_fn allocate;
  160. /* Free function for nodes and trees. Takes allocate_data as a parameter. */
  161. splay_tree_deallocate_fn deallocate;
  162. /* Parameter for allocate/free functions. */
  163. void *allocate_data;
  164. };
  165. typedef struct splay_tree_s *splay_tree;
  166. /* these routines are here */
  167. extern splay_tree splay_tree_new(splay_tree_compare_fn, splay_tree_delete_key_fn, splay_tree_delete_value_fn);
  168. extern splay_tree splay_tree_new_with_allocator(splay_tree_compare_fn,
  169. splay_tree_delete_key_fn,
  170. splay_tree_delete_value_fn,
  171. splay_tree_allocate_fn, splay_tree_deallocate_fn, void *);
  172. extern splay_tree splay_tree_new_typed_alloc(splay_tree_compare_fn,
  173. splay_tree_delete_key_fn,
  174. splay_tree_delete_value_fn,
  175. splay_tree_allocate_fn, splay_tree_allocate_fn, splay_tree_deallocate_fn, void *);
  176. extern void splay_tree_delete(splay_tree);
  177. extern splay_tree_node splay_tree_insert(splay_tree, splay_tree_key, splay_tree_value);
  178. extern void splay_tree_remove(splay_tree, splay_tree_key);
  179. extern splay_tree_node splay_tree_lookup(splay_tree, splay_tree_key);
  180. extern splay_tree_node splay_tree_predecessor(splay_tree, splay_tree_key);
  181. extern splay_tree_node splay_tree_successor(splay_tree, splay_tree_key);
  182. extern splay_tree_node splay_tree_max(splay_tree);
  183. extern splay_tree_node splay_tree_min(splay_tree);
  184. extern int splay_tree_foreach(splay_tree, splay_tree_foreach_fn, void *);
  185. extern int splay_tree_compare_ints(splay_tree_key, splay_tree_key);
  186. extern int splay_tree_compare_pointers(splay_tree_key, splay_tree_key);
  187. extern int splay_tree_compare_strings(splay_tree_key, splay_tree_key);
  188. extern void splay_tree_delete_pointers(splay_tree_value);
  189. /* old static void *splay_tree_xmalloc_allocate(int size, void *data ATTRIBUTE_UNUSED); */
  190. static void *splay_tree_xmalloc_allocate(size_t size, void *data ATTRIBUTE_UNUSED);
  191. static void splay_tree_xmalloc_deallocate(void *object, void *data ATTRIBUTE_UNUSED);
  192. /* liberty.h Array allocators. */
  193. #define XALLOCAVEC(T, N) ((T *) alloca (sizeof (T) * (N)))
  194. #define XNEWVEC(T, N) ((T *) xmalloc (sizeof (T) * (N)))
  195. #define XCNEWVEC(T, N) ((T *) xcalloc ((N), sizeof (T)))
  196. #define XDUPVEC(T, P, N) ((T *) xmemdup ((P), sizeof (T) * (N), sizeof (T) * (N)))
  197. #define XRESIZEVEC(T, P, N) ((T *) xrealloc ((void *) (P), sizeof (T) * (N)))
  198. #define XDELETEVEC(P) free ((void*) (P))
  199. /* xmalloc substitute */
  200. #define xmalloc(x) calloc((size_t)1,x)
  201. #define xrealloc(p,n) realloc(p,n)
  202. static void splay_tree_delete_helper(splay_tree, splay_tree_node);
  203. static inline void rotate_left(splay_tree_node *, splay_tree_node, splay_tree_node);
  204. static inline void rotate_right(splay_tree_node *, splay_tree_node, splay_tree_node);
  205. static void splay_tree_splay(splay_tree, splay_tree_key);
  206. static int splay_tree_foreach_helper(splay_tree_node, splay_tree_foreach_fn, void *);
  207. /* Deallocate NODE (a member of SP), and all its sub-trees. */
  208. static void splay_tree_delete_helper(splay_tree sp, splay_tree_node node)
  209. {
  210. splay_tree_node pending = 0;
  211. splay_tree_node active = 0;
  212. if (!node)
  213. return;
  214. #define KDEL(x) if (sp->delete_key) (*sp->delete_key)(x);
  215. #define VDEL(x) if (sp->delete_value) (*sp->delete_value)(x);
  216. KDEL(node->key);
  217. VDEL(node->value);
  218. /* We use the "key" field to hold the "next" pointer. */
  219. node->key = (splay_tree_key) pending;
  220. pending = (splay_tree_node) node;
  221. /* Now, keep processing the pending list until there aren't any
  222. more. This is a little more complicated than just recursing, but
  223. it doesn't toast the stack for large trees. */
  224. while (pending) {
  225. active = pending;
  226. pending = 0;
  227. while (active) {
  228. splay_tree_node temp;
  229. /* active points to a node which has its key and value
  230. deallocated, we just need to process left and right. */
  231. if (active->left) {
  232. KDEL(active->left->key);
  233. VDEL(active->left->value);
  234. active->left->key = (splay_tree_key) pending;
  235. pending = (splay_tree_node) (active->left);
  236. }
  237. if (active->right) {
  238. KDEL(active->right->key);
  239. VDEL(active->right->value);
  240. active->right->key = (splay_tree_key) pending;
  241. pending = (splay_tree_node) (active->right);
  242. }
  243. temp = active;
  244. active = (splay_tree_node) (temp->key);
  245. (*sp->deallocate) ((char *)temp, sp->allocate_data);
  246. }
  247. }
  248. #undef KDEL
  249. #undef VDEL
  250. }
  251. /* Rotate the edge joining the left child N with its parent P. PP is the
  252. grandparents' pointer to P. */
  253. static inline void rotate_left(splay_tree_node * pp, splay_tree_node p, splay_tree_node n)
  254. {
  255. splay_tree_node tmp;
  256. tmp = n->right;
  257. n->right = p;
  258. p->left = tmp;
  259. *pp = n;
  260. }
  261. /* Rotate the edge joining the right child N with its parent P. PP is the
  262. grandparents' pointer to P. */
  263. static inline void rotate_right(splay_tree_node * pp, splay_tree_node p, splay_tree_node n)
  264. {
  265. splay_tree_node tmp;
  266. tmp = n->left;
  267. n->left = p;
  268. p->right = tmp;
  269. *pp = n;
  270. }
  271. /* Bottom up splay of key. */
  272. static void splay_tree_splay(splay_tree sp, splay_tree_key key)
  273. {
  274. if (sp->root == 0)
  275. return;
  276. do {
  277. int cmp1, cmp2;
  278. splay_tree_node n, c;
  279. n = sp->root;
  280. cmp1 = (*sp->comp) (key, n->key);
  281. /* Found. */
  282. if (cmp1 == 0)
  283. return;
  284. /* Left or right? If no child, then we're done. */
  285. if (cmp1 < 0)
  286. c = n->left;
  287. else
  288. c = n->right;
  289. if (!c)
  290. return;
  291. /* Next one left or right? If found or no child, we're done
  292. after one rotation. */
  293. cmp2 = (*sp->comp) (key, c->key);
  294. if (cmp2 == 0 || (cmp2 < 0 && !c->left) || (cmp2 > 0 && !c->right)) {
  295. if (cmp1 < 0)
  296. rotate_left(&sp->root, n, c);
  297. else
  298. rotate_right(&sp->root, n, c);
  299. return;
  300. }
  301. /* Now we have the four cases of double-rotation. */
  302. if (cmp1 < 0 && cmp2 < 0) {
  303. rotate_left(&n->left, c, c->left);
  304. rotate_left(&sp->root, n, n->left);
  305. } else if (cmp1 > 0 && cmp2 > 0) {
  306. rotate_right(&n->right, c, c->right);
  307. rotate_right(&sp->root, n, n->right);
  308. } else if (cmp1 < 0 && cmp2 > 0) {
  309. rotate_right(&n->left, c, c->right);
  310. rotate_left(&sp->root, n, n->left);
  311. } else if (cmp1 > 0 && cmp2 < 0) {
  312. rotate_left(&n->right, c, c->left);
  313. rotate_right(&sp->root, n, n->right);
  314. }
  315. }
  316. while (1);
  317. }
  318. /* Call FN, passing it the DATA, for every node below NODE, all of
  319. which are from SP, following an in-order traversal. If FN every
  320. returns a non-zero value, the iteration ceases immediately, and the
  321. value is returned. Otherwise, this function returns 0. */
  322. static int splay_tree_foreach_helper(splay_tree_node node, splay_tree_foreach_fn fn, void *data)
  323. {
  324. int val;
  325. splay_tree_node *stack;
  326. int stack_ptr, stack_size;
  327. /* A non-recursive implementation is used to avoid filling the stack
  328. for large trees. Splay trees are worst case O(n) in the depth of
  329. the tree. */
  330. #define INITIAL_STACK_SIZE 100
  331. stack_size = INITIAL_STACK_SIZE;
  332. stack_ptr = 0;
  333. stack = XNEWVEC(splay_tree_node, (long unsigned int)stack_size);
  334. val = 0;
  335. for (;;) {
  336. /* added */
  337. if ((unsigned int)stack_size > maxstack) {
  338. maxstack = (unsigned int)stack_size;
  339. }
  340. while (node != NULL) {
  341. if (stack_ptr == stack_size) {
  342. stack_size *= 2;
  343. stack = XRESIZEVEC(splay_tree_node, stack, (long unsigned int)stack_size);
  344. /* how many realloc()'s */
  345. nrealloc++;
  346. }
  347. stack[stack_ptr++] = node;
  348. node = node->left;
  349. }
  350. if (stack_ptr == 0)
  351. break;
  352. node = stack[--stack_ptr];
  353. val = (*fn) (node, data);
  354. if (val)
  355. break;
  356. node = node->right;
  357. }
  358. XDELETEVEC(stack);
  359. return val;
  360. }
  361. /* Call FN, passing it the DATA, for every node below NODE, all of
  362. which are from SP, following an in-order traversal. If FN every
  363. returns a non-zero value, the iteration ceases immediately, and the
  364. value is returned. Otherwise, this function returns 0. */
  365. /* modified */
  366. static int splay_tree_foreach_helper2(splay_tree_node node, splay_tree_foreach_fn fn, void *data)
  367. {
  368. int val;
  369. splay_tree_node *stack;
  370. splay_tree_node sn;
  371. int stack_ptr, stack_size; /* this allows only 2G entries */
  372. /* A non-recursive implementation is used to avoid filling the stack
  373. for large trees. Splay trees are worst case O(n) in the depth of
  374. the tree. */
  375. #define INITIAL_STACK_SIZE 100
  376. stack_size = INITIAL_STACK_SIZE;
  377. stack_ptr = 0;
  378. stack = XNEWVEC(splay_tree_node, (long unsigned int)stack_size);
  379. val = 0;
  380. for (;;) {
  381. /* added */
  382. if ((unsigned int)stack_size > maxstack) {
  383. maxstack = (unsigned int)stack_size;
  384. }
  385. sn = node; /* save copy */
  386. val = 0;
  387. /* count how many */
  388. while (node != NULL) {
  389. val++;
  390. node = node->left;
  391. }
  392. if (val) {
  393. if (val > stack_size) {
  394. stack_size = val;
  395. if ((unsigned int)stack_size > maxstack) {
  396. maxstack = (unsigned int)stack_size;
  397. }
  398. /* allocate exact as much as needed */
  399. stack = XRESIZEVEC(splay_tree_node, stack, (long unsigned int)stack_size);
  400. /* how many realloc()'s */
  401. nrealloc++;
  402. }
  403. }
  404. /* copy the pointers */
  405. while (sn != NULL) {
  406. stack[stack_ptr++] = sn;
  407. sn = sn->left;
  408. }
  409. if (stack_ptr == 0)
  410. break;
  411. node = stack[--stack_ptr];
  412. val = (*fn) (node, data);
  413. if (val)
  414. break;
  415. node = node->right;
  416. }
  417. XDELETEVEC(stack);
  418. return val;
  419. }
  420. /* Call FN, passing it the DATA, for every node below NODE, all of
  421. which are from SP, following an in-order traversal. If FN every
  422. returns a non-zero value, the iteration ceases immediately, and the
  423. value is returned. Otherwise, this function returns 0. */
  424. /* modified does not use realloc() in XRESIZEVEC() */
  425. static int splay_tree_foreach_helper3(splay_tree_node node, splay_tree_foreach_fn fn, void *data, unsigned int count)
  426. {
  427. int val;
  428. splay_tree_node *stack;
  429. unsigned int stack_ptr = 0;
  430. unsigned int stack_size = 0; /* this allows only 4G entries */
  431. /* A non-recursive implementation is used to avoid filling the stack
  432. for large trees. Splay trees are worst case O(n) in the depth of
  433. the tree. */
  434. stack_size = count;
  435. stack_ptr = 0;
  436. stack = XNEWVEC(splay_tree_node, (long unsigned int)stack_size);
  437. val = 0;
  438. for (;;) {
  439. /* added */
  440. if (stack_size > maxstack) {
  441. maxstack = stack_size;
  442. }
  443. /* copy the pointers */
  444. while (node != NULL) {
  445. stack[stack_ptr++] = node;
  446. node = node->left;
  447. }
  448. if (stack_ptr == 0)
  449. break;
  450. node = stack[--stack_ptr];
  451. val = (*fn) (node, data);
  452. if (val)
  453. break;
  454. node = node->right;
  455. }
  456. XDELETEVEC(stack);
  457. return val;
  458. }
  459. /* An allocator and deallocator based on xmalloc. */
  460. static void *splay_tree_xmalloc_allocate(size_t size, void *data ATTRIBUTE_UNUSED)
  461. {
  462. if (data) { /* not used */
  463. }
  464. return (void *)xmalloc((size_t)size);
  465. }
  466. static void splay_tree_xmalloc_deallocate(void *object, void *data ATTRIBUTE_UNUSED)
  467. {
  468. if (object) {
  469. free(object);
  470. }
  471. if (data) { /* not used */
  472. }
  473. }
  474. /* Allocate a new splay tree, using COMPARE_FN to compare nodes,
  475. DELETE_KEY_FN to deallocate keys, and DELETE_VALUE_FN to deallocate
  476. values. Use xmalloc to allocate the splay tree structure, and any
  477. nodes added. */
  478. splay_tree
  479. splay_tree_new(splay_tree_compare_fn compare_fn, splay_tree_delete_key_fn delete_key_fn, splay_tree_delete_value_fn delete_value_fn)
  480. {
  481. return (splay_tree_new_with_allocator
  482. (compare_fn, delete_key_fn, delete_value_fn, splay_tree_xmalloc_allocate, splay_tree_xmalloc_deallocate, 0));
  483. }
  484. /* Allocate a new splay tree, using COMPARE_FN to compare nodes,
  485. DELETE_KEY_FN to deallocate keys, and DELETE_VALUE_FN to deallocate
  486. values. */
  487. splay_tree
  488. splay_tree_new_with_allocator(splay_tree_compare_fn compare_fn,
  489. splay_tree_delete_key_fn delete_key_fn,
  490. splay_tree_delete_value_fn delete_value_fn,
  491. splay_tree_allocate_fn allocate_fn, splay_tree_deallocate_fn deallocate_fn, void *allocate_data)
  492. {
  493. return
  494. splay_tree_new_typed_alloc(compare_fn, delete_key_fn, delete_value_fn,
  495. allocate_fn, allocate_fn, deallocate_fn, allocate_data);
  496. }
  497. /*
  498. @deftypefn Supplemental splay_tree splay_tree_new_with_typed_alloc @
  499. (splay_tree_compare_fn @var{compare_fn}, @
  500. splay_tree_delete_key_fn @var{delete_key_fn}, @
  501. splay_tree_delete_value_fn @var{delete_value_fn}, @
  502. splay_tree_allocate_fn @var{tree_allocate_fn}, @
  503. splay_tree_allocate_fn @var{node_allocate_fn}, @
  504. splay_tree_deallocate_fn @var{deallocate_fn}, @
  505. void * @var{allocate_data})
  506. This function creates a splay tree that uses two different allocators
  507. @var{tree_allocate_fn} and @var{node_allocate_fn} to use for allocating the
  508. tree itself and its nodes respectively. This is useful when variables of
  509. different types need to be allocated with different allocators.
  510. The splay tree will use @var{compare_fn} to compare nodes,
  511. @var{delete_key_fn} to deallocate keys, and @var{delete_value_fn} to
  512. deallocate values. Keys and values will be deallocated when the
  513. tree is deleted using splay_tree_delete or when a node is removed
  514. using splay_tree_remove. splay_tree_insert will release the previously
  515. inserted key and value using @var{delete_key_fn} and @var{delete_value_fn}
  516. if the inserted key is already found in the tree.
  517. @end deftypefn
  518. */
  519. splay_tree
  520. splay_tree_new_typed_alloc(splay_tree_compare_fn compare_fn,
  521. splay_tree_delete_key_fn delete_key_fn,
  522. splay_tree_delete_value_fn delete_value_fn,
  523. splay_tree_allocate_fn tree_allocate_fn,
  524. splay_tree_allocate_fn node_allocate_fn, splay_tree_deallocate_fn deallocate_fn, void *allocate_data)
  525. {
  526. splay_tree sp = (splay_tree) (*tree_allocate_fn)
  527. (sizeof(struct splay_tree_s), allocate_data);
  528. sp->root = 0;
  529. sp->comp = compare_fn;
  530. sp->delete_key = delete_key_fn;
  531. sp->delete_value = delete_value_fn;
  532. sp->allocate = node_allocate_fn;
  533. sp->deallocate = deallocate_fn;
  534. sp->allocate_data = allocate_data;
  535. return sp;
  536. }
  537. /* Deallocate SP. */
  538. void splay_tree_delete(splay_tree sp)
  539. {
  540. splay_tree_delete_helper(sp, sp->root);
  541. (*sp->deallocate) ((char *)sp, sp->allocate_data);
  542. }
  543. /* Insert a new node (associating KEY with DATA) into SP. If a
  544. previous node with the indicated KEY exists, its data is replaced
  545. with the new value. Returns the new node. */
  546. splay_tree_node splay_tree_insert(splay_tree sp, splay_tree_key key, splay_tree_value value)
  547. {
  548. int comparison = 0;
  549. splay_tree_splay(sp, key);
  550. if (sp->root)
  551. comparison = (*sp->comp) (sp->root->key, key);
  552. if (sp->root && comparison == 0) {
  553. /* If the root of the tree already has the indicated KEY, delete
  554. the old key and old value, and replace them with KEY and VALUE. */
  555. if (sp->delete_key)
  556. (*sp->delete_key) (sp->root->key);
  557. if (sp->delete_value)
  558. (*sp->delete_value) (sp->root->value);
  559. sp->root->key = key;
  560. sp->root->value = value;
  561. } else {
  562. /* Create a new node, and insert it at the root. */
  563. splay_tree_node node;
  564. node = ((splay_tree_node)
  565. (*sp->allocate) (sizeof(struct splay_tree_node_s), sp->allocate_data));
  566. node->key = key;
  567. node->value = value;
  568. if (!sp->root)
  569. node->left = node->right = 0;
  570. else if (comparison < 0) {
  571. node->left = sp->root;
  572. node->right = node->left->right;
  573. node->left->right = 0;
  574. } else {
  575. node->right = sp->root;
  576. node->left = node->right->left;
  577. node->right->left = 0;
  578. }
  579. sp->root = node;
  580. }
  581. return sp->root;
  582. }
  583. /* Remove KEY from SP. It is not an error if it did not exist. */
  584. void splay_tree_remove(splay_tree sp, splay_tree_key key)
  585. {
  586. splay_tree_splay(sp, key);
  587. if (sp->root && (*sp->comp) (sp->root->key, key) == 0) {
  588. splay_tree_node left, right;
  589. left = sp->root->left;
  590. right = sp->root->right;
  591. /* Delete the root node itself. */
  592. if (sp->delete_key)
  593. (*sp->delete_key) (sp->root->key);
  594. if (sp->delete_value)
  595. (*sp->delete_value) (sp->root->value);
  596. (*sp->deallocate) (sp->root, sp->allocate_data);
  597. /* One of the children is now the root. Doesn't matter much
  598. which, so long as we preserve the properties of the tree. */
  599. if (left) {
  600. sp->root = left;
  601. /* If there was a right child as well, hang it off the
  602. right-most leaf of the left child. */
  603. if (right) {
  604. while (left->right)
  605. left = left->right;
  606. left->right = right;
  607. }
  608. } else
  609. sp->root = right;
  610. }
  611. }
  612. /* Lookup KEY in SP, returning VALUE if present, and NULL
  613. otherwise. */
  614. splay_tree_node splay_tree_lookup(splay_tree sp, splay_tree_key key)
  615. {
  616. splay_tree_splay(sp, key);
  617. if (sp->root && (*sp->comp) (sp->root->key, key) == 0)
  618. return sp->root;
  619. else
  620. return 0;
  621. }
  622. /* Return the node in SP with the greatest key. */
  623. splay_tree_node splay_tree_max(splay_tree sp)
  624. {
  625. splay_tree_node n = sp->root;
  626. if (!n)
  627. return NULL;
  628. while (n->right)
  629. n = n->right;
  630. return n;
  631. }
  632. /* Return the node in SP with the smallest key. */
  633. splay_tree_node splay_tree_min(splay_tree sp)
  634. {
  635. splay_tree_node n = sp->root;
  636. if (!n)
  637. return NULL;
  638. while (n->left)
  639. n = n->left;
  640. return n;
  641. }
  642. /* Return the immediate predecessor KEY, or NULL if there is no
  643. predecessor. KEY need not be present in the tree. */
  644. splay_tree_node splay_tree_predecessor(splay_tree sp, splay_tree_key key)
  645. {
  646. int comparison;
  647. splay_tree_node node;
  648. /* If the tree is empty, there is certainly no predecessor. */
  649. if (!sp->root)
  650. return NULL;
  651. /* Splay the tree around KEY. That will leave either the KEY
  652. itself, its predecessor, or its successor at the root. */
  653. splay_tree_splay(sp, key);
  654. comparison = (*sp->comp) (sp->root->key, key);
  655. /* If the predecessor is at the root, just return it. */
  656. if (comparison < 0)
  657. return sp->root;
  658. /* Otherwise, find the rightmost element of the left subtree. */
  659. node = sp->root->left;
  660. if (node)
  661. while (node->right)
  662. node = node->right;
  663. return node;
  664. }
  665. /* Return the immediate successor KEY, or NULL if there is no
  666. successor. KEY need not be present in the tree. */
  667. splay_tree_node splay_tree_successor(splay_tree sp, splay_tree_key key)
  668. {
  669. int comparison;
  670. splay_tree_node node;
  671. /* If the tree is empty, there is certainly no successor. */
  672. if (!sp->root)
  673. return NULL;
  674. /* Splay the tree around KEY. That will leave either the KEY
  675. itself, its predecessor, or its successor at the root. */
  676. splay_tree_splay(sp, key);
  677. comparison = (*sp->comp) (sp->root->key, key);
  678. /* If the successor is at the root, just return it. */
  679. if (comparison > 0)
  680. return sp->root;
  681. /* Otherwise, find the leftmost element of the right subtree. */
  682. node = sp->root->right;
  683. if (node)
  684. while (node->left)
  685. node = node->left;
  686. return node;
  687. }
  688. /* Call FN, passing it the DATA, for every node in SP, following an
  689. in-order traversal. If FN every returns a non-zero value, the
  690. iteration ceases immediately, and the value is returned.
  691. Otherwise, this function returns 0. */
  692. int splay_tree_foreach(splay_tree sp, splay_tree_foreach_fn fn, void *data)
  693. {
  694. return splay_tree_foreach_helper(sp->root, fn, data);
  695. }
  696. /* other way */
  697. int splay_tree_foreach2(splay_tree sp, splay_tree_foreach_fn fn, void *data)
  698. {
  699. return splay_tree_foreach_helper2(sp->root, fn, data);
  700. }
  701. /* other way does not use realloc() */
  702. int splay_tree_foreach3(splay_tree sp, splay_tree_foreach_fn fn, void *data)
  703. {
  704. splay_tree_node spn;
  705. splay_tree_key key;
  706. unsigned int count = 0; /* allows 4 Giga tree nodes */
  707. if ((splay_tree) 0 == sp) {
  708. /* no data */
  709. return (0);
  710. }
  711. if (!sp->root) {
  712. /* no data */
  713. return (0);
  714. }
  715. /* this counting is very slow
  716. * or splay tree should maintain the count
  717. * of number of nodes in the splay tree
  718. */
  719. spn = splay_tree_min(sp);
  720. while (spn) {
  721. key = (splay_tree_key) spn->key;
  722. count++;
  723. spn = splay_tree_successor(sp, key);
  724. }
  725. if (count == 0) {
  726. /* no data */
  727. }
  728. printf("%s(): splay tree has %u nodes\n", __func__, count);
  729. fflush(stdout);
  730. return splay_tree_foreach_helper3(sp->root, fn, data, count);
  731. }
  732. /* other way does not use realloc() */
  733. int splay_tree_foreach4(splay_tree sp, splay_tree_foreach_fn fn, void *data)
  734. {
  735. splay_tree_node spn;
  736. splay_tree_key key;
  737. unsigned int count = 0; /* allows 4 Giga tree nodes */
  738. int val = 0;
  739. if ((splay_tree) 0 == sp) {
  740. /* no data */
  741. return (0);
  742. }
  743. if (!sp->root) {
  744. /* no data */
  745. return (0);
  746. }
  747. /* this is slow but will run with bigger splay tree */
  748. printf("%s(): starting ...\n", __func__);
  749. fflush(stdout);
  750. val = 0;
  751. spn = splay_tree_min(sp);
  752. while (spn) {
  753. key = (splay_tree_key) spn->key;
  754. count++;
  755. val = (*fn) (spn, data);
  756. /* stop if callback routine returns <> 0 */
  757. if (val)
  758. break;
  759. spn = splay_tree_successor(sp, key);
  760. }
  761. if (count == 0) {
  762. /* no data */
  763. }
  764. printf("%s(): splay tree has %u nodes return status is %d\n", __func__, count, val);
  765. fflush(stdout);
  766. return (val);
  767. }
  768. /* Splay-tree comparison function, treating the keys as ints. */
  769. int splay_tree_compare_ints(splay_tree_key k1, splay_tree_key k2)
  770. {
  771. if ((int)k1 < (int)k2)
  772. return -1;
  773. else if ((int)k1 > (int)k2)
  774. return 1;
  775. else
  776. return 0;
  777. }
  778. /* Splay-tree comparison function, treating the keys as pointers. */
  779. int splay_tree_compare_pointers(splay_tree_key k1, splay_tree_key k2)
  780. {
  781. if ((char *)k1 < (char *)k2)
  782. return -1;
  783. else if ((char *)k1 > (char *)k2)
  784. return 1;
  785. else
  786. return 0;
  787. }
  788. /* Splay-tree comparison function, treating the keys as strings. */
  789. int splay_tree_compare_strings(splay_tree_key k1, splay_tree_key k2)
  790. {
  791. return strcmp((char *)k1, (char *)k2);
  792. }
  793. /* Splay-tree delete function, simply using free. */
  794. void splay_tree_delete_pointers(splay_tree_value value)
  795. {
  796. if (value) {
  797. free((void *)value);
  798. }
  799. }
  800. /* fn0 small splay tree The type of a function used to iterate over the tree. */
  801. int fn0(splay_tree_node spn, void *data)
  802. {
  803. if (spn == (splay_tree_node) 0) {
  804. /* sgould not happen */
  805. return (1);
  806. }
  807. printf("%d ", (int)spn->key);
  808. if (data) { /* not used */
  809. }
  810. /* return 0 to continue */
  811. return (0);
  812. }
  813. /* fn1 bigger splay tree The type of a function used to iterate over the tree. */
  814. int fn1(splay_tree_node spn, void *data)
  815. {
  816. if (spn == (splay_tree_node) 0) {
  817. /* sgould not happen */
  818. return (1);
  819. }
  820. if (((int)spn->key % 100) == 0) {
  821. printf("%d \n", (int)spn->key);
  822. }
  823. if (data) { /* not used */
  824. }
  825. /* return 0 to continue */
  826. return (0);
  827. }
  828. /* fn3 even bigger splay tree The type of a function used to iterate over the tree. */
  829. int fn3(splay_tree_node spn, void *data)
  830. {
  831. if (spn == (splay_tree_node) 0) {
  832. /* sgould not happen */
  833. return (1);
  834. }
  835. if (data) { /* not used */
  836. }
  837. /* return 0 to continue */
  838. return (0);
  839. }
  840. /* the test code */
  841. int main(int argc, char *argv[])
  842. {
  843. splay_tree spt;
  844. unsigned long long int n; /* 64bits */
  845. unsigned long long int mb = 0; /* mem use in mb */
  846. unsigned long long int mbs = 0; /* saved mem use in mb */
  847. splay_tree_node spn;
  848. int status = 0;
  849. unsigned int v0 = 0;
  850. unsigned int v1 = 0;
  851. if (argc) {
  852. }
  853. if (argv) {
  854. }
  855. /* maybe safer to do a sync() here */
  856. sync();
  857. printf
  858. ("this may try to use more ram and disk swap memory then available\nstatus=0 means everything is ok\ntesting old splay_tree_foreach()\n");
  859. spt = splay_tree_new(splay_tree_compare_ints /* compare_fn */ ,
  860. (splay_tree_delete_key_fn) 0 /* delete_key_fn */ ,
  861. (splay_tree_delete_value_fn) 0 /* delete_value_fn */
  862. );
  863. /* create small splay tree does not use realloc() */
  864. for (n = 0; n < 10; n++) {
  865. spn /* splay_tree_node */ =
  866. splay_tree_insert((splay_tree) spt, (splay_tree_key) n, (splay_tree_value) 0);
  867. if (!spn) { /* shouldnothappen */
  868. }
  869. }
  870. /* how much stack used */
  871. maxstack = 0;
  872. nrealloc = 0;
  873. /* traverse */
  874. status = splay_tree_foreach((splay_tree) spt, (splay_tree_foreach_fn) fn0, (void *)0 /* data */ );
  875. /* how much mem used */
  876. mb = sizeof(splay_tree_node);
  877. mb = mb * maxstack;
  878. mb = mb / 1024; /* kb */
  879. mb = mb / 1024; /* mb */
  880. printf
  881. ("status=%d %llu tree nodes stack used max %u entries using %llu megabyte %lu bytes %d realloc()'s\n",
  882. status, n, maxstack, mb, sizeof(splay_tree_node) * maxstack, nrealloc);
  883. splay_tree_delete((splay_tree) spt);
  884. printf("testing old splay_tree_foreach()\n");
  885. spt = splay_tree_new(splay_tree_compare_ints /* compare_fn */ ,
  886. (splay_tree_delete_key_fn) 0 /* delete_key_fn */ ,
  887. (splay_tree_delete_value_fn) 0 /* delete_value_fn */
  888. );
  889. /* create bigher splay tree causes realloc */
  890. for (n = 0; n < 1000; n++) {
  891. spn /* splay_tree_node */ =
  892. splay_tree_insert((splay_tree) spt, (splay_tree_key) n, (splay_tree_value) 0);
  893. if (!spn) { /* shouldnothappen */
  894. }
  895. }
  896. /* how much stack used */
  897. maxstack = 0;
  898. nrealloc = 0;
  899. /* traverse */
  900. status = splay_tree_foreach((splay_tree) spt, (splay_tree_foreach_fn) fn1, (void *)0 /* data */ );
  901. v0 = maxstack;
  902. /* how much mem used */
  903. mb = sizeof(splay_tree_node);
  904. mb = mb * maxstack;
  905. mb = mb / 1024; /* kb */
  906. mb = mb / 1024; /* mb */
  907. printf
  908. ("status=%d %llu tree nodes stack used max %u entries using %llu megabyte %lu bytes %d realloc()'s\n",
  909. status, n, maxstack, mb, sizeof(splay_tree_node) * maxstack, nrealloc);
  910. splay_tree_delete((splay_tree) spt);
  911. printf("testing new splay_tree_foreach()\n");
  912. /* now same with other foreach */
  913. spt = splay_tree_new(splay_tree_compare_ints /* compare_fn */ ,
  914. (splay_tree_delete_key_fn) 0 /* delete_key_fn */ ,
  915. (splay_tree_delete_value_fn) 0 /* delete_value_fn */
  916. );
  917. /* create bigger splay tree causes realloc */
  918. for (n = 0; n < 1000; n++) {
  919. spn /* splay_tree_node */ =
  920. splay_tree_insert((splay_tree) spt, (splay_tree_key) n, (splay_tree_value) 0);
  921. if (!spn) { /* shouldnothappen */
  922. }
  923. }
  924. /* how much stack used */
  925. maxstack = 0;
  926. nrealloc = 0;
  927. /* traverse */
  928. status = splay_tree_foreach2((splay_tree) spt, (splay_tree_foreach_fn) fn1, (void *)0 /* data */ );
  929. v1 = maxstack;
  930. /* how much mem used */
  931. mb = sizeof(splay_tree_node);
  932. mb = mb * maxstack;
  933. mb = mb / 1024; /* kb */
  934. mb = mb / 1024; /* mb */
  935. /* how much mem saved */
  936. mbs = sizeof(splay_tree_node);
  937. mbs = mbs * (v0 - v1);
  938. mbs = mbs / 1024; /* kb */
  939. mbs = mbs / 1024; /* mb */
  940. printf
  941. ("status=%d %llu tree nodes stack used max %u entries using %llu megabyte %lu bytes %d realloc()'s saved %llu Mb\n",
  942. status, n, maxstack, mb, sizeof(splay_tree_node) * maxstack, nrealloc, mbs);
  943. splay_tree_delete((splay_tree) spt);
  944. printf("testing old splay_tree_foreach()\n");
  945. /* now going really big but not more the 2G nodes because that is too much but can be fixed */
  946. spt = splay_tree_new(splay_tree_compare_ints /* compare_fn */ ,
  947. (splay_tree_delete_key_fn) 0 /* delete_key_fn */ ,
  948. (splay_tree_delete_value_fn) 0 /* delete_value_fn */
  949. );
  950. /* create bigger splay tree causes realloc */
  951. for (n = 0; n < 1000 * 1000 * 100; n++) {
  952. spn /* splay_tree_node */ =
  953. splay_tree_insert((splay_tree) spt, (splay_tree_key) n, (splay_tree_value) 0);
  954. if (!spn) { /* shouldnothappen */
  955. }
  956. }
  957. /* how much stack used */
  958. maxstack = 0;
  959. nrealloc = 0;
  960. /* traverse */
  961. status = splay_tree_foreach((splay_tree) spt, (splay_tree_foreach_fn) fn3, (void *)0 /* data */ );
  962. v0 = maxstack;
  963. /* how much mem used */
  964. mb = sizeof(splay_tree_node);
  965. mb = mb * maxstack;
  966. mb = mb / 1024; /* kb */
  967. mb = mb / 1024; /* mb */
  968. printf
  969. ("status=%d %llu tree nodes stack used max %u entries using %llu megabyte %lu bytes %d realloc()'s\n",
  970. status, n, maxstack, mb, sizeof(splay_tree_node) * maxstack, nrealloc);
  971. splay_tree_delete((splay_tree) spt);
  972. printf("testing new splay_tree_foreach()\n");
  973. /* now going really big but not more the 2G nodes because that is too much but can be fixed */
  974. spt = splay_tree_new(splay_tree_compare_ints /* compare_fn */ ,
  975. (splay_tree_delete_key_fn) 0 /* delete_key_fn */ ,
  976. (splay_tree_delete_value_fn) 0 /* delete_value_fn */
  977. );
  978. /* create bigger splay tree causes realloc */
  979. for (n = 0; n < 1000 * 1000 * 100; n++) {
  980. spn /* splay_tree_node */ =
  981. splay_tree_insert((splay_tree) spt, (splay_tree_key) n, (splay_tree_value) 0);
  982. if (!spn) { /* shouldnothappen */
  983. }
  984. }
  985. /* how much stack used */
  986. maxstack = 0;
  987. nrealloc = 0;
  988. /* traverse */
  989. status = splay_tree_foreach2((splay_tree) spt, (splay_tree_foreach_fn) fn3, (void *)0 /* data */ );
  990. v1 = maxstack;
  991. /* how much mem saved */
  992. mbs = sizeof(splay_tree_node);
  993. mbs = mbs * (v0 - v1);
  994. mbs = mbs / 1024; /* kb */
  995. mbs = mbs / 1024; /* mb */
  996. /* how much mem used */
  997. mb = sizeof(splay_tree_node);
  998. mb = mb * maxstack;
  999. mb = mb / 1024; /* kb */
  1000. mb = mb / 1024; /* mb */
  1001. printf
  1002. ("status=%d %llu tree nodes stack used max %u entries using %llu megabyte %lu bytes %d realloc()'s saved %llu Mb\n",
  1003. status, n, maxstack, mb, sizeof(splay_tree_node) * maxstack, nrealloc, mbs);
  1004. splay_tree_delete((splay_tree) spt);
  1005. /* maybe safer to do a sync() here */
  1006. sync();
  1007. printf("testing old splay_tree_foreach()\n");
  1008. /* now going really big but not more the 2G nodes because that is too much but can be fixed */
  1009. spt = splay_tree_new(splay_tree_compare_ints /* compare_fn */ ,
  1010. (splay_tree_delete_key_fn) 0 /* delete_key_fn */ ,
  1011. (splay_tree_delete_value_fn) 0 /* delete_value_fn */
  1012. );
  1013. /* create bigger splay tree causes realloc */
  1014. for (n = 0; n < 1000 * 1000 * 256; n++) { /* fedora: 350 */
  1015. spn /* splay_tree_node */ =
  1016. splay_tree_insert((splay_tree) spt, (splay_tree_key) n, (splay_tree_value) 0);
  1017. if (!spn) { /* shouldnothappen */
  1018. }
  1019. }
  1020. /* how much stack used */
  1021. maxstack = 0;
  1022. nrealloc = 0;
  1023. /* traverse */
  1024. status = splay_tree_foreach((splay_tree) spt, (splay_tree_foreach_fn) fn3, (void *)0 /* data */ );
  1025. v0 = maxstack;
  1026. /* how much mem used */
  1027. mb = sizeof(splay_tree_node);
  1028. mb = mb * maxstack;
  1029. mb = mb / 1024; /* kb */
  1030. mb = mb / 1024; /* mb */
  1031. printf
  1032. ("status=%d %llu tree nodes stack used max %u entries using %llu megabyte %lu bytes %d realloc()'s\n",
  1033. status, n, maxstack, mb, sizeof(splay_tree_node) * maxstack, nrealloc);
  1034. splay_tree_delete((splay_tree) spt);
  1035. /* maybe safer to do a sync() here */
  1036. sync();
  1037. printf("testing new splay_tree_foreach()\n");
  1038. /* now going really big but not more the 2G nodes because that is too much but can be fixed */
  1039. spt = splay_tree_new(splay_tree_compare_ints /* compare_fn */ ,
  1040. (splay_tree_delete_key_fn) 0 /* delete_key_fn */ ,
  1041. (splay_tree_delete_value_fn) 0 /* delete_value_fn */
  1042. );
  1043. /* create bigger splay tree causes realloc */
  1044. for (n = 0; n < 1000 * 1000 * 256; n++) { /* fedora 350 */
  1045. spn /* splay_tree_node */ =
  1046. splay_tree_insert((splay_tree) spt, (splay_tree_key) n, (splay_tree_value) 0);
  1047. if (!spn) { /* shouldnothappen */
  1048. }
  1049. }
  1050. /* how much stack used */
  1051. maxstack = 0;
  1052. nrealloc = 0;
  1053. /* traverse */
  1054. status = splay_tree_foreach2((splay_tree) spt, (splay_tree_foreach_fn) fn3, (void *)0 /* data */ );
  1055. v1 = maxstack;
  1056. /* how much mem saved */
  1057. mbs = sizeof(splay_tree_node);
  1058. mbs = mbs * (v0 - v1);
  1059. mbs = mbs / 1024; /* kb */
  1060. mbs = mbs / 1024; /* mb */
  1061. /* how much mem used */
  1062. mb = sizeof(splay_tree_node);
  1063. mb = mb * maxstack;
  1064. mb = mb / 1024; /* kb */
  1065. mb = mb / 1024; /* mb */
  1066. printf
  1067. ("status=%d %llu tree nodes stack used max %u entries using %llu megabyte %lu bytes %d realloc()'s saved %llu Mb\n",
  1068. status, n, maxstack, mb, sizeof(splay_tree_node) * maxstack, nrealloc, mbs);
  1069. splay_tree_delete((splay_tree) spt);
  1070. printf("testing splay_tree_foreach() without realloc()\n");
  1071. /* now going really big but not more the 2G nodes because that is too much but can be fixed */
  1072. spt = splay_tree_new(splay_tree_compare_ints /* compare_fn */ ,
  1073. (splay_tree_delete_key_fn) 0 /* delete_key_fn */ ,
  1074. (splay_tree_delete_value_fn) 0 /* delete_value_fn */
  1075. );
  1076. /* create bigger splay tree no realloc */
  1077. for (n = 0; n < 1000 * 3; n++) {
  1078. spn /* splay_tree_node */ =
  1079. splay_tree_insert((splay_tree) spt, (splay_tree_key) n, (splay_tree_value) 0);
  1080. if (!spn) { /* shouldnothappen */
  1081. }
  1082. }
  1083. /* how much stack used */
  1084. maxstack = 0;
  1085. nrealloc = 0;
  1086. /* traverse */
  1087. status = splay_tree_foreach3((splay_tree) spt, (splay_tree_foreach_fn) fn3, (void *)0 /* data */ );
  1088. v0 = maxstack;
  1089. /* how much mem used */
  1090. mb = sizeof(splay_tree_node);
  1091. mb = mb * maxstack;
  1092. mb = mb / 1024; /* kb */
  1093. mb = mb / 1024; /* mb */
  1094. printf
  1095. ("status=%d %llu tree nodes stack used max %u entries using %llu megabyte %lu bytes %d realloc()'s\n",
  1096. status, n, maxstack, mb, sizeof(splay_tree_node) * maxstack, nrealloc);
  1097. splay_tree_delete((splay_tree) spt);
  1098. /* maybe safer to do a sync() here */
  1099. sync();
  1100. printf("testing new splay_tree_foreach() with maximum test machine limit using all physical ram and all disk swap space\n");
  1101. /* now going really big but not more the 2G nodes because that is too much but can be fixed */
  1102. spt = splay_tree_new(splay_tree_compare_ints /* compare_fn */ ,
  1103. (splay_tree_delete_key_fn) 0 /* delete_key_fn */ ,
  1104. (splay_tree_delete_value_fn) 0 /* delete_value_fn */
  1105. );
  1106. /* create bigger splay tree causes realloc */
  1107. for (n = 0; n < 1000 * 1000 * 256; n++) { /* fedora 370 */
  1108. spn /* splay_tree_node */ =
  1109. splay_tree_insert((splay_tree) spt, (splay_tree_key) n, (splay_tree_value) 0);
  1110. if (!spn) { /* shouldnothappen */
  1111. }
  1112. }
  1113. /* how much stack used */
  1114. maxstack = 0;
  1115. nrealloc = 0;
  1116. /* traverse and do NOT waste memory */
  1117. status = splay_tree_foreach2((splay_tree) spt, (splay_tree_foreach_fn) fn3, (void *)0 /* data */ );
  1118. v0 = maxstack;
  1119. /* how much mem used */
  1120. mb = sizeof(splay_tree_node);
  1121. mb = mb * maxstack;
  1122. mb = mb / 1024; /* kb */
  1123. mb = mb / 1024; /* mb */
  1124. printf
  1125. ("status=%d %llu tree nodes stack used max %u entries using %llu megabyte %lu bytes %d realloc()'s\n",
  1126. status, n, maxstack, mb, sizeof(splay_tree_node) * maxstack, nrealloc);
  1127. splay_tree_delete((splay_tree) spt);
  1128. /* maybe safer to do a sync() here */
  1129. sync();
  1130. /* this reaches more on debian Linux */
  1131. printf("testing another slower new splay_tree_foreach() without realloc()\nadding splay tree nodes ...\n");
  1132. /* now going really big but not more the 2G nodes because that is too much but can be fixed */
  1133. spt = splay_tree_new(splay_tree_compare_ints /* compare_fn */ ,
  1134. (splay_tree_delete_key_fn) 0 /* delete_key_fn */ ,
  1135. (splay_tree_delete_value_fn) 0 /* delete_value_fn */
  1136. );
  1137. /* create bigger splay tree causes realloc */
  1138. for (n = 0; n < 1000 * 1000 * 423; n++) { /* on debian 300 fedora 423 million */
  1139. spn /* splay_tree_node */ =
  1140. splay_tree_insert((splay_tree) spt, (splay_tree_key) n, (splay_tree_value) 0);
  1141. if (!spn) { /* shouldnothappen or maximum reached */
  1142. break;
  1143. }
  1144. }
  1145. /* maybe safer to do a sync() here */
  1146. sync();
  1147. /* how much stack used */
  1148. maxstack = 0;
  1149. nrealloc = 0;
  1150. /* traverse and do NOT waste memory */
  1151. status = splay_tree_foreach4((splay_tree) spt, (splay_tree_foreach_fn) fn3, (void *)0 /* data */ );
  1152. printf("%s(): status=%d\n", __func__, status);
  1153. fflush(stdout);
  1154. splay_tree_delete((splay_tree) spt);
  1155. /* maybe safer to do a sync() here */
  1156. sync();
  1157. printf("%s(): no memory leaks at exit\n", __func__);
  1158. return (0);
  1159. }
  1160. /* end. */