static int L_readini( lua_State* L ) { const char* filename; inifile_t* inifile = NULL; inireader_iterator_t* iter = NULL; inireader_entry_t* current = NULL; btree_element_t* root = NULL; btree_element_t* groupelement = NULL; btree_element_t* grouproot = NULL; PARAM_STRING( filename ); if ( ( inifile = inireader_open( filename ) ) == NULL ) { return_error(); } // Add every entry into a btree to get the arrays and groups in one piece later DEBUGLOG( "Creating btree" ); root = btree_create(); iter = inireader_get_iterator( inifile, 0, 0, 0, 0 ); DEBUGLOG( "Filling up btree" ); for ( current = inireader_iterate( iter ); current != NULL; current = inireader_iterate( iter ) ) { DEBUGLOG( "Searching for or adding group: %s", current->group ); // Find group element groupelement = btree_find( root, current->group ); if ( groupelement == NULL ) { // A new grouproot needs to be created DEBUGLOG( "Creating new grouproot" ); grouproot = btree_create(); btree_add( &root, current->group, grouproot ); } else { // Retrieve the already added grouproot DEBUGLOG( "Setting grouproot" ); grouproot = ( btree_element_t* )groupelement->data; } // Add the new element to the grouptree btree_add( &grouproot, current->identifier, current ); } // Traverse the tree and put our inivalues onto the lua stack DEBUGLOG( "Creating initial lua table" ); lua_newtable( L ); readini_tree_traverse( L, root ); DEBUGLOG( "Freeing the btree data" ); // Free the group trees readini_tree_free( root ); // Free the main tree btree_free( root ); // Close the inifile inireader_close( inifile ); return 1; }
int main() { btree_t tree; btree_init(&tree); btree_add(&tree, 3); btree_add(&tree, 1); btree_add(&tree, 2); btree_add(&tree, 4); btree_print(&tree); return 0; }
int pdb_item_create(pdb_t *p, uint16_t freq, uint32_t id) { pdb_entry_t *item; timestamp_t zero_ts; if (btree_find(p->db, id, (void**)&item)) { debug_msg("Item already exists\n"); return FALSE; } item = (pdb_entry_t*)xmalloc(sizeof(pdb_entry_t)); if (item == NULL) { return FALSE; } /* Initialize elements of item here as necesary **********************/ item->magic = 0xc001babe; item->ssrc = id; item->render_3D_data = NULL; item->enc = -1; item->enc_fmt_len = 2 * (CODEC_LONG_NAME_LEN + 1); item->enc_fmt = xmalloc(item->enc_fmt_len); item->sample_rate = freq; item->gain = 1.0; item->mute = 0; zero_ts = ts_map32(8000, 0); item->last_ui_update = zero_ts; item->first_mix = TRUE; /* Initial jitter estimate (30ms) */ item->jitter = ts_map32(8000, 240); item->transit = zero_ts; item->last_transit = zero_ts; item->last_last_transit = zero_ts; item->avg_transit = zero_ts; item->playout = zero_ts; item->last_arr = zero_ts; item->last_rtt = 0.0; item->avg_rtt = 0.0; /* Packet stats initialization */ item->received = 0; item->duplicates = 0; item->misordered = 0; item->jit_toged = 0; item->spike_toged = 0; item->spike_events = 0; pdb_item_validate(item); /*********************************************************************/ if (btree_add(p->db, id, (void*)item) == FALSE) { debug_msg("failed to add item to persistent database!\n"); return FALSE; } p->nelem++; return TRUE; }
static void *dmd_realloc(void *old, int size, const char *file, int line) { struct dmd_memblock *mb, sb; void *p; // debug4("realloc %s:%d (%ul -> %i)", file, line, (unsigned long) old, size); sb.ptr = old; mb = btree_get(&dmd_tree, &sb); if (!mb) { putlog(LOG_MISC, "*", "*** DMD: FAILED REALLOC %s:%d (%d). Old pointer not found. This is probably fatal!", file, line, size); return NULL; } p = mb->ptr; p = realloc(p, size); if (!p) { putlog(LOG_MISC, "*", "*** DMD: FAILED REALLOC %s:%d (%d). realloc() returned NULL", file, line, size); // fatal("Memory re-allocation failed", 0); } if (((unsigned long) p) != ((unsigned long) old)) { // debug0("newpointer"); btree_remove(&dmd_tree, mb); mb = dmd_create(p, size, line, file); btree_add(&dmd_tree, (void *) mb); } else { // debug0("oldpointer"); mb->size = size; /* p = strrchr(file, '/'); strncpy(mb->file, p ? p + 1 : file, DMD_FILE_SIZE); mb->file[DMD_FILE_SIZE] = 0; mb->line = line;*/ } return p; }
void print_files(char **paths, int flags) { t_stat attrib; t_file *file; t_padding paddings; t_btree *list; init_paddings(&paddings); list = NULL; while (*paths != NULL) { if (lstat(*paths, &attrib) != -1) { if (!S_ISDIR(attrib.st_mode) || flags & DIR_FILE) { file = get_file_infos(*paths, &paddings, flags); btree_add(&list, file, flags & TIME ? &timecmp : &namecmp); flags |= THERE_FILE; } else flags |= THERE_DIR; } paths++; } print_content(list, flags, paddings); if (flags & THERE_DIR && flags & THERE_FILE) ft_putchar('\n'); }
int main() { struct btree_node *n, *bt = NULL; //btree_init(&bt); struct mydata foo = { 10, "hello" }; struct mydata bar = { 20, "world" }; printf("EMPTY TREE: %d\n", btree_empty(&bt)); btree_add(&bt, &foo, mycmp); btree_add(&bt, &bar, mycmp); printf("EMPTY TREE: %d\n", btree_empty(&bt)); printf("==== go search ====\n"); /* find existent data */ struct mydata *p = btree_get(bt, &bar, mycmp); shownode("result for 20: ", p); printf("==== go search ====\n"); /* find unexistent data */ struct mydata nop = { 15, NULL }; p = btree_get(bt, &nop, mycmp); shownode("result for 15: ", p); printf("==== go get hittest ====\n"); n = btree_hittest(bt, NULL); shownode("hitest is: ", p); printf("==== go remove 20 ====\n"); if (btree_del(bt, &bar, mycmp, NULL)) printf("node found and removed\n"); else printf("oops\n"); printf("==== go search ====\n"); /* find existent data */ p = btree_get(bt, &bar, mycmp); shownode("result for 20: ", p); printf("==== go search ====\n"); /* find existent data */ p = btree_get(bt, &foo, mycmp); shownode("result for 10: ", p); btree_cleartree(bt, NULL); return 0; }
R_API int btree_optimize(struct btree_node **T, BTREE_CMP(cmp)) { struct btree_node *node, *NT = NULL; do { node = btree_hittest(*T, NULL); if (node) { btree_add (&NT, node->data, cmp); btree_del (*T, node->data, cmp, NULL); } } while(node); *T = NT; /* replace one tree with the other */ return 0; }
int main() { btree_t *b; uint32_t i, *x; uint32_t v[] = {15, 5, 16, 3, 12, 20, 10, 13, 18, 23, 6, 7}; uint32_t nv = sizeof(v) / sizeof(v[0]); btree_create(&b); for(i = 0; i < nv; i++) { x = (uint32_t*)xmalloc(sizeof(uint32_t)); *x = (uint32_t)random(); if (btree_add(b, v[i], (void*)x) != TRUE) { printf("Fail Add %lu %lu\n", v[i], *x); } } printf("depth %d\n", btree_depth(b->root)); btree_dump(b); sleep(3); btree_remove(b, 5, (void*)&x); btree_dump(b); sleep(3); btree_remove(b, 16, (void*)&x); btree_dump(b); sleep(3); btree_remove(b, 13, (void*)&x); btree_dump(b); while (btree_get_root_key(b, &i)) { if (btree_remove(b, i, (void*)&x) == FALSE) { fprintf(stderr, "Failed to remove %lu\n", i); } btree_dump(b); sleep(1); } if (btree_destroy(&b) == FALSE) { printf("Failed to destroy \n"); } return 0; }
static void *dmd_malloc(int size, const char *file, int line) { struct dmd_memblock *mb; void *ptr; // debug2("malloc %s:%d", file, line); ptr = malloc(size); // debug1("ptr: %u", (unsigned long) ptr); if (!ptr) { putlog(LOG_MISC, "*", "*** DMD: FAILED MALLOC %s (%d) (%d): %s", file, line, size, strerror(errno)); fatal("Memory allocation failed", 0); } mb = dmd_create(ptr, size, line, file); if (btree_get(&dmd_tree, mb)) putlog(LOG_MISC, "*", "*** DMD: DOUBLED POINTER?!?!?"); btree_add(&dmd_tree, (void *) mb); return mb->ptr; }
// Perform timed batch insertions in sequential order char *test_perf_insert_seq() { srand(time(NULL)); clock_t start = clock(), diff; for (int i=0; i<n; i++) { int item = i; root = btree_add(s, root, item, item); } diff = clock() - start; int msec = diff * 1000 / CLOCKS_PER_SEC; double ops = n * 1000; ops /= msec; printf("Time to insert %u items: %u ms\n", n, msec); printf("Time per operation: %u us\n", (1000*msec)/n); printf("Operations per second: %f\n", ops); return NULL; }
struct r_bin_mz_segment_t * r_bin_mz_get_segments(const struct r_bin_mz_obj_t *bin) { #if 0 int i; struct r_bin_mz_segment_t *ret; const MZ_image_relocation_entry * const relocs = bin->relocation_entries; const int num_relocs = bin->dos_header->num_relocs; eprintf ("cs 0x%x\n", bin->dos_header->cs); eprintf ("ss 0x%x\n", bin->dos_header->ss); for (i = 0; i < num_relocs; i++) { eprintf ("0x%08x segment 0x%08lx\n", relocs[i].offset, relocs[i].segment); // ut65 paddr = r_bin_mz_seg_to_paddr (bin, relocs[i].segment) + relocs[i].offset; // eprintf ("pa 0x%08llx\n", paddr); } btree_add (&tree, (void *)&first_segment, cmp_segs); /* Add segment address of stack segment if it's resides inside dos executable. */ if (r_bin_mz_seg_to_paddr (bin, stack_segment) < bin->dos_file_size) { btree_add (&tree, (void *)&stack_segment, cmp_segs); } return NULL; #endif #if 1 struct btree_node *tree; struct r_bin_mz_segment_t *ret; ut16 *segments, *curr_seg; int i, num_segs; ut64 paddr; const ut16 first_segment = 0; const ut16 stack_segment = bin->dos_header->ss; const MZ_image_relocation_entry * const relocs = bin->relocation_entries; const int num_relocs = bin->dos_header->num_relocs; const ut64 last_parag = ((bin->dos_file_size + 0xF) >> 4) - \ bin->dos_header->header_paragraphs; btree_init (&tree); for (i = 0; i < num_relocs; i++) { paddr = r_bin_mz_seg_to_paddr (bin, relocs[i].segment) + relocs[i].offset; if ((paddr + 2) < bin->dos_file_size) { curr_seg = (ut16 *)(bin->b->buf + paddr); /* Add segment only if it's located inside dos executable data */ if (r_read_le16 (curr_seg) <= last_parag) { btree_add (&tree, curr_seg, cmp_segs); } } } /* Add segment address of first segment to make sure that it will be added. If relocations empty or there isn't first segment in relocations.) */ btree_add (&tree, (void *)&first_segment, cmp_segs); /* Add segment address of stack segment if it's resides inside dos executable. */ if (r_bin_mz_seg_to_paddr (bin, stack_segment) < bin->dos_file_size) { btree_add (&tree, (void *)&stack_segment, cmp_segs); } if (!num_relocs) { btree_cleartree (tree, NULL); return NULL; } segments = calloc (1 + num_relocs, sizeof (*segments)); if (!segments) { eprintf ("Error: calloc (segments)\n"); btree_cleartree (tree, NULL); return NULL; } curr_seg = segments; btree_traverse (tree, 0, &curr_seg, trv_segs); num_segs = curr_seg - segments; ret = calloc (num_segs + 1, sizeof (struct r_bin_mz_segment_t)); if (!ret) { free (segments); btree_cleartree (tree, NULL); eprintf ("Error: calloc (struct r_bin_mz_segment_t)\n"); return NULL; } btree_cleartree (tree, NULL); ret[0].paddr = r_bin_mz_seg_to_paddr (bin, segments[0]); for (i = 1; i < num_segs; i++) { ret[i].paddr = r_bin_mz_seg_to_paddr (bin, segments[i]); ret[i - 1].size = ret[i].paddr - ret[i - 1].paddr; } ret[i - 1].size = bin->dos_file_size - ret[i - 1].paddr; ret[i].last = 1; free (segments); return ret; #endif }
int main(int argc, char **argv) { struct btree *bt; uint64_t ptr; int j, count, op, arg; if (argc != 4) { fprintf(stderr,"Usage: btree_example <op> <size/ptr> <count>\n"); exit(1); } count = atoi(argv[3]); arg = atoi(argv[2]); if (!strcasecmp(argv[1],"alloc")) { op = OP_ALLOC; } else if (!strcasecmp(argv[1],"free")) { op = OP_FREE; } else if (!strcasecmp(argv[1],"allocfree")) { op = OP_ALLOCFREE; } else if (!strcasecmp(argv[1],"add")) { op = OP_ADD; } else if (!strcasecmp(argv[1],"walk")) { op = OP_WALK; } else if (!strcasecmp(argv[1],"fill")) { op = OP_FILL; } else if (!strcasecmp(argv[1],"find")) { op = OP_FIND; } else { printf("not supported op %s\n", argv[1]); exit(1); } bt = btree_open(NULL, "./btree.db", BTREE_CREAT); btree_clear_flags(bt,BTREE_FLAG_USE_WRITE_BARRIER); if (bt == NULL) { perror("btree_open"); exit(1); } for (j = 0; j < count; j++) { if (op == OP_ALLOC) { ptr = btree_alloc(bt,arg); printf("PTR: %llu\n", ptr); } else if (op == OP_FREE) { btree_free(bt,arg); } else if (op == OP_ALLOCFREE) { ptr = btree_alloc(bt,arg); printf("PTR: %llu\n", ptr); btree_free(bt,ptr); } } if (op == OP_ADD) { int retval; char key[16]; memset(key,0,16); strcpy(key,argv[2]); retval = btree_add(bt,(unsigned char*)key, (unsigned char*)argv[3],strlen(argv[3]),1); printf("retval %d\n", retval); if (retval == -1) { printf("Error: %s\n", strerror(errno)); } } else if (op == OP_WALK) { btree_walk(bt,bt->rootptr); } else if (op == OP_FILL) { for (j = 0; j < count; j++) { int r = random()%arg; int retval; char key[64]; char val[64]; memset(key,0,64); snprintf(key,64,"k%d",r); snprintf(val,64,"val:%d",r); retval = btree_add(bt,(unsigned char*)key, (unsigned char*)val, strlen(val), 1); if (retval == -1) { printf("Error: %s\n", strerror(errno)); goto err; } } } else if (op == OP_FIND) { int retval; char key[16], *data; memset(key,0,16); strcpy(key,argv[2]); uint64_t voff; uint32_t datalen; retval = btree_find(bt,(unsigned char*)key,&voff); if (retval == -1) { if (errno == ENOENT) { printf("Key not found\n"); exit(0); } else { perror("Error searching for key"); exit(1); } } printf("Key found at %llu\n", voff); btree_alloc_size(bt,&datalen,voff); data = malloc(datalen+1); btree_pread(bt,(unsigned char*)data,datalen,voff); data[datalen] = '\0'; printf("Value: %s\n", data); free(data); } btree_close(bt); return 0; err: btree_close(bt); return 1; }