1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2 //
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 #ifndef nvs_pagemanager_hpp
15 #define nvs_pagemanager_hpp
16 
17 #include <memory>
18 #include <list>
19 #include "nvs_types.hpp"
20 #include "nvs_page.hpp"
21 #include "partition.hpp"
22 #include "intrusive_list.h"
23 
24 namespace nvs
25 {
26 class PageManager
27 {
28     using TPageList = intrusive_list<Page>;
29     using TPageListIterator = TPageList::iterator;
30 public:
31 
PageManager()32     PageManager() {}
33 
34     esp_err_t load(Partition *partition, uint32_t baseSector, uint32_t sectorCount);
35 
begin()36     TPageListIterator begin()
37     {
38         return mPageList.begin();
39     }
40 
end()41     TPageListIterator end()
42     {
43         return mPageList.end();
44     }
45 
back()46     Page& back()
47     {
48         return mPageList.back();
49     }
50 
getPageCount()51     uint32_t getPageCount() {
52         return mPageCount;
53     }
54 
55     esp_err_t requestNewPage();
56 
57     esp_err_t fillStats(nvs_stats_t& nvsStats);
58 
getBaseSector()59     uint32_t getBaseSector()
60     {
61         return mBaseSector;
62     }
63 
64 protected:
65     friend class Iterator;
66 
67     esp_err_t activatePage();
68 
69     TPageList mPageList;
70     TPageList mFreePageList;
71     std::unique_ptr<Page[]> mPages;
72     uint32_t mBaseSector;
73     uint32_t mPageCount;
74     uint32_t mSeqNumber;
75 }; // class PageManager
76 
77 
78 } // namespace nvs
79 
80 
81 #endif /* nvs_pagemanager_hpp */
82