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 "Types.hpp"
18 #include "Debug.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 <memory.h>
34
35 #undef allocate
36 #undef deallocate
37
38 #if (defined(__i386__) || defined(_M_IX86) || defined(__x86_64__) || defined (_M_X64)) && !defined(__x86__)
39 #define __x86__
40 #endif
41
42 namespace sw
43 {
44 namespace
45 {
46 struct Allocation
47 {
48 // size_t bytes;
49 unsigned char *block;
50 };
51
allocateRaw(size_t bytes,size_t alignment)52 void *allocateRaw(size_t bytes, size_t alignment)
53 {
54 ASSERT((alignment & (alignment - 1)) == 0); // Power of 2 alignment.
55
56 #if defined(LINUX_ENABLE_NAMED_MMAP)
57 if(alignment < sizeof(void*))
58 {
59 return malloc(bytes);
60 }
61 else
62 {
63 void *allocation;
64 int result = posix_memalign(&allocation, alignment, bytes);
65 if(result != 0)
66 {
67 errno = result;
68 allocation = nullptr;
69 }
70 return allocation;
71 }
72 #else
73 unsigned char *block = new unsigned char[bytes + sizeof(Allocation) + alignment];
74 unsigned char *aligned = nullptr;
75
76 if(block)
77 {
78 aligned = (unsigned char*)((uintptr_t)(block + sizeof(Allocation) + alignment - 1) & -(intptr_t)alignment);
79 Allocation *allocation = (Allocation*)(aligned - sizeof(Allocation));
80
81 // allocation->bytes = bytes;
82 allocation->block = block;
83 }
84
85 return aligned;
86 #endif
87 }
88 } // anonymous namespace
89
memoryPageSize()90 size_t memoryPageSize()
91 {
92 static int pageSize = 0;
93
94 if(pageSize == 0)
95 {
96 #if defined(_WIN32)
97 SYSTEM_INFO systemInfo;
98 GetSystemInfo(&systemInfo);
99 pageSize = systemInfo.dwPageSize;
100 #else
101 pageSize = sysconf(_SC_PAGESIZE);
102 #endif
103 }
104
105 return pageSize;
106 }
107
allocate(size_t bytes,size_t alignment)108 void *allocate(size_t bytes, size_t alignment)
109 {
110 void *memory = allocateRaw(bytes, alignment);
111
112 if(memory)
113 {
114 memset(memory, 0, bytes);
115 }
116
117 return memory;
118 }
119
deallocate(void * memory)120 void deallocate(void *memory)
121 {
122 #if defined(LINUX_ENABLE_NAMED_MMAP)
123 free(memory);
124 #else
125 if(memory)
126 {
127 unsigned char *aligned = (unsigned char*)memory;
128 Allocation *allocation = (Allocation*)(aligned - sizeof(Allocation));
129
130 delete[] allocation->block;
131 }
132 #endif
133 }
134
clear(uint16_t * memory,uint16_t element,size_t count)135 void clear(uint16_t *memory, uint16_t element, size_t count)
136 {
137 #if defined(_MSC_VER) && defined(__x86__) && !defined(MEMORY_SANITIZER)
138 __stosw(memory, element, count);
139 #elif defined(__GNUC__) && defined(__x86__) && !defined(MEMORY_SANITIZER)
140 __asm__ __volatile__("rep stosw" : "+D"(memory), "+c"(count) : "a"(element) : "memory");
141 #else
142 for(size_t i = 0; i < count; i++)
143 {
144 memory[i] = element;
145 }
146 #endif
147 }
148
clear(uint32_t * memory,uint32_t element,size_t count)149 void clear(uint32_t *memory, uint32_t element, size_t count)
150 {
151 #if defined(_MSC_VER) && defined(__x86__) && !defined(MEMORY_SANITIZER)
152 __stosd((unsigned long*)memory, element, count);
153 #elif defined(__GNUC__) && defined(__x86__) && !defined(MEMORY_SANITIZER)
154 __asm__ __volatile__("rep stosl" : "+D"(memory), "+c"(count) : "a"(element) : "memory");
155 #else
156 for(size_t i = 0; i < count; i++)
157 {
158 memory[i] = element;
159 }
160 #endif
161 }
162
163 }
164