Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 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 | |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 17 | #include "Debug.hpp" |
Ben Clayton | 595d911 | 2019-12-17 20:37:57 +0000 | [diff] [blame] | 18 | #include "Types.hpp" |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 19 | |
| 20 | #if defined(_WIN32) |
Ben Clayton | 595d911 | 2019-12-17 20:37:57 +0000 | [diff] [blame] | 21 | # ifndef WIN32_LEAN_AND_MEAN |
| 22 | # define WIN32_LEAN_AND_MEAN |
| 23 | # endif |
| 24 | # include <windows.h> |
| 25 | # include <intrin.h> |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 26 | #else |
Ben Clayton | 595d911 | 2019-12-17 20:37:57 +0000 | [diff] [blame] | 27 | # include <errno.h> |
| 28 | # include <sys/mman.h> |
| 29 | # include <stdlib.h> |
| 30 | # include <unistd.h> |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 31 | #endif |
| 32 | |
Nicolas Capens | 4cd9767e6 | 2019-07-11 01:13:55 -0400 | [diff] [blame] | 33 | #include <cstdlib> |
Ben Clayton | 595d911 | 2019-12-17 20:37:57 +0000 | [diff] [blame] | 34 | #include <cstring> |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 35 | |
| 36 | #undef allocate |
| 37 | #undef deallocate |
| 38 | |
Ben Clayton | 595d911 | 2019-12-17 20:37:57 +0000 | [diff] [blame] | 39 | #if(defined(__i386__) || defined(_M_IX86) || defined(__x86_64__) || defined(_M_X64)) && !defined(__x86__) |
| 40 | # define __x86__ |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 41 | #endif |
| 42 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 43 | namespace sw { |
| 44 | |
| 45 | namespace { |
| 46 | |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 47 | struct Allocation |
| 48 | { |
Ben Clayton | 595d911 | 2019-12-17 20:37:57 +0000 | [diff] [blame] | 49 | // size_t bytes; |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 50 | unsigned char *block; |
| 51 | }; |
| 52 | |
| 53 | void *allocateRaw(size_t bytes, size_t alignment) |
| 54 | { |
Ben Clayton | 595d911 | 2019-12-17 20:37:57 +0000 | [diff] [blame] | 55 | ASSERT((alignment & (alignment - 1)) == 0); // Power of 2 alignment. |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 56 | |
Ben Clayton | 595d911 | 2019-12-17 20:37:57 +0000 | [diff] [blame] | 57 | #if defined(LINUX_ENABLE_NAMED_MMAP) |
| 58 | if(alignment < sizeof(void *)) |
| 59 | { |
| 60 | return malloc(bytes); |
| 61 | } |
| 62 | else |
| 63 | { |
| 64 | void *allocation; |
| 65 | int result = posix_memalign(&allocation, alignment, bytes); |
| 66 | if(result != 0) |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 67 | { |
Ben Clayton | 595d911 | 2019-12-17 20:37:57 +0000 | [diff] [blame] | 68 | errno = result; |
| 69 | allocation = nullptr; |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 70 | } |
Ben Clayton | 595d911 | 2019-12-17 20:37:57 +0000 | [diff] [blame] | 71 | return allocation; |
| 72 | } |
| 73 | #else |
| 74 | unsigned char *block = (unsigned char *)malloc(bytes + sizeof(Allocation) + alignment); |
| 75 | unsigned char *aligned = nullptr; |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 76 | |
Ben Clayton | 595d911 | 2019-12-17 20:37:57 +0000 | [diff] [blame] | 77 | if(block) |
| 78 | { |
| 79 | aligned = (unsigned char *)((uintptr_t)(block + sizeof(Allocation) + alignment - 1) & -(intptr_t)alignment); |
| 80 | Allocation *allocation = (Allocation *)(aligned - sizeof(Allocation)); |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 81 | |
| 82 | // allocation->bytes = bytes; |
Ben Clayton | 595d911 | 2019-12-17 20:37:57 +0000 | [diff] [blame] | 83 | allocation->block = block; |
| 84 | } |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 85 | |
Ben Clayton | 595d911 | 2019-12-17 20:37:57 +0000 | [diff] [blame] | 86 | return aligned; |
| 87 | #endif |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 88 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 89 | |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 90 | } // anonymous namespace |
| 91 | |
| 92 | size_t memoryPageSize() |
| 93 | { |
| 94 | static int pageSize = 0; |
| 95 | |
| 96 | if(pageSize == 0) |
| 97 | { |
Ben Clayton | 595d911 | 2019-12-17 20:37:57 +0000 | [diff] [blame] | 98 | #if defined(_WIN32) |
| 99 | SYSTEM_INFO systemInfo; |
| 100 | GetSystemInfo(&systemInfo); |
| 101 | pageSize = systemInfo.dwPageSize; |
| 102 | #else |
| 103 | pageSize = sysconf(_SC_PAGESIZE); |
| 104 | #endif |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | return pageSize; |
| 108 | } |
| 109 | |
| 110 | void *allocate(size_t bytes, size_t alignment) |
| 111 | { |
| 112 | void *memory = allocateRaw(bytes, alignment); |
| 113 | |
| 114 | if(memory) |
| 115 | { |
| 116 | memset(memory, 0, bytes); |
| 117 | } |
| 118 | |
| 119 | return memory; |
| 120 | } |
| 121 | |
| 122 | void deallocate(void *memory) |
| 123 | { |
Ben Clayton | 595d911 | 2019-12-17 20:37:57 +0000 | [diff] [blame] | 124 | #if defined(LINUX_ENABLE_NAMED_MMAP) |
| 125 | free(memory); |
| 126 | #else |
| 127 | if(memory) |
| 128 | { |
| 129 | unsigned char *aligned = (unsigned char *)memory; |
| 130 | Allocation *allocation = (Allocation *)(aligned - sizeof(Allocation)); |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 131 | |
Ben Clayton | 595d911 | 2019-12-17 20:37:57 +0000 | [diff] [blame] | 132 | free(allocation->block); |
| 133 | } |
| 134 | #endif |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 135 | } |
| 136 | |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 137 | void clear(uint16_t *memory, uint16_t element, size_t count) |
| 138 | { |
Ben Clayton | 595d911 | 2019-12-17 20:37:57 +0000 | [diff] [blame] | 139 | #if defined(_MSC_VER) && defined(__x86__) && !defined(MEMORY_SANITIZER) |
| 140 | __stosw(memory, element, count); |
| 141 | #elif defined(__GNUC__) && defined(__x86__) && !defined(MEMORY_SANITIZER) |
| 142 | __asm__ __volatile__("rep stosw" |
| 143 | : "+D"(memory), "+c"(count) |
| 144 | : "a"(element) |
| 145 | : "memory"); |
| 146 | #else |
| 147 | for(size_t i = 0; i < count; i++) |
| 148 | { |
| 149 | memory[i] = element; |
| 150 | } |
| 151 | #endif |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | void clear(uint32_t *memory, uint32_t element, size_t count) |
| 155 | { |
Ben Clayton | 595d911 | 2019-12-17 20:37:57 +0000 | [diff] [blame] | 156 | #if defined(_MSC_VER) && defined(__x86__) && !defined(MEMORY_SANITIZER) |
| 157 | __stosd((unsigned long *)memory, element, count); |
| 158 | #elif defined(__GNUC__) && defined(__x86__) && !defined(MEMORY_SANITIZER) |
| 159 | __asm__ __volatile__("rep stosl" |
| 160 | : "+D"(memory), "+c"(count) |
| 161 | : "a"(element) |
| 162 | : "memory"); |
| 163 | #else |
| 164 | for(size_t i = 0; i < count; i++) |
| 165 | { |
| 166 | memory[i] = element; |
| 167 | } |
| 168 | #endif |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 169 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 170 | |
| 171 | } // namespace sw |