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