mirror of
https://github.com/fumiama/fumidb.git
synced 2026-06-11 05:30:26 +08:00
add page
This commit is contained in:
66
src/binary.h
66
src/binary.h
@@ -1,66 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifdef __linux__
|
||||
# include <endian.h>
|
||||
#endif
|
||||
#ifdef __FreeBSD__
|
||||
# include <sys/endian.h>
|
||||
#endif
|
||||
#ifdef __NetBSD__
|
||||
# include <sys/endian.h>
|
||||
#endif
|
||||
#ifdef __OpenBSD__
|
||||
# include <sys/types.h>
|
||||
# define be16toh(x) betoh16(x)
|
||||
# define be32toh(x) betoh32(x)
|
||||
# define be64toh(x) betoh64(x)
|
||||
#endif
|
||||
#ifdef __MAC_10_0
|
||||
# define be16toh(x) ntohs(x)
|
||||
# define be32toh(x) ntohl(x)
|
||||
# define be64toh(x) ntohll(x)
|
||||
# define htobe16(x) ntohs(x)
|
||||
# define htobe32(x) htonl(x)
|
||||
# define htobe64(x) htonll(x)
|
||||
#endif
|
||||
#ifdef _WIN64
|
||||
#ifdef WORDS_BIGENDIAN
|
||||
# define be16toh(x) (x)
|
||||
# define be32toh(x) (x)
|
||||
# define be64toh(x) (x)
|
||||
# define htobe16(x) (x)
|
||||
# define htobe32(x) (x)
|
||||
# define htobe64(x) (x)
|
||||
#else
|
||||
# define be16toh(x) _byteswap_ushort(x)
|
||||
# define be32toh(x) _byteswap_ulong(x)
|
||||
# define be64toh(x) _byteswap_uint64(x)
|
||||
# define htobe16(x) _byteswap_ushort(x)
|
||||
# define htobe32(x) _byteswap_ulong(x)
|
||||
# define htobe64(x) _byteswap_uint64(x)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef _WIN64
|
||||
#ifdef WORDS_BIGENDIAN
|
||||
#define putle16(buf, x) (*(uint16_t*)(buf) = _byteswap_ushort((uint16_t)(x)))
|
||||
#define putle32(buf, x) (*(uint32_t*)(buf) = _byteswap_ulong((uint32_t)(x)))
|
||||
#define putle64(buf, x) (*(uint64_t*)(buf) = _byteswap_uint64((uint64_t)(x)))
|
||||
#else
|
||||
#define putle16(buf, x) (*(uint16_t*)(buf) = (uint16_t)(x))
|
||||
#define putle32(buf, x) (*(uint32_t*)(buf) = (uint32_t)(x))
|
||||
#define putle64(buf, x) (*(uint64_t*)(buf) = (uint64_t)(x))
|
||||
#endif
|
||||
#else
|
||||
#ifdef WORDS_BIGENDIAN
|
||||
#define putle16(buf, x) (*(uint16_t*)(buf) = __builtin_bswap16((uint16_t)(x)))
|
||||
#define putle32(buf, x) (*(uint32_t*)(buf) = __builtin_bswap32((uint32_t)(x)))
|
||||
#define putle64(buf, x) (*(uint64_t*)(buf) = __builtin_bswap64((uint64_t)(x)))
|
||||
#else
|
||||
#define putle16(buf, x) (*(uint16_t*)(buf) = (uint16_t)(x))
|
||||
#define putle32(buf, x) (*(uint32_t*)(buf) = (uint32_t)(x))
|
||||
#define putle64(buf, x) (*(uint64_t*)(buf) = (uint64_t)(x))
|
||||
#endif
|
||||
#endif
|
||||
38
src/page.c
Normal file
38
src/page.c
Normal file
@@ -0,0 +1,38 @@
|
||||
// page.c
|
||||
// 管理文件中的空闲块
|
||||
|
||||
#define _GNU_SOURCE /* See feature_test_macros(7) */
|
||||
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
#include "../include/binary.h"
|
||||
#include "../include/page.h"
|
||||
|
||||
uint8_t nullpage[PAGESZ];
|
||||
|
||||
void* alloc_page(int fd) {
|
||||
uint64_t ptr, prev_ptr = 8;
|
||||
void* page;
|
||||
if(lseek(fd, 8, SEEK_SET) < 0) return EOF;
|
||||
// 对于 page,只关心位于第一页 8~15 字节的 ptr of unused blk
|
||||
readle64(fd, ptr);
|
||||
while(ptr) {
|
||||
if(!(ptr%PAGESZ)) { // 找到符合要求的页
|
||||
page = mmap(NULL, PAGESZ, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, ptr);
|
||||
if(page < 0) return page;
|
||||
if(lseek(fd, prev_ptr, SEEK_SET) < 0) return EOF;
|
||||
write(fd, page, 8); // 从空闲块链表移除本块
|
||||
return page;
|
||||
}
|
||||
prev_ptr = ptr;
|
||||
if(lseek(fd, ptr, SEEK_SET) < 0) return EOF;
|
||||
readle64(fd, ptr);
|
||||
}
|
||||
ptr = lseek(fd, 0, SEEK_END);
|
||||
if(ptr < 0 || !(ptr%PAGESZ)) return EOF;
|
||||
if(write(fd, nullpage, PAGESZ) != PAGESZ) return EOF;
|
||||
return mmap(NULL, PAGESZ, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, ptr);
|
||||
}
|
||||
Reference in New Issue
Block a user