int main(int argc, char* argv[]) { fprintf(stderr, "\n\nTesting two puts, a get, a del and two gets\n\n"); hd_t hd; hdt_t key1 = hdt_string(0, "foo"), value1 = hdt_string(0, "bar"); hdt_t key2 = hdt_string(0, "pun"), value2 = hdt_string(0, "ach"); char mem[100]; hdt_t rvalue = { mem, 100 }; memset(area, 0, MEMSIZE); hd_init_mem(&hd, 0, (void*)area, MEMSIZE); CHECK(hd_put(&hd, &key1, &value1, 0)); CHECK(hd_put(&hd, &key2, &value2, 0)); hd_debug(&hd); debug_mem(area, 512); CHECK(hd_get(&hd, &key1, &rvalue)); fprintf(stderr, "Received '%-.*s'\n", rvalue.size, (char*)(rvalue.data)); CHECK(hd_del(&hd, &key1, &rvalue)); hd_debug(&hd); debug_mem(area, 512); if (hd_get(&hd, &key1, &rvalue) != HDERR_NOTFOUND) { return ~0; } CHECK(hd_get(&hd, &key2, &rvalue)); fprintf(stderr, "Received '%-.*s'\n", rvalue.size, (char*)(rvalue.data)); return 0; }
int main(int argc, char* argv[]) { fprintf(stderr, "\n\nTesting rehash\n\n"); hd_t hd; hdt_t key, value; memset(area, 0, MEMSIZE); hd_init_mem(&hd, 0, (void*)area, MEMSIZE); int i=0; for (; i<30; i++) { char keymem[3], valmem[3]; keymem[0] = alphabet[i % 7]; keymem[1] = alphabet[i % 13]; keymem[2] = alphabet[i % 23]; valmem[0] = alphabet[25 - (i % 7)]; valmem[1] = alphabet[25 - (i % 13)]; valmem[2] = alphabet[25 - (i % 23)]; key = (hdt_t) { keymem, 3 }; value = (hdt_t) { valmem, 3 }; fprintf(stderr, "\nPutting %-.*s -> %-.*s\n", 3, keymem, 3, valmem); CHECK(hd_put(&hd, &key, &value, 0)); hd_debug(&hd); } hd_debug(&hd); debug_mem(area, 1024); return 0; }
// bad implementation, without quite important error-checking, // may not work in real-world hard drive int disk_read(const disk_t *disk, uint32 logic_sector, unsigned char *buf) { if ( buf == NULL ) return -1; struct hd_chs_struct chs = { 0, 0, 0 }; chs.cylinder = cylinder_from_lba(logic_sector, &disk->disk_cap); chs.head = head_from_lba(logic_sector, &disk->disk_cap); chs.sect = sector_from_lba(logic_sector, &disk->disk_cap); hd_debug( "chs(c %d, h %d, s %d)\n", chs.cylinder, chs.head, chs.sect ); byte status = inb( HD_PORT_STATUS ); // hd_debug( "drive status: %x\n", status ); #if 0 /* only VMWare can pass this test, so comment it out */ while ( !(status & HD_STATUS_DRIVE_READY) ) { hd_debug( "drive is not ready\n" ); delay(1); status = inb( HD_PORT_STATUS ); hd_debug( "drive status: %x\n", status ); } #endif while ( (status & HD_STATUS_BUSY) ) { delay(1); hd_debug( "drive is busy\n" ); } // select drive 0 / head outb( 0b10100000 | (chs.head & 0x0f), HD_PORT_DRV_HEAD ); outb( 1, HD_PORT_SECT_COUNT ); outb( chs.sect, HD_PORT_SECT_NUM ); outb( chs.cylinder, HD_PORT_CYL_LOW ); outb( chs.cylinder >> 8, HD_PORT_CYL_HIGH ); outb( READ_WITH_RETRY, HD_PORT_COMMAND ); // cause we choose drive above, which requires a little time inb( HD_PORT_STATUS ); inb( HD_PORT_STATUS ); inb( HD_PORT_STATUS ); inb( HD_PORT_STATUS ); status = inb( HD_PORT_STATUS ); if ( status & HD_STATUS_ERROR ) { early_kprint( PL_ERROR, "read error\n" ); return -HD_STATUS_ERROR; } if ( status & HD_STATUS_DRQ ) { __asm__ __volatile__ ( "pushl %%es \n\t" "movl %%ds, %%eax \n\t" "movl %%eax, %%es \n\t" "cld \n\t" "rep insl \n\t" "popl %%es \n\t" ::"d"(HD_PORT_DATA), "D"(buf), "c"(512>>2) : "eax" ); } else {