Nicolas Capens | c07dc4b | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 1 | // Copyright 2016 The SwiftShader Authors. All Rights Reserved. |
| 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 | |
Nicolas Capens | 1a3ce87 | 2018-10-10 10:42:36 -0400 | [diff] [blame] | 15 | #ifndef rr_ExecutableMemory_hpp |
| 16 | #define rr_ExecutableMemory_hpp |
Nicolas Capens | c07dc4b | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 17 | |
Nicolas Capens | 8d2cf75 | 2018-11-22 11:13:45 -0500 | [diff] [blame] | 18 | #include <cstddef> |
| 19 | #include <cstdint> |
| 20 | #include <cstring> |
Nicolas Capens | c07dc4b | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 21 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 22 | namespace rr { |
| 23 | |
Nicolas Capens | c07dc4b | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 24 | size_t memoryPageSize(); |
| 25 | |
Sergey Ulanov | ebb0bec | 2019-12-12 11:53:04 -0800 | [diff] [blame] | 26 | enum MemoryPermission { |
| 27 | PERMISSION_READ = 1, |
| 28 | PERMISSION_WRITE = 2, |
| 29 | PERMISSION_EXECUTE = 4, |
| 30 | }; |
| 31 | |
| 32 | // Allocates memory with the specified permissions. If |need_exec| is true then |
| 33 | // the allocate memory can be made marked executable using protectMemoryPages(). |
| 34 | void* allocateMemoryPages(size_t bytes, int permissions, bool need_exec); |
| 35 | |
| 36 | // Sets permissions for memory allocated with allocateMemoryPages(). |
| 37 | void protectMemoryPages(void *memory, size_t bytes, int permissions); |
| 38 | |
| 39 | // Releases memory allocated with allocateMemoryPages(). |
| 40 | void deallocateMemoryPages(void *memory, size_t bytes); |
Nicolas Capens | 8d2cf75 | 2018-11-22 11:13:45 -0500 | [diff] [blame] | 41 | |
| 42 | template<typename P> |
| 43 | P unaligned_read(P *address) |
| 44 | { |
| 45 | P value; |
| 46 | memcpy(&value, address, sizeof(P)); |
| 47 | return value; |
| 48 | } |
| 49 | |
| 50 | template<typename P, typename V> |
| 51 | void unaligned_write(P *address, V value) |
| 52 | { |
| 53 | static_assert(sizeof(V) == sizeof(P), "value size must match pointee size"); |
| 54 | memcpy(address, &value, sizeof(P)); |
| 55 | } |
| 56 | |
| 57 | template<typename P> |
| 58 | class unaligned_ref |
| 59 | { |
| 60 | public: |
| 61 | explicit unaligned_ref(void *ptr) : ptr((P*)ptr) {} |
| 62 | |
| 63 | template<typename V> |
| 64 | P operator=(V value) |
| 65 | { |
| 66 | unaligned_write(ptr, value); |
| 67 | return value; |
| 68 | } |
| 69 | |
| 70 | operator P() |
| 71 | { |
| 72 | return unaligned_read((P*)ptr); |
| 73 | } |
| 74 | |
| 75 | private: |
| 76 | P *ptr; |
| 77 | }; |
| 78 | |
| 79 | template<typename P> |
| 80 | class unaligned_ptr |
| 81 | { |
| 82 | template<typename S> |
| 83 | friend class unaligned_ptr; |
| 84 | |
| 85 | public: |
| 86 | unaligned_ptr(P *ptr) : ptr(ptr) {} |
| 87 | |
| 88 | unaligned_ref<P> operator*() |
| 89 | { |
| 90 | return unaligned_ref<P>(ptr); |
| 91 | } |
| 92 | |
| 93 | template<typename S> |
| 94 | operator S() |
| 95 | { |
| 96 | return S(ptr); |
| 97 | } |
| 98 | |
| 99 | private: |
| 100 | void *ptr; |
| 101 | }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 102 | |
| 103 | } // namespace rr |
Nicolas Capens | c07dc4b | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 104 | |
Nicolas Capens | 1a3ce87 | 2018-10-10 10:42:36 -0400 | [diff] [blame] | 105 | #endif // rr_ExecutableMemory_hpp |