/** * Convert the XML input to INFO output. */ int process(FILE* is, FILE* os) { struct state_t state; char buf[4096]; state.parser = XML_ParserCreate(NULL); if (!state.parser) { fprintf(stderr, "Couldn't allocate memory for parser\n"); return -1; } state.depth = -1; state.os = os; state.error = 0; container_alloc(&state.game_map, &state.game_mac, &state.game_max); container_alloc(&state.reference_map, &state.reference_mac, &state.reference_max); XML_SetUserData(state.parser, &state); XML_SetElementHandler(state.parser, start_handler, end_handler); XML_SetCharacterDataHandler(state.parser, data_handler); while (1) { int done; int len; len = fread(buf, 1, sizeof(buf), is); if (ferror(is)) { process_error(&state, "", "read error"); break; } done = feof(is); if (XML_Parse(state.parser, buf, len, done) == XML_STATUS_ERROR) { process_error(&state, "", XML_ErrorString(XML_GetErrorCode(state.parser))); break; } if (done) break; } XML_ParserFree(state.parser); if (state.error) { return -1; } if (check(&state) != 0) { return -1; } container_free(&state.game_map, &state.game_mac, &state.game_max); container_free(&state.reference_map, &state.reference_mac, &state.reference_max); return 0; }
/** * This function will be called when there's no mapping found in the page structure * for the given virtual address [vaddr], e.g., by the page fault handler when * a page fault happened because the user process accessed a virtual address * that is not mapped yet. * The task of this function is to allocate a physical page and use it to register * the mapping for the virtual address with given permission. * It should return the physical page index registered in the page directory, i.e., the * return value from map_page. * In the case of error, it should return the MagicNumber. */ unsigned int alloc_page (unsigned int proc_index, unsigned int vaddr, unsigned int perm) { unsigned int page_id; if (page_id = container_alloc(proc_index)) { return map_page(proc_index, vaddr, page_id, perm); } return MagicNumber; }
/** * Allocates a page (with container_alloc) for the page table, * and registers it in page directory for the given virtual address, * and clears (set to 0) the whole page table entries for this newly mapped page table. * It returns the page index of the newly allocated physical page. * In the case when there's no physical page available, it returns 0. */ unsigned int alloc_ptbl(unsigned int proc_index, unsigned int vadr) { // TODO unsigned int i; unsigned int pi; unsigned int pde_index; pi = container_alloc(proc_index); if (pi != 0) { set_pdir_entry_by_va(proc_index, vadr, pi); pde_index = vadr / (4096 * 1024); i = 0; while (i < 1024) { rmv_ptbl_entry(proc_index, pde_index, i); i ++; } } return pi; }