static int wordsplit_finish (struct wordsplit *wsp) { struct wordsplit_node *p; size_t n; n = 0; for (p = wsp->ws_head; p; p = p->next) n++; if (alloc_space (wsp, n + 1)) return 1; for (p = wsp->ws_head; p; p = p->next) { const char *str = wsnode_ptr (wsp, p); size_t slen = wsnode_len (p); char *newstr = malloc (slen + 1); /* Assign newstr first, even if it is NULL. This way wordsplit_free will work even if we return nomem later. */ wsp->ws_wordv[wsp->ws_offs + wsp->ws_wordc] = newstr; if (!newstr) return _wsplt_nomem (wsp); memcpy (newstr, str, slen); newstr[slen] = 0; wsp->ws_wordc++; } wsp->ws_wordv[wsp->ws_offs + wsp->ws_wordc] = NULL; return 0; }
void test_space_manager_4(void) { container_handle_t *ct; object_handle_t *obj; uint64_t start_blk; int32_t ret; CU_ASSERT(ofs_create_container("sm", 1000, &ct) == 0); CU_ASSERT(ofs_create_object(ct, TEST_OBJID, FLAG_TABLE | CR_EXTENT | (CR_EXTENT << 4), &obj) == 0); // test free_space with discontinuous space CU_ASSERT(free_space(obj, 100, 50) == 0); CU_ASSERT(free_space(obj, 80, 19) == 0); CU_ASSERT(free_space(obj, 151, 29) == 0); ret = alloc_space(obj, 80, 100, &start_blk); CU_ASSERT(start_blk == 80); CU_ASSERT(ret == 19); ret = alloc_space(obj, 80, 100, &start_blk); CU_ASSERT(start_blk == 100); CU_ASSERT(ret == 50); ret = alloc_space(obj, 80, 100, &start_blk); CU_ASSERT(start_blk == 151); CU_ASSERT(ret == 29); // test free_space with continuous space CU_ASSERT(free_space(obj, 100, 50) == 0); CU_ASSERT(free_space(obj, 80, 20) == 0); CU_ASSERT(free_space(obj, 150, 30) == 0); ret = alloc_space(obj, 80, 100, &start_blk); CU_ASSERT(start_blk == 80); CU_ASSERT(ret == 100); // test free_space with continuous space CU_ASSERT(free_space(obj, 100, 50) == 0); CU_ASSERT(free_space(obj, 80, 10) == 0); CU_ASSERT(free_space(obj, 160, 20) == 0); CU_ASSERT(free_space(obj, 90, 10) == 0); CU_ASSERT(free_space(obj, 150, 10) == 0); ret = alloc_space(obj, 80, 100, &start_blk); CU_ASSERT(start_blk == 80); CU_ASSERT(ret == 100); CU_ASSERT(ofs_close_object(obj) == 0); CU_ASSERT(ofs_close_container(ct) == 0); }
NNLayer::NNLayer (size_t num_input, size_t num_neuron) : LearnModel(num_input, num_neuron) { alloc_space(); winit_min = -1; winit_max = 1; quick_tanh_setup(); }
/*----------------------------------------------------------------------------- * patch a value at a position, or append to the end of the code area * the patch address is relative to current module and current section * and is incremented after store *----------------------------------------------------------------------------*/ void patch_value( int addr, int value, int num_bytes ) { Byte *buffer; init_module(); buffer = alloc_space( addr, num_bytes ); while ( num_bytes-- > 0 ) { *buffer++ = value & 0xFF; value >>= 8; } }
/****************************************************************** * Copy constructor * ******************************************************************/ pcheck_mat_dense::pcheck_mat_dense(pcheck_mat& rhs) { this->n = rhs.get_n(); this->k = rhs.get_k(); alloc_space(); /* copy data */ for(int i = 0; i < (n - k); i++){ for(int j = 0; j < n; j++){ H[i][j] = rhs.get(i,j); } } }
NNLayer::NNLayer (NNLayer &l) : LearnModel(l) { alloc_space(); for (size_t i = 0; i < n_neuron; i++) { rv_copy(weight[i], l.weight[i], n_input+1); rv_copy(wbackup[i], l.wbackup[i], n_input+1); rv_copy(wdelta[i], l.wdelta[i], n_input+1); rv_copy(wdir[i], l.wdir[i], n_input+1); rv_copy(wdel_old[i], l.wdel_old[i], n_input+1); } rv_copy(sig_der, l.sig_der, n_neuron); winit_min = l.winit_min; winit_max = l.winit_max; }
/****************************************************************** * Function to load matrix H from file * ******************************************************************/ int pcheck_mat_dense::load(char* filename) { ifstream fh; int tmp; fh.open(filename); if(fh.fail()){ cout<<"File not found."<<endl; return -1; } /* free previous content */ if(H != NULL){ free_space(); } /* read k and n */ fh >> tmp; if(tmp <= 0) { cout<<"Wrong value of k."<<endl; return -1; } k = tmp; fh >> tmp; if(tmp <= 0 || tmp<=k){ cout<<"Wrong value of n."<<endl; return -1; } n = tmp; alloc_space(); for(int i = 0; i < n - k; i++){ for(int j = 0; j < n; j++){ fh >> tmp; if(tmp==1) H[i][j] = true; else if(tmp==0) H[i][j] = false; else { cout<<"Wrong matrix data."<<endl; return -1; } } } fh.close(); return 0; }
// no space void test_space_manager_1(void) { container_handle_t *ct; object_handle_t *obj; uint64_t start_blk; CU_ASSERT(ofs_create_container("sm", 1000, &ct) == 0); CU_ASSERT(ofs_create_object(ct, TEST_OBJID, FLAG_TABLE | CR_EXTENT | (CR_EXTENT << 4), &obj) == 0); CU_ASSERT(alloc_space(obj, 100, 50, &start_blk) == -INDEX_ERR_NO_FREE_BLOCKS); // no space now CU_ASSERT(ofs_close_object(obj) == 0); CU_ASSERT(ofs_close_container(ct) == 0); CU_ASSERT(ofs_open_container("sm", &ct) == 0); CU_ASSERT(ofs_close_container(ct) == 0); }
/****************************************************************** * Assignment operator * ******************************************************************/ pcheck_mat_dense& pcheck_mat_dense::operator = (pcheck_mat& rhs) { /* free previous content */ if(H != NULL){ free_space(); } this->n = rhs.get_n(); this->k = rhs.get_k(); alloc_space(); /* copy data */ for(int i = 0; i < (n - k); i++){ for(int j = 0; j < n; j++){ H[i][j] = rhs.get(i,j); } } return *this; }
BOOL NNLayer::load_model (FILE *f) { assert(f != NULL); free_space(); ignore_comment(f); if (fscanf(f, "%u %u %lf %lf\n", &n_input, &n_neuron, &winit_min, &winit_max) != 4) return FALSE; if (winit_min >= winit_max) return FALSE; if (n_input <= 0 || n_neuron <= 0) return FALSE; alloc_space(); for (size_t i = 0; i < n_neuron; i++) for (size_t j = 0; j <= n_input; j++) if (fscanf(f, "%lf", &weight[i][j]) != 1) return FALSE; return TRUE; }
void test_space_manager_3(void) { container_handle_t *ct; object_handle_t *obj; uint64_t start_blk; int32_t ret; CU_ASSERT(ofs_create_container("sm", 1000, &ct) == 0); CU_ASSERT(ofs_create_object(ct, TEST_OBJID, FLAG_TABLE | CR_EXTENT | (CR_EXTENT << 4), &obj) == 0); CU_ASSERT(free_space(obj, 100, 50) == 0); ret = alloc_space(obj, 110, 20, &start_blk); // will alloc 110, 20 CU_ASSERT(start_blk == 110); CU_ASSERT(ret == 20); ret = alloc_space(obj, 130, 5, &start_blk); // will alloc 130, 5 CU_ASSERT(start_blk == 130); CU_ASSERT(ret == 5); ret = alloc_space(obj, 135, 30, &start_blk); // will alloc 135, 15 CU_ASSERT(start_blk == 135); CU_ASSERT(ret == 15); ret = alloc_space(obj, 150, 4, &start_blk); // will alloc 100, 4 CU_ASSERT(start_blk == 100); CU_ASSERT(ret == 4); ret = alloc_space(obj, 104, 4, &start_blk); // will alloc 104, 4 CU_ASSERT(start_blk == 104); CU_ASSERT(ret == 4); ret = alloc_space(obj, 108, 4, &start_blk); // will alloc 108, 2 CU_ASSERT(start_blk == 108); CU_ASSERT(ret == 2); ret = alloc_space(obj, 112, 4, &start_blk); // no space CU_ASSERT(ret == -INDEX_ERR_NO_FREE_BLOCKS); CU_ASSERT(free_space(obj, 100, 50) == 0); CU_ASSERT(ofs_close_object(obj) == 0); CU_ASSERT(ofs_close_container(ct) == 0); CU_ASSERT(ofs_open_container("sm", &ct) == 0); CU_ASSERT(ofs_close_container(ct) == 0); }
/* append binary contents of file, whole file if num_bytes < 0 */ void patch_file_contents( FILE *file, int addr, long num_bytes ) { long start_ptr; Byte *buffer; init_module(); /* get bin file size */ if ( num_bytes < 0 ) { start_ptr = ftell( file ); fseek( file, 0, SEEK_END ); /* file pointer to end of file */ num_bytes = ftell( file ) - start_ptr; fseek( file, start_ptr, SEEK_SET ); /* file pointer to original position */ } if ( num_bytes > 0 ) { buffer = alloc_space( addr, num_bytes ); xfget_chars( file, (char *) buffer, num_bytes ); } }
// allocate all space by one/two allocation action void test_space_manager_2(void) { container_handle_t *ct; object_handle_t *obj; uint64_t start_blk; int32_t ret; CU_ASSERT(ofs_create_container("sm", 1000, &ct) == 0); CU_ASSERT(ofs_create_object(ct, TEST_OBJID, FLAG_TABLE | CR_EXTENT | (CR_EXTENT << 4), &obj) == 0); CU_ASSERT(free_space(obj, 100, 50) == 0); ret = alloc_space(obj, 10, 80, &start_blk); // will alloc 100, 50 CU_ASSERT(start_blk == 100); CU_ASSERT(ret == 50); ret = alloc_space(obj, 10, 80, &start_blk); // no space now CU_ASSERT(ret == -INDEX_ERR_NO_FREE_BLOCKS); CU_ASSERT(free_space(obj, 100, 50) == 0); ret = alloc_space(obj, 10, 90, &start_blk); // will alloc 100, 50 CU_ASSERT(start_blk == 100); CU_ASSERT(ret == 50); ret = alloc_space(obj, 10, 90, &start_blk); // no space now CU_ASSERT(ret == -INDEX_ERR_NO_FREE_BLOCKS); CU_ASSERT(free_space(obj, 100, 50) == 0); ret = alloc_space(obj, 10, 100, &start_blk); // will alloc 100, 50 CU_ASSERT(start_blk == 100); CU_ASSERT(ret == 50); ret = alloc_space(obj, 10, 100, &start_blk); // no space now CU_ASSERT(ret == -INDEX_ERR_NO_FREE_BLOCKS); CU_ASSERT(free_space(obj, 100, 50) == 0); ret = alloc_space(obj, 10, 200, &start_blk); // will alloc 100, 50 CU_ASSERT(start_blk == 100); CU_ASSERT(ret == 50); ret = alloc_space(obj, 10, 200, &start_blk); // no space now CU_ASSERT(ret == -INDEX_ERR_NO_FREE_BLOCKS); CU_ASSERT(free_space(obj, 100, 50) == 0); ret = alloc_space(obj, 100, 50, &start_blk); // will alloc 100, 50 CU_ASSERT(start_blk == 100); CU_ASSERT(ret == 50); ret = alloc_space(obj, 100, 50, &start_blk); // no space now CU_ASSERT(ret == -INDEX_ERR_NO_FREE_BLOCKS); CU_ASSERT(free_space(obj, 100, 50) == 0); ret = alloc_space(obj, 100, 60, &start_blk); // will alloc 100, 50 CU_ASSERT(start_blk == 100); CU_ASSERT(ret == 50); ret = alloc_space(obj, 100, 60, &start_blk); // no space now CU_ASSERT(ret == -INDEX_ERR_NO_FREE_BLOCKS); CU_ASSERT(free_space(obj, 100, 50) == 0); ret = alloc_space(obj, 120, 60, &start_blk); // will alloc 120, 30 CU_ASSERT(start_blk == 120); CU_ASSERT(ret == 30); ret = alloc_space(obj, 120, 60, &start_blk); // will alloc 100, 20 CU_ASSERT(start_blk == 100); CU_ASSERT(ret == 20); ret = alloc_space(obj, 120, 60, &start_blk); // no space now CU_ASSERT(ret == -INDEX_ERR_NO_FREE_BLOCKS); CU_ASSERT(free_space(obj, 100, 50) == 0); ret = alloc_space(obj, 150, 60, &start_blk); // will alloc 100, 50 CU_ASSERT(start_blk == 100); CU_ASSERT(ret == 50); ret = alloc_space(obj, 150, 60, &start_blk); // no space now CU_ASSERT(ret == -INDEX_ERR_NO_FREE_BLOCKS); CU_ASSERT(free_space(obj, 100, 50) == 0); ret = alloc_space(obj, 160, 60, &start_blk); // will alloc 100, 50 CU_ASSERT(start_blk == 100); CU_ASSERT(ret == 50); ret = alloc_space(obj, 160, 60, &start_blk); // no space now CU_ASSERT(ret == -INDEX_ERR_NO_FREE_BLOCKS); CU_ASSERT(ofs_close_object(obj) == 0); CU_ASSERT(ofs_close_container(ct) == 0); CU_ASSERT(ofs_open_container("sm", &ct) == 0); CU_ASSERT(ofs_close_container(ct) == 0); }
pcheck_mat_dense::pcheck_mat_dense(int n, int k) { this->n = n; this->k = k; alloc_space(); }
int main(int argc, char **argv) { #ifdef THREADS int my_index; int *my_index_p; pthread_key_create (&index_key, (void*)free_registers); #endif #ifdef THREADS my_index_p = (int *)malloc (sizeof (int)); *my_index_p = get_next_index(); pthread_setspecific (index_key, (void*)my_index_p); my_index_p = pthread_getspecific(index_key); my_index = *my_index_p; gc_ready[my_index] = 0; /* inc_next_index();*/ value_stack_array[my_index] = (oakstack*)malloc (sizeof (oakstack)); cntxt_stack_array[my_index] = (oakstack*)malloc(sizeof (oakstack)); value_stack.size = 1024; value_stack.filltarget = 1024/2; context_stack.size = 512; context_stack.filltarget = 512/2; gc_examine_ptr = gc_examine_buffer; #endif parse_cmd_line(argc, argv); init_weakpointer_tables(); init_stacks(); read_world(world_file_name); new_space.size = e_next_newspace_size = original_newspace_size; alloc_space(&new_space, new_space.size); free_point = new_space.start; #ifdef THREADS register_array[my_index] = (register_set_t*)malloc(sizeof(register_set_t)); #else reg_set = (register_set_t*)malloc(sizeof(register_set_t)); #endif /* Set the registers to the boot code */ e_current_method = e_boot_code; e_env = REF_TO_PTR(REF_SLOT(e_current_method, METHOD_ENV_OFF)); e_code_segment = REF_SLOT(e_current_method, METHOD_CODE_OFF); e_pc = CODE_SEG_FIRST_INSTR(e_code_segment); /* Put a reasonable thing in e_bp to avoid confusing GC */ e_bp = e_env; /* Tell the boot function the truth */ e_nargs = 0; /* Big virtual machine interpreter loop */ loop(INT_TO_REF(54321)); return 0; }
/* advance code pointer reserving space, return address of start of buffer */ Byte *append_reserve( int num_bytes ) { init_module(); return alloc_space( get_cur_module_size(), num_bytes ); }