static void load_xcode(void) { FILE *fp; int xcode_version; int filemode; initialize_colorize(); fprintf(stderr, "LOADING: %s\n", mudconf.hcode_db); fp = my_open_file(mudconf.hcode_db, "rb", &filemode); if (!fp) { fprintf(stderr, "ERROR: %s not found.\n", mudconf.hcode_db); return; } fread(&xcode_version, sizeof(xcode_version), 1, fp); if (xcode_version != XCODE_MAGIC) { fprintf(stderr, "LOADING: %s (skipped xcodetree - version difference: 0x%08X vs 0x%08X)\n", mudconf.hcode_db, xcode_version, XCODE_MAGIC); return; } if (!load_btdb()) { /* TODO: We could be more graceful about this... */ exit(EXIT_FAILURE); } rb_walk(xcode_tree, WALK_INORDER, load_update1, fp); rb_walk(xcode_tree, WALK_INORDER, load_update2, NULL); rb_walk(xcode_tree, WALK_INORDER, load_update3, NULL); rb_walk(xcode_tree, WALK_INORDER, load_update4, NULL); /* Read in autopilot data */ rb_walk(xcode_tree, WALK_INORDER, load_autopilot_data, NULL); if (!feof(fp)) loadrepairs(fp); my_close_file(fp, &filemode); fprintf(stderr, "LOADING: %s (done)\n", mudconf.hcode_db); #ifdef BT_ADVANCED_ECON load_econdb(); #endif heartbeat_init(); }
void mech_remove_from_all_maps_except(MECH *mech, int num) { /* TODO: Put the mech and the except_map into a structure for arg. */ except_map = num; rb_walk(xcode_tree, WALK_INORDER, remove_from_all_maps_except_func, mech); except_map = -1; }
void bu_rb_walk(struct bu_rb_tree *tree, int order, void (*visit)(void), int trav_type) { BU_CKMAG(tree, BU_RB_TREE_MAGIC, "red-black tree"); RB_CKORDER(tree, order); rb_walk(tree, order, visit, WALK_DATA, trav_type); }
static void rb_walk(const struct rbnode *x, void (*action)(const void *, const VISIT, const int, void *), void *arg, int level) { if (x==RBNULL) return; if (x->left==RBNULL && x->right==RBNULL) { /* leaf */ (*action)(x->key, leaf, level, arg); } else { (*action)(x->key, preorder, level, arg); rb_walk(x->left, action, arg, level+1); (*action)(x->key, postorder, level, arg); rb_walk(x->right, action, arg, level+1); (*action)(x->key, endorder, level, arg); } }
void zap_unneccessary_hcode(void) { for (;;) { zappable_node = -1; rb_walk(xcode_tree, WALK_INORDER, zap_check, NULL); if(zappable_node >= 0) rb_delete(xcode_tree, (void *)zappable_node); else break; } }
/* Simple stress test procedure for the red-black tree routines. Does the following: * Generate a random number seed. By default this is generated from the current time. You can also pass an integer seed value on the command line if you want to test the same case. The seed value is displayed. * Create a tree and insert the integers from 0 up to TREE_SIZE - 1 into it, in random order. Verifies and displays the tree structure after each insertion. * Removes each integer from the tree, in a different random order. Verifies and displays the tree structure after each deletion. * Destroys the tree. This is pretty good test code if you write some of your own red-black tree routines. If you do so you will probably want to modify the code below so that it increments the random seed and goes on to new test cases automatically. */ int main(int argc, char **argv) { int array[TREE_SIZE]; struct rb_tree *tree; int i; randomize(argc, argv); for (i = 0; i < TREE_SIZE; i++) array[i] = i; shuffle(array, TREE_SIZE); tree = rb_create(); for (i = 0; i < TREE_SIZE; i++) { int result = rb_insert(tree, array[i]); if (result != 1) { printf("Error %d inserting element %d, %d, into tree.\n", result, i, array[i]); exit(EXIT_FAILURE); } printf("Inserted %d: ", array[i]); /*print_structure(tree->root, 0);*/ rb_walk(tree); putchar('\n'); verify_tree(tree, array); } shuffle(array, TREE_SIZE); for (i = 0; i < TREE_SIZE; i++) { if (rb_delete(tree, array[i]) == 0) { printf("Error removing element %d, %d, from tree.\n", i, array[i]); exit(EXIT_FAILURE); } printf("Removed %d: ", array[i]); /*print_structure(tree->root, 0);*/ rb_traverse(tree); putchar('\n'); verify_tree(tree, array + i + 1); } rb_destroy(tree); printf("Success!\n"); return EXIT_SUCCESS; }
void debug_memory(dbref player, void *data, char *buffer) { int i, gtotal = 0; Create(number, int, global_specials); Create(smallest, int, global_specials); Create(largest, int, global_specials); Create(total, int, global_specials); for(i = 0; i < global_specials; i++) { number[i] = 0; smallest[i] = -1; largest[i] = -1; total[i] = 0; } cheat_player = player; skipws(buffer); if(strcmp(buffer, "")) cheat_player = player; else cheat_player = -1; rb_walk(xcode_tree, WALK_INORDER, debug_check_stuff, NULL); for(i = 0; i < global_specials; i++) { if(number[i]) { if(smallest[i] == largest[i]) notify_printf(player, "%4d %-20s: %d bytes total, %d each", number[i], SpecialObjects[i].type, total[i], total[i] / number[i]); else notify_printf(player, "%4d %-20s: %d bytes total, %d avg, %d/%d small/large", number[i], SpecialObjects[i].type, total[i], total[i] / number[i], smallest[i], largest[i]); } gtotal += total[i]; } notify_printf(player, "Grand total: %d bytes.", gtotal); free((void *) number); free((void *) total); free((void *) smallest); free((void *) largest); }
void UpdateSpecialObjects(void) { static time_t lastrun = 0; char *cmdsave; int i; int times = lastrun ? (mudstate.now - lastrun) : 1; if(times > 20) times = 20; /* Machine's hopelessly lagged, we don't want to make it [much] worse */ cmdsave = mudstate.debug_cmd; for(i = 0; i < times; i++) { muxevent_run(); mudstate.debug_cmd = (char *) "< Generic hcode update handler>"; rb_walk(xcode_tree, WALK_INORDER, UpdateSpecialObject_func, NULL); } lastrun = mudstate.now; mudstate.debug_cmd = cmdsave; }
void rbwalk(const struct rbtree *rbinfo, void (*action)(const void *, const VISIT, const int, void *), void *arg) { if (rbinfo==NULL) return; rb_walk(rbinfo->rb_root, action, arg, 0); }
void SaveSpecialObjects(int i) { FILE *fp; int filemode; int xcode_version = XCODE_MAGIC; char target[LBUF_SIZE]; switch (i) { case DUMP_KILLED: sprintf(target, "%s.KILLED", mudconf.hcode_db); break; case DUMP_CRASHED: sprintf(target, "%s.CRASHED", mudconf.hcode_db); break; default: /* RESTART / normal */ sprintf(target, "%s.tmp", mudconf.hcode_db); break; } if (!save_btdb()) { /* TODO: We could be more graceful about this... */ exit(EXIT_FAILURE); } fp = my_open_file(target, "wb", &filemode); if(!fp) { log_perror("SAV", "FAIL", "Opening new hcode-save file", target); SendDB("ERROR occured during opening of new hcode-savefile."); return; } fwrite(&xcode_version, sizeof(xcode_version), 1, fp); /* Then, check each xcode thing for stuff */ rb_walk(xcode_tree, WALK_INORDER, save_maps_func, fp); /* Save autopilot data */ /* GoThruTree(xcode_tree, save_autopilot_data); */ saverepairs(fp); my_close_file(fp, &filemode); if (i == DUMP_RESTART || i == DUMP_NORMAL) { if (rename(mudconf.hcode_db, tprintf("%s.prev", mudconf.hcode_db)) < 0) { log_perror("SAV", "FAIL", "Renaming old hcode-save file ", target); SendDB("ERROR occured during renaming of old hcode save-file."); } if (rename(target, mudconf.hcode_db) < 0) { log_perror("SAV", "FAIL", "Renaming new hcode-save file ", target); SendDB("ERROR occured during renaming of new hcode save-file."); } } /* TODO: This used to report the number of entries saved. */ SendDB("Hcode saved."); #ifdef BT_ADVANCED_ECON save_econdb(target, i); #endif }
void mech_remove_from_all_maps(MECH *mech) { rb_walk(xcode_tree, WALK_INORDER, remove_from_all_maps_func, mech); }