• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  * Description: KV Storage Library page access interface
15  */
16 
17 #ifndef NV_PAGE_H
18 #define NV_PAGE_H
19 
20 #include "nv_key.h"
21 #include "errcode.h"
22 
23 #ifdef __cplusplus
24 #if __cplusplus
25 extern "C" {
26 #endif /* __cplusplus */
27 #endif /* __cplusplus */
28 
29 /** Represents the location in flash of a KV page. Provided in such a way that it can't be dereferenced,
30  * as flash is not directly addressable */
31 typedef const void *kv_page_location;
32 
33 typedef struct {
34     uint16_t store_id;     /* Identifies the page as belonging to a certain store */
35     uint8_t  ver;
36     uint8_t  page_index;   /* Uniquely identifies a page within a store */
37 } kv_page_details_t;
38 
39 typedef struct {
40     kv_page_details_t  details;
41     uint32_t           inverted_details_word;
42     uint32_t           sequence_number;
43     uint32_t           inverted_sequence_number;
44 } kv_page_header_t;
45 
46 /* A KV page handle, which provides both the page header information and the location in flash */
47 typedef struct {
48     kv_page_header_t    page_header;
49     kv_page_location    page_location;   /* Location in flash of the KV page */
50 } kv_page_handle_t;
51 
52 typedef struct {
53     uint32_t total_space;               /* 本页最大空间(不含页头) */
54     uint32_t used_space;                /* 本页已使用空间 */
55     uint32_t reclaimable_space;         /* 本页可回收空间(包含已损坏空间) */
56     uint32_t corrupted_space;           /* 本页已损坏空间 */
57     uint32_t max_key_space;             /* 本页可写的最大空间(未使用+可回收) */
58     uint32_t first_writable_location;   /* 本页第一个可写入位置 */
59 } kv_page_status_t;
60 
61 typedef struct {
62     uint32_t page_location;             /* 本页首地址(包含页头) */
63     uint16_t used_space;                /* 本页已使用空间 */
64     uint16_t reclaimable_space;         /* 本页可回收空间(包含已损坏空间) */
65     uint16_t corrupted_space;           /* 本页已损坏空间 */
66     uint16_t first_writable_offset;     /* 本页第一个可写入位置(相对页首地址的偏移) */
67 } nv_page_status_map_t;
68 
69 errcode_t kv_page_get_index(kv_page_handle_t *page, uint32_t *page_index);
70 errcode_t kv_page_find_first_key(const kv_page_handle_t *page, kv_key_filter_t *search_filter,
71     kv_key_handle_t *key);
72 errcode_t kv_page_find_next_key(const kv_page_handle_t *page, kv_key_filter_t *search_filter,
73     kv_key_handle_t *key);
74 void kv_page_get_status(kv_page_handle_t *page, kv_page_status_t *page_status);
75 void kv_page_get_status_from_map(kv_page_handle_t *page, kv_page_status_t *page_status);
76 void kv_page_read_status_to_map(kv_page_handle_t *page, nv_page_status_map_t *status_map);
77 
78 #ifdef __cplusplus
79 #if __cplusplus
80 }
81 #endif /* __cplusplus */
82 #endif /* __cplusplus */
83 
84 #endif /* NV_PAGE_H_ */
85