int main(void) { struct rbuf in; char buf[4096], *p; int fd = open("test/run-term-eof.c", O_RDONLY), len; /* This is how many tests you plan to run */ plan_tests(6); /* Grab ourselves for comparison. */ len = read(fd, buf, sizeof(buf)); buf[len] = '\0'; lseek(fd, SEEK_SET, 0); /* We have exact-size buffer, which causes problems adding term. */ rbuf_init(&in, fd, malloc(len), len); p = rbuf_read_str(&in, 64, NULL); /* At symbol does not appear. */ ok1(errno == ENOMEM); ok1(!p); /* This should succeed... */ p = rbuf_read_str(&in, 64, realloc); ok1(p); ok1(strcmp(p, buf) == 0); free(in.buf); /* Try again. */ lseek(fd, SEEK_SET, 0); rbuf_init(&in, fd, malloc(len), len); p = rbuf_read_str(&in, 64, realloc); ok1(p); ok1(strcmp(p, buf) == 0); free(in.buf); return exit_status(); }
// hash and array may be included in one table at the same time // keep array in order // if an array contains nil value, such as {1,nil, nil, 2}, the result keeps that static int SerializeMixData(lua_State *L) { rbuf_t buf; rbuf_t *pbuf = &buf; rbuf_init(pbuf); int isShowCommentIdx = 0; int isInLine = 1; int argc = lua_gettop(L); if (argc > 3) { luaL_error(L, "SerializeMixData args:(value, isShowComment, InLine)"); } if (argc >= 2) { if ( argc == 3) { isShowCommentIdx = (int)lua_toboolean(L, -1); // L : value, isInLine, isShowCommentIdx lua_pop(L, 1); } isInLine = (int)lua_toboolean(L, -1); // L : value, isInLine if (isInLine) { isShowCommentIdx = 0; } lua_pop(L, 1); } if (!ConvMixData(L, 0, pbuf, isShowCommentIdx, isInLine)) { luaL_error(L, "dump too deep or alloc memory failed!"); } lua_pushlstring(L, (const char *)(pbuf->ptr), rbuf_len(pbuf)); lua_pushinteger(L, rbuf_len(pbuf)); rbuf_free(pbuf); return 2; }
int main(void) { struct rbuf in; char buf[4096], *p; int fd = open("test/run-term-eof.c", O_RDONLY), len; /* This is how many tests you plan to run */ plan_tests(10); /* Grab ourselves for comparison. */ len = read(fd, buf, sizeof(buf)); buf[len] = '\0'; lseek(fd, SEEK_SET, 0); /* We have exact-size buffer, which causes problems adding term. */ rbuf_init(&in, fd, malloc(len), len, test_realloc); test_realloc_fail = true; p = rbuf_read_str(&in, 64); /* At symbol does not appear. */ ok1(errno == ENOMEM); ok1(!p); /* This should succeed... */ test_realloc_fail = false; p = rbuf_read_str(&in, 64); ok1(p); ok1(strcmp(p, buf) == 0); ok1(rbuf_start(&in) == p + strlen(p)); free(rbuf_cleanup(&in)); /* Try again. */ lseek(fd, SEEK_SET, 0); rbuf_init(&in, fd, malloc(len), len, test_realloc); p = rbuf_read_str(&in, 64); ok1(p); ok1(strcmp(p, buf) == 0); ok1(rbuf_start(&in) == p + strlen(p)); free(rbuf_cleanup(&in)); /* Normal case, we get rbuf_start after nul */ lseek(fd, SEEK_SET, 0); rbuf_init(&in, fd, NULL, 0, test_realloc); p = rbuf_read_str(&in, '^'); ok1(p); ok1(rbuf_start(&in) == p + strlen(p) + 1); free(rbuf_cleanup(&in)); return exit_status(); }
int sem_init( Semaphore* sem, int count ) { sem->srv_tid = WhoIs( SEMAPHORE_SERVER_NAME ); sem->count = count; rbuf_init( &sem->wait_queue, ( uchar* )sem->wait_queue_buf, sizeof( int ), sizeof( int ) * SEMAPHORE_WAIT_QUEUE_SIZE ); return ERR_NONE; }
vcfbuf_t *vcfbuf_init(bcf_hdr_t *hdr, int win) { vcfbuf_t *buf = (vcfbuf_t*) calloc(1,sizeof(vcfbuf_t)); buf->hdr = hdr; buf->win = win; buf->overlap.rid = -1; rbuf_init(&buf->rbuf, 0); return buf; }
// convert every array to hash static int SerializeHashDataIndent(lua_State *L) { rbuf_t buf; rbuf_t *pbuf = &buf; rbuf_init(pbuf); ConvHashData(L, 0, pbuf); lua_pushlstring(L, (const char *)(pbuf->ptr), rbuf_len(pbuf)); lua_pushinteger(L, rbuf_len(pbuf)); rbuf_free(pbuf); return 2; }
static void test() { int i; const char *repr = "repr begin hello\t\tworld\n \b repr end"; rbuf_t buf; rbuf_init(&buf); for (i=0; i<1000; i++) { rbuf_catprintf(&buf, "%d\n", i); } rbuf_catprintf(&buf, "hello\t\tworld %s\n", "kkk"); rbuf_catrepr(&buf, repr, strlen(repr)); printf("%s\n", buf.ptr); rbuf_free(&buf); }
int main(void) { RBUF *r; char *word; r = rbuf_init(1024); word = "this is a"; r->write(r, word, strlen(word)); word = " test.\rI think, I should ha"; r->write(r, word, strlen(word)); word = "ve seen a whole article here."; r->write(r, word, strlen(word)); word = "\rIs it?\r"; r->write(r, word, strlen(word)); printf("%s\n", r->readline(r)); printf("%s\n", r->readline(r)); printf("%s\n", r->readline(r)); return 0; }
static void init_data(args_t *args) { args->files = bcf_sr_init(); args->files->require_index = 1; if ( !bcf_sr_add_reader(args->files,args->fname) ) error("Failed to open %s: %s\n", args->fname, bcf_sr_strerror(args->files->errnum)); args->hdr = args->files->readers[0].header; args->isample = -1; if ( args->sample ) { args->isample = bcf_hdr_id2int(args->hdr,BCF_DT_SAMPLE,args->sample); if ( args->isample<0 ) error("No such sample: %s\n", args->sample); } if ( args->haplotype && args->isample<0 ) { if ( bcf_hdr_nsamples(args->hdr) > 1 ) error("The --sample option is expected with --haplotype\n"); args->isample = 0; } if ( args->mask_fname ) { args->mask = regidx_init(args->mask_fname,NULL,NULL,0,NULL); if ( !args->mask ) error("Failed to initialize mask regions\n"); } // In case we want to store the chains if ( args->chain_fname ) { args->fp_chain = fopen(args->chain_fname,"w"); if ( ! args->fp_chain ) error("Failed to create %s: %s\n", args->chain_fname, strerror(errno)); args->chain_id = 0; } rbuf_init(&args->vcf_rbuf, 100); args->vcf_buf = (bcf1_t**) calloc(args->vcf_rbuf.m, sizeof(bcf1_t*)); if ( args->output_fname ) { args->fp_out = fopen(args->output_fname,"w"); if ( ! args->fp_out ) error("Failed to create %s: %s\n", args->output_fname, strerror(errno)); } else args->fp_out = stdout; }
worker_t* worker_init(int id) { int n; worker_t* worker = malloc(sizeof(worker_t)); bzero(worker, sizeof(worker_t)); worker->outbox = malloc(sizeof(rbuf_t *) * num_workers); for (n = 0; n < num_workers; n++) { if (n != id) worker->outbox[n] = rbuf_init(10); // FIXPAUL } worker->id = id; worker->running = 1; if (pipe(worker->conn_queue) == -1) { printf("create pipe failed!\n"); return NULL; } if (fcntl(worker->conn_queue[0], F_SETFL, O_NONBLOCK) == -1) perror("fcntl(pipe, O_NONBLOCK)"); if (pipe(worker->msg_queue) == -1) { printf("create pipe failed!\n"); return NULL; } if (fcntl(worker->msg_queue[0], F_SETFL, O_NONBLOCK) == -1) perror("fcntl(pipe, O_NONBLOCK)"); if (fcntl(worker->msg_queue[1], F_SETFL, O_NONBLOCK) == -1) perror("fcntl(pipe, O_NONBLOCK)"); return worker; }
/* init a device by its device file, e.g. "ttyUSB0" */ MODAT *modat_init(char *dev_file, unsigned int speed) { static int device_count = 0; MODAT *pat = NULL; if (strlen(dev_file) >= sizeof(pat->dev_file)) return NULL; /* do the malloc */ pat = malloc(sizeof(MODAT)); if (pat == NULL) return NULL; bzero(pat, sizeof(MODAT)); /* set the speed of port */ if (_set_speed(pat, speed)) { dbg(1, "speed of port %d not supported.", speed); goto free_modat; } /* malloc for rbuf */ pat->r = rbuf_init(RBUF_SIZE); if (pat->r == NULL) goto free_modat; if (pat) { /* malloc ok, do the initialize. * default enabled */ pat->enabled = 1; /* set device file */ strcpy(pat->dev_file, dev_file); /* type cannot be changed after init of the modat */ pat->type = modat_detect(dev_file); /* format name from type */ snprintf(pat->name, sizeof(pat->name), "%s/%d", modat_supports[pat->type].product_name, device_count++); /* default status is INIT */ pat->status = INIT; /* init mutex */ pthread_mutex_init(&(pat->mutex), NULL); /* register related model functions to this device */ modat_register_methods(pat); /* init simcard number by 0000 */ strncpy(pat->sim, "0000", sizeof(pat->sim)); /* defaultly set engineer mode off */ pat->engineer_mode = 0; } /* create daemon thread for the device. */ if (pthread_create(&pat->thread_id, NULL, device_daemon, pat)) { /* thread creation error */ dbg(1, "create thread for device [%d][%s] error.", pat->type, pat->name); goto free_rbuf; } /* all ok */ dbg(2, "%s initialized successfully.", pat->name); return pat; free_rbuf: pthread_mutex_destroy(&(pat->mutex)); free(pat->r); free_modat: free(pat); return NULL; }
int main(int argc, char *argv[]) { pthread_t tid; int is_running = 1; uint32_t frames_rdy; uint32_t frames_req; rbuf_cblk_t *pcblk = &g_buffer_cblk; void *ptr1; uint32_t size1; void *ptr2; uint32_t size2; uint32_t delay = -1; uint32_t cnt; char *buf = NULL; if (argc > 1) { delay = atoi(argv[1]); } pthread_mutex_init(&g_lock, NULL); pthread_cond_init(&g_cond, NULL); buf = malloc(BUF_FRAME_COUNT*BUF_FRAME_SIZE); #ifdef __RBUF_FRAME_SUPPORT__ rbuf_init(pcblk, buf, BUF_FRAME_COUNT, BUF_FRAME_SIZE); #else rbuf_init(pcblk, buf, BUF_FRAME_COUNT*BUF_FRAME_SIZE); #endif printf("[init] buf = 0x%x, end = 0x%x\n", (unsigned int)pcblk->buffer, (unsigned int)pcblk->buffer + BUF_FRAME_SIZE*BUF_FRAME_COUNT); pthread_create(&tid, NULL, thread_client, pcblk); cnt = 0; while (is_running && g_client_runnig) { frames_rdy = rbuf_read_available(pcblk); if (frames_rdy == 0) { printf("[s]wait client\n"); usleep(100); continue; } frames_req = rand() & (BUF_FRAME_REQ_S - 1); if (frames_req == 0) { frames_req = BUF_FRAME_REQ_S; } if (frames_req > frames_rdy) { frames_req = frames_rdy; } frames_req = rbuf_get_read_buffer(pcblk, frames_req, &ptr1, &size1, &ptr2, &size2); if (frames_req > 0) { printf("[s]frames_req = 0x%x\n", frames_req); printf("[s]buf1 = 0x%x, size1 = 0x%x, buf2 = 0x%x, size2 = 0x%x\n", (unsigned int)ptr1, size1, (unsigned int)ptr2, size2); pthread_mutex_lock(&g_lock); rbuf_advance_read_index(pcblk, frames_req); pthread_cond_signal(&g_cond); pthread_mutex_unlock(&g_lock); } usleep(DELAY_S*1000); cnt++; if (cnt >= (1000/DELAY_S)) { cnt = 0; delay--; if (delay == 0) { printf("exit main loop\n"); is_running = 0; } } } g_client_runnig = 0; pthread_cond_signal(&g_cond); pthread_join(tid, NULL); pthread_cond_destroy(&g_cond); pthread_mutex_destroy(&g_lock); if (buf) { free(buf); buf = NULL; } return 0; }
static void init_data(args_t *args) { args->out_fh = hts_open(args->output_fname,hts_bcf_wmode(args->output_type)); if ( args->out_fh == NULL ) error("Can't write to \"%s\": %s\n", args->output_fname, strerror(errno)); if ( args->n_threads ) hts_set_threads(args->out_fh, args->n_threads); args->hdr = args->files->readers[0].header; args->flt_pass = bcf_hdr_id2int(args->hdr,BCF_DT_ID,"PASS"); assert( !args->flt_pass ); // sanity check: required by BCF spec // -i or -e: append FILTER line if ( args->soft_filter && args->filter_logic ) { kstring_t flt_name = {0,0,0}; if ( strcmp(args->soft_filter,"+") ) kputs(args->soft_filter, &flt_name); else { // Make up a filter name int i = 0, id = -1; do { ksprintf(&flt_name,"Filter%d", ++i); id = bcf_hdr_id2int(args->hdr,BCF_DT_ID,flt_name.s); } while ( bcf_hdr_idinfo_exists(args->hdr,BCF_HL_FLT,id) ); } // escape quotes kstring_t tmp = {0,0,0}; char *t = args->filter_str; while ( *t ) { if ( *t=='"' ) kputc('\\',&tmp); kputc(*t,&tmp); t++; } int ret = bcf_hdr_printf(args->hdr, "##FILTER=<ID=%s,Description=\"Set if %s: %s\">", flt_name.s,args->filter_logic & FLT_INCLUDE ? "not true" : "true", tmp.s); if ( ret!=0 ) error("Failed to append header line: ##FILTER=<ID=%s,Description=\"Set if %s: %s\">\n", flt_name.s,args->filter_logic & FLT_INCLUDE ? "not true" : "true", tmp.s); args->flt_fail = bcf_hdr_id2int(args->hdr,BCF_DT_ID,flt_name.s); assert( args->flt_fail>=0 ); free(flt_name.s); free(tmp.s); } if ( args->snp_gap || args->indel_gap ) { if ( !args->filter_logic && args->soft_filter && strcmp(args->soft_filter,"+") ) { kstring_t tmp = {0,0,0}; if ( args->snp_gap ) kputs("\"SnpGap\"", &tmp); if ( args->indel_gap ) { if ( tmp.s ) kputs(" and ", &tmp); kputs("\"IndelGap\"", &tmp); } fprintf(stderr,"Warning: using %s filter name instead of \"%s\"\n", tmp.s,args->soft_filter); free(tmp.s); } rbuf_init(&args->rbuf, 64); args->rbuf_lines = (bcf1_t**) calloc(args->rbuf.m, sizeof(bcf1_t*)); if ( args->snp_gap ) { bcf_hdr_printf(args->hdr, "##FILTER=<ID=SnpGap,Description=\"SNP within %d bp of an indel\">", args->snp_gap); args->SnpGap_id = bcf_hdr_id2int(args->hdr, BCF_DT_ID, "SnpGap"); assert( args->SnpGap_id>=0 ); } if ( args->indel_gap ) { bcf_hdr_printf(args->hdr, "##FILTER=<ID=IndelGap,Description=\"Indel within %d bp of an indel\">", args->indel_gap); args->IndelGap_id = bcf_hdr_id2int(args->hdr, BCF_DT_ID, "IndelGap"); assert( args->IndelGap_id>=0 ); } } if (args->record_cmd_line) bcf_hdr_append_version(args->hdr, args->argc, args->argv, "bcftools_filter"); if ( args->filter_str ) args->filter = filter_init(args->hdr, args->filter_str); }
int main(void) { rbuf b; // test rounding. { assert(rbuf_init(&b, 0, BYTE) == -1); rbuf_destroy(&b); assert(rbuf_init(&b, page_size/2, BYTE) == page_size); rbuf_destroy(&b); assert(rbuf_init(&b, page_size/2 + 1, BYTE) == page_size); rbuf_destroy(&b); assert(rbuf_init(&b, page_size, BYTE) == page_size); rbuf_destroy(&b); assert(rbuf_init(&b, page_size * 2, BYTE) == page_size * 2); rbuf_destroy(&b); assert(rbuf_init(&b, page_size * 3, BYTE) == page_size * 3); rbuf_destroy(&b); assert(rbuf_init(&b, (page_size * 3) - (page_size / 3), BYTE) == page_size * 3); rbuf_destroy(&b); } // Test BYTE { rbuf_init(&b, page_size, BYTE); // fill buffer. for (int i = 0; i < b.size; i++) { rbuf_write(&b, "f", 1); } assert(rbuf_free(&b) == 0); // overfill buffer. assert(rbuf_write(&b, "f", 1) == 0); assert(rbuf_free(&b) == 0); // empty buffer. char c; for (int i = 0; i < b.size; i++) { rbuf_read(&b, &c, 1); assert(c == 'f'); } assert(rbuf_count(&b) == 0); // read from empty buffer. assert(rbuf_read(&b, &c, 1) == 0); assert(rbuf_count(&b) == 0); rbuf_destroy(&b); } // Test STRING. { rbuf_init(&b, page_size, STRING); static char data[] = "test string"; // fill buffer. while(rbuf_write(&b, data, strlen(data))); assert(rbuf_free(&b) == 0); // overfill buffer. assert(rbuf_write(&b, data, strlen(data)) == 0); assert(rbuf_free(&b) == 0); // empty buffer char out[strlen(data) + 1]; size_t read, old_read; while((read = rbuf_read(&b, out, strlen(data) + 1))) { old_read = read; assert(strcmp(out, data) <= 0); } assert(rbuf_count(&b) == 0); assert(out[old_read - 1] == '\0'); assert((strlen(out) + 1) == old_read); assert((page_size % (strlen(data) + 1) == old_read)); // read from empty buffer. assert(rbuf_read(&b, out, strlen(data) + 1) == 0); rbuf_destroy(&b); } // Test STREAM { rbuf_init(&b, page_size, STREAM); static char data[] = "test stream\n"; // fill buffer. while(rbuf_write(&b, data, strlen(data))); assert(rbuf_free(&b) == 0); // overfill buffer. assert(rbuf_write(&b, data, strlen(data)) == 0); assert(rbuf_free(&b) == 0); // empty buffer char out[strlen(data)]; while(rbuf_read(&b, out, strlen(data)) != -1) { assert(strncmp(out, data, strlen(out)) == 0); } assert(rbuf_count(&b) == page_size % strlen(data)); // read from empty buffer. assert(rbuf_read(&b, out, strlen(data)) == -1); // Terminate end stream in buffer. assert(rbuf_count(&b) > 0); assert(rbuf_write(&b, data + rbuf_count(&b), strlen(data) - (page_size % strlen(data)))); assert(rbuf_read(&b, out, strlen(data)) != -1); assert(strncmp(out, data, strlen(out)) == 0); rbuf_destroy(&b); } // All tests passed succesfully! return 0; }