static Oc_bpt_node* node_alloc(Oc_wu *wu_p) { Oc_bpt_test_node *tnode_p; Oc_bpt_node *node_p; // simple checking for ref-counts g_refcnt++; if (wu_p->po_id != 0) if (oc_bpt_test_utl_random_number(2) == 0) oc_crt_yield_task(); tnode_p = (struct Oc_bpt_test_node*) wrap_malloc(sizeof(Oc_bpt_test_node)); memset(tnode_p, 0, sizeof(Oc_bpt_test_node)); oc_crt_init_rw_lock(&tnode_p->node.lock); tnode_p->node.data = (char*) wrap_malloc(NODE_SIZE); tnode_p->node.disk_addr = oc_bpt_test_fs_alloc(); tnode_p->magic = MAGIC; // add the node into the virtual-disk vd_node_insert(tnode_p); node_p = &tnode_p->node; // Lock the node oc_utl_trk_crt_lock_write(wu_p, &node_p->lock); return node_p; }
bool test_poison(void) { size_t size; for (size = 1; size <= 16; size++) { { umm_init(); corruption_cnt = 0; char *ptr = wrap_malloc(size); ptr[size]++; wrap_free(ptr); if (corruption_cnt == 0) { printf("corruption_cnt should not be 0, but it is\n"); return false; } } { umm_init(); corruption_cnt = 0; char *ptr = wrap_calloc(1, size); ptr[-1]++; wrap_free(ptr); if (corruption_cnt == 0) { printf("corruption_cnt should not be 0, but it is\n"); return false; } } } return true; }
bool test_oom_random(void) { umm_init(); corruption_cnt = 0; void *ptrs[OOM_PTRS_CNT]; size_t size = 100; while (1) { size_t i; for (i = 0; i < OOM_PTRS_CNT; i++) { size += rand() % 40 - 10; ptrs[i] = wrap_malloc(size); if (ptrs[i] == NULL) { goto out; } } /* free some of the blocks, so we have "holes" */ for (i = 0; i < OOM_PTRS_CNT; i++) { if ((rand() % 10) <= 2) { wrap_free(ptrs[i]); } } } out: if (corruption_cnt != 0) { printf("corruption_cnt should be 0, but it is %d\n", corruption_cnt); return false; } return true; }
int main(int argc, char **argv) { if (argc < 2) error_and_exit("not enough arguments"); char* argv_exec[argc]; argv_exec[0] = argv[1]; argv_exec[1] = argv[2]; for (int i = 2; i < argc-1; ++i) { argv_exec[i] = wrap_malloc(FILDES_LEN); memset(argv_exec[i], 0, FILDES_LEN); int fildes = wrap_open(argv[i+1], O_RDONLY); sprintf(argv_exec[i], "%d", fildes); } argv_exec[argc-1] = NULL; pid_t pid = wrap_fork(); if (pid != 0) { int stat; wrap_waitpid(pid, &stat, 0); if (!(WIFEXITED(stat) && WEXITSTATUS(stat) == 0)) error_and_exit("exit status of executed program"); } else { wrap_execvp(argv_exec[0], argv_exec); _exit(EXIT_SUCCESS); } for (int i = 2; i < argc-1; ++i) free(argv_exec[i]); return EXIT_SUCCESS; }
static Oc_bpt_test_node *tnode_clone(Oc_bpt_test_node *tnode_p) { Oc_bpt_test_node *new_tnode_p; new_tnode_p = (Oc_bpt_test_node *) wrap_malloc(sizeof(Oc_bpt_test_state)); memset(new_tnode_p, 0, sizeof(Oc_bpt_test_state)); new_tnode_p->node.disk_addr = tnode_p->node.disk_addr; new_tnode_p->node.data = wrap_malloc(NODE_SIZE); memcpy(new_tnode_p->node.data, tnode_p->node.data, NODE_SIZE); oc_crt_init_rw_lock(&new_tnode_p->node.lock); new_tnode_p->magic = MAGIC; return new_tnode_p; }
Oc_bpt_test_state *oc_bpt_test_utl_btree_init(struct Oc_wu *wu_p, uint64 tid) { Oc_bpt_test_state *s_p; s_p = (Oc_bpt_test_state *) wrap_malloc(sizeof(Oc_bpt_test_state)); oc_bpt_init_state_b(NULL, &s_p->bpt_s, &cfg, tid); oc_bpt_alt_init_state_b(NULL, &s_p->alt_s, &alt_cfg); return s_p; }
char * wrap_strdup(WRAPPERS_ARGS, const char *s) { char *ptr; assert(file && function); if (!s) WRAPPERS_ERR_INVALID_PARAMETERS("strdup"); ptr = wrap_malloc(file, function, line, strlen(s) + 1); strcpy(ptr, s); return ptr; }
Oc_xt_node* oc_xt_test_nd_alloc(Oc_wu *wu_p) { Oc_xt_test_node *tnode_p; // simple checking for ref-counts g_refcnt++; if (wu_p->po_id != 0) if (random_choose(2) == 0) oc_crt_yield_task(); tnode_p = (struct Oc_xt_test_node*) wrap_malloc(sizeof(Oc_xt_test_node)); memset(tnode_p, 0, sizeof(Oc_xt_test_node)); oc_crt_init_rw_lock(&tnode_p->node.lock); tnode_p->node.data = (char*) wrap_malloc(OC_XT_TEST_ND_SIZE); tnode_p->node.disk_addr = fs_alloc(); tnode_p->magic = MAGIC; // add the node into the virtual-disk vd_node_insert(tnode_p); return &tnode_p->node; }
bool test_integrity_check(void) { size_t size; for (size = 1; size <= 16; size++) { { umm_init(); corruption_cnt = 0; char *ptr = wrap_malloc(size); memset(ptr, 0xfe, size + 8 /* size of umm_block*/); /* * NOTE: we don't use wrap_free here, because we've just corrupted the * heap, and gathering of the umm info on corrupted heap can cause * segfault */ umm_free(ptr); if (corruption_cnt == 0) { printf("corruption_cnt should not be 0, but it is\n"); return false; } } { umm_init(); corruption_cnt = 0; char *ptr = wrap_calloc(1, size); ptr[-1]++; /* * NOTE: we don't use wrap_free here, because we've just corrupted the * heap, and gathering of the umm info on corrupted heap can cause * segfault */ umm_free(ptr); if (corruption_cnt == 0) { printf("corruption_cnt should not be 0, but it is\n"); return false; } } } return true; }
bool random_stress(void) { void *ptr_array[256]; size_t i; int idx; corruption_cnt = 0; printf("Size of umm_heap is %u\n", (unsigned int) sizeof(test_umm_heap)); umm_init(); umm_info(NULL, 1); for (idx = 0; idx < 256; ++idx) ptr_array[idx] = (void *) NULL; for (idx = 0; idx < 100000; ++idx) { i = rand() % 256; /* try to realloc some pointer to deliberately too large value */ { void *tmp = wrap_realloc(ptr_array[i], UMM_MALLOC_CFG__HEAP_SIZE); if (tmp != NULL) { printf("realloc to too large buffer should return NULL"); return false; } } switch (rand() % 16) { case 0: case 1: case 2: case 3: case 4: case 5: case 6: { ptr_array[i] = wrap_realloc(ptr_array[i], 0); break; } case 7: case 8: { size_t size = rand() % 40; ptr_array[i] = wrap_realloc(ptr_array[i], size); memset(ptr_array[i], 0xfe, size); break; } case 9: case 10: case 11: case 12: { size_t size = rand() % 100; ptr_array[i] = wrap_realloc(ptr_array[i], size); memset(ptr_array[i], 0xfe, size); break; } case 13: case 14: { size_t size = rand() % 200; wrap_free(ptr_array[i]); ptr_array[i] = wrap_calloc(1, size); if (ptr_array[i] != NULL) { int a; for (a = 0; a < size; a++) { if (((char *) ptr_array[i])[a] != 0x00) { printf("calloc returned non-zeroed memory\n"); return false; } } } memset(ptr_array[i], 0xfe, size); break; } default: { size_t size = rand() % 400; wrap_free(ptr_array[i]); ptr_array[i] = wrap_malloc(size); memset(ptr_array[i], 0xfe, size); break; } } } return (corruption_cnt == 0); }
void AnimMesh::load(char *fn) { s_corefile f; Face *facelist; TVertex *tvertex1; int id, flags, animflags; int numtfaces; int vdatasize = 0; int i, j; numvertices = 0; numfacegroups = 0; numuvchannels = 0; // empty out numfaces[] for (i = 0; i < MAXFACEGROUPS; i++) numfaces[i] = 0; corefile_open(fn, &f, FILE_READ); // === Read ID === corefile_read(&id, 4, 1, &f); corefile_read(&numvertices, 2, 1, &f); corefile_read(&numfacegroups, 2, 1, &f); corefile_read(&numuvchannels, 2, 1, &f); corefile_read(&flags, 2, 1, &f); // === Read vertices === vertex = (Vertex*)wrap_malloc(numvertices * sizeof(Vertex)); for (i = 0; i < numvertices; i++) { corefile_read(&vertex[i].x, 4, 1, &f); corefile_read(&vertex[i].y, 4, 1, &f); corefile_read(&vertex[i].z, 4, 1, &f); } // === read faces === for (j = 0; j < numfacegroups; j++) { corefile_read(&numfaces[j], 2, 1, &f); corefile_read(&matid[j], 2, 1, &f); //TraceDebug( "numfaces: %i", numfaces[j]); face[j] = (Face*)wrap_malloc(numfaces[j] * sizeof(Face)); facelist = (Face*)face[j]; for (i = 0; i < numfaces[j]; i++) { corefile_read(&facelist[i].a, 2, 1, &f); corefile_read(&facelist[i].b, 2, 1, &f); corefile_read(&facelist[i].c, 2, 1, &f); corefile_read(&facelist[i].flags, 2, 1, &f); } } // empty out numtvertices[] for (int x = 0; x < MAXUVCHANNELS; x++) numtvertices[x] = 0; // === read UV's === for (j = 0; j < numuvchannels; j++) { corefile_read(&numtvertices[j], 2, 1, &f); corefile_read(&numtfaces, 2, 1, &f); tvertex[j] = (TVertex*)wrap_malloc(numtvertices[j] * sizeof(TVertex)); tvertex1 = (TVertex*)tvertex[j]; for (i = 0; i < numtvertices[j]; i++) { corefile_read(&tvertex1[i].u, 4, 1, &f); corefile_read(&tvertex1[i].v, 4, 1, &f); } facelist = (Face*)face[0]; for (i = 0; i < numfaces[0]; i++) { // was: numtfaces if (j == 0) { corefile_read(&facelist[i].ta, 2, 1, &f); corefile_read(&facelist[i].tb, 2, 1, &f); corefile_read(&facelist[i].tc, 2, 1, &f); } else { corefile_read(&facelist[i].ta2, 2, 1, &f); corefile_read(&facelist[i].tb2, 2, 1, &f); corefile_read(&facelist[i].tc2, 2, 1, &f); } } } // === Read animation data === if (flags & 4) { // numanimations = 0; corefile_read(&numanimations, 2, 1, &f); corefile_read(&animtype, 2, 1, &f); corefile_read(&animflags, 2, 1, &f); TraceDebug("animation found! %i animations", numanimations);; for (j = 0; j < numanimations; j++) { anim[j].numframes = 0; anim[j].speed = 0; anim[j].loop = 0; } for (j = 0; j < numanimations; j++) { //corefile_read(anim[j].name,16,1,&f); corefile_read(&anim[j].numframes, 2, 1, &f); corefile_read(&anim[j].speed, 2, 1, &f); corefile_read(&anim[j].loop, 2, 1, &f); //anim[j].speed*=4; anim[j].numframes++; TraceDebug("anim: %i", j, "numframes: %i", anim[j].numframes, " ,speed: %i", anim[j].speed, " , loop: %i", anim[j].loop);; vdatasize = anim[j].numframes * numvertices * sizeof(Vertex); anim[j].vdata = (Vertex*)(wrap_malloc(vdatasize)); corefile_read(anim[j].vdata, vdatasize, 1, &f); } } // animation corefile_close(&f); lastvdata = vertex; TraceDebug("- animmesh loaded"); //getchar(); }
/////////////////////////////////////////////////////////////////////////// /// @brief Load a model into memory via a file. /// @param fn File (name) to open /////////////////////////////////////////////////////////////////////////// void Mesh::load(char *fn) { s_corefile f; Face *facelist; TVertex *tvertex1; int id, flags; int numtfaces; int i, j; numvertices = 0; numfacegroups = 0; numuvchannels = 0; corefile_open(fn, &f, FILE_READ); // === Read ID === corefile_read(&id, 4, 1, &f); corefile_read(&numvertices, 2, 1, &f); corefile_read(&numfacegroups, 2, 1, &f); corefile_read(&numuvchannels, 2, 1, &f); corefile_read(&flags, 2, 1, &f); // === Read vertices === vertex = (Vertex *)wrap_malloc(numvertices * sizeof(Vertex)); assert(vertex && "Vertex allocation failed"); for (i = 0; i < numvertices; i++) { corefile_read(&vertex[i].x, 4, 1, &f); corefile_read(&vertex[i].y, 4, 1, &f); corefile_read(&vertex[i].z, 4, 1, &f); } for (i = 0; i < numfacegroups; i++) numfaces[i] = 0; // === read faces === for (j = 0; j < numfacegroups; j++) { corefile_read(&numfaces[j], 2, 1, &f); corefile_read(&matid[j], 2, 1, &f); face[j] = (Face *)wrap_malloc(numfaces[j] * sizeof(Face)); facelist = (Face *)face[j]; for (i = 0; i < numfaces[j]; i++) { corefile_read(&facelist[i].a, 2, 1, &f); corefile_read(&facelist[i].b, 2, 1, &f); corefile_read(&facelist[i].c, 2, 1, &f); corefile_read(&facelist[i].flags, 2, 1, &f); } } // === read UV's === for (j = 0; j < numuvchannels; j++) { corefile_read(&numtvertices[j], 2, 1, &f); corefile_read(&numtfaces, 2, 1, &f); tvertex[j] = (TVertex *)wrap_malloc(numtvertices[j] * sizeof(TVertex)); tvertex1 = (TVertex *)tvertex[j]; for (i = 0; i < numtvertices[j]; i++) { corefile_read(&tvertex1[i].u, 4, 1, &f); corefile_read(&tvertex1[i].v, 4, 1, &f); } facelist = (Face *)face[0]; for (i = 0; i < numfaces[0]; i++) { if (j == 0) { corefile_read(&facelist[i].ta, 2, 1, &f); corefile_read(&facelist[i].tb, 2, 1, &f); corefile_read(&facelist[i].tc, 2, 1, &f); } else { corefile_read(&facelist[i].ta2, 2, 1, &f); corefile_read(&facelist[i].tb2, 2, 1, &f); corefile_read(&facelist[i].tc2, 2, 1, &f); } } } corefile_close(&f); build(); }
/////////////////////////////////////////////////////////////////////////// /// @brief Create a grid. /// @param xres Size of the terrain's texture in width. /// @param yres Size of the terrain's texture in height. /// @param tilewidth Size of a tile in width. /// @param tileheight Size of a tile in height. /// @param texturesize Size of the texture. /////////////////////////////////////////////////////////////////////////// void TerrainMesh::addGrid (int xres, int yres, int tilewidth, int tileheight, int texturesize) { mWidth = xres; mHeight = yres; mTileSize = tilewidth; Face *faceptr; int x, y, pos, i; numvertices = (xres) * (yres); numtvertices[0] = (xres) * (yres); numtvertices[1] = 4; numfacegroups = 1; numfaces[0] = (xres - 1) * (yres - 1) * 2; numuvchannels = 2; //totalfaces=(xres*yres)*2; vertex = (Vertex *) (wrap_malloc (numvertices * sizeof (Vertex))); //object->normal = (s_vertex *)(wrap_malloc(numvertices*sizeof(s_vertex))); tvertex[0] = (TVertex *) (wrap_malloc (numtvertices[0] * sizeof (TVertex))); tvertex[1] = (TVertex *) (wrap_malloc (numtvertices[1] * sizeof (TVertex))); face[0] = (Face *) (wrap_malloc (numfaces[0] * sizeof (Face))); if ((xres > 0) && (yres > 0)) { // generate vertices pos = 0; for (y = 0; y < yres; y++) { for (x = 0; x < xres; x++) { vertex[pos].x = x * tilewidth; vertex[pos].y = 0; vertex[pos].z = y * tileheight; tvertex[0][pos].u = (float) (x + 1) / (texturesize); tvertex[0][pos].v = (float) (y + 1) / (texturesize); pos++; } } // generate uv2 tvertex[1][0].u = 0; tvertex[1][0].v = 0; tvertex[1][1].u = 1; tvertex[1][1].v = 0; tvertex[1][2].u = 0; tvertex[1][2].v = 1; tvertex[1][3].u = 1; tvertex[1][3].v = 1; // generate faces pos = 0; for (y = 0; y < yres - 1; y++) { for (x = 0; x < xres - 1; x++) { face[0][pos].a = (y * xres) + x; face[0][pos].c = (y * xres) + x + 1; face[0][pos].b = ( (y + 1) * xres) + x + 1; face[0][pos].ta2 = 0; face[0][pos].tc2 = 1; face[0][pos].tb2 = 3; pos++; face[0][pos].a = (y * xres) + x; face[0][pos].c = ( (y + 1) * xres) + x + 1; face[0][pos].b = ( (y + 1) * xres) + x; face[0][pos].ta2 = 0; face[0][pos].tc2 = 3; face[0][pos].tb2 = 2; pos++; } } } faceptr = (Face *) (face[0]); for (i = 0; i < numfaces[0]; i++) { faceptr->ta = faceptr->a; faceptr->tb = faceptr->b; faceptr->tc = faceptr->c; faceptr++; } build(); }