From ab0c62fa280cea7b7d10bda57f9698acf1a8fd19 Mon Sep 17 00:00:00 2001 From: GJDuck Date: Fri, 19 May 2023 21:55:57 +0800 Subject: [PATCH] Add balanced trees to stdlib.c Based on the POSIX tsearch() API (see man tsearch). Balanced trees are useful for many applications. --- examples/stdlib.c | 444 ++++++++++++++++++++++++++++++++++++++++- test/regtest/patch.cpp | 75 ++++++- test/regtest/tree.exp | 2 + test/regtest/tree.in | 1 + 4 files changed, 517 insertions(+), 5 deletions(-) create mode 100644 test/regtest/tree.exp create mode 100644 test/regtest/tree.in diff --git a/examples/stdlib.c b/examples/stdlib.c index e858329..3935e1f 100644 --- a/examples/stdlib.c +++ b/examples/stdlib.c @@ -94,6 +94,7 @@ #include #include #include +#include #include #include @@ -297,6 +298,12 @@ extern "C" #undef strdup #undef strerror +#undef tsearch +#undef tfind +#undef tdelete +#undef twalk +#undef tdestroy + /***/ #define __errno_location e9___errno_location @@ -450,6 +457,12 @@ extern "C" #define strncpy e9_strncpy #define strerror e9_strerror +#define tsearch e9_tsearch +#define tfind e9_tfind +#define tdelete e9_tdelete +#define twalk e9_twalk +#define tdestroy e9_tdestroy + /****************************************************************************/ /* DEBUG */ /****************************************************************************/ @@ -4255,7 +4268,436 @@ asm ( "retq\n" ); -#endif /* defined(LIBC) */ +#endif /* defined(LIBDL) */ + +/****************************************************************************/ +/* SEARCH */ +/****************************************************************************/ + +/* + * This is an implementation of the POSIX tree-search (tsearch) family of + * functions, see (man tsearch) for more information. Unlike the POSIX + * specification, this version additionally guarantees that the tree is + * balanced, for O(log N) worst-case behaviour. + * + * The implementation uses code that is derived from Niels Provos' red-black + * tree implementation. See the copyright and license (BSD) below. + */ + +/* + * Copyright 2002 Niels Provos + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +struct node_s +{ + void *key; + struct node_s *parent; + struct node_s *left; + struct node_s *right; + int color; +}; +struct tree_s +{ + struct node_s *root; +}; + +#define TREE_BLACK 0 +#define TREE_RED 1 +#define TREE_PARENT(N) ((N)->parent) +#define TREE_LEFT(N) ((N)->left) +#define TREE_RIGHT(N) ((N)->right) +#define TREE_COLOR(N) ((N)->color) + +static void tree_rotate_left(struct tree_s *t, struct node_s *n) +{ + struct node_s *tmp = TREE_RIGHT(n); + if ((TREE_RIGHT(n) = TREE_LEFT(tmp)) != NULL) + TREE_PARENT(TREE_LEFT(tmp)) = n; + if ((TREE_PARENT(tmp) = TREE_PARENT(n)) != NULL) + { + if (n == TREE_LEFT(TREE_PARENT(n))) + TREE_LEFT(TREE_PARENT(n)) = tmp; + else + TREE_RIGHT(TREE_PARENT(n)) = tmp; + } + else + t->root = tmp; + TREE_LEFT(tmp) = n; + TREE_PARENT(n) = tmp; +} + +static void tree_rotate_right(struct tree_s *t, struct node_s *n) +{ + struct node_s *tmp = TREE_LEFT(n); + if ((TREE_LEFT(n) = TREE_RIGHT(tmp)) != NULL) + TREE_PARENT(TREE_RIGHT(tmp)) = n; + if ((TREE_PARENT(tmp) = TREE_PARENT(n)) != NULL) + { + if (n == TREE_LEFT(TREE_PARENT(n))) + TREE_LEFT(TREE_PARENT(n)) = tmp; + else + TREE_RIGHT(TREE_PARENT(n)) = tmp; + } else + t->root = tmp; + TREE_RIGHT(tmp) = n; + TREE_PARENT(n) = tmp; +} + +static void tree_rebalance_insert(struct tree_s *t, struct node_s *n) +{ + struct node_s *parent, *gparent, *tmp; + while ((parent = TREE_PARENT(n)) != NULL && + TREE_COLOR(parent) == TREE_RED) + { + gparent = TREE_PARENT(parent); + if (parent == TREE_LEFT(gparent)) + { + tmp = TREE_RIGHT(gparent); + if (tmp != NULL && TREE_COLOR(tmp) == TREE_RED) + { + TREE_COLOR(tmp) = TREE_BLACK; + TREE_COLOR(parent) = TREE_BLACK; + TREE_COLOR(gparent) = TREE_RED; + n = gparent; + continue; + } + if (TREE_RIGHT(parent) == n) + { + tree_rotate_left(t, parent); + tmp = parent; + parent = n; + n = tmp; + } + TREE_COLOR(parent) = TREE_BLACK; + TREE_COLOR(gparent) = TREE_RED; + tree_rotate_right(t, gparent); + } + else + { + tmp = TREE_LEFT(gparent); + if (tmp != NULL && TREE_COLOR(tmp) == TREE_RED) + { + TREE_COLOR(tmp) = TREE_BLACK; + TREE_COLOR(parent) = TREE_BLACK; + TREE_COLOR(gparent) = TREE_RED; + n = gparent; + continue; + } + if (TREE_LEFT(parent) == n) + { + tree_rotate_right(t, parent); + tmp = parent; + parent = n; + n = tmp; + } + TREE_COLOR(parent) = TREE_BLACK; + TREE_COLOR(gparent) = TREE_RED; + tree_rotate_left(t, gparent); + } + } + TREE_COLOR(t->root) = TREE_BLACK; +} + +static void tree_rebalance_remove(struct tree_s *t, struct node_s *parent, + struct node_s *n) +{ + struct node_s *tmp; + while ((n == NULL || TREE_COLOR(n) == TREE_BLACK) && n != t->root) + { + if (TREE_LEFT(parent) == n) + { + tmp = TREE_RIGHT(parent); + if (TREE_COLOR(tmp) == TREE_RED) + { + TREE_COLOR(tmp) = TREE_BLACK; + TREE_COLOR(parent) = TREE_RED; + tree_rotate_left(t, parent); + tmp = TREE_RIGHT(parent); + } + if ((TREE_LEFT(tmp) == NULL || + TREE_COLOR(TREE_LEFT(tmp)) == TREE_BLACK) && + (TREE_RIGHT(tmp) == NULL || + TREE_COLOR(TREE_RIGHT(tmp)) == TREE_BLACK)) + { + TREE_COLOR(tmp) = TREE_RED; + n = parent; + parent = TREE_PARENT(n); + } + else + { + if (TREE_RIGHT(tmp) == NULL || + TREE_COLOR(TREE_RIGHT(tmp)) == TREE_BLACK) + { + struct node_s *oleft; + if ((oleft = TREE_LEFT(tmp)) != NULL) + TREE_COLOR(oleft) = TREE_BLACK; + TREE_COLOR(tmp) = TREE_RED; + tree_rotate_right(t, tmp); + tmp = TREE_RIGHT(parent); + } + TREE_COLOR(tmp) = TREE_COLOR(parent); + TREE_COLOR(parent) = TREE_BLACK; + if (TREE_RIGHT(tmp)) + TREE_COLOR(TREE_RIGHT(tmp)) = TREE_BLACK; + tree_rotate_left(t, parent); + n = t->root; + break; + } + } + else + { + tmp = TREE_LEFT(parent); + if (TREE_COLOR(tmp) == TREE_RED) + { + TREE_COLOR(tmp) = TREE_BLACK; + TREE_COLOR(parent) = TREE_RED; + tree_rotate_right(t, parent); + tmp = TREE_LEFT(parent); + } + if ((TREE_LEFT(tmp) == NULL || + TREE_COLOR(TREE_LEFT(tmp)) == TREE_BLACK) && + (TREE_RIGHT(tmp) == NULL || + TREE_COLOR(TREE_RIGHT(tmp)) == TREE_BLACK)) + { + TREE_COLOR(tmp) = TREE_RED; + n = parent; + parent = TREE_PARENT(n); + } + else + { + if (TREE_LEFT(tmp) == NULL || + TREE_COLOR(TREE_LEFT(tmp)) == TREE_BLACK) + { + struct node_s *oright; + if ((oright = TREE_RIGHT(tmp)) != NULL) + TREE_COLOR(oright) = TREE_BLACK; + TREE_COLOR(tmp) = TREE_RED; + tree_rotate_left(t, tmp); + tmp = TREE_LEFT(parent); + } + TREE_COLOR(tmp) = TREE_COLOR(parent); + TREE_COLOR(parent) = TREE_BLACK; + if (TREE_LEFT(tmp)) + TREE_COLOR(TREE_LEFT(tmp)) = TREE_BLACK; + tree_rotate_right(t, parent); + n = t->root; + break; + } + } + } + if (n != NULL) + TREE_COLOR(n) = TREE_BLACK; +} + +static struct node_s *tree_remove(struct tree_s *t, struct node_s *n) +{ + struct node_s *child, *parent, *old = n; + int color; + if (TREE_LEFT(n) == NULL) + child = TREE_RIGHT(n); + else if (TREE_RIGHT(n) == NULL) + child = TREE_LEFT(n); + else + { + struct node_s *left; + n = TREE_RIGHT(n); + while ((left = TREE_LEFT(n)) != NULL) + n = left; + child = TREE_RIGHT(n); + parent = TREE_PARENT(n); + color = TREE_COLOR(n); + if (child != NULL) + TREE_PARENT(child) = parent; + if (parent != NULL) + { + if (TREE_LEFT(parent) == n) + TREE_LEFT(parent) = child; + else + TREE_RIGHT(parent) = child; + } + else + t->root = child; + if (TREE_PARENT(n) == old) + parent = n; + TREE_PARENT(n) = TREE_PARENT(old); + TREE_LEFT(n) = TREE_LEFT(old); + TREE_RIGHT(n) = TREE_RIGHT(old); + TREE_COLOR(n) = TREE_COLOR(old); + if (TREE_PARENT(old) != NULL) + { + if (TREE_LEFT(TREE_PARENT(old)) == old) + TREE_LEFT(TREE_PARENT(old)) = n; + else + TREE_RIGHT(TREE_PARENT(old)) = n; + } + else + t->root = n; + TREE_PARENT(TREE_LEFT(old)) = n; + if (TREE_RIGHT(old) != NULL) + TREE_PARENT(TREE_RIGHT(old)) = n; + goto color; + } + parent = TREE_PARENT(n); + color = TREE_COLOR(n); + if (child != NULL) + TREE_PARENT(child) = parent; + if (parent) + { + if (TREE_LEFT(parent) == n) + TREE_LEFT(parent) = child; + else + TREE_RIGHT(parent) = child; + } + else + t->root = child; +color: + if (color == TREE_BLACK) + tree_rebalance_remove(t, parent, child); + return old; +} + +static void *tsearch(const void *key, void **root, + int (*compare)(const void *, const void *)) +{ + if (root == NULL) + return NULL; + struct tree_s *t = (struct tree_s *)root; + struct node_s *n = t->root, *parent = NULL; + int cmp = 0; + while (n != NULL) + { + parent = n; + cmp = compare(key, n->key); + if (cmp < 0) + n = TREE_LEFT(n); + else if (cmp > 0) + n = TREE_RIGHT(n); + else + return (void *)n; + } + n = (struct node_s *)malloc(sizeof(struct node_s)); + n->key = (void *)key; + TREE_PARENT(n) = parent; + TREE_LEFT(n) = TREE_RIGHT(n) = NULL; + TREE_COLOR(n) = TREE_RED; + if (parent != NULL) + { + if (cmp < 0) + TREE_LEFT(parent) = n; + else + TREE_RIGHT(parent) = n; + } + else + t->root = n; + tree_rebalance_insert(t, n); + return n; +} + +static void *tfind(const void *key, void **root, + int (*compare)(const void *, const void *)) +{ + if (root == NULL) + return NULL; + struct tree_s *t = (struct tree_s *)root; + struct node_s *n = t->root; + int cmp = 0; + while (n != NULL) + { + cmp = compare(key, n->key); + if (cmp < 0) + n = TREE_LEFT(n); + else if (cmp > 0) + n = TREE_RIGHT(n); + else + return (void *)n; + } + return NULL; +} + +static void *tdelete(const void *key, void **root, + int (*compare)(const void *, const void *)) +{ + if (root == NULL) + return NULL; + struct tree_s *t = (struct tree_s *)root; + struct node_s *n = t->root; + int cmp = 0; + while (n != NULL) + { + cmp = compare(key, n->key); + if (cmp < 0) + n = TREE_LEFT(n); + else if (cmp > 0) + n = TREE_RIGHT(n); + else + { + struct node_s *parent = TREE_PARENT(n); + n = tree_remove(t, n); + free(n); + return (parent == NULL? (void *)root: parent); + } + } + return NULL; +} + +static void tree_walk(const struct node_s *n, int depth, + void (*action)(const void *, const VISIT, const int)) +{ + if (TREE_LEFT(n) == NULL && TREE_RIGHT(n) == NULL) + action(n, leaf, depth); + else + { + action(n, preorder, depth); + if (TREE_LEFT(n) != NULL) + tree_walk(TREE_LEFT(n), depth+1, action); + action(n, postorder, depth); + if (TREE_RIGHT(n) != NULL) + tree_walk(TREE_RIGHT(n), depth+1, action); + action(n, endorder, depth); + } +} +static void twalk(const void *root, + void (*action)(const void *, const VISIT, const int)) +{ + if (root == NULL || action == NULL) + return; + struct node_s *n = (struct node_s *)root; + tree_walk(n, 0, action); +} + +static void tree_destroy(const struct node_s *n, void (*free_node)(void *)) +{ + if (n == NULL) + return; + tree_destroy(TREE_LEFT(n), free_node); + tree_destroy(TREE_RIGHT(n), free_node); +} +static void tdestroy(const void *root, void (*free_node)(void *)) +{ + struct node_s *n = (struct node_s *)root; + tree_destroy(n, free_node); +} /****************************************************************************/ /* MISC */ diff --git a/test/regtest/patch.cpp b/test/regtest/patch.cpp index 04e06e0..cc2afef 100644 --- a/test/regtest/patch.cpp +++ b/test/regtest/patch.cpp @@ -650,7 +650,7 @@ void record(const void *addr, intptr_t val) vals = n; } -struct ENTRY +struct LOG_ENTRY { const void *addr; intptr_t val; @@ -658,11 +658,12 @@ struct ENTRY void log(const void *addr, intptr_t val) { - static struct ENTRY **log = nullptr; + static struct LOG_ENTRY **log = nullptr; static size_t log_size = 0; static size_t log_ptr = 0; - struct ENTRY *entry = (struct ENTRY *)malloc(sizeof(struct ENTRY)); + struct LOG_ENTRY *entry = + (struct LOG_ENTRY *)malloc(sizeof(struct LOG_ENTRY)); if (entry == nullptr) { fprintf(stderr, "malloc() failed (%s)\n", strerror(errno)); @@ -673,7 +674,8 @@ void log(const void *addr, intptr_t val) if (log_ptr >= log_size) { log_size = (log_size == 0? 1: 2 * log_size); - log = (struct ENTRY **)realloc(log, log_size * sizeof(struct ENTRY *)); + log = (struct LOG_ENTRY **)realloc(log, + log_size * sizeof(struct LOG_ENTRY *)); if (log == nullptr) { fprintf(stderr, "realloc() failed (%s)\n", strerror(errno)); @@ -1091,6 +1093,71 @@ void test_string(void) free(s2); } +static int tree_compare(const void *a, const void *b) +{ + return (int)*(size_t *)a - (int)*(size_t *)b; +} +static void tree_print(const void *node, const VISIT which, const int depth) +{ + switch (which) + { + case leaf: case postorder: + fprintf(stderr, "%zu ", **(size_t **)node); + break; + default: + break; + } +} +void test_tree(void) +{ + const uint8_t sbox[256] = + { + 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, + 0xfe, 0xd7, 0xab, 0x76, 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, + 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, 0xb7, 0xfd, 0x93, 0x26, + 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, + 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, + 0xeb, 0x27, 0xb2, 0x75, 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, + 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, 0x53, 0xd1, 0x00, 0xed, + 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, + 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, + 0x50, 0x3c, 0x9f, 0xa8, 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, + 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, 0xcd, 0x0c, 0x13, 0xec, + 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, + 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, + 0xde, 0x5e, 0x0b, 0xdb, 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, + 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, 0xe7, 0xc8, 0x37, 0x6d, + 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, + 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, + 0x4b, 0xbd, 0x8b, 0x8a, 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, + 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, 0xe1, 0xf8, 0x98, 0x11, + 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, + 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, + 0xb0, 0x54, 0xbb, 0x16 + }; + void *root = NULL; + for (size_t i = 0; i < 256; i++) + { + size_t *ptr = (size_t *)malloc(sizeof(size_t)); + *ptr = (size_t)sbox[i]; + void *r = tsearch(ptr, &root, tree_compare); + if (r == NULL) + abort(); + if (*(size_t **)r != ptr) + fprintf(stderr, "insert %zu failed\n", *ptr); + } + const uint8_t xs[] = {101, 0, 103, 255, 107, 1, 101, 109}; + for (size_t i = 0; i < sizeof(xs); i++) + { + size_t key = (size_t)xs[i]; + void *r = tdelete(&key, &root, tree_compare); + if (r == NULL) + fprintf(stderr, "failed to delete %zu\n", key); + } + twalk(root, tree_print); + tdestroy(root, free); +} + extern "C" { void format(const char *msg, intptr_t a1, intptr_t a2, intptr_t a3, diff --git a/test/regtest/tree.exp b/test/regtest/tree.exp new file mode 100644 index 0000000..76c8435 --- /dev/null +++ b/test/regtest/tree.exp @@ -0,0 +1,2 @@ +failed to delete 101 +2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 102 104 105 106 108 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 PASSED diff --git a/test/regtest/tree.in b/test/regtest/tree.in new file mode 100644 index 0000000..4bcdbd4 --- /dev/null +++ b/test/regtest/tree.in @@ -0,0 +1 @@ +./test -M 'addr == 0xa0001d9' -P 'test_tree()@patch'