static void collect_session_watchers(zhandle_t *zh, watcher_object_list_t **list) { copy_table(zh->active_node_watchers, *list); copy_table(zh->active_exist_watchers, *list); copy_table(zh->active_child_watchers, *list); }
void collect_session_watchers(zhandle_t *zh, watcher_object_list_t **list) { #ifdef THREADED pthread_mutex_lock(&zh->active_node_watchers->lock); pthread_mutex_lock(&zh->active_exist_watchers->lock); pthread_mutex_lock(&zh->active_child_watchers->lock); #endif copy_table(zh->active_node_watchers, *list); copy_table(zh->active_exist_watchers, *list); copy_table(zh->active_child_watchers, *list); #ifdef THREADED pthread_mutex_unlock(&zh->active_node_watchers->lock); pthread_mutex_unlock(&zh->active_exist_watchers->lock); pthread_mutex_unlock(&zh->active_child_watchers->lock); #endif }
/* Initialization*/ int check_conflict(){ int row,col,val; sudoku_to_problem(); copy_table(problem,sudoku_modified); /* Initialy, any value can be put into any positions*/ /* Rows initialization*/ for(row=0; row<SIZE; ++row){ for(val=0; val<SIZE; ++val){ rows[row][val]=AVAILABLE; } } /* Column initialization*/ for(col=0; col<SIZE; ++col){ for(val=0; val<SIZE; ++val){ column[col][val]=AVAILABLE; } } /* Block initialization*/ for(row=0; row<SIZE; ++row){ for(col=0; col<SIZE/3; ++col){ for(val=0; val<SIZE/3; ++val){ block[row][col][val]=AVAILABLE; } } } /* Initialize available state of sudoku puzzles! If there is a conflict, exit program*/ for(row=0; row<SIZE; ++row){ for(col=0; col<SIZE; ++col){ val=sudoku_modified[row][col]; if(val<0){ printf("The input puzzle has an empty grid (%d,%d)\n",col+1,val+1); exit(1); } if(column[col][val]==!AVAILABLE|| rows[row][val]==!AVAILABLE|| block[row][col/3][val/3]==!AVAILABLE){ fprintf(stderr,"The input solution has a conflict.\n"); fprintf(stderr,"%d can't be in (%d,%d) grid.\n",row_index[row]+1,col+1,val+1); return 1; } column[col][val]=!AVAILABLE; rows[row][val]=!AVAILABLE; block[row][col/3][val/3]=!AVAILABLE; } } /* Initialize available state of sudoku puzzles!*/ for(row=0; row<SIZE; ++row){ for(col=0; col<SIZE; ++col){ if((val=sudoku_modified[row][col])>=0){ column[col][val]=AVAILABLE; rows[row][val]=AVAILABLE; block[row][col/3][val/3]=AVAILABLE; } } } return 0; }
// Push data into stack void push(struct stack **top, int table[][SIZE],int step){ struct stack *sp; sp=(struct stack *)malloc(sizeof(struct stack)); copy_table(sp->table, table); sp->step=step; sp->next=*top; *top=sp; }
// Push data into stack void push(struct stack **top, int *table,int step){ struct stack *sp=NULL; sp=(struct stack *)malloc(sizeof(struct stack)); // allocate memory for a new stack sp->table=new_table(); // allocate memory for a new table copy_table(sp->table, table); sp->step=step; sp->next=*top; *top=sp; }
// Pop data from stack struct data pop(struct stack **top){ struct data data; struct stack *sp; sp=*top; copy_table(data.table,(*top)->table); data.step=(*top)->step; *top=(*top)->next; free(sp); return data; }
void xen_biostable_setup(void) { struct xen_seabios_info *info = (void *)INFO_PHYSICAL_ADDRESS; void **tables = (void*)info->tables; int i; dprintf(1, "xen: copy BIOS tables...\n"); for (i=0; i<info->tables_nr; i++) copy_table(tables[i]); find_acpi_features(); }
// Pop data from stack struct data *pop(struct stack **top){ struct data *data; struct stack *sp; sp=*top; data=(struct data*)malloc(sizeof(struct data)); data->table=new_table(); copy_table(data->table,sp->table); data->step=sp->step; *top=sp->next; free(sp->table); free(sp); return data; }
struct global_state *copy_global_state(struct global_state *gstate) /* Returns: A copy of global state gstate, which includes copying global variable and module state */ { struct global_state *newp; value tmp; GCPRO1(gstate); newp = (struct global_state *)allocate_record(type_vector, 8); GCPRO1(newp); tmp = copy_table(gstate->modules); newp->modules = tmp; tmp = copy_vector(gstate->mvars); newp->mvars = tmp; tmp = copy_vector(gstate->types); newp->types = tmp; tmp = copy_vector(gstate->names); newp->names = tmp; tmp = copy_table(gstate->global); newp->global = tmp; tmp = copy_table(gstate->gsymbols); newp->gsymbols = tmp; tmp = copy_env(gstate->environment); newp->environment = tmp; newp->machine = gstate->machine; GCPOP(2); return newp; }
/* Extend path_list, depth-first*/ struct stack* extend(struct stack **path_list,int table[][SIZE],int step){ int tmp_table[SIZE][SIZE]; struct around aro; int value=pro_num_pos[step].value; struct position current_position=pro_num_pos[step].pos; for(aro.left=0; aro.left<value; ++aro.left){ for(aro.right=0; aro.right<value-aro.left; ++aro.right){ for(aro.up=0; aro.up<value-aro.left-aro.right; ++aro.up){ // assure the number of white mass is equal to value aro.down=value-1-aro.left-aro.right-aro.up; if(check(table,current_position,aro)){ copy_table(tmp_table,table); update_table(tmp_table,current_position,aro); push(path_list,tmp_table,step+1); } } } } return *path_list; }
/* Create the initial path list for solution's finding*/ void create_initial_path_list(struct stack **initial_path_list){ int tmp_table[SIZE][SIZE]; struct around aro; int value=pro_num_pos[0].value; struct position current_position=pro_num_pos[0].pos; for(aro.left=0; aro.left<value; ++aro.left){ for(aro.right=0; aro.right<value-aro.left; ++aro.right){ for(aro.up=0; aro.up<value-aro.left-aro.right; ++aro.up){ aro.down=value-1-aro.left-aro.right-aro.up; if(check(blank_table,current_position,aro)){ copy_table(tmp_table,blank_table); update_table(tmp_table,current_position,aro); push(initial_path_list,tmp_table,1); } } } } }
void save_result(int table[][SIZE]){ int row,col; if(result_flag) return; result_flag=1; copy_table(result,table); fp=fopen("result.txt","w"); for(row=0; row<SIZE; ++row){ for(col=0; col<SIZE; ++col){ if(table[row][col]>=0) fprintf(fp,"%d",table[row][col]+1); else fprintf(fp,"0"); } fprintf(fp,"\n"); } fprintf(fp,"\n"); fclose(fp); }
/* vtblcp */ static rc_t vtblcp ( const vtblcp_parms *pb, const VTable *stbl, VTable *dtbl, const Vector *v ) { const VCursor *scurs; rc_t rc = VTableCreateCursorRead ( stbl, & scurs ); if ( rc != 0 ) LOGERR ( klogInt, rc, "failed to create empty destination cursor" ); else { VCursor *dcurs; rc = VTableCreateCursorWrite ( dtbl, & dcurs, kcmInsert ); if ( rc != 0 ) LOGERR ( klogInt, rc, "failed to create empty destination cursor" ); else { uint32_t count = VectorLength ( v ); vtblcp_column_map *cm = malloc ( sizeof *cm * count ); if ( cm == NULL ) { rc = RC ( rcExe, rcVector, rcAllocating, rcMemory, rcExhausted ); LOGERR ( klogInt, rc, "failed to create column index map" ); } else { uint32_t rd_filt; rc = populate_cursors ( dtbl, dcurs, scurs, cm, v, & rd_filt ); if ( rc == 0 ) rc = copy_table ( pb, dcurs, scurs, cm, count, rd_filt ); free ( cm ); } VCursorRelease ( dcurs ); } VCursorRelease ( scurs ); } return rc; }
void init(){ int row,col,val; sudoku_to_problem(); copy_table(problem,sudoku_modified); /* Initialy, any value can be put into any positions*/ /* Rows initialization*/ for(row=0; row<SIZE; ++row){ for(val=0; val<SIZE; ++val){ rows[row][val]=AVAILABLE; } } /* Column initialization*/ for(col=0; col<SIZE; ++col){ for(val=0; val<SIZE; ++val){ column[col][val]=AVAILABLE; } } /* Block initialization*/ for(row=0; row<SIZE; ++row){ for(col=0; col<SIZE/3; ++col){ for(val=0; val<SIZE/3; ++val){ block[row][col][val]=AVAILABLE; } } } /* Initialize available state of sudoku puzzles!*/ for(row=0; row<SIZE; ++row){ for(col=0; col<SIZE; ++col){ if((val=sudoku_modified[row][col])>=0){ column[col][val]=!AVAILABLE; rows[row][val]=!AVAILABLE; block[row][col/3][val/3]=!AVAILABLE; } } } }
/* Extend path_list with all new possibilities, depth-first*/ struct stack* extend(struct stack **path_list,int *table,int step){ int *tmp_table=NULL; struct around aro; int value=number_position[step].value; tmp_table=new_table(); // allocate memory for temporary table struct position current_position=number_position[step].pos; for(aro.left=0; aro.left<value; ++aro.left){ for(aro.right=0; aro.right<value-aro.left; ++aro.right){ for(aro.up=0; aro.up<value-aro.left-aro.right; ++aro.up){ // assure the number of white grid is equal to value aro.down=value-1-aro.left-aro.right-aro.up; // if black grids can be placed around the grid in question // push the updated tatble to the path_list if(check(table,current_position,aro)){ copy_table(tmp_table,table); update_table(tmp_table,current_position,aro); push(path_list,tmp_table,step+1); } } } } free(tmp_table); return *path_list; }
Hash_Table *Copy_Hash_Table(Hash_Table *hash_table) { return ((Hash_Table *) copy_table(((Table *) hash_table))); }
lsb_lua_sandbox* lsb_create(void *parent, const char *lua_file, const char *cfg, lsb_logger logger) { if (!lua_file) { if (logger) logger(__FUNCTION__, 3, "lua_file must be specified"); return NULL; } if (!set_tz()) { if (logger) logger(__FUNCTION__, 3, "fail to set the TZ to UTC"); return NULL; } set_random_seed(); lsb_lua_sandbox *lsb = malloc(sizeof*lsb); if (!lsb) { if (logger) logger(__FUNCTION__, 3, "memory allocation failed"); return NULL; } memset(lsb->usage, 0, sizeof(lsb->usage)); #ifdef LUA_JIT lsb->lua = luaL_newstate(); #else lsb->lua = lua_newstate(memory_manager, lsb); #endif if (!lsb->lua) { if (logger) logger(__FUNCTION__, 3, "lua state creation failed"); free(lsb); return NULL; } // add the config to the lsb_config registry table lua_State *lua_cfg = load_sandbox_config(cfg, logger); if (!lua_cfg) { lua_close(lsb->lua); free(lsb); return NULL; } lua_pushnil(lua_cfg); lua_pushvalue(lua_cfg, LUA_GLOBALSINDEX); copy_table(lsb->lua, lua_cfg, logger); lua_pop(lua_cfg, 2); lua_close(lua_cfg); size_t ml = get_usage_config(lsb->lua, -1, "memory_limit"); size_t il = get_usage_config(lsb->lua, -1, "instruction_limit"); size_t ol = get_usage_config(lsb->lua, -1, "output_limit"); lua_setfield(lsb->lua, LUA_REGISTRYINDEX, LSB_CONFIG_TABLE); lua_pushcclosure(lsb->lua, &read_config, 0); lua_setglobal(lsb->lua, "read_config"); lua_pushlightuserdata(lsb->lua, (void *)lsb); lua_pushcclosure(lsb->lua, &output, 1); lua_setglobal(lsb->lua, "output"); lsb->parent = parent; lsb->usage[LSB_UT_MEMORY][LSB_US_LIMIT] = ml; lsb->usage[LSB_UT_INSTRUCTION][LSB_US_LIMIT] = il; lsb->usage[LSB_UT_OUTPUT][LSB_US_LIMIT] = ol; lsb->state = LSB_UNKNOWN; lsb->error_message[0] = 0; lsb->lua_file = malloc(strlen(lua_file) + 1); lsb->state_file = NULL; if (!lsb->lua_file || lsb_init_output_buffer(&lsb->output, ol)) { if (logger) logger(__FUNCTION__, 3, "memory allocation failed failed"); lsb_free_output_buffer(&lsb->output); free(lsb->lua_file); lua_close(lsb->lua); lsb->lua = NULL; free(lsb); return NULL; } strcpy(lsb->lua_file, lua_file); return lsb; }
static void copy_table(lua_State *sb, lua_State *cfg, lsb_logger logger) { lua_newtable(sb); lua_pushnil(cfg); while (lua_next(cfg, -2) != 0) { int kt = lua_type(cfg, -2); int vt = lua_type(cfg, -1); switch (kt) { case LUA_TNUMBER: case LUA_TSTRING: switch (vt) { case LUA_TSTRING: { size_t len; const char *tmp = lua_tolstring(cfg, -1, &len); if (tmp) { lua_pushlstring(sb, tmp, len); if (kt == LUA_TSTRING) { lua_setfield(sb, -2, lua_tostring(cfg, -2)); } else { lua_rawseti(sb, -2, (int)lua_tointeger(cfg, -2)); } } } break; case LUA_TNUMBER: lua_pushnumber(sb, lua_tonumber(cfg, -1)); if (kt == LUA_TSTRING) { lua_setfield(sb, -2, lua_tostring(cfg, -2)); } else { lua_rawseti(sb, -2, (int)lua_tointeger(cfg, -2)); } break; case LUA_TBOOLEAN: lua_pushboolean(sb, lua_toboolean(cfg, -1)); if (kt == LUA_TSTRING) { lua_setfield(sb, -2, lua_tostring(cfg, -2)); } else { lua_rawseti(sb, -2, (int)lua_tointeger(cfg, -2)); } break; case LUA_TTABLE: copy_table(sb, cfg, logger); break; default: if (logger) { logger(__FUNCTION__, 4, "skipping config value type: %s", lua_typename(cfg, vt)); } break; } break; default: if (logger) { logger(__FUNCTION__, 4, "skipping config key type: %s", lua_typename(cfg, kt)); } break; } lua_pop(cfg, 1); } switch (lua_type(cfg, -2)) { case LUA_TSTRING: lua_setfield(sb, -2, lua_tostring(cfg, -2)); break; case LUA_TNUMBER: lua_rawseti(sb, -2, (int)lua_tointeger(cfg, -2)); break; } }
static foreign_t pl_new_order_table(term_t name, term_t options) { OrdTable t = malloc(sizeof(ordtable)); term_t tail = PL_copy_term_ref(options); term_t head = PL_new_term_ref(); exact_table(t); if ( !PL_get_atom(name, &t->name) ) { free(t); return error(ERR_INSTANTIATION, "new_order_table/2", 1, name); } while(PL_get_list(tail, head, tail)) { atom_t name; int arity; if ( PL_get_name_arity(head, &name, &arity) ) { if ( name == ATOM_case_insensitive ) { case_insensitive_table(t); } else if ( name == ATOM_iso_latin_1 ) { iso_latin_1_table(t); } else if ( name == ATOM_iso_latin_1_case_insensitive ) { iso_latin_1_case_table(t); } else if ( name == ATOM_copy && arity == 1 ) { term_t a = PL_new_term_ref(); OrdTable from; _PL_get_arg(1, head, a); if ( get_order_table(a, &from) ) { copy_table(t, from); } else { free(t); return FALSE; } } else if ( arity == 1 ) { fid_t fid = PL_open_foreign_frame(); term_t a = PL_new_term_ref(); _PL_get_arg(1, head, a); if ( !parse_set(t, name, a) ) goto err1; PL_close_foreign_frame(fid); } else if ( name == ATOM_eq && arity == 2 ) { fid_t fid = PL_open_foreign_frame(); term_t c = PL_new_term_ref(); int from, to; if ( !PL_get_arg(1, head, c) || !get_char(c, &from) || !PL_get_arg(2, head, c) || !get_char(c, &to) ) { free(t); return FALSE; } ORD(t, from) = to; PL_close_foreign_frame(fid); } else goto err1; } else { err1: free(t); return error(ERR_INSTANTIATION, "new_order_table/2", 2, options); } } if ( !PL_get_nil(tail) ) goto err1; register_table(t); PL_succeed; }