void parse(FILE *in, struct block *b) { char line[BUFFER_SIZE]; if (fgets(line, sizeof(line), in)) { for (char *tok = strtok(line, " \n"); tok != NULL; tok = strtok(NULL, " \n")) { if (strncmp(tok, "end.", 4) == 0) return; strncat(b->text, tok, BUFFER_SIZE); strncat(b->text, " ", BUFFER_SIZE); if (strncmp(tok, "do.", 3) == 0) { b->inner = block_new(); parse(in, b->inner); b->next = block_new(); parse(in, b->next); return; } } b->text[strlen(b->text) - 1] = '\0'; // Trim space at end-of-line b->next = block_new(); parse(in, b->next); } }
struct strings *strings_new() { struct strings *strings = malloc(sizeof(*strings)); if (!strings) { return NULL; } strings->hashes = block_new(PAGE_SIZE); strings->strings = block_new(PAGE_SIZE); strings->index = block_new(PAGE_SIZE); if (!strings->hashes || !strings->strings || !strings->index) { goto error; } tree_new(&strings->hash_map); strings->total = 0; strings->hash_seed = 5381; return strings; error: if (strings->hashes) { block_free(strings->hashes); } if (strings->strings) { block_free(strings->strings); } if (strings->index) { block_free(strings->index); } free(strings); return NULL; }
t_block *block_clone(t_block *block) { if(block) { t_block *clone = block_new(block->id.name); vcp3i(clone->idcol,block->idcol); set_name(clone->type,block->type); vcp3f(clone->pos,block->pos); clone->width = block->width; clone->height = block->height; clone->block_state = block->block_state; clone->tot_bricks = block->tot_bricks; clone->rhizome_order = block->rhizome_order; clone->rhizome_pos = block->rhizome_pos; clone->bricks = list_clone(block->bricks,dt_brick); clone->submenu = NULL; clone->hover = NULL; clone->rhizome = NULL; //XXX init cls ??? clone->cls = block->cls; return clone; } else { return NULL; } }
CTEST(node_serial_test, leaf_empty) { int ret = 0; int fd = ness_os_open(BRT_FILE, O_RDWR | O_CREAT, 0777); struct block *b = block_new(); struct hdr *hdr = (struct hdr*)xcalloc(1, sizeof(*hdr)); struct options *opts = options_new(); /* * dummy brt leaf */ uint64_t nid = 3; struct node *dummy_leaf = leaf_alloc_empty(nid); leaf_alloc_bsm(dummy_leaf); ret = serialize_node_to_disk(fd, b, dummy_leaf, hdr); ASSERT_TRUE(ret > 0); //free node_free(dummy_leaf); struct node *dummy_leaf1; ret = deserialize_node_from_disk(fd, b, nid, &dummy_leaf1, 0); ASSERT_TRUE(ret > 0); ASSERT_EQUAL(0, basement_count(dummy_leaf1->u.l.le->bsm)); //free node_free(dummy_leaf1); ness_os_close(fd); block_free(b); xfree(hdr); options_free(opts); xcheck_all_free(); }
static void * block_alloc(block *b, size_t size) { void *p; assert(b); size = _Py_SIZE_ROUND_UP(size, ALIGNMENT); if (b->ab_offset + size > b->ab_size) { /* If we need to allocate more memory than will fit in the default block, allocate a one-off block that is exactly the right size. */ /* TODO(jhylton): Think about space waste at end of block */ block *newbl = block_new( size < DEFAULT_BLOCK_SIZE ? DEFAULT_BLOCK_SIZE : size); if (!newbl) return NULL; assert(!b->ab_next); b->ab_next = newbl; b = newbl; } assert(b->ab_offset + size <= b->ab_size); p = (void *)(((char *)b->ab_mem) + b->ab_offset); b->ab_offset += size; return p; }
PyArena * PyArena_New() { PyArena* arena = (PyArena *)malloc(sizeof(PyArena)); if (!arena) return (PyArena*)PyErr_NoMemory(); arena->a_head = block_new(DEFAULT_BLOCK_SIZE); arena->a_cur = arena->a_head; if (!arena->a_head) { free((void *)arena); return (PyArena*)PyErr_NoMemory(); } arena->a_objects = PyList_New(0); if (!arena->a_objects) { block_free(arena->a_head); free((void *)arena); return (PyArena*)PyErr_NoMemory(); } #if defined(Py_DEBUG) arena->total_allocs = 0; arena->total_size = 0; arena->total_blocks = 1; arena->total_block_size = DEFAULT_BLOCK_SIZE; arena->total_big_blocks = 0; #endif return arena; }
fs_t* fs_new(unsigned num_blocks, int disk_delay) { fs_t* fs = (fs_t*) malloc(sizeof(fs_t)); fs->blocks = block_new(num_blocks,BLOCK_SIZE); fsi_load_fsdata(fs); io_delay_on(disk_delay); return fs; }
int main(int argc, char **argv) { struct block *block = block_new(); parse(stdin, block); print_basic(stdout, block); block_free(block); return 0; }
block_t *block_load(int32_t fd, int32_t offset, int32_t size) { block_t *l; entry_t *o; int32_t len; int8_t *buffer, *ptr; if (fd < 0 || offset < 0 || size < 0) return(NULL); if ((l = block_new()) == NULL) return(NULL); if ((buffer = malloc(size)) == NULL) __ERROR_LOG(B_ERROR); lseek(fd,offset,SEEK_SET); if (size != read(fd,buffer,size)) __ERROR_LOG(R_ERROR); ptr = buffer; l->magic = *(uint64_t *)ptr; ptr = ptr + sizeof(uint64_t); if (l->magic != crc32_encode(ptr,size - sizeof(uint64_t))) __ERROR_LOG(C_ERROR); while (ptr - buffer < size) { if ((o = entry_new()) == NULL) continue; len = *(int32_t *)ptr; ptr = ptr + sizeof(int32_t); o->key = sdsnnew(ptr,len); ptr = ptr + len; len = *(int32_t *)ptr; ptr = ptr + sizeof(int32_t); o->value = sdsnnew(ptr,len); ptr = ptr + len; if (o->key && o->value) { block_add(l,o); continue; } if (o->key) sdsdel(o->key); if (o->value) sdsdel(o->value); } free(buffer); return(l); C_ERROR: R_ERROR: free(buffer); B_ERROR: block_free(l); return(NULL); }
void test_block() { int i; block_t *block; block = block_new(); for(i = 0; i < VAL; i++) block_insert(block, KEY, i); assert_int_equal(block_size(block), 1); block_foreach(block, test_block_foreach, NULL); block_foreach_remove(block, test_block_foreach_rm, NULL); assert_int_equal(block_size(block), 0); block_free(block); }
static struct block *add_utf8_line(struct buffer *b, struct block *blk, const unsigned char *line, size_t len) { size_t size = len + 1; if (blk) { size_t avail = blk->alloc - blk->size; if (size <= avail) goto copy; add_block(b, blk); } if (size < 8192) size = 8192; blk = block_new(size); copy: memcpy(blk->data + blk->size, line, len); blk->size += len; blk->data[blk->size++] = '\n'; blk->nl++; return blk; }
void uncompress(image* in, image* out, const float* quantify) { Block block = block_new(); int colBlock = 0; int lineBlock = 0; int kIn = 0; int n,m; out->size = in->h*in->w; out->h = in->h; out->w = in->w; // Iterator on image : block by block for(int i = 0 ; i < in->h * in->w ; i += 64) { kIn = 0; ZIterator zit = zIterator_new(block, 8); // Z Parcours for reposition of blocks block.data[0] = in->data[i]; // First iterator value while(zIterator_hasNext(zit)) { block.data[zit.line*8 + zit.column] = in->data[i + (kIn++)]; zIterator_next(&zit); } block.data[zit.line*8 + zit.column] = in->data[i + (kIn++)]; // last pixel // Quantify for(n = 0; n < 8 ; ++n) { for(m = 0; m < 8 ; ++m) { block.data[n*8 + m] *= quantify[n*8 + m]; } } idct(out, block.data, colBlock, lineBlock); colBlock += 8; if(colBlock >= in->w) { colBlock = 0; lineBlock += 8; } } block_delete(&block); }
CTEST(node_serial_test, leaf_2_record) { int ret = 0; int fd = ness_os_open(BRT_FILE, O_RDWR | O_CREAT, 0777); struct block *b = block_new(); struct hdr *hdr = (struct hdr*)xcalloc(1, sizeof(*hdr)); struct options *opts = options_new(); /* * dummy brt leaf */ uint64_t nid = 3; struct node *dummy_leaf = leaf_alloc_empty(nid); leaf_alloc_bsm(dummy_leaf); MSN msn = 0U; struct xids *xids = NULL; struct msg k, v; k.size = 6; k.data = "hello"; v.size = 6; v.data = "world"; basement_put(dummy_leaf->u.l.le->bsm, &k, &v, MSG_INSERT, msn, xids); struct msg k1, v1; k1.size = 6; k1.data = "hellx"; v1.size = 6; v1.data = "worlx"; basement_put(dummy_leaf->u.l.le->bsm, &k1, &v1, MSG_INSERT, msn, xids); ret = serialize_node_to_disk(fd, b, dummy_leaf, hdr); ASSERT_TRUE(ret > 0); //free node_free(dummy_leaf); struct node *dummy_leaf1; ret = deserialize_node_from_disk(fd, b, nid, &dummy_leaf1, 0); ASSERT_TRUE(ret > 0); ASSERT_EQUAL(2, basement_count(dummy_leaf1->u.l.le->bsm)); struct basement_iter iter; basement_iter_init(&iter, dummy_leaf1->u.l.le->bsm); basement_iter_seek(&iter, &k); ASSERT_EQUAL(1, iter.valid); ASSERT_STR("world", iter.val.data); basement_iter_seek(&iter, &k1); ASSERT_EQUAL(1, iter.valid); ASSERT_STR("worlx", iter.val.data); //free node_free(dummy_leaf1); ness_os_close(fd); block_free(b); xfree(hdr); options_free(opts); xcheck_all_free(); }
CTEST(node_serial_test, node_2th_part_empty) { int ret = 0; NID nid; uint32_t n_children = 3; int fd = ness_os_open(BRT_FILE, O_RDWR | O_CREAT, 0777); struct block *b = block_new(); struct hdr *hdr = (struct hdr*)xcalloc(1, sizeof(*hdr)); struct options *opts = options_new(); /* * serialize */ struct node *dummy_node; hdr->last_nid++; nid = hdr->last_nid; dummy_node = nonleaf_alloc_empty(nid, 1, n_children); nonleaf_alloc_buffer(dummy_node); struct msg p0; p0.size = 6; p0.data = "pivot0"; msgcpy(&dummy_node->u.n.pivots[0], &p0); struct msg p1; p1.size = 6; p1.data = "pivot1"; msgcpy(&dummy_node->u.n.pivots[1], &p1); MSN msn = 0U; struct xids *xids = NULL; struct msg k, v; k.size = 5; k.data = "hello"; v.size = 5; v.data = "world"; basement_put(dummy_node->u.n.parts[0].buffer, &k, &v, MSG_INSERT, msn, xids); hdr->method = NESS_QUICKLZ_METHOD; ret = serialize_node_to_disk(fd, b, dummy_node, hdr); ASSERT_TRUE(ret > 0); node_free(dummy_node); //deserialize int light = 0; struct node *dummy_node1; ret = deserialize_node_from_disk(fd, b, nid, &dummy_node1, light); ASSERT_TRUE(ret > 0); ASSERT_EQUAL(1, dummy_node1->height); ASSERT_EQUAL(3, dummy_node1->u.n.n_children); ASSERT_DATA((const unsigned char*)"pivot0", 6, (const unsigned char*)dummy_node1->u.n.pivots[0].data, dummy_node1->u.n.pivots[0].size); ASSERT_DATA((const unsigned char*)"pivot1", 6, (const unsigned char*)dummy_node1->u.n.pivots[1].data, dummy_node1->u.n.pivots[1].size); ASSERT_EQUAL(3, dummy_node1->u.n.n_children); if (!light) { int cmp; struct basement_iter iter; struct basement *bsm; bsm = dummy_node1->u.n.parts[0].buffer; basement_iter_init(&iter, bsm); int mb_c = basement_count(dummy_node1->u.n.parts[0].buffer); ASSERT_EQUAL(1, mb_c); basement_iter_seek(&iter, &k); ret = basement_iter_valid(&iter); ASSERT_EQUAL(1, ret); cmp = msg_key_compare(&k, &iter.key); ASSERT_EQUAL(0, cmp); cmp = msg_key_compare(&v, &iter.val); ASSERT_EQUAL(0, cmp); mb_c = basement_count(dummy_node1->u.n.parts[1].buffer); ASSERT_EQUAL(0, mb_c); } node_free(dummy_node1); ness_os_close(fd); block_free(b); xfree(hdr); options_free(opts); xcheck_all_free(); }
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); }
CTEST(block_test, block_get) { struct status *status = status_new(); struct block *b = block_new(status); uint32_t height = 0; uint32_t skeleton_size = 0; uint64_t nid0 = 0; DISKOFF off0 = block_alloc_off(b, nid0, 513, skeleton_size, height); /* {1024 to 2048} */ ASSERT_EQUAL(1024, off0); uint64_t nid1 = 1; off0 = block_alloc_off(b, nid1, 513, skeleton_size, height); /* {2048 to 3072} */ ASSERT_EQUAL(2048, off0); uint64_t nid2 = 2; off0 = block_alloc_off(b, nid2, 512, skeleton_size, height); /* {3072 to 3584} */ ASSERT_EQUAL(3072, off0); uint64_t nid3 = 3; off0 = block_alloc_off(b, nid3, 511, skeleton_size, height); /* {3584 to 4096} */ ASSERT_EQUAL(3584, off0); /* realloc nid1 */ nid1 = 1; off0 = block_alloc_off(b, nid1, 513, skeleton_size, height); /* * {4096 to 5120} is used * {2048 to 3072} is unused */ ASSERT_EQUAL(4096, off0); /* realloc nid2 */ nid2 = 2; off0 = block_alloc_off(b, nid2, 512, skeleton_size, height); /* * {5120 to 5632} used * {3072 to 3584} is unused */ ASSERT_EQUAL(5120, off0); block_shrink(b); uint64_t nid4 = 4; off0 = block_alloc_off(b, nid4, 511, skeleton_size, height); ASSERT_EQUAL(2048, off0); uint64_t nid5 = 5; off0 = block_alloc_off(b, nid5, 511, skeleton_size, height); ASSERT_EQUAL(2560, off0); block_free(b); status_free(status); }