1
0
mirror of https://github.com/fumiama/fumidb.git synced 2026-06-12 14:10:27 +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

@@ -10,7 +10,7 @@
#include "../include/binary.h"
#include "../include/page.h"
uint8_t nullpage[PAGESZ];
static const uint8_t nullpage[PAGESZ];
void* alloc_page(int fd) {
uint64_t ptr = 8, prev_ptr = 0, prev_prev_ptr = 0;
@@ -62,6 +62,20 @@ void* alloc_page(int fd) {
return page+8;
}
void* get_page(int fd, uint64_t ptr) {
if(ptr%PAGESZ) return NULL;
if(lseek(fd, ptr, SEEK_SET) < 0) return NULL;
void* page = malloc(PAGESZ+8);
if(page == NULL) return NULL;
putle64(page, ptr);
page += 8;
if(read(fd, page, PAGESZ) != PAGESZ) {
free(page-8);
return NULL;
}
return page;
}
int sync_page(int fd, void* page) {
uint64_t ptr = le64(page-8);
if(lseek(fd, ptr, SEEK_SET) < 0) return EOF;
@@ -181,6 +195,20 @@ void* alloc_block(int fd, uint16_t size) {
return blk+10;
}
void* get_block(int fd, uint16_t size, uint64_t ptr) {
if(lseek(fd, ptr, SEEK_SET) < 0) return NULL;
void* blk = malloc(size+10);
if(blk == NULL) return NULL;
putle64(blk, ptr);
putle16(blk+8, size);
blk += 10;
if(read(fd, blk, size) != size) {
free(blk);
return NULL;
}
return blk;
}
int sync_block(int fd, void* blk) {
uint64_t off = le64(blk-10);
uint16_t size = le16(blk-2);