1 /*
2  * Copyright (C) 2016 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 #include "berberis/base/mmap.h"
18 
19 #include <sys/mman.h>
20 
21 #include "berberis/base/checks.h"
22 
23 namespace berberis {
24 
MmapImpl(MmapImplArgs args)25 void* MmapImpl(MmapImplArgs args) {
26   return mmap(args.addr, args.size, args.prot, args.flags, args.fd, args.offset);
27 }
28 
MmapImplOrDie(MmapImplArgs args)29 void* MmapImplOrDie(MmapImplArgs args) {
30   void* ptr = MmapImpl(args);
31   CHECK_NE(ptr, MAP_FAILED);
32   return ptr;
33 }
34 
MunmapOrDie(void * ptr,size_t size)35 void MunmapOrDie(void* ptr, size_t size) {
36   int res = munmap(ptr, size);
37   CHECK_EQ(res, 0);
38 }
39 
MprotectOrDie(void * ptr,size_t size,int prot)40 void MprotectOrDie(void* ptr, size_t size, int prot) {
41   int res = mprotect(ptr, size, prot);
42   CHECK_EQ(res, 0);
43 }
44 
45 }  // namespace berberis
46