1
0
mirror of https://github.com/fumiama/fumidb.git synced 2026-06-24 06:40:25 +08:00

add types

This commit is contained in:
源文雨
2022-05-01 21:16:17 +08:00
parent eda10fba91
commit 67b9176615
17 changed files with 310 additions and 26 deletions

View File

@@ -8,6 +8,14 @@
void* pages[16];
uint8_t nullpage[PAGESZ];
static uint64_t get_second_unused_block(int fd) {
uint64_t ptr = get_first_unused_block(fd);
if(!ptr) return 0;
if(lseek(fd, ptr, SEEK_SET) < 0) return 0;
readle64(fd, ptr);
return ptr;
}
int main() {
int fd = open("page_test_tmp.bin", O_RDWR | O_CREAT | O_TRUNC, 0644);
if(fd < 0) {
@@ -25,15 +33,20 @@ int main() {
}
puts("free!");
free_page(fd, pages[15]);
if(get_first_unused_block(fd) != 16*PAGESZ) return 5;
if(get_second_unused_block(fd) != 16*PAGESZ) return 5;
puts("free 15!");
free_page(fd, pages[12]);
if(get_first_unused_block(fd) != 13*PAGESZ) return 6;
if(get_second_unused_block(fd) != 13*PAGESZ) return 6;
puts("free 12!");
free_page(fd, pages[1]);
if(get_first_unused_block(fd) != 2*PAGESZ) return 7;
if(get_second_unused_block(fd) != 2*PAGESZ) return 7;
puts("free 1!");
free_page(fd, pages[10]);
if(get_first_unused_block(fd) != 2*PAGESZ) return 8;
if(get_second_unused_block(fd) != 2*PAGESZ) return 8;
puts("free 10!");
free_page(fd, pages[9]);
if(get_first_unused_block(fd) != 2*PAGESZ) return 8;
if(get_second_unused_block(fd) != 2*PAGESZ) return 8;
puts("free 9!");
pages[1] = alloc_page(fd);
if(le64(pages[1]-8) != (uint64_t)(2*PAGESZ)) {
printf("1: %016llx != %016llx\n", le64(pages[1]-8), (uint64_t)(2*PAGESZ));
@@ -72,30 +85,35 @@ int main() {
uint8_t* blk4 = alloc_block(fd, 4095);
memcpy(blk1, "hello world!", 13);
sync_block(fd, blk1);
lseek(fd, PAGESZ, SEEK_SET);
lseek(fd, HEADERSZ, SEEK_SET);
read(fd, blk2, 13);
puts("hello world!");
if(strcmp((const char *)blk2, (const char *)blk1)) {
return 16;
}
puts("hello world 1!");
sync_block(fd, blk2);
lseek(fd, PAGESZ+40, SEEK_SET);
lseek(fd, HEADERSZ+40, SEEK_SET);
read(fd, blk3, 13);
if(strcmp((const char *)blk3, (const char *)blk1)) {
return 17;
}
puts("hello world 2!");
sync_block(fd, blk3);
lseek(fd, PAGESZ+40+22, SEEK_SET);
lseek(fd, HEADERSZ+40+22, SEEK_SET);
read(fd, blk4+222, 13);
sync_block(fd, blk4);
if(strcmp((const char *)&blk4[222], (const char *)blk1)) {
return 18;
}
puts("hello world 3!");
memset(blk1, 0, 40);
lseek(fd, PAGESZ*2+222, SEEK_SET);
lseek(fd, PAGESZ+222, SEEK_SET);
read(fd, blk1, 13);
if(strcmp((const char *)blk1, (const char *)blk2)) {
return 19;
}
puts("hello world4!");
if(free_block(fd, blk1)) {
perror("free_block(fd, blk1)");
return 20;
@@ -115,7 +133,7 @@ int main() {
blk1 = alloc_block(fd, 40+22+33);
memcpy(blk1+44, "hello world!", 13);
sync_block(fd, blk1);
lseek(fd, PAGESZ+44, SEEK_SET);
lseek(fd, HEADERSZ+44, SEEK_SET);
char buf[13];
read(fd, buf, 13);
if(strcmp((const char *)&blk1[44], (const char *)buf)) {
@@ -135,26 +153,26 @@ int main() {
blk4 = alloc_block(fd, 4095);
memcpy(blk1, "hello world!", 13);
sync_block(fd, blk1);
lseek(fd, PAGESZ, SEEK_SET);
lseek(fd, HEADERSZ, SEEK_SET);
read(fd, blk2, 13);
if(strcmp((const char *)blk2, (const char *)blk1)) {
return 16;
}
sync_block(fd, blk2);
lseek(fd, PAGESZ+40, SEEK_SET);
lseek(fd, HEADERSZ+40, SEEK_SET);
read(fd, blk3, 13);
if(strcmp((const char *)blk3, (const char *)blk1)) {
return 17;
}
sync_block(fd, blk3);
lseek(fd, PAGESZ+40+22, SEEK_SET);
lseek(fd, HEADERSZ+40+22, SEEK_SET);
read(fd, blk4+222, 13);
sync_block(fd, blk4);
if(strcmp((const char *)&blk4[222], (const char *)blk1)) {
return 18;
}
memset(blk1, 0, 40);
lseek(fd, PAGESZ*2+222, SEEK_SET);
lseek(fd, PAGESZ+222, SEEK_SET);
read(fd, blk1, 13);
if(strcmp((const char *)blk1, (const char *)blk2)) {
return 19;
@@ -178,7 +196,7 @@ int main() {
blk1 = alloc_block(fd, 40+22+33);
memcpy(blk1+44, "hello world!", 13);
sync_block(fd, blk1);
lseek(fd, PAGESZ+44, SEEK_SET);
lseek(fd, HEADERSZ+44, SEEK_SET);
memset(buf, 0, 13);
read(fd, buf, 13);
if(strcmp((const char *)&blk1[44], (const char *)buf)) {