-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage.c
85 lines (71 loc) · 1.96 KB
/
page.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// page.c ... functions on Pages
// part of signature indexed files
// Reading/writing pages into buffers and manipulating contents
// Written by John Shepherd, March 2019
#include <unistd.h>
#include "defs.h"
#include "page.h"
#include "reln.h"
// internal representation of pages
struct _PageRep {
Count nitems; // #items in this page
Byte items[1]; // start of data
};
// A Page is a chunk of memory containing PAGESIZE bytes
// It is implemented as a struct (nitems, items[])
// - nitems is #items in page (all items are same size)
// - items[] is a sequence of bytes containing items
// - items can be tuples, tsigs, psigs or bsigs
// - PageID values count # pages from start of file
// create a new initially empty page in memory
Page newPage()
{
Page p = malloc(PAGESIZE);
assert(p != NULL);
memset(p, 0, PAGESIZE);
return p;
}
// append a new Page to a file; return its PageID
void addPage(File f)
{
int ok = lseek(f, 0, SEEK_END);
assert(ok >= 0);
Page p = newPage();
int n = write(f, p, PAGESIZE);
assert(n == PAGESIZE);
}
// fetch a Page from a file
// store it in a newly-allocated memory buffer
Page getPage(File f, PageID pid)
{
//fprintf(stderr,"getPage(%d)\n",pid);
assert(pid >= 0);
Page p = malloc(PAGESIZE);
assert(p != NULL);
int ok = lseek(f, pid*PAGESIZE, SEEK_SET);
assert(ok >= 0);
int n = read(f, p, PAGESIZE);
assert(n == PAGESIZE);
return p;
}
// write a Page to a file; release allocated buffer
Status putPage(File f, PageID pid, Page p)
{
//fprintf(stderr, "putPage(%d)\n", pid);
assert(pid >= 0);
int ok = lseek(f, pid*PAGESIZE, SEEK_SET);
assert(ok >= 0);
int n = write(f, p, PAGESIZE);
assert(n == PAGESIZE);
free(p);
return 0;
}
// given a byte offset to an item in a Page
// return an absolute pointer (start addr of object)
Byte *addrInPage(Page p, int off, int size)
{
return (Byte *)(&(p->items[0]) + size*off);
}
// manipulate page info
Count pageNitems(Page p) { return p->nitems; }
void addOneItem(Page p) { p->nitems++; }