• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
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  */
15 #ifndef DFX_MMAP_H
16 #define DFX_MMAP_H
17 
18 #include <atomic>
19 #include <cstdint>
20 #include <string>
21 #if !is_mingw
22 #include <sys/mman.h>
23 #else
24 #include "dfx_nonlinux_define.h"
25 #endif
26 #include "dfx_memory.h"
27 
28 namespace OHOS {
29 namespace HiviewDFX {
30 class DfxMmap : public DfxMemory {
31 public:
32     DfxMmap() = default;
~DfxMmap()33     virtual ~DfxMmap() { Clear(); }
34 
35     bool Init(const int fd, const size_t size, const off_t offset = 0);
36     bool Init(uint8_t *decompressedData, size_t size);
37     void Clear();
38 
Get()39     inline void* Get()
40     {
41         if (mmap_ != MAP_FAILED) {
42             return mmap_;
43         }
44         return nullptr;
45     }
Size()46     inline size_t Size() { return size_; }
SetNeedUnmap(bool need)47     inline void SetNeedUnmap(bool need) { needUnmap_ = need; }
48 
49     size_t Read(uintptr_t& addr, void* val, size_t size, bool incre = false) override;
50 
51 private:
52     void *mmap_ = MAP_FAILED;
53     size_t size_ = 0;
54     bool needUnmap_ = true;
55 };
56 } // namespace HiviewDFX
57 } // namespace OHOS
58 #endif
59