1 | |
---|
2 | /* public interfaces for block io routines */ |
---|
3 | |
---|
4 | #ifndef _BLOCK_H |
---|
5 | #define _BLOCK_H |
---|
6 | |
---|
7 | /*#define IBEX_STATS*/ /* define to get/dump block access stats */ |
---|
8 | |
---|
9 | #include <glib.h> |
---|
10 | #include <setjmp.h> |
---|
11 | |
---|
12 | /* version of file format */ |
---|
13 | #define IBEX_VERSION "ibx6" |
---|
14 | |
---|
15 | typedef guint32 nameid_t; |
---|
16 | typedef guint32 blockid_t; |
---|
17 | |
---|
18 | #define BLOCK_BITS (8) |
---|
19 | #define BLOCK_SIZE (1<<BLOCK_BITS) |
---|
20 | #define CACHE_SIZE 256 /* blocks in disk cache */ |
---|
21 | |
---|
22 | /* root block */ |
---|
23 | struct _root { |
---|
24 | char version[4]; |
---|
25 | |
---|
26 | blockid_t free; /* list of free blocks */ |
---|
27 | blockid_t roof; /* top of allocated space, everything below is in a free or used list */ |
---|
28 | blockid_t tail; /* list of 'tail' blocks */ |
---|
29 | |
---|
30 | blockid_t words; /* root of words index */ |
---|
31 | blockid_t names; /* root of names index */ |
---|
32 | |
---|
33 | char flags; /* state flags */ |
---|
34 | |
---|
35 | /* makes structure fill up to 1024 bytes */ |
---|
36 | char dummy[1024 - (sizeof(char)*5) - (sizeof(blockid_t)*5)]; |
---|
37 | }; |
---|
38 | |
---|
39 | #define IBEX_ROOT_SYNCF (1<<0) /* file is synced */ |
---|
40 | |
---|
41 | /* basic disk structure for (data) blocks */ |
---|
42 | struct _block { |
---|
43 | unsigned int next:32-BLOCK_BITS; /* next block */ |
---|
44 | unsigned int used:BLOCK_BITS; /* number of elements used */ |
---|
45 | |
---|
46 | nameid_t bl_data[(BLOCK_SIZE-4)/4]; /* references */ |
---|
47 | }; |
---|
48 | |
---|
49 | /* custom list structure, for a simple/efficient cache */ |
---|
50 | struct _listnode { |
---|
51 | struct _listnode *next; |
---|
52 | struct _listnode *prev; |
---|
53 | }; |
---|
54 | struct _list { |
---|
55 | struct _listnode *head; |
---|
56 | struct _listnode *tail; |
---|
57 | struct _listnode *tailpred; |
---|
58 | }; |
---|
59 | |
---|
60 | void ibex_list_new(struct _list *v); |
---|
61 | struct _listnode *ibex_list_addhead(struct _list *l, struct _listnode *n); |
---|
62 | struct _listnode *ibex_list_addtail(struct _list *l, struct _listnode *n); |
---|
63 | struct _listnode *ibex_list_remove(struct _listnode *n); |
---|
64 | |
---|
65 | /* in-memory structure for block cache */ |
---|
66 | struct _memblock { |
---|
67 | struct _memblock *next; |
---|
68 | struct _memblock *prev; |
---|
69 | |
---|
70 | blockid_t block; |
---|
71 | int flags; |
---|
72 | |
---|
73 | struct _block data; |
---|
74 | }; |
---|
75 | #define BLOCK_DIRTY (1<<0) |
---|
76 | |
---|
77 | struct _memcache { |
---|
78 | struct _list nodes; |
---|
79 | int count; /* nodes in cache */ |
---|
80 | |
---|
81 | GHashTable *index; /* blockid->memblock mapping */ |
---|
82 | |
---|
83 | int fd; /* file fd */ |
---|
84 | char *name; /* file name */ |
---|
85 | |
---|
86 | jmp_buf failenv; /* for exception failure */ |
---|
87 | int failed; /* indicates the file failed */ |
---|
88 | |
---|
89 | #ifdef IBEX_STATS |
---|
90 | GHashTable *stats; |
---|
91 | #endif |
---|
92 | struct _root root; /* root block */ |
---|
93 | |
---|
94 | /* temporary here */ |
---|
95 | struct _IBEXWord *words; /* word index */ |
---|
96 | }; |
---|
97 | |
---|
98 | #ifdef IBEX_STATS |
---|
99 | struct _stat_info { |
---|
100 | int read; |
---|
101 | int write; |
---|
102 | int cache_hit; |
---|
103 | int cache_miss; |
---|
104 | }; |
---|
105 | #endif /* IBEX_STATS */ |
---|
106 | |
---|
107 | struct _memcache *ibex_block_cache_open(const char *name, int flags, int mode); |
---|
108 | void ibex_block_cache_close(struct _memcache *block_cache); |
---|
109 | void ibex_block_cache_sync(struct _memcache *block_cache); |
---|
110 | void ibex_block_cache_flush(struct _memcache *block_cache); |
---|
111 | |
---|
112 | #define ibex_block_cache_setjmp(bc) (((bc)==NULL)?1:setjmp((bc)->failenv)) |
---|
113 | #define ibex_block_cache_assert(bc, cond) { if (!(cond)) { ibex_block_cache_fail(bc, __FILE__, __LINE__, # cond); } } |
---|
114 | |
---|
115 | void ibex_block_cache_fail(struct _memcache *block_cache, char *file, int line, char *why); |
---|
116 | |
---|
117 | blockid_t ibex_block_get(struct _memcache *block_cache); |
---|
118 | void ibex_block_free(struct _memcache *block_cache, blockid_t blockid); |
---|
119 | void ibex_block_dirty(struct _block *block); |
---|
120 | struct _block *ibex_block_read(struct _memcache *block_cache, blockid_t blockid); |
---|
121 | |
---|
122 | #define block_number(x) ((x)>>BLOCK_BITS) |
---|
123 | #define block_location(x) ((x)<<BLOCK_BITS) |
---|
124 | |
---|
125 | #endif /* ! _BLOCK_H */ |
---|