1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef BERBERIS_BASE_PAGESIZE_H_
18 #define BERBERIS_BASE_PAGESIZE_H_
19 
20 #include "berberis/base/checks.h"
21 
22 #include <unistd.h>
23 
24 namespace berberis {
25 
26 // Accessor for page size constant.
27 //
28 // Only allows access to the page size constant if
29 // it has been initialized.
30 //
31 // For cases where page size value is needed before kPageSize
32 // has been initialized, getpagesize() is returned.
33 struct PageSize {
PageSizePageSize34   PageSize() : value_(getpagesize()) {}
35 
size_tPageSize36   operator size_t() const {
37     if (value_ == 0) {
38       return getpagesize();
39     }
40     CHECK((value_ & (value_ - 1)) == 0);  // Power of 2
41     return value_;
42   }
43 
44  private:
45   const size_t value_;
46 };
47 
48 namespace {
49 
50 inline constexpr size_t kMaxPageSize = 0x10000;  // 64KB
51 inline const PageSize kPageSize;
52 
53 }  // namespace
54 
55 }  // namespace berberis
56 
57 #endif  // BERBERIS_BASE_PAGESIZE_H_
58