int state_load(memfile_t *file) { stateheader_t header; block_t *block; u32 size = 0; int i; readvar(header.ident,4); readvar(header.version,2); readvar(header.flags,2); readvar(header.usize,4); readvar(header.csize,4); readvar(header.crc32,4); log_printf("state_load: state header loaded. version %04X\n",header.version); while(memfile_eof(file) == 0 && size < header.usize) { if((block = block_load(file)) == 0) break; size += 8 + block->size; log_printf("state_load: loaded block '%4s' (%08X) (%d bytes)\n",&block->type,block->type,block->size); for(i=0;blockinfo[i].type;i++) { if(blockinfo[i].type == block->type) { blockinfo[i].func(STATE_LOAD,block->data); break; } } if(blockinfo[i].type == 0) { log_printf("state_load: no handler for block type '%4s' (%d bytes)\n",&block->type,block->size); } block_destroy(block); } return(0); }
block_t *block_new() { block_t *l; if ((l = malloc(sizeof(block_t))) == NULL) return(NULL); memset(l,0,sizeof(block_t)); if ((l->_head = _entry_new_by_level(LEVEL_MAX)) == NULL) { block_destroy(l); return(NULL); } l->cmp = _entry_compare; return(l); }
static void access_avi_destroy(Access *thiz) { if(thiz) { DECL_PRIV(thiz, priv); block_destroy(priv->block); FTK_FREE(priv->file_path); av_close_input_file(priv->format_ctx); FTK_ZFREE(thiz, sizeof(*thiz) + sizeof(PrivInfo)); } return; }
void hfile_destroy(hfile_t *fp) { meta_t *o; if (fp == NULL) return; hinfo_destroy(fp->fileinfo); htail_destroy(fp->trailer); for (o = index_head(fp->metas); o != NULL; o = meta_next(o)) block_destroy(fp->blocks[o->id]); index_destroy(fp->metas); bloom_destroy(fp->bloom); free(fp->blocks); hfile_free(fp); }
/* * Searches through the cached huffman_codeset_t tables looking for a stored * huffman code of type 'code_set'. * NB: only code_sets >= CODE_USER will be stored here. * * Returns codes on success, * NULL on failure */ ztr_hcode_t *ztr_find_hcode(ztr_t *ztr, int code_set) { int i; if (code_set < CODE_USER) return NULL; /* computed on-the-fly or use a hard-coded set */ /* Check through chunks for undecoded HUFF chunks */ if (!ztr->hcodes_checked) { for (i = 0; i < ztr->nchunks; i++) { if (ztr->chunk[i].type == ZTR_TYPE_HUFF) { block_t *blk; huffman_codeset_t *cs; uncompress_chunk(ztr, &ztr->chunk[i]); blk = block_create((unsigned char *)(ztr->chunk[i].data+2), ztr->chunk[i].dlength-2); cs = restore_codes(blk, NULL); if (!cs) { block_destroy(blk, 1); return NULL; } cs->code_set = (unsigned char)(ztr->chunk[i].data[1]); ztr_add_hcode(ztr, cs, 1); block_destroy(blk, 1); } } ztr->hcodes_checked = 1; } /* Check cached copies */ for (i = 0; i < ztr->nhcodes; i++) { if (ztr->hcodes[i].codes->code_set == code_set) return &ztr->hcodes[i]; } return NULL; }
void pge_deinit() { pge_finish(); text_layer_destroy(s_status_layer); // Deallocate for(int z = 0; z < GRID_DEPTH; z++) { for(int y = 0; y < GRID_HEIGHT; y++) { for(int x = 0; x < GRID_WIDTH; x++) { block_destroy(s_block_array[vec2i(Vec3(x, y, z))]); } } } for(int i = 0; i < MAX_CLOUDS; i++) { cloud_destroy(s_cloud_array[i]); } }
/* * Stores held ztr huffman_codes as ZTR chunks. * Returns 0 for success * -1 for failure */ int ztr_store_hcodes(ztr_t *ztr) { int i; ztr_chunk_t *chunks; int nchunks; if (ztr->nhcodes == 0) return 0; /* Extend chunks array */ nchunks = ztr->nchunks + ztr->nhcodes; chunks = (ztr_chunk_t *)realloc(ztr->chunk, nchunks * sizeof(*chunks)); if (!chunks) return -1; ztr->chunk = chunks; /* Encode */ for (i = 0; i < ztr->nhcodes; i++) { block_t *blk = block_create(NULL, 2); int j = ztr->nchunks; unsigned char bytes[2]; ztr->chunk[j].type = ZTR_TYPE_HUFF; ztr->chunk[j].mdata = 0; ztr->chunk[j].mdlength = 0; ztr->chunk[j].ztr_owns = 1; bytes[0] = 0; bytes[1] = ztr->hcodes[i].codes->code_set; store_bytes(blk, bytes, 2); /* FIXME: Now already cached in ztr_hcode_t */ if (0 == store_codes(blk, ztr->hcodes[i].codes, 1)) { /* Last byte is always merged with first of stream */ if (blk->bit == 0) { unsigned char zero = 0; store_bytes(blk, &zero, 1); } ztr->chunk[j].data = (char *)blk->data; ztr->chunk[j].dlength = blk->byte + (blk->bit != 0); block_destroy(blk, 1); ztr->nchunks++; } } return ztr->nchunks == nchunks ? 0 : -1; }
int state_save(memfile_t *file) { stateheader_t header; block_t *block; int i; //clear the state info memset(&header,0,sizeof(stateheader_t)); //set the ident/version of the state header header.ident = ident; header.version = version; //calculate total data size for(i=0;blockinfo[i].type;i++) { blockinfo[i].size = 0; blockinfo[i].func(STATE_SIZE,(u8*)&blockinfo[i].size); if(blockinfo[i].size) { header.usize += blockinfo[i].size + 8; } } //write the state header writevar(header.ident,4); writevar(header.version,2); writevar(header.flags,2); writevar(header.usize,4); writevar(header.csize,4); writevar(header.crc32,4); //write each block for(i=0;blockinfo[i].type;i++) { if(blockinfo[i].size == 0) continue; printf("saving block '%4s' (%d bytes)\n",&blockinfo[i].type,blockinfo[i].size); block = block_create(blockinfo[i].type,blockinfo[i].size); blockinfo[i].func(STATE_SAVE,block->data); block_save(file,block); block_destroy(block); } return(0); }
void sprk_dataset_destroy (sprk_dataset_t **self_p) { assert (self_p); if (*self_p) { sprk_dataset_t *self = *self_p; block_t *block = (block_t *) zhash_first (self->blocks); while (block != NULL) { block_destroy (&block); zhash_delete (self->blocks, zhash_cursor (self->blocks)); block = (block_t *) zhash_next (self->blocks); } zhash_destroy (&self->blocks); // Free object itself free (self); *self_p = NULL; } }
block_data_t * block_manager_read_and_store_block ( block_manager_t *self, const char *block_id, block_t **block_ref) { assert (self); // Steal ownership of the block. assert (block_ref); block_t *block = *block_ref; assert (block); *block_ref = NULL; block_data_t *existing = block_manager_get_block (self, block_id); if (existing != NULL) { block_destroy (&block); return existing; } descriptor_t *descriptor = block_descriptor (block); FILE *fp = fopen (descriptor_uri (descriptor), "r"); assert (fp); fseek (fp, descriptor_offset (descriptor), SEEK_SET); uint32_t length = descriptor_length (descriptor); size_t floats = length / sizeof (float); float *data = (float *)malloc (length); int n = fread (data, sizeof (float), floats, fp); assert (n == length); fclose (fp); block_data_t *blockdata = block_data_new ( block, data, length); pthread_mutex_lock(&self->lock); zhash_insert (self->blocks, block_id, blockdata); pthread_mutex_unlock(&self->lock); return blockdata; }
void* writer_run(void *arg) { writer_t *w = (writer_t*)arg; block_t *b = NULL; int32_t wait; uint64_t n = 0; //fprintf(stderr, "writer starting w->output->n=%d\n", w->output->n); while(!w->is_done) { #ifdef PBGZF_USE_LOCAL_POOLS while(w->pool_local->n < w->pool_local->m) { // more to read from the output queue wait = (0 == w->pool_local->n) ? 1 : 0; b = queue_get(w->output, wait); if(NULL == b) { if(1 == wait) { if(QUEUE_STATE_OK == w->output->state) { fprintf(stderr, "writer queue_get: bug encountered\n"); exit(1); } else if(QUEUE_STATE_EOF == w->output->state || 0 == w->output->num_adders) { break; } else { queue_wait_until_not_flush(w->output); continue; } } else { break; } } if(0 == block_pool_add(w->pool_local, b)) { fprintf(stderr, "writer block_pool_add: bug encountered\n"); exit(1); } b = NULL; } //fprintf(stderr, "writer: read from output w->pool_local->n=%d %d\n", w->pool_local->n, w->pool_local->m); if(0 == w->pool_local->n && (QUEUE_STATE_EOF == w->output->state || 0 == w->output->num_adders)) { break; } while(0 < w->pool_local->n) { // write all the blocks b = block_pool_get(w->pool_local); if(NULL == b) { fprintf(stderr, "writer block_pool_get: bug encountered\n"); exit(1); } if(0 == w->compress) { if(writer_write_block1(w->fp_file, b) != b->block_length) { fprintf(stderr, "writer writer_write_block: bug encountered\n"); exit(1); } } else { if(writer_write_block2(w->fp_bgzf, b) != b->block_length) { fprintf(stderr, "writer writer_write_block: bug encountered\n"); exit(1); } } if(0 == block_pool_add(w->pool_fp, b)) { fprintf(stderr, "writer block_pool_add: bug encountered\n"); exit(1); } n++; } #else wait = 1; b = queue_get(w->output, wait); if(NULL == b) { if(1 == wait) { if(QUEUE_STATE_OK == w->output->state) { fprintf(stderr, "writer queue_get: bug encountered\n"); exit(1); } else if(QUEUE_STATE_EOF == w->output->state || 0 == w->output->num_adders) { break; } else { queue_wait_until_not_flush(w->output); continue; } } else { break; } } if(0 == w->compress) { if(writer_write_block1(w->fp_file, b) != b->block_length) { fprintf(stderr, "writer writer_write_block: bug encountered\n"); exit(1); } } else { if(writer_write_block2(w->fp_bgzf, b) != b->block_length) { fprintf(stderr, "writer writer_write_block: bug encountered\n"); exit(1); } } block_destroy(b); b = NULL; n++; #endif } w->is_done = 1; //fprintf(stderr, "writer written %llu blocks\n", n); // NB: will wake all queue_remove_getter(w->output); return arg; }
void allefant_destroy(Block * super) { game->player2 = NULL; block_destroy(super); }
int main(int argc, char **argv) { block_t *l; entry_t *e; int8_t buffer[KEY_MAX]; sds key; int32_t count, cost, i; int32_t ok_c, err_c; if (argc != 2) return(-1); count = atoi(argv[1]); l = block_new(); printf("\nBlock add Test...\n"); ok_c = err_c = 0; cost = time(NULL); for (i = 0; i < count; i++) { e = entry_new(); snprintf(buffer,KEY_MAX,"%d",i); e->key = sdsnew(buffer); snprintf(buffer,KEY_MAX,"key = %d",i); e->value = sdsnew(buffer); if (0 == block_add(l,e)) ok_c++; else err_c++; } cost = time(NULL) - cost; printf("%d add ok and %d error\n",ok_c,err_c); printf("total cost : %d\n",cost); printf("\nBlock search Test...\n"); ok_c = err_c = 0; cost = time(NULL); for (i = 0; i < count; i++) { snprintf(buffer,KEY_MAX,"%d",i); key = sdsnew(buffer); if ((e = block_loup(l,key)) == NULL) err_c++; else { if (strstr(e->value,"0") != NULL) { printf("%s del ok\n",e->value); block_del(l,e);; } ok_c++; } sdsdel(key); } cost = time(NULL) - cost; printf("%d find ok and %d error\n",ok_c,err_c); printf("total cost : %d\n",cost); printf("\nBlock research Test...\n"); ok_c = err_c = 0; cost = time(NULL); for (i = 0; i < count; i++) { snprintf(buffer,KEY_MAX,"%d",i); key = sdsnew(buffer); if ((e = block_loup(l,key)) == NULL) err_c++; else { ok_c++; } sdsdel(key); } cost = time(NULL) - cost; printf("%d find ok and %d error\n",ok_c,err_c); printf("total cost : %d\n",cost); sleep(10); block_destroy(l); return(0); }
void* reader_run(void *arg) { reader_t *r = (reader_t*)arg; block_t *b = NULL; int32_t wait; uint64_t n = 0; block_pool_t *pool; //fprintf(stderr, "reader staring\n"); pool = block_pool_init2(PBGZF_BLOCKS_POOL_NUM); while(!r->is_done) { #ifdef PBGZF_USE_LOCAL_POOLS // read block while(pool->n < pool->m) { if(NULL == r->pool || NULL == (b = block_pool_get(r->pool))) { b = block_init(); } if(0 == r->compress) { if(reader_read_block(r->fp_bgzf, b) < 0) { fprintf(stderr, "reader reader_read_block: bug encountered\n"); exit(1); } } else { if((b->block_length = read(r->fd_file, b->buffer, WINDOW_SIZE)) < 0) { fprintf(stderr, "reader read: bug encountered\n"); exit(1); } } if(NULL == b || 0 == b->block_length) { block_pool_add(r->pool, b); b = NULL; break; } if(0 == block_pool_add(pool, b)) { fprintf(stderr, "reader block_pool_add: bug encountered\n"); exit(1); } } //fprintf(stderr, "reader: read in pool->n=%d\n", pool->n); if(0 == pool->n) { break; } // add to the queue while(0 < pool->n) { b = block_pool_peek(pool); if(NULL == b) { fprintf(stderr, "reader block_pool_get: bug encountered\n"); exit(1); } wait = (pool->n == pool->m) ? 1 : 0; // NB: only wait if we cannot read in any more... if(0 == queue_add(r->input, b, wait)) { if(1 == wait) { if(QUEUE_STATE_OK == r->input->state) { fprintf(stderr, "reader queue_add: bug encountered\n"); exit(1); } else if(QUEUE_STATE_EOF == r->input->state) { // EOF, quit break; } else { // NB: if the reader has blocks, it does not make sense to // flush fprintf(stderr, "reader queue_add: bug encountered\n"); exit(1); } } else { break; } } block_pool_get(pool); // ignore return b = NULL; n++; } //fprintf(stderr, "reader: add to pool->n=%d\n", pool->n); //fprintf(stderr, "r->output->n=%d\n", r->input->n); #else // read block //fprintf(stderr, "Reader #%d read block\n", 0); b = block_init(); if(0 == r->compress) { if(reader_read_block(r->fp_bgzf, b) < 0) { fprintf(stderr, "reader reader_read_block: bug encountered\n"); exit(1); } } else { if((b->block_length = read(r->fd_file, b->buffer, WINDOW_SIZE)) < 0) { fprintf(stderr, "reader read: bug encountered\n"); exit(1); } } if(NULL == b || 0 == b->block_length) { block_destroy(b); b = NULL; break; } // add to the queue //fprintf(stderr, "Reader #%d add to queue\n", 0); wait = 1; if(0 == queue_add(r->input, b, wait)) { if(1 == wait) { if(QUEUE_STATE_OK == r->input->state) { fprintf(stderr, "reader queue_add: bug encountered\n"); exit(1); } else if(QUEUE_STATE_EOF == r->input->state) { // EOF, quit block_destroy(b); b = NULL; break; } else { queue_wait_until_not_flush(r->input); continue; } } else { block_destroy(b); b = NULL; break; } } b = NULL; n++; //fprintf(stderr, "reader read %llu blocks\n", n); #endif } block_destroy(b); b = NULL; r->is_done = 1; // NB: EOF should be handled when the adder is removed queue_remove_adder(r->input); //fprintf(stderr, "reader read %llu blocks\n", n); //fprintf(stderr, "reader r->input->n=%d\n", r->input->n); //fprintf(stderr, "reader r->input->state=%d QUEUE_STATE_EOF=%d\n", r->input->state, QUEUE_STATE_EOF); block_pool_destroy(pool); return arg; }