static inline void load_config(void) { /* FIXME */ if ((cfg = config_load("/etc/xcb/pd.conf"))) { char *cat = category_browse(cfg, NULL); while (cat) { if (!strcasecmp(cat, "pair")) { struct variable *var = variable_browse(cfg, cat); struct cpl *cpl = NULL; while (var) { if (!strcasecmp(var->name, "contract1")) { if (!strcasecmp(var->value, "")) break; if (cpl == NULL) { if (NEW(cpl) == NULL) break; cpl->contract2 = NULL; cpl->price1 = cpl->price2 = cpl->prevpd = -1.0; pthread_spin_init(&cpl->lock, 0); } cpl->contract1 = var->value; } else if (!strcasecmp(var->name, "contract2")) { if (!strcasecmp(var->value, "")) break; if (cpl == NULL) { if (NEW(cpl) == NULL) break; cpl->contract1 = NULL; cpl->price1 = cpl->price2 = cpl->prevpd = -1.0; pthread_spin_init(&cpl->lock, 0); } cpl->contract2 = var->value; } else xcb_log(XCB_LOG_WARNING, "Unknown variable '%s' in " "category '%s' of pd.conf", var->name, cat); var = var->next; } if (cpl && cpl->contract1 && cpl->contract2) { dlist_t dlist; if ((dlist = table_get_value(contracts, cpl->contract1)) == NULL) { dlist = dlist_new(NULL, NULL); table_insert(contracts, cpl->contract1, dlist); } dlist_insert_tail(dlist, cpl); if ((dlist = table_get_value(contracts, cpl->contract2)) == NULL) { dlist = dlist_new(NULL, NULL); table_insert(contracts, cpl->contract2, dlist); } dlist_insert_tail(dlist, cpl); dlist_insert_tail(pairs, cpl); } else if (cpl) FREE(cpl); } cat = category_browse(cfg, cat); } } }
/* FIXME */ void sall_command(client c) { dstr res = get_indices(); dstr *fields = NULL; int nfield = 0, i; RTRIM(res); fields = dstr_split_len(res, dstr_length(res), ",", 1, &nfield); for (i = 1; i < nfield; ++i) { dstr pkey = dstr_new(fields[i]); dstr skey = dstr_new(pkey); dlist_t dlist; struct kvd *kvd; table_rwlock_wrlock(subscribers); if ((dlist = table_get_value(subscribers, pkey)) == NULL) { if (NEW(kvd)) { kvd->key = skey; kvd->u.dlist = dlist_new(NULL, NULL); dlist_insert_tail(kvd->u.dlist, c); dlist = dlist_new(cmpkvd, kdfree); dlist_insert_sort(dlist, kvd); table_insert(subscribers, pkey, dlist); } else { add_reply_error(c, "error allocating memory for kvd\r\n"); dstr_free(skey); dstr_free(pkey); } } else { if (NEW(kvd)) { dlist_node_t node; kvd->key = skey; kvd->u.dlist = dlist_new(NULL, NULL); if ((node = dlist_find(dlist, kvd)) == NULL) { dlist_insert_tail(kvd->u.dlist, c); dlist_insert_sort(dlist, kvd); } else { kdfree(kvd); kvd = (struct kvd *)dlist_node_value(node); if (dlist_find(kvd->u.dlist, c) == NULL) dlist_insert_tail(kvd->u.dlist, c); } } else { add_reply_error(c, "error allocating memory for kvd\r\n"); dstr_free(skey); } dstr_free(pkey); } table_rwlock_unlock(subscribers); } dstr_free(res); //-------------------------------------------------------------------------------------------------------------------- // FIXME //-------------------------------------------------------------------------------------------------------------------- }
/** * Initialize client state. */ int init_client_state() { client.host = NULL; client.client = NULL; client.state = SVCECLIENT_STATE_DISCONNECTED; client.input_buffer_size = 1024*1024; /* or 1<<20? */ client.input_buffer = (char*)malloc(client.input_buffer_size); client.input_queue = dlist_new(); client.input_error = 0; mutex_create(&client.input_lock); thread_create(&client.input_thread, input_thread, NULL); mutex_create(&client.network_lock); client.server_info = NULL; client.peers = NULL; client.peers_size = 0; client.my_id = -1; client.server_host_name = dnew(); client.server_host_addr = dnew(); client.server_host_port = 0; return 0; }
dstrlist* dsplit_on_cs(const dstring* text, const char* on, size_t max) { dstrlist* result = dlist_new(); int slen = strlen(on); unsigned int i, j, k = 0; if (max == 1) { dlist_add(result, text); return result; } else if (max <= 0) { max = text->len; } j = 0; i = dposcs(text, on, 0); while(i != -1) { dlist_push(result, dsub(text, j, i - j)); k++; j = i + slen; if (k + 1 == max) { dlist_push(result, dsub(text, j, text->len - j)); return result; } i = dposcs(text, on, j); } dlist_push(result, dsub(text, j, text->len - j)); return result; }
int main(void) { char *line1; char *line2; t_dlist *list; ft_putstr("\n WELCOME ON MINISHELL2 BY PBOURDON \n"); list = NULL; list = dlist_new(list); list = ft_cpy_env(list); if (ft_list_size(list->p_head) == 0) list = ft_cpy_env2(list); ft_putstr("$>"); while (get_next_line(1, &line1, 0) == 1) { line2 = ft_delete_tab(line1); list = ft_choose(line2, list); free(line2); free(line1); if (list == NULL) return (0); ft_show_prompt(); } return (0); }
static int load_module(void) { contracts = table_new(cmpstr, hashmurmur2, NULL, lfree); pairs = dlist_new(NULL, vfree); load_config(); if (msgs_hook(&default_msgs, pd_exec, NULL) == -1) return MODULE_LOAD_FAILURE; return register_application(app, pd_exec, desc, fmt, mod_info->self); }
real_t convexhull_compute_min_perimeter_rectangle(polygon_t *minrect, polygon_t *chull) { real_t perimeter, temp; dlist_t *circumrects; polygon_t *rect; point_t *p, *q, *r; dlink_t *a, *b; assert(minrect); // assert(minrect->count == 4); assert(chull); assert(chull->count >= 3); circumrects = dlist_new(); convexhull_create_circumference_rectangles(circumrects, chull); b = NULL; for (a = circumrects->tail->next; a != circumrects->head; a = a->next) { rect = (polygon_t *)a->object; p = polygon_glimpse(0, rect); q = polygon_glimpse(1, rect); r = polygon_glimpse(2, rect); temp = 2 * (get_distance_of_p2p(p, q) + get_distance_of_p2p(q, r)); if ((b == NULL) || (temp < perimeter)) { b = a; perimeter = temp; } } /* dlink_cutoff(b); circumrects->count--; minrect = (polygon_t *)b->object; minrect->reference--; dlink_destroy(b); */ /* polygon_insert(point_new(), minrect); polygon_insert(point_new(), minrect); polygon_insert(point_new(), minrect); polygon_insert(point_new(), minrect); polygon_copy(minrect, (polygon_t *)b->object); */ polygon_insert(polygon_glimpse(0, (polygon_t *)b->object), minrect); polygon_insert(polygon_glimpse(1, (polygon_t *)b->object), minrect); polygon_insert(polygon_glimpse(2, (polygon_t *)b->object), minrect); polygon_insert(polygon_glimpse(3, (polygon_t *)b->object), minrect); convexhull_delete_circumference_rectangles(circumrects); dlist_destroy(circumrects); return perimeter; }
dlist_t* dlist_append(dlist_t *list, const void *data) { dlist_t *p; p = dlist_new(data); if (! list) { return p; } p->prev = list->prev, list->prev->next = p; p->next = list, list->prev = p; return list; }
static struct usbip_imported_device *usbip_imported_device_new(struct usbip_vhci_driver *driver, char *busid) { struct usbip_imported_device *idev = NULL; struct sysfs_device *sudev; idev = (struct usbip_imported_device *) calloc(1, sizeof(*idev)); if(!idev) return NULL; /* * The members of idev->cdev_list are also linked from * driver->cdev_list. They are freed by destroy of driver->cdev_list. * */ idev->cdev_list = dlist_new(sizeof(struct class_device)); if(!idev->cdev_list) goto err; sudev = sysfs_open_device("usb", busid); if(!sudev) { err("sysfs_open_device %s", busid); goto err; } read_usb_device(sudev, &idev->udev); sysfs_close_device(sudev); /* add class devices of this imported device */ struct class_device *cdev; dlist_for_each_data(driver->cdev_list, cdev, struct class_device) { if(!strncmp(cdev->devpath, idev->udev.path, strlen(idev->udev.path))) { struct class_device *new_cdev; new_cdev = calloc(1, sizeof(*new_cdev)); if(!new_cdev) goto err; memcpy(new_cdev, cdev, sizeof(*new_cdev)); dlist_unshift(idev->cdev_list, (void*) new_cdev); } } return idev; err: if(idev->cdev_list) dlist_destroy(idev->cdev_list); if(idev) free(idev); return NULL; }
mbox_mail_t * mail_new (mbox_t *mbox) { mbox_mail_t *ret; assert(ret = malloc(sizeof(mbox_mail_t))); ret->trace = dlist_new(); /* NOTE: the fields works directly with addresses as key thanks to the * parse_t::keys hash table. */ ret->fields = dhash_new(MAIL_HASH_SIZE, NULL, NULL); ret->body = dstrbuf_new("\n", 1); ret->body_str = NULL; ret->mbox = mbox; return ret; }
t_dlist *get_list(char **tetriminos) { t_dlist *list; char **tetri; t_point **point; int i; i = 0; list = dlist_new(); while (tetriminos[i]) { tetri = NULL; tetri = move_upleft(get_tab(tetriminos[i])); point = get_point(tetri); list = dlist_append(list, tetri, point); i++; } return (list); }
int main() { dlist *list; list = dlist_new(); dlist_append(list, strdup("one")); dlist_append(list, strdup("two")); dlist_foreach(list, print_string_cb, NULL); dlist_foreach(list, string_to_upper_cb, NULL); dlist_foreach(list, print_string_cb, NULL); dlist_foreach(list, free_string_cb, NULL); dlist_free(list); return 0; }
static int quickhull(polygon_t *chull, point_list_t *points) { real_t ymin, ymax, yval; point_t *p, *v1, *v2, *v3; dlink_t *ax, *bx, *x, *y, *next; dlist_t *right_group, *left_group; assert(chull); assert(points); // Allocate the structure element of convex hull for (x = points->tail->next; x != points->head; x = x->next) { p = (point_t *)x->object; point_inc_ref(p); y = dlink_new(); y->object = (void *)p; dlist_insert(y, chull); } // find the extreme points along y-axis ax = NULL; bx = NULL; for (x = chull->tail->next; x != chull->head; x = x->next) { yval = point_get_y((point_t *)(x->object)); if (ax == NULL || yval < ymin) { ax = x; ymin = yval; } if (bx == NULL || yval > ymax) { bx = x; ymax = yval; } } dlink_cutoff(ax); dlist_dec_count(chull); dlink_cutoff(bx); dlist_dec_count(chull); //point_dump((point_t *)ax->object); //point_dump((point_t *)bx->object); v1 = point_new(); v2 = point_new(); v3 = point_new(); //printf("for right section\n"); right_group= dlist_new(); dlist_insert(ax, right_group); point_subtract(v2, (point_t *)bx->object, (point_t *)ax->object); for (x = chull->tail->next; x != chull->head;) { //point_dump((point_t *)x->object); point_subtract(v1, (point_t *)x->object, (point_t *)ax->object); point_xproduct(v3, v1, v2); if (point_get_z(v3) > 0) { next = x->next; dlink_cutoff(x); dlist_dec_count(chull); dlist_insert(x, right_group); //printf(" "); //point_dump((point_t *)x->object); x = next; } else x = x->next; } dlist_insert(bx, right_group); quickhull_grouping(right_group); //printf("for left section\n"); ax = dlist_pop(right_group); bx = dlist_extract(right_group); //point_dump((point_t *)ax->object); //point_dump((point_t *)bx->object); left_group = dlist_new(); dlist_insert(bx, left_group); point_subtract(v2, (point_t *)ax->object, (point_t *)bx->object); for (x = chull->tail->next; x != chull->head; ) { point_subtract(v1, (point_t *)x->object, (point_t *)bx->object); point_xproduct(v3, v1, v2); if (point_get_z(v3) > 0) { next = x->next; dlink_cutoff(x); dlist_dec_count(chull); dlist_insert(x, left_group); //point_dump((point_t *)x->object); x = next; } else { next = x->next; dlink_cutoff(x); dlist_dec_count(chull); point_destroy((point_t *)x->object); dlink_destroy(x); x = next; } } dlist_insert(ax, left_group); quickhull_grouping(left_group); ax = dlist_extract(left_group); bx = dlist_pop(left_group); dlist_insert(ax, chull); while (dlist_get_count(right_group) > 0) { x = dlist_pop(right_group); dlist_insert(x, chull); } dlist_insert(bx, chull); while (dlist_get_count(left_group) > 0) { x = dlist_pop(left_group); dlist_insert(x, chull); } dlist_destroy(left_group); dlist_destroy(right_group); point_destroy(v3); point_destroy(v2); point_destroy(v1); return dlist_get_count(chull); }
#include "helpers.h" TEST(purge_does_nothing_on_empty_list) { USING(dlist_new(sizeof(int))) { assertEquals(dlist_purge(list), list); assertEquals(dlist_length(list), 0); assertEquals(dlist_size(list), sizeof(int)); } } TEST(purge_removes_all_elements_of_list) { USING(dlist_new(sizeof(int))) { assertNotEquals(dlist_append(list, NULL), NULL); assertEquals(dlist_length(list), 1); assertEquals(dlist_purge(list), list); assertEquals(dlist_length(list), 0); assertEquals(dlist_size(list), sizeof(int)); } USING(dlist_new(sizeof(int))) { assertNotEquals(dlist_append(list, NULL), NULL); assertNotEquals(dlist_append(list, NULL), NULL); assertEquals(dlist_length(list), 2); assertEquals(dlist_purge(list), list); assertEquals(dlist_length(list), 0); assertEquals(dlist_size(list), sizeof(int)); } USING(dlist_new(sizeof(int))) { assertNotEquals(dlist_append(list, NULL), NULL);
dstrlist* dstrlex_parse(const dstring* text, int* errorcode) { int in_escape = 0; int in_string = 0; dstring* buffer = dnew(); dstrlist* tokens = dlist_new(); dstrlen_t i, len; int ch; if (errorcode) { *errorcode = DSTRLEX_OK; } len = text->len; for(i = 0; i < len; i++) { ch = text->data[i]; if (in_escape) { switch (ch) { case 'n': dcatc(buffer, '\n'); break; case 'r': dcatc(buffer, '\r'); break; case '0': dcatc(buffer, '\0'); break; case '"': dcatc(buffer, '"'); break; case 't': dcatc(buffer, '\t'); break; case '\\': dcatc(buffer, '\\'); break; default: dcatc(buffer, ch); break; } in_escape = 0; } else if (in_string) { if (ch == '"') { dlist_add(tokens, buffer); dclear(buffer); in_string = 0; } else if (ch == '\\') { in_escape = 1; } else { dcatc(buffer, ch); } } else { if (ch == '"') { if (buffer->len) { dlist_add(tokens, buffer); dclear(buffer); } in_string = 1; } else if (ch == '\\') { in_escape = 1; } else if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r') { if (buffer->len) { dlist_add(tokens, buffer); dclear(buffer); } } else { dcatc(buffer, ch); } } } if (in_escape) { if (errorcode) *errorcode = DSTRLEX_ESCAPE; dfree(buffer); dlist_free(tokens); return NULL; } else if (in_string) { if (errorcode) *errorcode = DSTRLEX_STRING; dfree(buffer); dlist_free(tokens); return NULL; } else if (buffer->len) { dlist_add(tokens, buffer); } dfree(buffer); return tokens; }
static int vxo_exec(void *data, void *data2) { RAII_VAR(struct msg *, msg, (struct msg *)data, msg_decr); struct msgs *out = (struct msgs *)data2; dstr *fields = NULL; int nfield = 0; double vol, vol2, vol3, spot, strike, r, expiry; int sec, msec; dstr spotname; char *type; struct pd *pd; struct scp *scp; fields = dstr_split_len(msg->data, strlen(msg->data), ",", 1, &nfield); /* FIXME */ if (nfield != 19) { xcb_log(XCB_LOG_WARNING, "Message '%s' garbled", msg->data); goto end; } vol = !strcasecmp(fields[5], "nan") ? NAN : atof(fields[5]); vol2 = !strcasecmp(fields[7], "nan") ? NAN : atof(fields[7]); vol3 = !strcasecmp(fields[9], "nan") ? NAN : atof(fields[9]); if (isnan(vol) && isnan(vol2) && isnan(vol3)) goto end; sec = atoi(fields[1]); msec = atoi(fields[2]); spot = atof(fields[10]); spotname = dstr_new(fields[11]); type = fields[12]; strike = atof(fields[13]); r = atof(fields[14]); expiry = atof(fields[15]); table_lock(spots); if ((pd = table_get_value(spots, spotname)) == NULL) { /* can't happen */ if (NEW(scp) == NULL) { xcb_log(XCB_LOG_WARNING, "Error allocating memory for scp"); table_unlock(spots); goto end; } scp->strike = strike; if (!strcasecmp(type, "C")) { scp->cvol = vol; scp->pvol = NAN; scp->cvol2 = vol2; scp->pvol2 = NAN; scp->cvol3 = vol3; scp->pvol3 = NAN; } else { scp->cvol = NAN; scp->pvol = vol; scp->cvol2 = NAN; scp->pvol2 = vol2; scp->cvol3 = NAN; scp->pvol3 = vol3; } if (NEW(pd) == NULL) { xcb_log(XCB_LOG_WARNING, "Error allocating memory for pd"); FREE(scp); table_unlock(spots); goto end; } pd->prevxo = pd->prevxo2 = pd->prevxo3 = NAN; pd->sep = strchr(fields[3], '-') ? "-" : ""; pd->dlist = dlist_new(NULL, scpfree); dlist_insert_tail(pd->dlist, scp); table_insert(spots, spotname, pd); } else { dlist_iter_t iter = dlist_iter_new(pd->dlist, DLIST_START_HEAD); dlist_node_t node; while ((node = dlist_next(iter))) { scp = (struct scp *)dlist_node_value(node); if (scp->strike > strike || fabs(scp->strike - strike) <= 0.000001) break; } if (node && fabs(scp->strike - strike) <= 0.000001) { if (!strcasecmp(type, "C")) { scp->cvol = vol; scp->cvol2 = vol2; scp->cvol3 = vol3; } else { scp->pvol = vol; scp->pvol2 = vol2; scp->pvol3 = vol3; } } else { /* can't happen */ if (NEW(scp) == NULL) { xcb_log(XCB_LOG_WARNING, "Error allocating memory for scp"); table_unlock(spots); goto end; } scp->strike = strike; if (!strcasecmp(type, "C")) { scp->cvol = vol; scp->pvol = NAN; scp->cvol2 = vol2; scp->pvol2 = NAN; scp->cvol3 = vol3; scp->pvol3 = NAN; } else { scp->cvol = NAN; scp->pvol = vol; scp->cvol2 = NAN; scp->pvol2 = vol2; scp->cvol3 = NAN; scp->pvol3 = vol3; } if (node == NULL) dlist_insert_tail(pd->dlist, scp); else dlist_insert(pd->dlist, node, scp, 0); } dlist_iter_rewind_head(iter, pd->dlist); while ((node = dlist_next(iter))) { scp = (struct scp *)dlist_node_value(node); if (scp->strike > spot || fabs(scp->strike - spot) <= 0.000001) break; } dlist_iter_free(&iter); if (node && dlist_node_prev(node)) { struct scp *scp1 = (struct scp *)dlist_node_value(node); struct scp *scp2 = (struct scp *)dlist_node_value(dlist_node_prev(node)); double vxo = NAN, vxo2 = NAN, vxo3 = NAN; int flag1, flag2, flag3; if (!isnan(scp1->cvol) && !isnan(scp1->pvol) && !isnan(scp2->cvol) && !isnan(scp2->pvol)) vxo = ((scp1->cvol + scp1->pvol) / 2) * (spot - scp2->strike) / (scp1->strike - scp2->strike) + ((scp2->cvol + scp2->pvol) / 2) * (scp1->strike - spot) / (scp1->strike - scp2->strike); if (!isnan(scp1->cvol2) && !isnan(scp1->pvol2) && !isnan(scp2->cvol2) && !isnan(scp2->pvol2)) vxo2 = ((scp1->cvol2 + scp1->pvol2) / 2) * (spot - scp2->strike) / (scp1->strike - scp2->strike) + ((scp2->cvol2 + scp2->pvol2) / 2) * (scp1->strike - spot) / (scp1->strike - scp2->strike); if (!isnan(scp1->cvol3) && !isnan(scp1->pvol3) && !isnan(scp2->cvol3) && !isnan(scp2->pvol3)) vxo3 = ((scp1->cvol3 + scp1->pvol3) / 2) * (spot - scp2->strike) / (scp1->strike - scp2->strike) + ((scp2->cvol3 + scp2->pvol3) / 2) * (scp1->strike - spot) / (scp1->strike - scp2->strike); if ((flag1 = (isnan(pd->prevxo) && !isnan(vxo)) || (!isnan(pd->prevxo) && isnan(vxo)) || (!isnan(pd->prevxo) && !isnan(vxo) && fabs(pd->prevxo - vxo) > 0.000001) ? 1 : 0)) pd->prevxo = vxo; if ((flag2 = (isnan(pd->prevxo2) && !isnan(vxo2)) || (!isnan(pd->prevxo2) && isnan(vxo2)) || (!isnan(pd->prevxo2) && !isnan(vxo2) && fabs(pd->prevxo2 - vxo2) > 0.000001) ? 1 : 0)) pd->prevxo2 = vxo2; if ((flag3 = (isnan(pd->prevxo3) && !isnan(vxo3)) || (!isnan(pd->prevxo3) && isnan(vxo3)) || (!isnan(pd->prevxo3) && !isnan(vxo3) && fabs(pd->prevxo3 - vxo3) > 0.000001) ? 1 : 0)) pd->prevxo3 = vxo3; if (flag1 || flag2 || flag3) { char *res; if ((res = ALLOC(4096))) { time_t t = (time_t)sec; struct tm lt; char datestr[64]; int off; strftime(datestr, sizeof datestr, "%F %T", localtime_r(&t, <)); snprintf(res, 4096, "VXO,%s.%03d,%s|%f,%f,%f", datestr, msec, spotname, vxo, vxo2, vxo3); out2rmp(res); off = snprintf(res, 4096, "VXO,%d,%d,%s,", sec, msec, spotname); iter = dlist_iter_new(pd->dlist, DLIST_START_HEAD); while ((node = dlist_next(iter))) { scp = (struct scp *)dlist_node_value(node); off += snprintf(res + off, 4096 - off, "%.f,%f,%f,%f,", scp->strike, vxo, vxo2, vxo3); } dlist_iter_free(&iter); snprintf(res + off, 4096 - off, "%.2f,%f,%f,%s", spot, r, expiry, pd->sep); if (out2msgs(res, out) == -1) FREE(res); } } } dstr_free(spotname); } table_unlock(spots); end: dstr_free_tokens(fields, nfield); return 0; }
int main(int argc, char **argv) { int ncmds, i; const char *tmp; pgm_error_t *pgm_err = NULL; /* FIXME */ signal(SIGPIPE, SIG_IGN); setup_signal_handlers(); cmds = table_new(cmpstr, hashmurmur2, NULL, NULL); ncmds = sizeof commands / sizeof (struct cmd); for (i = 0; i < ncmds; ++i) { struct cmd *cmd = commands + i; table_insert(cmds, cmd->name, cmd); } if (argc != 2 && argc != 3) usage(); else if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) usage(); else if (argc == 3 && strcmp(argv[1], "-f")) usage(); if (argc == 2 && daemon(1, 0) == -1) fprintf(stderr, "Error daemonizing: %s\n", strerror(errno)); /* FIXME */ if (init_logger("/var/log/xcb/xcb-dp2.log", __LOG_DEBUG) == -1) { fprintf(stderr, "Error initializing logger\n"); exit(1); } cfg_path = argc == 2 ? argv[1] : argv[2]; if ((cfg = config_load(cfg_path)) == NULL) exit(1); if ((tmp = variable_retrieve(cfg, "general", "log_level"))) { if (!strcasecmp(tmp, "info")) set_logger_level(__LOG_INFO); else if (!strcasecmp(tmp, "notice")) set_logger_level(__LOG_NOTICE); else if (!strcasecmp(tmp, "warning")) set_logger_level(__LOG_WARNING); } /* FIXME */ if (addms) times = table_new(cmpstr, hashmurmur2, kfree, vfree); clients_to_close = dlist_new(NULL, NULL); clients = dlist_new(NULL, NULL); monitors = dlist_new(NULL, NULL); tp = thrpool_new(16, 512, 200, NULL); if (!pgm_init(&pgm_err)) { xcb_log(XCB_LOG_ERROR, "Error starting PGM engine: %s", pgm_err->message); pgm_error_free(pgm_err); goto err; } /* FIXME */ if (NEW(pgm_send_cfg) == NULL) { xcb_log(XCB_LOG_ERROR, "Error allocating memory for PGM cfg"); goto err; } pgm_send_cfg->network = NULL; pgm_send_cfg->port = 0; init_pgm_send_cfg(pgm_send_cfg); if (pgm_send_cfg->network == NULL) { xcb_log(XCB_LOG_ERROR, "PGM network can't be NULL"); goto err; } if (pgm_send_cfg->port == 0) { xcb_log(XCB_LOG_ERROR, "PGM port can't be zero"); goto err; } if ((pgm_sender = pgmsock_create(pgm_send_cfg->network, pgm_send_cfg->port, PGMSOCK_SENDER)) == NULL) goto err; /* FIXME */ if ((el = create_event_loop(1024 + 1000)) == NULL) { xcb_log(XCB_LOG_ERROR, "Error creating event loop"); goto err; } create_time_event(el, 1, server_cron, NULL, NULL); if ((tmp = variable_retrieve(cfg, "general", "udp_port")) && strcmp(tmp, "")) { if ((udpsock = net_udp_server(NULL, atoi(tmp), neterr, sizeof neterr)) == -1) { xcb_log(XCB_LOG_ERROR, "Opening port '%s': %s", tmp, neterr); goto err; } if (net_nonblock(udpsock, neterr, sizeof neterr) == -1) { xcb_log(XCB_LOG_ERROR, "Setting port '%s' nonblocking: %s", tmp, neterr); goto err; } } if ((tmp = variable_retrieve(cfg, "general", "tcp_port")) && strcmp(tmp, "")) if ((tcpsock = net_tcp_server(NULL, atoi(tmp), neterr, sizeof neterr)) == -1) { xcb_log(XCB_LOG_ERROR, "Opening port '%s': %s", tmp, neterr); goto err; } if (udpsock > 0 && create_file_event(el, udpsock, EVENT_READABLE, read_quote, NULL) == -1) { xcb_log(XCB_LOG_ERROR, "Unrecoverable error creating udpsock '%d' file event", udpsock); goto err; } if (tcpsock > 0 && create_file_event(el, tcpsock, EVENT_READABLE, tcp_accept_handler, NULL) == -1) { xcb_log(XCB_LOG_ERROR, "Unrecoverable error creating tcpsock '%d' file event", tcpsock); goto err; } xcb_log(XCB_LOG_NOTICE, "Server dispatcher started"); start_event_loop(el, ALL_EVENTS); delete_event_loop(el); pgm_shutdown(); return 0; err: close_logger(); exit(1); }
static inline void load_config(void) { /* FIXME */ if ((cfg = config_load("/etc/xcb/vxo.conf"))) { char *cat = category_browse(cfg, NULL); struct variable *var; while (cat) { if (!strcasecmp(cat, "general")) { struct variable *var = variable_browse(cfg, cat); while (var) { if (!strcasecmp(var->name, "inmsg")) { if (strcmp(var->value, "")) inmsg = var->value; } else xcb_log(XCB_LOG_WARNING, "Unknown variable '%s' in " "category '%s' of vxo.conf", var->name, cat); var = var->next; } } else if (!strcasecmp(cat, "expiries")) { var = variable_browse(cfg, cat); while (var) { char *p; if ((p = strrchr(var->name, 'C')) == NULL) p = strrchr(var->name, 'P'); if (p && p != var->name && p != var->name + strlen(var->name) - 1 && ((*(p - 1) == '-' && *(p + 1) == '-') || (isdigit(*(p - 1)) && isdigit(*(p + 1))))) { dstr spotname, strike; struct pd *pd; struct scp *scp; spotname = *(p - 1) == '-' ? dstr_new_len(var->name, p - var->name - 1) : dstr_new_len(var->name, p - var->name); strike = *(p + 1) == '-' ? dstr_new_len(p + 2, var->name + strlen(var->name) - p - 2) : dstr_new_len(p + 1, var->name + strlen(var->name) - p - 1); if ((pd = table_get_value(spots, spotname)) == NULL) { if (NEW(scp)) { scp->strike = atof(strike); scp->cvol = scp->pvol = NAN; scp->cvol2 = scp->pvol2 = NAN; scp->cvol3 = scp->pvol3 = NAN; if (NEW(pd)) { pd->prevxo = pd->prevxo2 = pd->prevxo3 = NAN; pd->sep = *(p - 1) == '-' ? "-" : ""; pd->dlist = dlist_new(NULL, scpfree); dlist_insert_tail(pd->dlist, scp); table_insert(spots, spotname, pd); } else FREE(scp); } else dstr_free(spotname); } else { dlist_iter_t iter = dlist_iter_new(pd->dlist, DLIST_START_HEAD); dlist_node_t node; while ((node = dlist_next(iter))) { scp = (struct scp *)dlist_node_value(node); if (scp->strike > atof(strike) || fabs(scp->strike - atof(strike)) <= 0.000001) break; } dlist_iter_free(&iter); if (node == NULL || scp->strike > atof(strike)) { if (NEW(scp)) { scp->strike = atof(strike); scp->cvol = scp->pvol = NAN; scp->cvol2 = scp->pvol2 = NAN; scp->cvol3 = scp->pvol3 = NAN; } if (node == NULL) dlist_insert_tail(pd->dlist, scp); else dlist_insert(pd->dlist, node, scp, 0); } dstr_free(spotname); } dstr_free(strike); } var = var->next; } } cat = category_browse(cfg, cat); } } }
/* * Quick-Hull * Here's an algorithm that deserves its name. * It's a fast way to compute the convex hull of a set of points on the plane. * It shares a few similarities with its namesake, quick-sort: * - it is recursive. * - each recursive step partitions data into several groups. * * The partitioning step does all the work. The basic idea is as follows: * 1. We are given a some points, * and line segment AB which we know is a chord of the convex hull. * 2. Among the given points, find the one which is farthest from AB. * Let's call this point C. * 3. The points inside the triangle ABC cannot be on the hull. * Put them in set s0. * 4. Put the points which lie outside edge AC in set s1, * and points outside edge BC in set s2. * * Once the partitioning is done, we recursively invoke quick-hull on sets s1 and s2. * The algorithm works fast on random sets of points * because step 3 of the partition typically discards a large fraction of the points. */ static int quickhull_grouping(dlist_t *group) { dlink_t *x, *ax, *bx, *cx, *next; dlist_t *s1, *s2; point_t *v1, *v2, *v3; real_t area; assert(group); if (dlist_get_count(group) <= 2) return dlist_get_count(group); v1 = point_new(); v2 = point_new(); v3 = point_new(); // Find the point with maximum parallelogram's area ax = dlist_pop(group); bx = dlist_extract(group); cx = NULL; point_subtract(v2, (point_t *)(bx->object), (point_t *)(ax->object)); for (x = group->tail->next; x != group->head; x = x->next) { point_subtract(v1, (point_t *)(x->object), (point_t *)(ax->object)); point_xproduct(v3, v1, v2); if (cx == NULL || point_get_z(v3) > area) { cx = x; area = point_get_z(v3); } } dlink_cutoff(cx); dlist_dec_count(group); // S1 grouping s1 = dlist_new(); dlist_insert(ax, s1); point_subtract(v2, (point_t *)(cx->object), (point_t *)(ax->object)); for (x = group->tail->next; x != group->head; ) { point_subtract(v1, (point_t *)(x->object), (point_t *)(ax->object)); point_xproduct(v3, v1, v2); if (point_get_z(v3) > 0) { next = x->next; dlink_cutoff(x); dlist_dec_count(group); dlist_insert(x, s1); x = next; } else x = x->next; } dlist_insert(cx, s1); quickhull_grouping(s1); assert(cx == s1->head->prev); // S2 grouping and pop out others cx = dlist_extract(s1); s2 = dlist_new(); dlist_insert(cx, s2); point_subtract(v2, (point_t *)bx->object, (point_t *)cx->object); for (x = group->tail->next; x != group->head;) { point_subtract(v1, (point_t *)x->object, (point_t *)cx->object); point_xproduct(v3, v1, v2); if (point_get_z(v3) > 0) { next = x->next; dlink_cutoff(x); dlist_dec_count(group); dlist_insert(x, s2); x = next; } else { next = x->next; dlink_cutoff(x); dlist_dec_count(group); point_destroy((point_t *)x->object); dlink_destroy(x); x = next; } } dlist_insert(bx, s2); quickhull_grouping(s2); assert(bx == s2->head->prev); assert(dlist_get_count(group) == 0); //assert(group->count == 0); // Merge s1 and s2 into group while (dlist_get_count(s1) > 0) { x = dlist_pop(s1); dlist_insert(x, group); } while (dlist_get_count(s2) > 0) { x = dlist_pop(s2); dlist_insert(x, group); } dlist_destroy(s2); dlist_destroy(s1); point_destroy(v3); point_destroy(v2); point_destroy(v1); return dlist_get_count(group); }
static int parse_status(char *value, struct usbip_vhci_driver *driver) { int ret = 0; char *c; for(int i = 0; i < driver->nports; i++) bzero(&driver->idev[i], sizeof(struct usbip_imported_device)); /* skip a header line */ c = strchr(value, '\n') + 1; while(*c != '\0') { int port, status, speed, busnum, devnum; char ipaddr[NI_MAXHOST]; char ipport[NI_MAXSERV]; char rbusid[SYSFS_BUS_ID_SIZE]; char lbusid[SYSFS_BUS_ID_SIZE]; ret = sscanf(c, "%d %d %d %d %d %s %s %s %s\n", &port, &status, &speed, &busnum, &devnum, ipaddr, ipport, rbusid, lbusid); if(ret < 7) { err("scanf %d", ret); BUG(); } dbg("port %d status %d speed %d busnum %d devnum %d", port, status, speed, busnum, devnum); dbg("ipaddr %s ipport %s rbusid %s lbusid %s", ipaddr, ipport, rbusid, lbusid); /* if a device is connected, look at it */ { struct usbip_imported_device *idev = &driver->idev[port]; // /* add device */ // dlist_unshift(driver->idev_list, (void*) idev); idev->port = port; idev->status = status; idev->busnum = busnum; idev->devnum = devnum; idev->cdev_list = dlist_new(sizeof(struct class_device)); if(!idev->cdev_list) { err("init new device"); return -1; } if(idev->status != VDEV_ST_NULL && idev->status != VDEV_ST_NOTASSIGNED) { idev = imported_device_init(driver, idev, lbusid); if(!idev) { err("init new device"); return -1; } strncpy(idev->remote_busid, rbusid, SYSFS_BUS_ID_SIZE); if(!get_sockaddr_s(ipaddr, ipport, &idev->ss)) { err("get sockaddr storage"); continue; } } } /* go to the next line */ c = strchr(c, '\n') + 1; } dbg("exit"); return 0; }
/* FIXME */ void s_command(client c) { dstr pkey, skey; int i; dlist_t dlist; struct kvd *kvd; if (dstr_length(c->argv[0]) == 1) { add_reply_error(c, "index can't be empty\r\n"); return; } pkey = dstr_new(c->argv[0] + 1); skey = dstr_new(pkey); if (c->argc > 1) for (i = 1; i < c->argc; ++i) { skey = dstr_cat(skey, ","); skey = dstr_cat(skey, c->argv[i]); } table_lock(cache); if ((dlist = table_get_value(cache, pkey))) { dlist_iter_t iter = dlist_iter_new(dlist, DLIST_START_HEAD); dlist_node_t node; /* FIXME: still has room to improve */ while ((node = dlist_next(iter))) { kvd = (struct kvd *)dlist_node_value(node); if (dstr_length(kvd->key) >= dstr_length(skey) && !memcmp(kvd->key, skey, dstr_length(skey))) { dstr *fields = NULL; int nfield = 0; dstr res, contracts = NULL; fields = dstr_split_len(kvd->key, dstr_length(kvd->key), ",", 1, &nfield); res = dstr_new(fields[0]); if (nfield > 1) { contracts = dstr_new(fields[1]); for (i = 2; i < nfield; ++i) { contracts = dstr_cat(contracts, ","); contracts = dstr_cat(contracts, fields[i]); } } dstr_free_tokens(fields, nfield); fields = NULL, nfield = 0; fields = dstr_split_len(kvd->u.value, dstr_length(kvd->u.value), ",", 1, &nfield); res = dstr_cat(res, ","); res = dstr_cat(res, fields[0]); if (contracts) { res = dstr_cat(res, ","); res = dstr_cat(res, contracts); } for (i = 1; i < nfield; ++i) { res = dstr_cat(res, ","); res = dstr_cat(res, fields[i]); } res = dstr_cat(res, "\r\n"); pthread_spin_lock(&c->lock); if (!(c->flags & CLIENT_CLOSE_ASAP)) { if (net_try_write(c->fd, res, dstr_length(res), 10, NET_NONBLOCK) == -1) { xcb_log(XCB_LOG_WARNING, "Writing to client '%p': %s", c, strerror(errno)); if (++c->eagcount >= 3) { client_free_async(c); pthread_spin_unlock(&c->lock); dstr_free(contracts); dstr_free(res); dstr_free_tokens(fields, nfield); table_unlock(cache); dstr_free(skey); dstr_free(pkey); return; } } else if (c->eagcount) c->eagcount = 0; } pthread_spin_unlock(&c->lock); dstr_free(contracts); dstr_free(res); dstr_free_tokens(fields, nfield); } } dlist_iter_free(&iter); } table_unlock(cache); table_rwlock_wrlock(subscribers); if ((dlist = table_get_value(subscribers, pkey)) == NULL) { if (NEW(kvd)) { kvd->key = skey; kvd->u.dlist = dlist_new(NULL, NULL); dlist_insert_tail(kvd->u.dlist, c); dlist = dlist_new(cmpkvd, kdfree); dlist_insert_sort(dlist, kvd); table_insert(subscribers, pkey, dlist); } else { add_reply_error(c, "error allocating memory for kvd\r\n"); dstr_free(skey); dstr_free(pkey); } } else { if (NEW(kvd)) { dlist_node_t node; kvd->key = skey; kvd->u.dlist = dlist_new(NULL, NULL); if ((node = dlist_find(dlist, kvd)) == NULL) { dlist_insert_tail(kvd->u.dlist, c); dlist_insert_sort(dlist, kvd); } else { kdfree(kvd); kvd = (struct kvd *)dlist_node_value(node); if (dlist_find(kvd->u.dlist, c) == NULL) dlist_insert_tail(kvd->u.dlist, c); } } else { add_reply_error(c, "error allocating memory for kvd\r\n"); dstr_free(skey); } dstr_free(pkey); } table_rwlock_unlock(subscribers); }