1 // Copyright 2018 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 "VkMemory.hpp"
16
17 #include "VkConfig.hpp"
18 #include "System/Memory.hpp"
19
20 namespace vk {
21
allocateDeviceMemory(size_t bytes,size_t alignment)22 void *allocateDeviceMemory(size_t bytes, size_t alignment)
23 {
24 // TODO(b/140991626): Use allocateZeroOrPoison() instead of allocateZero() to detect MemorySanitizer errors.
25 #if defined(SWIFTSHADER_ZERO_INITIALIZE_DEVICE_MEMORY)
26 return sw::allocateZero(bytes, alignment);
27 #else
28 // TODO(b/140991626): Use allocateUninitialized() instead of allocateZeroOrPoison() to improve startup peformance.
29 return sw::allocateZeroOrPoison(bytes, alignment);
30 #endif
31 }
32
freeDeviceMemory(void * ptr)33 void freeDeviceMemory(void *ptr)
34 {
35 sw::freeMemory(ptr);
36 }
37
allocateHostMemory(size_t bytes,size_t alignment,const VkAllocationCallbacks * pAllocator,VkSystemAllocationScope allocationScope)38 void *allocateHostMemory(size_t bytes, size_t alignment, const VkAllocationCallbacks *pAllocator, VkSystemAllocationScope allocationScope)
39 {
40 if(pAllocator)
41 {
42 return pAllocator->pfnAllocation(pAllocator->pUserData, bytes, alignment, allocationScope);
43 }
44 else
45 {
46 // TODO(b/140991626): Use allocateUninitialized() instead of allocateZeroOrPoison() to improve startup peformance.
47 return sw::allocateZeroOrPoison(bytes, alignment);
48 }
49 }
50
freeHostMemory(void * ptr,const VkAllocationCallbacks * pAllocator)51 void freeHostMemory(void *ptr, const VkAllocationCallbacks *pAllocator)
52 {
53 if(pAllocator)
54 {
55 pAllocator->pfnFree(pAllocator->pUserData, ptr);
56 }
57 else
58 {
59 sw::freeMemory(ptr);
60 }
61 }
62
63 } // namespace vk
64