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
101 // TODO(b/140991626): Rename to allocate().
allocateUninitialized(size_t bytes,size_t alignment)102 void *allocateUninitialized(size_t bytes, size_t alignment)
103 {
104 return allocate(bytes, alignment, false);
105 }
106
allocateZero(size_t bytes,size_t alignment)107 void *allocateZero(size_t bytes, size_t alignment)
108 {
109 return allocate(bytes, alignment, true);
110 }
111
112 // This funtion allocates memory that is zero-initialized for security reasons
113 // only. In MemorySanitizer enabled builds it is left uninitialized.
allocateZeroOrPoison(size_t bytes,size_t alignment)114 void *allocateZeroOrPoison(size_t bytes, size_t alignment)
115 {
116 return allocate(bytes, alignment, !__has_feature(memory_sanitizer));
117 }
118
freeMemory(void * memory)119 void freeMemory(void *memory)
120 {
121 if(memory)
122 {
123 unsigned char *aligned = (unsigned char *)memory;
124 Allocation *allocation = (Allocation *)(aligned - sizeof(Allocation));
125
126 free(allocation->block);
127 }
128 }
129
clear(uint16_t * memory,uint16_t element,size_t count)130 void clear(uint16_t *memory, uint16_t element, size_t count)
131 {
132 #if defined(_MSC_VER) && defined(__x86__) && !defined(MEMORY_SANITIZER)
133 __stosw(memory, element, count);
134 #elif defined(__GNUC__) && defined(__x86__) && !defined(MEMORY_SANITIZER)
135 __asm__ __volatile__("rep stosw"
136 : "+D"(memory), "+c"(count)
137 : "a"(element)
138 : "memory");
139 #else
140 for(size_t i = 0; i < count; i++)
141 {
142 memory[i] = element;
143 }
144 #endif
145 }
146
clear(uint32_t * memory,uint32_t element,size_t count)147 void clear(uint32_t *memory, uint32_t element, size_t count)
148 {
149 #if defined(_MSC_VER) && defined(__x86__) && !defined(MEMORY_SANITIZER)
150 __stosd((unsigned long *)memory, element, count);
151 #elif defined(__GNUC__) && defined(__x86__) && !defined(MEMORY_SANITIZER)
152 __asm__ __volatile__("rep stosl"
153 : "+D"(memory), "+c"(count)
154 : "a"(element)
155 : "memory");
156 #else
157 for(size_t i = 0; i < count; i++)
158 {
159 memory[i] = element;
160 }
161 #endif
162 }
163
164 } // namespace sw
165