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 void *allocation;
58 int result = posix_memalign(&allocation, alignment, bytes);
59 if(result != 0)
60 {
61 errno = result;
62 allocation = nullptr;
63 }
64 return allocation;
65 #else
66 unsigned char *block = new unsigned char[bytes + sizeof(Allocation) + alignment];
67 unsigned char *aligned = nullptr;
68
69 if(block)
70 {
71 aligned = (unsigned char*)((uintptr_t)(block + sizeof(Allocation) + alignment - 1) & -(intptr_t)alignment);
72 Allocation *allocation = (Allocation*)(aligned - sizeof(Allocation));
73
74 // allocation->bytes = bytes;
75 allocation->block = block;
76 }
77
78 return aligned;
79 #endif
80 }
81 } // anonymous namespace
82
memoryPageSize()83 size_t memoryPageSize()
84 {
85 static int pageSize = 0;
86
87 if(pageSize == 0)
88 {
89 #if defined(_WIN32)
90 SYSTEM_INFO systemInfo;
91 GetSystemInfo(&systemInfo);
92 pageSize = systemInfo.dwPageSize;
93 #else
94 pageSize = sysconf(_SC_PAGESIZE);
95 #endif
96 }
97
98 return pageSize;
99 }
100
allocate(size_t bytes,size_t alignment)101 void *allocate(size_t bytes, size_t alignment)
102 {
103 void *memory = allocateRaw(bytes, alignment);
104
105 if(memory)
106 {
107 memset(memory, 0, bytes);
108 }
109
110 return memory;
111 }
112
deallocate(void * memory)113 void deallocate(void *memory)
114 {
115 #if defined(LINUX_ENABLE_NAMED_MMAP)
116 free(memory);
117 #else
118 if(memory)
119 {
120 unsigned char *aligned = (unsigned char*)memory;
121 Allocation *allocation = (Allocation*)(aligned - sizeof(Allocation));
122
123 delete[] allocation->block;
124 }
125 #endif
126 }
127
clear(uint16_t * memory,uint16_t element,size_t count)128 void clear(uint16_t *memory, uint16_t element, size_t count)
129 {
130 #if defined(_MSC_VER) && defined(__x86__) && !defined(MEMORY_SANITIZER)
131 __stosw(memory, element, count);
132 #elif defined(__GNUC__) && defined(__x86__) && !defined(MEMORY_SANITIZER)
133 __asm__("rep stosw" : : "D"(memory), "a"(element), "c"(count));
134 #else
135 for(size_t i = 0; i < count; i++)
136 {
137 memory[i] = element;
138 }
139 #endif
140 }
141
clear(uint32_t * memory,uint32_t element,size_t count)142 void clear(uint32_t *memory, uint32_t element, size_t count)
143 {
144 #if defined(_MSC_VER) && defined(__x86__) && !defined(MEMORY_SANITIZER)
145 __stosd((unsigned long*)memory, element, count);
146 #elif defined(__GNUC__) && defined(__x86__) && !defined(MEMORY_SANITIZER)
147 __asm__("rep stosl" : : "D"(memory), "a"(element), "c"(count));
148 #else
149 for(size_t i = 0; i < count; i++)
150 {
151 memory[i] = element;
152 }
153 #endif
154 }
155 }
156