aboutsummaryrefslogtreecommitdiffstats
path: root/libibex/block.h
blob: dbc8fd5ad8a172d15f2363ccb81b122911ab4f34 (plain) (blame)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

/* public interfaces for block io routines */

#ifndef _BLOCK_H
#define _BLOCK_H

/*#define IBEX_STATS*/      /* define to get/dump block access stats */

#include <glib.h>

/* version of file format */
#define IBEX_VERSION "ibx6"

typedef guint32 nameid_t;
typedef guint32 blockid_t;

#define BLOCK_BITS (8)
#define BLOCK_SIZE (1<<BLOCK_BITS)
#define CACHE_SIZE 256      /* blocks in disk cache */

/* root block */
struct _root {
    char version[4];

    blockid_t free;     /* list of free blocks */
    blockid_t roof;     /* top of allocated space, everything below is in a free or used list */
    blockid_t tail;     /* list of 'tail' blocks */

    blockid_t words;    /* root of words index */
    blockid_t names;    /* root of names index */

    char flags;     /* state flags */

    /* makes structure fill up to 1024 bytes */
    char dummy[1024 - (sizeof(char)*5) - (sizeof(blockid_t)*5)];
};

#define IBEX_ROOT_SYNCF (1<<0)  /* file is synced */

/* basic disk structure for (data) blocks */
struct _block {
    unsigned int next:32-BLOCK_BITS;    /* next block */
    unsigned int used:BLOCK_BITS;       /* number of elements used */

    nameid_t bl_data[(BLOCK_SIZE-4)/4]; /* references */
};

/* custom list structure, for a simple/efficient cache */
struct _listnode {
    struct _listnode *next;
    struct _listnode *prev;
};
struct _list {
    struct _listnode *head;
    struct _listnode *tail;
    struct _listnode *tailpred;
};

void ibex_list_new(struct _list *v);
struct _listnode *ibex_list_addhead(struct _list *l, struct _listnode *n);
struct _listnode *ibex_list_addtail(struct _list *l, struct _listnode *n);
struct _listnode *ibex_list_remove(struct _listnode *n);

/* in-memory structure for block cache */
struct _memblock {
    struct _memblock *next;
    struct _memblock *prev;

    blockid_t block;
    int flags;

    struct _block data;
};
#define BLOCK_DIRTY (1<<0)

struct _memcache {
    struct _list nodes;
    int count;      /* nodes in cache */

    GHashTable *index;  /* blockid->memblock mapping */
    int fd;         /* file fd */

#ifdef IBEX_STATS
    GHashTable *stats;
#endif
    struct _root root;  /* root block */

    /* temporary here */
    struct _IBEXWord *words; /* word index */
};

#ifdef IBEX_STATS
struct _stat_info {
    int read;
    int write;
    int cache_hit;
    int cache_miss;
};
#endif /* IBEX_STATS */

struct _memcache *ibex_block_cache_open(const char *name, int flags, int mode);
void ibex_block_cache_close(struct _memcache *block_cache);
void ibex_block_cache_sync(struct _memcache *block_cache);
void ibex_block_cache_flush(struct _memcache *block_cache);

blockid_t ibex_block_get(struct _memcache *block_cache);
void ibex_block_free(struct _memcache *block_cache, blockid_t blockid);
void ibex_block_dirty(struct _block *block);
struct _block *ibex_block_read(struct _memcache *block_cache, blockid_t blockid);

#define block_number(x) ((x)>>BLOCK_BITS)
#define block_location(x) ((x)<<BLOCK_BITS)

#endif /* ! _BLOCK_H */