AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
bcache.c
Go to the documentation of this file.
1
6
7#include <core/system.h>
8#include <fs/bcache.h>
9
10MALLOC_DEFINE(M_BCACHE, "bcache", "block cache structure");
11MALLOC_DEFINE(M_CACHE_BLOCK, "cache block", "cached block structure");
12
15 off_t off; /* its number */
16 void *data; /* its contents */
17
18 int dirty; /* changed since it was read (not written back yet) */
19};
20
22static int bcache_eq(void *_a, void *_b)
23{
24 struct cache_block *a = (struct cache_block *) _a;
25 off_t *b = (off_t *) _b;
26
27 return a->off == *b;
28}
29
36{
37 bcache->hashmap = hashmap_new(0, bcache_eq);
38}
39
49{
50 if (!bcache)
51 return -EINVAL;
52
53 if (!bcache->hashmap)
55
56 struct cache_block *block = kmalloc(sizeof(struct cache_block), &M_CACHE_BLOCK, M_ZERO);
57 if (!block) return -ENOMEM;
58
59 block->off = off;
60 block->data = data;
61
62 hash_t hash = hashmap_digest(&off, sizeof(off));
63 return hashmap_insert(bcache->hashmap, hash, block);
64}
65
74{
75 if (!bcache || !bcache->hashmap)
76 return -1;
77
78 hash_t hash = hashmap_digest(&off, sizeof(off));
79
80 struct hashmap_node *node = hashmap_lookup(bcache->hashmap, hash, &off);
81
82 if (node) {
83 void *block = node->entry;
84 node->entry = NULL;
85 kfree(block);
86 hashmap_node_remove(bcache->hashmap, node);
87 return 0;
88 }
89
90 return -1;
91}
92
101{
102 if (!bcache || !bcache->hashmap)
103 return NULL;
104
105 hash_t hash = hashmap_digest(&off, sizeof(off));
106
107 struct hashmap_node *node = hashmap_lookup(bcache->hashmap, hash, &off);
108
109 if (node) {
110 struct cache_block *block = (struct cache_block *) node->entry;
111 return block->data;
112 }
113
114 return NULL;
115}
116
125{
126 if (!bcache || !bcache->hashmap)
127 return;
128
129 hash_t hash = hashmap_digest(&off, sizeof(off));
130
131 struct hashmap_node *node = hashmap_lookup(bcache->hashmap, hash, &off);
132
133 if (node) {
134 struct cache_block *block = (struct cache_block *) node->entry;
135 block->dirty = 1;
136 }
137}
int bcache_remove(struct bcache *bcache, uint64_t off)
Takes a block out of a cache.
Definition bcache.c:73
void bcache_dirty(struct bcache *bcache, uint64_t off)
Marks a cached block as changed.
Definition bcache.c:124
int bcache_insert(struct bcache *bcache, uint64_t off, void *data)
Adds a block to a cache, which keeps its memory from now on.
Definition bcache.c:48
static int bcache_eq(void *_a, void *_b)
Says whether a cached block is the one whose number _b points to.
Definition bcache.c:22
void * bcache_find(struct bcache *bcache, uint64_t off)
Finds a cached block.
Definition bcache.c:100
void bcache_init(struct bcache *bcache)
Sets up an empty block cache.
Definition bcache.c:35
uintptr_t hash_t
Definition hashmap.h:22
#define MALLOC_DEFINE(type, _name, _desc)
Definition kvmem.h:30
void * kmalloc(size_t size, struct malloc_type *type, int flags)
Allocates memory for one of the kernel's structures.
Definition kvmem.c:123
#define M_ZERO
Definition kvmem.h:26
void kfree(void *)
Frees an object that kmalloc() gave.
Definition kvmem.c:146
#define NULL
Definition stddef.h:27
unsigned long long int uint64_t
Definition stddef.h:23
A filesystem's block cache (fs/bcache.c): its blocks that were read, by their offset.
Definition bcache.h:9
struct hashmap * hashmap
Definition bcache.h:10
A cached block.
Definition bcache.c:14
off_t off
Definition bcache.c:15
void * data
Definition bcache.c:16
int dirty
Definition bcache.c:18
A hash table's node: an entry, with its key's hash.
Definition hashmap.h:43
hash_t hash
Definition hashmap.h:44
void * entry
Definition hashmap.h:45
long int off_t
Definition types.h:10