xref: /aosp_15_r20/external/swiftshader/src/System/Memory.cpp (revision 03ce13f70fcc45d86ee91b7ee4cab1936a95046e)
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 #include "Memory.hpp"
16 
17 #include "Debug.hpp"
18 #include "Types.hpp"
19 
20 #if defined(_WIN32)
21 #	ifndef WIN32_LEAN_AND_MEAN
22 #		define WIN32_LEAN_AND_MEAN
23 #	endif
24 #	include <windows.h>
25 #	include <intrin.h>
26 #else
27 #	include <errno.h>
28 #	include <sys/mman.h>
29 #	include <stdlib.h>
30 #	include <unistd.h>
31 #endif
32 
33 #include <cstdlib>
34 #include <cstring>
35 
36 #undef allocate
37 #undef deallocate
38 
39 #if(defined(__i386__) || defined(_M_IX86) || defined(__x86_64__) || defined(_M_X64)) && !defined(__x86__)
40 #	define __x86__
41 #endif
42 
43 // A Clang extension to determine compiler features.
44 // We use it to detect Sanitizer builds (e.g. -fsanitize=memory).
45 #ifndef __has_feature
46 #	define __has_feature(x) 0
47 #endif
48 
49 namespace sw {
50 
51 namespace {
52 
53 struct Allocation
54 {
55 	// size_t bytes;
56 	unsigned char *block;
57 };
58 
59 }  // anonymous namespace
60 
memoryPageSize()61 size_t memoryPageSize()
62 {
63 	static int pageSize = [] {
64 #if defined(_WIN32)
65 		SYSTEM_INFO systemInfo;
66 		GetSystemInfo(&systemInfo);
67 		return systemInfo.dwPageSize;
68 #else
69 		return sysconf(_SC_PAGESIZE);
70 #endif
71 	}();
72 
73 	return pageSize;
74 }
75 
allocate(size_t bytes,size_t alignment,bool clearToZero)76 static void *allocate(size_t bytes, size_t alignment, bool clearToZero)
77 {
78 	ASSERT((alignment & (alignment - 1)) == 0);  // Power of 2 alignment.
79 
80 	size_t size = bytes + sizeof(Allocation) + alignment;
81 	unsigned char *block = (unsigned char *)malloc(size);
82 	unsigned char *aligned = nullptr;
83 
84 	if(block)
85 	{
86 		if(clearToZero)
87 		{
88 			memset(block, 0, size);
89 		}
90 
91 		aligned = (unsigned char *)((uintptr_t)(block + sizeof(Allocation) + alignment - 1) & -(intptr_t)alignment);
92 		Allocation *allocation = (Allocation *)(aligned - sizeof(Allocation));
93 
94 		// allocation->bytes = bytes;
95 		allocation->block = block;
96 	}
97 
98 	return aligned;
99 }
100 
allocate(size_t bytes,size_t alignment)101 void *allocate(size_t bytes, size_t alignment)
102 {
103 	return allocate(bytes, alignment, false);
104 }
105 
106 // This funtion allocates memory that is zero-initialized for security reasons
107 // only. In MemorySanitizer enabled builds it is left uninitialized.
allocateZeroOrPoison(size_t bytes,size_t alignment)108 void *allocateZeroOrPoison(size_t bytes, size_t alignment)
109 {
110 	return allocate(bytes, alignment, !__has_feature(memory_sanitizer));
111 }
112 
freeMemory(void * memory)113 void freeMemory(void *memory)
114 {
115 	if(memory)
116 	{
117 		unsigned char *aligned = (unsigned char *)memory;
118 		Allocation *allocation = (Allocation *)(aligned - sizeof(Allocation));
119 
120 		free(allocation->block);
121 	}
122 }
123 
clear(uint16_t * memory,uint16_t element,size_t count)124 void clear(uint16_t *memory, uint16_t element, size_t count)
125 {
126 #if defined(_MSC_VER) && defined(__x86__) && !defined(MEMORY_SANITIZER)
127 	__stosw(memory, element, count);
128 #elif defined(__GNUC__) && defined(__x86__) && !defined(MEMORY_SANITIZER)
129 	__asm__ __volatile__("rep stosw"
130 	                     : "+D"(memory), "+c"(count)
131 	                     : "a"(element)
132 	                     : "memory");
133 #else
134 	for(size_t i = 0; i < count; i++)
135 	{
136 		memory[i] = element;
137 	}
138 #endif
139 }
140 
clear(uint32_t * memory,uint32_t element,size_t count)141 void clear(uint32_t *memory, uint32_t element, size_t count)
142 {
143 #if defined(_MSC_VER) && defined(__x86__) && !defined(MEMORY_SANITIZER)
144 	__stosd((unsigned long *)memory, element, count);
145 #elif defined(__GNUC__) && defined(__x86__) && !defined(MEMORY_SANITIZER)
146 	__asm__ __volatile__("rep stosl"
147 	                     : "+D"(memory), "+c"(count)
148 	                     : "a"(element)
149 	                     : "memory");
150 #else
151 	for(size_t i = 0; i < count; i++)
152 	{
153 		memory[i] = element;
154 	}
155 #endif
156 }
157 
158 }  // namespace sw
159