-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxatable.c
61 lines (54 loc) · 1.58 KB
/
xatable.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
/*
* Use multiple XArrays to improve concurrency.
*
* Copyright (c) 2020-2022 Jiansheng Qiu <[email protected]>
*
* This file is licensed under the terms of the GNU General Public
* License version 2. This program is licensed "as is" without any
* warranty of any kind, whether express or implied.
*/
#include "xatable.h"
#include <linux/slab.h>
#include "stats.h"
int xatable_init(struct xatable *xat, unsigned long num_bit)
{
unsigned long i, num = 1UL << num_bit;
xat->xa = kmalloc(sizeof(struct xarray) * num, GFP_KERNEL);
if (xat->xa == NULL)
return -ENOMEM;
for (i = 0; i < num; ++i)
xa_init(xat->xa + i);
xat->num_bit = num_bit;
return 0;
}
void xatable_destroy(struct xatable *xat)
{
unsigned long i, num;
if (xat->xa == NULL)
return;
num = 1UL << xat->num_bit;
for (i = 0; i < num; ++i)
xa_destroy(xat->xa + i);
}
void *xatable_store(struct xatable *xat, unsigned long index, void *entry, gfp_t gfp)
{
unsigned long which = index & ((1UL << xat->num_bit) - 1);
INIT_TIMING(xatable_store_time);
void *ret;
NOVA_START_TIMING(xatable_store_t, xatable_store_time);
index >>= xat->num_bit;
ret = xa_store(xat->xa + which, index, entry, gfp);
NOVA_END_TIMING(xatable_store_t, xatable_store_time);
return ret;
}
void *xatable_load(struct xatable *xat, unsigned long index)
{
unsigned long which = index & ((1UL << xat->num_bit) - 1);
INIT_TIMING(xatable_load_time);
void *ret;
NOVA_START_TIMING(xatable_load_t, xatable_load_time);
index >>= xat->num_bit;
ret = xa_load(xat->xa + which, index);
NOVA_END_TIMING(xatable_load_t, xatable_load_time);
return ret;
}