-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkv.h
More file actions
67 lines (55 loc) · 1.94 KB
/
kv.h
File metadata and controls
67 lines (55 loc) · 1.94 KB
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
// SPDX-License-Identifier: GPL-2.0-only
// Copyright (C) 2024-2026, Shu De Zheng <[email protected]>. All Rights Reserved.
#ifndef __UMEM_CACHE_KV_H
#define __UMEM_CACHE_KV_H
#include <sys/uio.h>
#include "slab.h"
#include "list.h"
struct concat_val {
struct slab_obj_offset *soo_ptr;
unsigned char data[];
};
/* memory migration requires this member shows first */
static_assert(offsetof(struct concat_val, soo_ptr) == 0);
struct kv_borrower {
struct hlist_node kv_ref_node;
struct kv *kv;
};
/**
* kv -
* @soo: it has a trick involved, see kv_malloc() and kv_is_concat()
* @lru: kv is on lru and is ready to serve command GET if kv_enabled()
* @borrower_list: the list of kv_borrower
* @val_size: value size
* @hash_node: resides in a hash_table if kv_enabled()
* @data: data of key and value
*/
struct kv {
struct slab_obj_offset soo;
struct list_head lru;
struct hlist_head borrower_list;
uint64_t on_s_lru : 1;
uint64_t val_size : 63;
struct hlist_node hash_node;
unsigned char data[];
};
/* this offset is required for hashtable to locate the key */
static_assert(offsetof(struct kv, data) - offsetof(struct kv, hash_node) ==
sizeof(struct hlist_node));
/* memory migration requires this member shows first */
static_assert(offsetof(struct kv, soo) == 0);
#define KV_KEY(kv) ((kv)->data)
#define KEY_SIZE(key) (1 + (int)((key)[0]))
#define KV_KEY_SIZE(kv) KEY_SIZE(KV_KEY(kv))
#define KV_VAL(kv) ((kv)->data + KV_KEY_SIZE(kv))
#define KV_SIZE(kv) (sizeof(struct kv) + KV_KEY_SIZE(kv) + (kv)->val_size)
void kv_init(struct kv *kv, const unsigned char *key, uint64_t val_size);
void kv_set_fake(struct kv *kv);
bool kv_enabled(struct kv *kv);
void kv_borrow(struct kv *kv, struct kv_borrower *borrower);
void kv_return(struct kv_borrower *borrower);
bool kv_is_concat(struct kv *kv);
void kv_borrower_init(struct kv_borrower *borrower);
bool kv_no_borrower(struct kv *kv);
int kv_val_to_iovec(struct kv *kv, uint64_t i, struct iovec *iov);
#endif