• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
15 #ifndef rr_ExecutableMemory_hpp
16 #define rr_ExecutableMemory_hpp
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <cstring>
21 
22 namespace rr {
23 
24 size_t memoryPageSize();
25 
26 enum MemoryPermission
27 {
28 	PERMISSION_READ = 1,
29 	PERMISSION_WRITE = 2,
30 	PERMISSION_EXECUTE = 4,
31 };
32 
33 // Allocates memory with the specified permissions. If |need_exec| is true then
34 // the allocate memory can be made marked executable using protectMemoryPages().
35 void *allocateMemoryPages(size_t bytes, int permissions, bool need_exec);
36 
37 // Sets permissions for memory allocated with allocateMemoryPages().
38 void protectMemoryPages(void *memory, size_t bytes, int permissions);
39 
40 // Releases memory allocated with allocateMemoryPages().
41 void deallocateMemoryPages(void *memory, size_t bytes);
42 
43 template<typename P>
unaligned_read(P * address)44 P unaligned_read(P *address)
45 {
46 	P value;
47 	memcpy(&value, address, sizeof(P));
48 	return value;
49 }
50 
51 template<typename P, typename V>
unaligned_write(P * address,V value)52 void unaligned_write(P *address, V value)
53 {
54 	static_assert(sizeof(V) == sizeof(P), "value size must match pointee size");
55 	memcpy(address, &value, sizeof(P));
56 }
57 
58 template<typename P>
59 class unaligned_ref
60 {
61 public:
unaligned_ref(void * ptr)62 	explicit unaligned_ref(void *ptr)
63 	    : ptr((P *)ptr)
64 	{}
65 
66 	template<typename V>
operator =(V value)67 	P operator=(V value)
68 	{
69 		unaligned_write(ptr, value);
70 		return value;
71 	}
72 
operator P()73 	operator P()
74 	{
75 		return unaligned_read((P *)ptr);
76 	}
77 
78 private:
79 	P *ptr;
80 };
81 
82 template<typename P>
83 class unaligned_ptr
84 {
85 	template<typename S>
86 	friend class unaligned_ptr;
87 
88 public:
unaligned_ptr(P * ptr)89 	unaligned_ptr(P *ptr)
90 	    : ptr(ptr)
91 	{}
92 
operator *()93 	unaligned_ref<P> operator*()
94 	{
95 		return unaligned_ref<P>(ptr);
96 	}
97 
98 	template<typename S>
operator S()99 	operator S()
100 	{
101 		return S(ptr);
102 	}
103 
104 private:
105 	void *ptr;
106 };
107 
108 }  // namespace rr
109 
110 #endif  // rr_ExecutableMemory_hpp
111