bp_Document * bp_parseDoc(bp_Context * context, xmlDocPtr doc) { bp_Document * result = malloc(sizeof(bp_Document)); xmlNodePtr rootNode; context->doc = (void *) result; context->nLabels = 0; result->root = NULL; for (rootNode = doc->children; rootNode; rootNode = rootNode->next) { switch (ELEM(rootNode)) { case BPE_BOOK: result->root = (bp_Node *) bp_parseBook(context, rootNode); break; default:; } } if (!result->root) { char errmsg[500]; snprintf(errmsg, sizeof(errmsg), "%s: root element must be \"book\"" , doc->name); LOG_ERROR(errmsg); exit(1); } context->labels = st_create(context->nLabels + 1); st_addptr(context->labels, "", NULL); // allow for empty "to" attributes bp_collectLabels(context, result->root); st_commit(context->labels); bp_linkReferences(context, result->root); st_destroy(context->labels); return result; }
void fd_free() { if (fd_fonts) { int i; for (i=0; i < fd_nFonts; i++) { fd_Font * font = fd_fonts[i]; if (font) { fd_fonts[i] = NULL; if (font->chars) free(font->chars); free(font); } } free(fd_fonts); fd_fonts = NULL; } fd_nFonts = 0; if(bp_fonts) { st_destroy(bp_fonts); bp_fonts = NULL; } }
/* the functions frees the allocated memory */ void garbage_collect(void){ if(synerrno) printf("\nSyntax errors: %d\n",synerrno); printf("\nCollecting garbage...\n"); b_destroy(sc_buf); b_destroy(str_LTBL); st_destroy(sym_table); }
void names_free(struct name_list *l) { st_destroy(&l->taken, ST_DONT_FREE_DATA); ptrarray_free(l->prefix); ptrarray_free(l->first); ptrarray_free(l->second); ptrarray_free(l->suffix); }
/** * Free search table, nullifying its pointer. */ void st_free(search_table_t **ptr) { g_assert(ptr); if (*ptr) { search_table_t *table = *ptr; st_destroy(table); table->magic = 0; WFREE(table); *ptr = NULL; } }
void env_destroy_scope(env_t **env) { node_t *cur; cur = (*env)->head->next; st_destroy(&(*env)->head->st); util_free((*env)->head); if (cur) { (*env)->head = cur; } else { util_free(*env); *env = NULL; } }
void httpd(void) { int listenfd; struct sockaddr_in saddr; fd_set readset; fd_set writeset; int i, maxfdp1; http_state_t *hs; listenfd = socket(AF_INET, SOCK_STREAM, 0); if (listenfd < 0) { printf("httpd: socket create failed\n"); return; } memset(&saddr, 0, sizeof(saddr)); saddr.sin_family = AF_INET; saddr.sin_addr.s_addr = htonl(INADDR_ANY); saddr.sin_port = htons(80); if (bind(listenfd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) { printf("httpd: bind failed\n"); close(listenfd); return; } if (listen(listenfd, 10) < 0) { printf("httpd: listen failed\n"); close(listenfd); return; } st_init(); printf("httpd: listening for connections on socket %d\n", listenfd); for ( ; ; ) { maxfdp1 = listenfd + 1; FD_ZERO(&readset); FD_ZERO(&writeset); FD_SET(listenfd, &readset); // maxfdp1 = st_add_fds(&readset, maxfdp1); // st_add_fds(&writeset); i = select(maxfdp1, &readset, &writeset, 0, 0); if (i == 0) continue; // Check for new incoming connections if (FD_ISSET(listenfd, &readset)) { int tmp = 1; hs = st_create(); hs->client_size = sizeof(hs->client); hs->socket = accept(listenfd, (struct sockaddr *)&hs->client, &hs->client_size); printf("httpd: connect from %08lx, port %d, socket %d\n", hs->client.sin_addr.s_addr, hs->client.sin_port, hs->socket); if (hs->socket < 0) { st_destroy(hs); } else { hs->thd = thd_create(client_thread, hs); // hs->thd = sys_thread_new(client_thread, hs, 0); } /* else if (ioctl(hs->socket, FIONBIO, &tmp) < 0) { printf("httpd: failed to set non-blocking\n"); st_destroy(hs); } */ } #if 0 // Process data from connected clients st_foreach(hs) { if (FD_ISSET(hs->socket, &readset)) { if (handle_read(hs) < 0) { printf("httpd: disconnected socket %d\n", hs->socket); close(hs->socket); st_destroy(hs); break; } } /* if (FD_ISSET(hs->socket, &writeset)) { } */ } #endif } }
void client_thread(void *p) { http_state_t * hs = (http_state_t *)p; char * buf, * ext; const char * ct; file_t f = -1; int r, o, cnt; stat_t st; printf("httpd: client thread started, sock %d\n", hs->socket); buf = malloc(BUFSIZE); if (read_headers(hs, buf, BUFSIZE) < 0) { goto out; } printf("httpd: client requested '%s'\n", buf); // Is it a directory or a file? f = fs_open(buf, O_RDONLY | O_DIR); if (f >= 0) { do_dirlist(buf, hs, f); } else { f = fs_open(buf, O_RDONLY); if (f < 0) { send_error(hs, 404, "File not found or unreadable"); goto out; } ext = strrchr(buf, '.'); ct = "application/octet-stream"; if (ext) { ext++; if (!strcasecmp(ext, "jpg")) ct = "image/jpeg"; else if (!strcasecmp(ext, "png")) ct = "image/png"; else if (!strcasecmp(ext, "gif")) ct = "image/gif"; else if (!strcasecmp(ext, "txt")) ct = "text/plain"; else if (!strcasecmp(ext, "mp3")) ct = "audio/mpeg"; else if (!strcasecmp(ext, "ogg")) ct = "application/ogg"; else if (!strcasecmp(ext, "html")) ct = "text/html"; } send_ok(hs, ct); while ((cnt = fs_read(f, buf, BUFSIZE)) != 0) { o = 0; while (cnt > 0) { r = write(hs->socket, buf+o, cnt); if (r <= 0) goto out; cnt -= r; o += r; } } } fs_close(f); out: free(buf); printf("httpd: closed client connection %d\n", hs->socket); close(hs->socket); st_destroy(hs); if (f >= 0) fs_close(f); }
int main(int argc, char *argv[]) { struct xnet_type_ops ops = { .buf_alloc = __mds_buf_alloc, .buf_free = NULL, .recv_handler = mds_spool_dispatch, .dispatcher = mds_fe_dispatch, }; int err = 0; int self, sport = -1, i, j; int memonly, memlimit, mode, plot_method; char *value; char *ring_ip = NULL; char profiling_fname[256]; hvfs_info(xnet, "MDS Unit Testing...\n"); hvfs_info(xnet, "Mode is 0/1 (no ring/with ring)\n"); if (argc < 2) { hvfs_err(xnet, "Self ID is not provided.\n"); err = EINVAL; return err; } else { self = atoi(argv[1]); hvfs_info(xnet, "Self type+ID is mds:%d.\n", self); if (argc == 4) { ring_ip = argv[2]; sport = atoi(argv[3]); } else if (argc == 3) ring_ip = argv[2]; } value = getenv("memonly"); if (value) { memonly = atoi(value); } else memonly = 1; value = getenv("memlimit"); if (value) { memlimit = atoi(value); } else memlimit = 0; value = getenv("mode"); if (value) { mode = atoi(value); } else mode = 0; value = getenv("fsid"); if (value) { fsid = atoi(value); } else fsid = 0; value = getenv("plot"); if (value) { plot_method = atoi(value); } else plot_method = MDS_PROF_PLOT; st_init(); mds_pre_init(); hmo.prof.xnet = &g_xnet_prof; hmo.conf.itbid_check = 1; hmo.conf.prof_plot = plot_method; hmo.conf.option |= HVFS_MDS_NOSCRUB; hmo.cb_branch_init = mds_cb_branch_init; hmo.cb_branch_destroy = mds_cb_branch_destroy; mds_init(11); /* set the uuid base! */ hmi.uuid_base = (u64)self << 45; for (i = 0; i < 4; i++) { for (j = 0; j < 4; j++) { xnet_update_ipaddr(HVFS_TYPE(i, j), 1, &ipaddr[i], (short *)(&port[i][j])); } } xnet_update_ipaddr(HVFS_CLIENT(12), 1, &ipaddr[4], (short *)(&port[4][0])); xnet_update_ipaddr(HVFS_MDS(4), 1, &ipaddr[0], (short *)(&port[4][1])); /* prepare the ring address */ if (!ring_ip) { xnet_update_ipaddr(HVFS_RING(0), 1, &ipaddr[3], (short *)(&port[3][0])); if (sport == -1) sport = port[TYPE_MDS][self]; } else { xnet_update_ipaddr(HVFS_RING(0), 1, &ring_ip, (short *)(&port[3][0])); if (sport == -1) sport = port[TYPE_MDS][0]; } /* setup the profiling file */ memset(profiling_fname, 0, sizeof(profiling_fname)); sprintf(profiling_fname, "./CP-BACK-mds.%d", self); hmo.conf.pf_file = fopen(profiling_fname, "w+"); if (!hmo.conf.pf_file) { hvfs_err(xnet, "fopen() profiling file %s faield %d\n", profiling_fname, errno); return EINVAL; } self = HVFS_MDS(self); hmo.xc = xnet_register_type(0, sport, self, &ops); if (IS_ERR(hmo.xc)) { err = PTR_ERR(hmo.xc); return err; } hmo.site_id = self; if (mode == 0) { hmi.gdt_salt = 0; hvfs_info(xnet, "Select GDT salt to %lx\n", hmi.gdt_salt); hmi.root_uuid = 1; hmi.root_salt = 0xdfeadb0; hvfs_info(xnet, "Select root salt to %lx\n", hmi.root_salt); #if 0 ring_add(&hmo.chring[CH_RING_MDS], HVFS_MDS(0)); ring_add(&hmo.chring[CH_RING_MDS], HVFS_MDS(1)); ring_add(&hmo.chring[CH_RING_MDS], HVFS_MDS(2)); ring_add(&hmo.chring[CH_RING_MDS], HVFS_MDS(3)); #else ring_add(&hmo.chring[CH_RING_MDS], HVFS_MDS(4)); #endif ring_add(&hmo.chring[CH_RING_MDSL], HVFS_MDSL(0)); ring_add(&hmo.chring[CH_RING_MDSL], HVFS_MDSL(1)); ring_resort_nolock(hmo.chring[CH_RING_MDS]); ring_resort_nolock(hmo.chring[CH_RING_MDSL]); ring_dump(hmo.chring[CH_RING_MDS]); ring_dump(hmo.chring[CH_RING_MDSL]); /* insert the GDT DH */ dh_insert(hmi.gdt_uuid, hmi.gdt_uuid, hmi.gdt_salt); bitmap_insert(0, 0); } else { hmo.cb_exit = mds_cb_exit; hmo.cb_hb = mds_cb_hb; hmo.cb_ring_update = mds_cb_ring_update; /* use ring info to init the mds */ err = r2cli_do_reg(self, HVFS_RING(0), fsid, 0); if (err) { hvfs_err(xnet, "reg self %x w/ r2 %x failed w/ %d\n", self, HVFS_RING(0), err); goto out; } hvfs_info(xnet, "HMI gdt uuid %ld salt %lx txg %ld\n", hmi.gdt_uuid, hmi.gdt_salt, atomic64_read(&hmi.mi_txg)); } err = mds_verify(); if (err) { hvfs_err(xnet, "Verify MDS configration failed!\n"); goto out; } // SET_TRACING_FLAG(xnet, HVFS_DEBUG); // SET_TRACING_FLAG(mds, HVFS_DEBUG | HVFS_VERBOSE); hvfs_info(xnet, "MDS is UP for serving requests now.\n"); msg_wait(); xnet_unregister_type(hmo.xc); st_destroy(); mds_destroy(); return 0; out: st_destroy(); mds_destroy(); return err; }
int main(int argc, char *argv[]) { struct xnet_msg *msg; struct xnet_type_ops ops = { .buf_alloc = NULL, .buf_free = NULL, .recv_handler = xnet_test_handler, }; int err = 0; int dsite; short port; hvfs_info(xnet, "XNET Simple UNIT TESTing ...\n"); if (argc == 2) { port = 8210; dsite = 0; } else { port = 8412; dsite = 1; } mds_init(10); st_init(); xc = xnet_register_type(0, port, !dsite, &ops); if (IS_ERR(xc)) { err = PTR_ERR(xc); goto out; } xnet_update_ipaddr(0, 1, ipaddr1, port1); xnet_update_ipaddr(1, 1, ipaddr2, port2); /* alloc one msg and send it to the peer site */ msg = xnet_alloc_msg(XNET_MSG_NORMAL); if (!msg) { hvfs_err(xnet, "xnet_alloc_msg() failed\n"); err = -ENOMEM; goto out_unreg; } // SET_TRACING_FLAG(xnet, HVFS_DEBUG); xnet_msg_fill_tx(msg, XNET_MSG_REQ, XNET_NEED_DATA_FREE | XNET_NEED_REPLY, !dsite, dsite); if (dsite) { int i; lib_timer_def(); lib_timer_start(&begin); for (i = 0; i < 100; i++) { xnet_send(xc, msg); } lib_timer_stop(&end); lib_timer_echo_plus(&begin, &end, 10, "Ping-Poing Latency\t"); } else { int i; lib_timer_def(); lib_timer_start(&begin); for (i = 0; i < 100; i++) { xnet_wait_any(xc); } lib_timer_stop(&end); lib_timer_echo_plus(&begin, &end, 10, "Handle Latency\t"); } out_unreg: xnet_unregister_type(xc); out: st_destroy(); mds_destroy(); return err; }
/* The function frees all dynamically allocated memory. This function is always called despite how the program terminates - normally or abnormally. */ void garbage_collect(void){ printf("\nCollecting garbage...\n"); b_destroy(sc_buf); b_destroy(str_LTBL); st_destroy(sym_table); }
void cli_tree_destroy(struct list_head *root) { st_destroy(root, 1); }