void bt_free(struct list_s *n, free_fn freev) { if(!n) return; freev(n->data[BT_VALUE]); type_free(UnsignedLong, n->data[BT_COUNT]); bt_free(n->data[BT_LEFT], freev); bt_free(n->data[BT_RIGHT], freev); list_free(n); }
static void vmem_destroy1(vmem_t *vm) { #if defined(QCACHE) qc_destroy(vm); #endif /* defined(QCACHE) */ if (vm->vm_hashlist != NULL) { int i; for (i = 0; i < vm->vm_hashsize; i++) { bt_t *bt; while ((bt = LIST_FIRST(&vm->vm_hashlist[i])) != NULL) { KASSERT(bt->bt_type == BT_TYPE_SPAN_STATIC); bt_free(vm, bt); } } if (vm->vm_hashlist != &vm->vm_hash0) { xfree(vm->vm_hashlist, sizeof(struct vmem_hashlist *) * vm->vm_hashsize); } } bt_freetrim(vm, 0); VMEM_CONDVAR_DESTROY(vm); VMEM_LOCK_DESTROY(vm); xfree(vm, sizeof(*vm)); }
/* * The matching process is defined as "each and every UUID * specified in the "search pattern" must be present in the * "target pattern". Here "search pattern" is the set of UUIDs * specified by the service discovery client and "target pattern" * is the set of UUIDs present in a service record. * * Return 1 if each and every UUID in the search * pattern exists in the target pattern, 0 if the * match succeeds and -1 on error. */ static int sdp_match_uuid(sdp_list_t *search, sdp_list_t *pattern) { /* * The target is a sorted list, so we need not look * at all elements to confirm existence of an element * from the search pattern */ int patlen = sdp_list_len(pattern); if (patlen < sdp_list_len(search)) return -1; for (; search; search = search->next) { uuid_t *uuid128; void *data = search->data; sdp_list_t *list; if (data == NULL) return -1; // create 128-bit form of the search UUID uuid128 = sdp_uuid_to_uuid128((uuid_t *)data); list = sdp_list_find(pattern, uuid128, sdp_uuid128_cmp); bt_free(uuid128); if (!list) return 0; } return 1; }
void test_bt_1(CuTest* tc) { int val; int i; bt* tree; tree = bt_new(sizeof(int), 4); printf("Empty:\n"); bt_print(tree, print_int); printf("\n"); { int vals[] = { 10, 5, 100, 10, 50, 50, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 200,200,200,200,200,250,150 }; for (i=0; i<sizeof(vals)/sizeof(int); i++) { val = vals[i]; printf("Insert %i:\n", val); bt_insert(tree, &val, 0, compare_ints); //bt_print(tree, print_int); printf("\n"); } } printf("Values: "); for (i=0; i<tree->N; i++) { int val = *(int*)bt_access(tree, i); printf("%i ", val); // these tests depend on the values in the "vals" array above. if (i < 11) { CuAssertIntEquals(tc, 1, val); } else if (i < 12) { CuAssertIntEquals(tc, 5, val); } else if (i < 14) { CuAssertIntEquals(tc, 10, val); } else if (i < 16) { CuAssertIntEquals(tc, 50, val); } else if (i < 17) { CuAssertIntEquals(tc, 100, val); } else if (i < 18) { CuAssertIntEquals(tc, 150, val); } else if (i < 23) { CuAssertIntEquals(tc, 200, val); } else { CuAssertIntEquals(tc, 250, val); } } printf("\n"); { int vals[] = { 0, 1, 2, 9, 10, 11, 49, 50, 51, 99, 100, 101, 149, 150, 151, 199, 200, 201, 249, 250, 251 }; int doesit[]={ 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0 }; for (i=0; i<sizeof(vals)/sizeof(int); i++) { int youthink; val = vals[i]; youthink = bt_contains(tree, &val, compare_ints); printf("Contains %i: %s\n", val, (youthink ? "yes" : "no")); CuAssertIntEquals(tc, doesit[i], youthink); } } bt_free(tree); }
void check_version(int dev_id) { struct hci_version ver; char *lmpver; if ((typ.dd = hci_open_dev(dev_id)) < 0) die("Could not open device\n"); if (hci_read_local_version(typ.dd, &ver, 1000) < 0) die("Can't read version info hci0\n"); lmpver = lmp_vertostr(ver.lmp_ver); if (strcmp(lmpver, "4.0")) { printf("You need a Bluetooth 4.0 LE device\n"); bt_free(lmpver); exit(1); } else { bt_free(lmpver); } hci_close_dev(typ.dd); }
/* Search and connect * Returns: * -1 - critical error (exit persist mode) * 1 - non critical error * 0 - success */ static int do_connect(void) { inquiry_info *ii; int reconnect = 0; int i, n, r = 0; do { if (reconnect) sleep(persist); reconnect = 1; if (cache.valid) { /* Use cached bdaddr */ r = create_connection(cache.dst, &cache.bdaddr, 0); if (r < 0) { terminate = 1; break; } continue; } syslog(LOG_INFO, "Inquiring"); /* FIXME: Should we use non general LAP here ? */ ii = NULL; n = hci_inquiry(src_dev, search_duration, 0, NULL, &ii, 0); if (n < 0) { syslog(LOG_ERR, "Inquiry failed. %s(%d)", strerror(errno), errno); continue; } for (i = 0; i < n; i++) { char dst[40]; ba2str(&ii[i].bdaddr, dst); r = create_connection(dst, &ii[i].bdaddr, 0); if (r < 0) { terminate = 1; break; } } bt_free(ii); } while (!terminate && persist); return r; }
void test_bt_many(CuTest* tc) { int val; int i; bt* tree; printf("Inserting many items...\n"); tree = bt_new(sizeof(int), 32); for (i=0; i<100000; i++) { val = rand() % 1000; bt_insert(tree, &val, 0, compare_ints); //bt_check(tree); } printf("Checking...\n"); CuAssertIntEquals(tc, 0, bt_check(tree)); printf("Done.\n"); bt_free(tree); }
static int vmem_add1(vmem_t *vm, vmem_addr_t addr, vmem_size_t size, vm_flag_t flags, int spanbttype) { bt_t *btspan; bt_t *btfree; KASSERT((flags & (VM_SLEEP|VM_NOSLEEP)) != 0); KASSERT((~flags & (VM_SLEEP|VM_NOSLEEP)) != 0); KASSERT(spanbttype == BT_TYPE_SPAN || spanbttype == BT_TYPE_SPAN_STATIC); btspan = bt_alloc(vm, flags); if (btspan == NULL) { return ENOMEM; } btfree = bt_alloc(vm, flags); if (btfree == NULL) { bt_free(vm, btspan); return ENOMEM; } btspan->bt_type = spanbttype; btspan->bt_start = addr; btspan->bt_size = size; btfree->bt_type = BT_TYPE_FREE; btfree->bt_start = addr; btfree->bt_size = size; VMEM_LOCK(vm); bt_insseg_tail(vm, btspan); bt_insseg(vm, btfree, btspan); bt_insfree(vm, btfree); vm->vm_size += size; VMEM_UNLOCK(vm); return 0; }
int hpquads(startree_t* starkd, codefile_t* codes, quadfile_t* quads, int Nside, double scale_min_arcmin, double scale_max_arcmin, int dimquads, int passes, int Nreuses, int Nloosen, int id, anbool scanoccupied, void* sort_data, int (*sort_func)(const void*, const void*), int sort_size, char** args, int argc) { hpquads_t myhpquads; hpquads_t* me = &myhpquads; int i; int pass; anbool circle = TRUE; double radius2; il* hptotry; int Nhptotry = 0; int nquads; double hprad; double quadscale; int skhp, sknside; qfits_header* qhdr; qfits_header* chdr; int N; int dimcodes; int quadsize; int NHP; memset(me, 0, sizeof(hpquads_t)); if (Nside > HP_MAX_INT_NSIDE) { ERROR("Error: maximum healpix Nside = %i", HP_MAX_INT_NSIDE); return -1; } if (Nreuses > 255) { ERROR("Error, reuse (-r) must be less than 256"); return -1; } me->Nside = Nside; me->dimquads = dimquads; NHP = 12 * Nside * Nside; dimcodes = dimquad2dimcode(dimquads); quadsize = sizeof(unsigned int) * dimquads; logmsg("Nside=%i. Nside^2=%i. Number of healpixes=%i. Healpix side length ~ %g arcmin.\n", me->Nside, me->Nside*me->Nside, NHP, healpix_side_length_arcmin(me->Nside)); me->sort_data = sort_data; me->sort_func = sort_func; me->sort_size = sort_size; tic(); me->starkd = starkd; N = startree_N(me->starkd); logmsg("Star tree contains %i objects.\n", N); // get the "HEALPIX" header from the skdt... skhp = qfits_header_getint(startree_header(me->starkd), "HEALPIX", -1); if (skhp == -1) { if (!qfits_header_getboolean(startree_header(me->starkd), "ALLSKY", FALSE)) { logmsg("Warning: skdt does not contain \"HEALPIX\" header. Code and quad files will not contain this header either.\n"); } } // likewise "HPNSIDE" sknside = qfits_header_getint(startree_header(me->starkd), "HPNSIDE", 1); if (sknside && Nside % sknside) { logerr("Error: Nside (-n) must be a multiple of the star kdtree healpixelisation: %i\n", sknside); return -1; } if (!scanoccupied && (N*(skhp == -1 ? 1 : sknside*sknside*12) < NHP)) { logmsg("\n\n"); logmsg("NOTE, your star kdtree is sparse (has only a fraction of the stars expected)\n"); logmsg(" so you probably will get much faster results by setting the \"-E\" command-line\n"); logmsg(" flag.\n"); logmsg("\n\n"); } quads->dimquads = me->dimquads; codes->dimcodes = dimcodes; quads->healpix = skhp; codes->healpix = skhp; quads->hpnside = sknside; codes->hpnside = sknside; if (id) { quads->indexid = id; codes->indexid = id; } qhdr = quadfile_get_header(quads); chdr = codefile_get_header(codes); add_headers(qhdr, args, argc, startree_header(me->starkd), circle, passes); add_headers(chdr, args, argc, startree_header(me->starkd), circle, passes); if (quadfile_write_header(quads)) { ERROR("Couldn't write headers to quad file"); return -1; } if (codefile_write_header(codes)) { ERROR("Couldn't write headers to code file"); return -1; } quads->numstars = codes->numstars = N; me->quad_dist2_upper = arcmin2distsq(scale_max_arcmin); me->quad_dist2_lower = arcmin2distsq(scale_min_arcmin); codes->index_scale_upper = quads->index_scale_upper = distsq2rad(me->quad_dist2_upper); codes->index_scale_lower = quads->index_scale_lower = distsq2rad(me->quad_dist2_lower); me->nuses = calloc(N, sizeof(unsigned char)); // hprad = sqrt(2) * (healpix side length / 2.) hprad = arcmin2dist(healpix_side_length_arcmin(Nside)) * M_SQRT1_2; quadscale = 0.5 * sqrt(me->quad_dist2_upper); // 1.01 for a bit of safety. we'll look at a few extra stars. radius2 = square(1.01 * (hprad + quadscale)); me->radius2 = radius2; logmsg("Healpix radius %g arcsec, quad scale %g arcsec, total %g arcsec\n", distsq2arcsec(hprad*hprad), distsq2arcsec(quadscale*quadscale), distsq2arcsec(radius2)); hptotry = il_new(1024); if (scanoccupied) { logmsg("Scanning %i input stars...\n", N); for (i=0; i<N; i++) { double xyz[3]; int j; if (startree_get(me->starkd, i, xyz)) { ERROR("Failed to get star %i", i); return -1; } j = xyzarrtohealpix(xyz, Nside); il_insert_unique_ascending(hptotry, j); if (log_get_level() > LOG_VERB) { double ra,dec; if (startree_get_radec(me->starkd, i, &ra, &dec)) { ERROR("Failed to get RA,Dec for star %i\n", i); return -1; } logdebug("star %i: RA,Dec %g,%g; xyz %g,%g,%g; hp %i\n", i, ra, dec, xyz[0], xyz[1], xyz[2], j); } } logmsg("Will check %zu healpixes.\n", il_size(hptotry)); if (log_get_level() > LOG_VERB) { logdebug("Checking healpixes: [ "); for (i=0; i<il_size(hptotry); i++) logdebug("%i ", il_get(hptotry, i)); logdebug("]\n"); } } else { if (skhp == -1) { // Try all healpixes. il_free(hptotry); hptotry = NULL; Nhptotry = NHP; } else { // The star kdtree may itself be healpixed int starhp, starx, stary; // In that case, the healpixes we are interested in form a rectangle // within a big healpix. These are the coords (in [0, Nside)) of // that rectangle. int x0, x1, y0, y1; int x, y; healpix_decompose_xy(skhp, &starhp, &starx, &stary, sknside); x0 = starx * (Nside / sknside); x1 = (starx+1) * (Nside / sknside); y0 = stary * (Nside / sknside); y1 = (stary+1) * (Nside / sknside); for (y=y0; y<y1; y++) { for (x=x0; x<x1; x++) { int j = healpix_compose_xy(starhp, x, y, Nside); il_append(hptotry, j); } } assert(il_size(hptotry) == (Nside/sknside) * (Nside/sknside)); } } if (hptotry) Nhptotry = il_size(hptotry); me->quadlist = bl_new(65536, quadsize); if (Nloosen) me->retryhps = il_new(1024); for (pass=0; pass<passes; pass++) { char key[64]; int nthispass; logmsg("Pass %i of %i.\n", pass+1, passes); logmsg("Trying %i healpixes.\n", Nhptotry); nthispass = build_quads(me, Nhptotry, hptotry, Nreuses); logmsg("Made %i quads (out of %i healpixes) this pass.\n", nthispass, Nhptotry); logmsg("Made %i quads so far.\n", (me->bigquadlist ? bt_size(me->bigquadlist) : 0) + (int)bl_size(me->quadlist)); sprintf(key, "PASS%i", pass+1); fits_header_mod_int(chdr, key, nthispass, "quads created in this pass"); fits_header_mod_int(qhdr, key, nthispass, "quads created in this pass"); logmsg("Merging quads...\n"); if (!me->bigquadlist) me->bigquadlist = bt_new(quadsize, 256); for (i=0; i<bl_size(me->quadlist); i++) { void* q = bl_access(me->quadlist, i); bt_insert2(me->bigquadlist, q, FALSE, compare_quads, &me->dimquads); } bl_remove_all(me->quadlist); } il_free(hptotry); hptotry = NULL; if (Nloosen) { int R; for (R=Nreuses+1; R<=Nloosen; R++) { il* trylist; int nthispass; logmsg("Loosening reuse maximum to %i...\n", R); logmsg("Trying %zu healpixes.\n", il_size(me->retryhps)); if (!il_size(me->retryhps)) break; trylist = me->retryhps; me->retryhps = il_new(1024); nthispass = build_quads(me, il_size(trylist), trylist, R); logmsg("Made %i quads (out of %zu healpixes) this pass.\n", nthispass, il_size(trylist)); il_free(trylist); for (i=0; i<bl_size(me->quadlist); i++) { void* q = bl_access(me->quadlist, i); bt_insert2(me->bigquadlist, q, FALSE, compare_quads, &me->dimquads); } bl_remove_all(me->quadlist); } } if (me->retryhps) il_free(me->retryhps); kdtree_free_query(me->res); me->res = NULL; me->inds = NULL; me->stars = NULL; free(me->nuses); me->nuses = NULL; logmsg("Writing quads...\n"); // add the quads from the big-quadlist nquads = bt_size(me->bigquadlist); for (i=0; i<nquads; i++) { unsigned int* q = bt_access(me->bigquadlist, i); quad_write(codes, quads, q, me->starkd, me->dimquads, dimcodes); } // add the quads that were made during the final round. for (i=0; i<bl_size(me->quadlist); i++) { unsigned int* q = bl_access(me->quadlist, i); quad_write(codes, quads, q, me->starkd, me->dimquads, dimcodes); } // fix output file headers. if (quadfile_fix_header(quads)) { ERROR("Failed to fix quadfile headers"); return -1; } if (codefile_fix_header(codes)) { ERROR("Failed to fix codefile headers"); return -1; } bl_free(me->quadlist); bt_free(me->bigquadlist); toc(); logmsg("Done.\n"); return 0; }
static DBusMessage *request_authorization(DBusConnection *conn, DBusMessage *msg, void *data) { struct record_data *user_record; struct service_adapter *serv_adapter = data; sdp_record_t *record; sdp_list_t *services; const char *sender; dbus_uint32_t handle; const char *address; struct pending_auth *auth; char uuid_str[MAX_LEN_UUID_STR]; uuid_t *uuid, *uuid128; bdaddr_t src; if (dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &address, DBUS_TYPE_UINT32, &handle, DBUS_TYPE_INVALID) == FALSE) return NULL; sender = dbus_message_get_sender(msg); if (find_pending_by_sender(serv_adapter, sender)) return btd_error_does_not_exist(msg); user_record = find_record(serv_adapter, handle, sender); if (!user_record) { user_record = find_record(serv_adapter_any, handle, sender); if (!user_record) return btd_error_not_authorized(msg); } record = sdp_record_find(user_record->handle); if (record == NULL) return btd_error_not_authorized(msg); if (sdp_get_service_classes(record, &services) < 0) { sdp_record_free(record); return btd_error_not_authorized(msg); } if (services == NULL) return btd_error_not_authorized(msg); uuid = services->data; uuid128 = sdp_uuid_to_uuid128(uuid); sdp_list_free(services, bt_free); if (sdp_uuid2strn(uuid128, uuid_str, MAX_LEN_UUID_STR) < 0) { bt_free(uuid128); return btd_error_not_authorized(msg); } bt_free(uuid128); auth = g_new0(struct pending_auth, 1); auth->msg = dbus_message_ref(msg); auth->conn = dbus_connection_ref(connection); auth->sender = user_record->sender; memcpy(auth->uuid, uuid_str, MAX_LEN_UUID_STR); str2ba(address, &auth->dst); serv_adapter->pending_list = g_slist_append(serv_adapter->pending_list, auth); auth = next_pending(serv_adapter); if (auth == NULL) return btd_error_does_not_exist(msg); if (serv_adapter->adapter) adapter_get_address(serv_adapter->adapter, &src); else bacpy(&src, BDADDR_ANY); if (btd_request_authorization(&src, &auth->dst, auth->uuid, auth_cb, serv_adapter) < 0) { serv_adapter->pending_list = g_slist_remove(serv_adapter->pending_list, auth); g_free(auth); return btd_error_not_authorized(msg); } return NULL; }
void extra_info(int dd, int dev_id, bdaddr_t* bdaddr) { uint16_t handle, offset; uint8_t features[8], max_page = 0; char name[249], *tmp; char addr[19] = { 0 }; uint8_t mode, afh_map[10]; struct hci_version version; struct hci_dev_info di; struct hci_conn_info_req *cr; int i, cc = 0; if (hci_devinfo(dev_id, &di) < 0) { perror("Can't get device info"); exit(1); } printf("Requesting information ...\n"); cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info)); if (!cr) { perror("Can't get connection info"); exit(1); } bacpy(&cr->bdaddr, bdaddr); cr->type = ACL_LINK; if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) { if (hci_create_connection(dd, bdaddr, htobs(di.pkt_type & ACL_PTYPE_MASK), 0, 0x01, &handle, 25000) < 0) { perror("Can't create connection"); return; } sleep(1); cc = 1; } else handle = htobs(cr->conn_info->handle); ba2str(bdaddr, addr); printf("\tBD Address: %s\n", addr); if (hci_read_remote_name(dd, bdaddr, sizeof(name), name, 25000) == 0) printf("\tDevice Name: %s\n", name); if (hci_read_remote_version(dd, handle, &version, 20000) == 0) { char *ver = lmp_vertostr(version.lmp_ver); printf("\tLMP Version: %s (0x%x) LMP Subversion: 0x%x\n" "\tManufacturer: %s (%d)\n", ver ? ver : "n/a", version.lmp_ver, version.lmp_subver, bt_compidtostr(version.manufacturer), version.manufacturer); if (ver) bt_free(ver); } memset(features, 0, sizeof(features)); hci_read_remote_features(dd, handle, features, 20000); if ((di.features[7] & LMP_EXT_FEAT) && (features[7] & LMP_EXT_FEAT)) hci_read_remote_ext_features(dd, handle, 0, &max_page, features, 20000); printf("\tFeatures%s: 0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x " "0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x\n", (max_page > 0) ? " page 0" : "", features[0], features[1], features[2], features[3], features[4], features[5], features[6], features[7]); tmp = lmp_featurestostr(features, "\t\t", 63); printf("%s\n", tmp); bt_free(tmp); for (i = 1; i <= max_page; i++) { if (hci_read_remote_ext_features(dd, handle, i, NULL, features, 20000) < 0) continue; printf("\tFeatures page %d: 0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x " "0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x\n", i, features[0], features[1], features[2], features[3], features[4], features[5], features[6], features[7]); } if (hci_read_clock_offset(dd, handle, &offset, 1000) < 0) { perror("Reading clock offset failed"); exit(1); } printf("\tClock offset: 0x%4.4x\n", btohs(offset)); if(hci_read_afh_map(dd, handle, &mode, afh_map, 1000) < 0) { perror("HCI read AFH map request failed"); } if(mode == 0x01) { // DGS: Replace with call to btbb_print_afh_map - need a piconet printf("\tAFH Map: 0x"); for(i=0; i<10; i++) printf("%02x", afh_map[i]); printf("\n"); } else { printf("AFH disabled.\n"); } free(cr); if (cc) { usleep(10000); hci_disconnect(dd, handle, HCI_OE_USER_ENDED_CONNECTION, 10000); } }
static void getDevices(std::vector<al::Bluetooth>& devs){ /* See cmd_inq and cmd_scan in tools/hcitool.c. */ /* From bluetooth/hci.h: typedef struct { bdaddr_t bdaddr; uint8_t pscan_rep_mode; uint8_t pscan_period_mode; uint8_t pscan_mode; uint8_t dev_class[3]; uint16_t clock_offset; } __attribute__ ((packed)) inquiry_info; */ /*static const char *inq_help = "Usage:\n" "\tinq [--length=N] maximum inquiry duration in 1.28 s units\n" "\t [--numrsp=N] specify maximum number of inquiry responses\n" "\t [--iac=lap] specify the inquiry access code\n" "\t [--flush] flush the inquiry cache\n";*/ // First determine if there is a Bluetooth controller int dev_id = hci_get_route(NULL); if (dev_id < 0) { perror("Bluetooth not available"); return; } inquiry_info *info = NULL; uint8_t lap[3] = { 0x33, 0x8b, 0x9e }; int num_rsp = 16; // max number responses int length = 8; // max inquiry response time, in 1.28 s int flags = 0; char addr[18], name[249]; num_rsp = hci_inquiry(dev_id, length, num_rsp, lap, &info, flags); if (num_rsp < 0) { perror("Bluetooth HCI inquiry failed."); return; } int dd = hci_open_dev(dev_id); if (dd < 0) { perror("Bluetooth HCI device open failed"); bt_free(info); return; } Bluetooth bt; for (int i = 0; i < num_rsp; i++) { ba2str(&info[i].bdaddr, addr); if(hci_read_remote_name_with_clock_offset(dd, &info[i].bdaddr, info[i].pscan_rep_mode, info[i].clock_offset | 0x8000, sizeof(name), name, 100000) < 0){ strcpy(name, "n/a"); } bt.mName = std::string(name); bt.mAddr = std::string(addr); bt.mClass= (unsigned(info[i].dev_class[2])<<16) | (unsigned(info[i].dev_class[1])<<8) | info[i].dev_class[0]; devs.push_back(bt); } /**/ bt_free(info); }
lui_bt *bt_newA(tagitem *taglist) { tagitem *t; lui_bt *bt; SDL_Rect src, dst; SDL_PixelFormat *format; bt = calloc(1, sizeof(lui_bt)); if(!bt) return NULL; bt->id = -1; bt->type = GAD_BUTTON; t = tag_next(&taglist); while(t) { switch(t->tag) { case LUI_NAME: bt->name = strdup((char *)t->data); break; case LUI_ID: bt->id = t->data; break; case LUI_DATA: bt->data = (void *)t->data; break; case LUI_ACTIVATEHOOK: bt->onactivate = (void *)t->data; break; case LUI_XPOS: bt->x = t->data; break; case LUI_YPOS: bt->y = t->data; break; case LUI_WIDTH: bt->w = t->data; break; case LUI_HEIGHT: bt->h = t->data; break; case LUI_SCREEN: bt->screen = (SDL_Surface *)t->data; break; case LBT_IMAGE: bt->image = IMG_Load((char *)t->data); break; case LBT_HLIMAGE: bt->hlimage = IMG_Load((char *)t->data); break; case LBT_CLICKIMAGE: bt->click = IMG_Load((char *)t->data); break; case LBT_HLCLICKIMAGE: bt->hlclick = IMG_Load((char *)t->data); break; case LBT_TILESET: bt->tileset = (xitiles *)t->data; break; case LBT_TILE: bt->tile = t->data; break; case LBT_HLTILE: bt->hltile = t->data; break; case LBT_CLICKTILE: bt->ctile = t->data; break; case LBT_HLCLICKTILE: bt->hlctile = t->data; break; case LBT_TOGGLE: bt->type = GAD_TOGGLE; break; } t = tag_next(&taglist); } if(((!bt->image) && (!bt->tileset)) || (!bt->screen)) { bt_free(bt); return NULL; } if(!bt->w) { if(bt->tileset) { bt->w = bt->tileset->w; } else { bt->w = bt->image->w; } } if(!bt->h) { if(bt->tileset) { bt->h = bt->tileset->h; } else { bt->h = bt->image->h; } } format = bt->screen->format; bt->store = SDL_CreateRGBSurface(SDL_RLEACCEL, bt->w, bt->h, format->BitsPerPixel, format->Rmask, format->Gmask, format->Bmask, format->Amask); bt->ia = iarea_new(IA_INPUT, IA_CTL_MOUSE, IA_MBUTTONS, IA_LMB_MASK, IA_XPOS, bt->x, IA_YPOS, bt->y, IA_WIDTH, bt->w, IA_HEIGHT, bt->h, IA_CLICK_HOOK, bt_activate, IA_PASS_HOOK, lui_highlight, IA_DATA, bt, IA_ID, bt->id, IA_DELAY, 75, TAG_END); src.x = bt->x; src.y = bt->y; src.w = bt->w; src.h = bt->h; dst.x = 0; dst.y = 0; SDL_BlitSurface(bt->screen, &src, bt->store, &dst); if(!bt->ia) { bt_free(bt); return NULL; } bt->destroy = bt_free; bt->activate = bt_activate; bt->draw = bt_draw; bt->highlight = bt_highlight; return bt; }
int main() { struct bt_node *root; int i, n; root = bt_node_new(651); #if 0 n = 20; /* for(i = n; i >= 1; --i) { root = bt_insert(root, i, i+1); bt_dump(root); } printf("----------------\n"); root = bt_node_new(5); */ for(i = 1; i <= n; ++i) { root = bt_insert(root, i, i+1); } bt_dump(root); printf("----------------\n"); bt_save(root, "/tmp/large.bin"); root = bt_load("/tmp/large.bin"); bt_dump(root); return 0; #endif n = 1000*1000; //n = 20; #if 1 struct timespec t0, t1, t2, t3, t4; clock_gettime(CLOCK_MONOTONIC, &t0); //for(i = n; i >= 1; i--) { for(i = 1; i < n; i++) { struct bt_entry *e; char *key = calloc(20, 1); sprintf(key, "key-%d", i); root = bt_insert(root, key, strlen(key), i+1, 0); continue; e = bt_lookup(root, key, strlen(key)); /* immediate read-back */ if(e && e->value_offset != (uint32_t)i+1) { printf("at k=[%d]: %d\n", i, e->value_offset); } } clock_gettime(CLOCK_MONOTONIC, &t1); printf("saving index of %d elements.\n", n); bt_save(root, "/tmp/large.bin"); clock_gettime(CLOCK_MONOTONIC, &t2); // bt_dump(root); bt_free(root); printf("saved.\n"); uint32_t offset, size; char *key = malloc(40); sprintf(key, "key-%d", 1 + (rand() % (n-1))); if(0 == bt_find("/tmp/large.bin", key, strlen(key), &offset, &size)) { printf("%s: offset=%d, size=%d\n", key, (int)offset, (int)size); } else { printf("could not find %s\n", key); } return 0; // root = bt_load("/tmp/large.bin"); // clock_gettime(CLOCK_MONOTONIC, &t3); // printf("loaded.\n"); bt_dump(root); for(i = 1; i < n; i++) { char *key = calloc(20, 1); sprintf(key, "key-%d", i); struct bt_entry *e = bt_lookup(root, key, strlen(key)); /* read-back after the whole insertion */ if(!e) { printf("e=nil.\n"); } else if(e->value_offset != (uint32_t)i+1) { printf("at k=[%d]: %d\n", i, e->value_offset); } } clock_gettime(CLOCK_MONOTONIC, &t4); printf("checked.\n"); float mili0 = t0.tv_sec * 1000 + t0.tv_nsec / 1000000; float mili1 = t1.tv_sec * 1000 + t1.tv_nsec / 1000000; float mili2 = t2.tv_sec * 1000 + t2.tv_nsec / 1000000; float mili3 = t3.tv_sec * 1000 + t3.tv_nsec / 1000000; float mili4 = t4.tv_sec * 1000 + t4.tv_nsec / 1000000; printf("build time: %0.2f sec.\n", (mili1 - mili0)/1000.0); printf("dump time: %0.2f sec.\n", (mili2 - mili1)/1000.0); printf("reload time: %0.2f sec.\n", (mili3 - mili2)/1000.0); printf("check time: %0.2f sec.\n", (mili4 - mili3)/1000.0); /* root = bt_node_new(5); bt_insert(root, 706); bt_insert(root, 176); bt_insert(root, 601); bt_insert(root, 153); bt_insert(root, 513); bt_insert(root, 773); */ #endif return EXIT_SUCCESS; }