xref: /aosp_15_r20/external/swiftshader/src/Vulkan/VkMemory.cpp (revision 03ce13f70fcc45d86ee91b7ee4cab1936a95046e)
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/Debug.hpp"
19 #include "System/Memory.hpp"
20 
21 namespace vk {
22 
allocateDeviceMemory(size_t bytes,size_t alignment)23 void *allocateDeviceMemory(size_t bytes, size_t alignment)
24 {
25 	ASSERT(bytes <= vk::MAX_MEMORY_ALLOCATION_SIZE);
26 
27 #if defined(SWIFTSHADER_ZERO_INITIALIZE_DEVICE_MEMORY)
28 	return sw::allocateZeroOrPoison(bytes, alignment);
29 #else
30 	return sw::allocate(bytes, alignment);
31 #endif
32 }
33 
freeDeviceMemory(void * ptr)34 void freeDeviceMemory(void *ptr)
35 {
36 	sw::freeMemory(ptr);
37 }
38 
allocateHostMemory(size_t bytes,size_t alignment,const VkAllocationCallbacks * pAllocator,VkSystemAllocationScope allocationScope)39 void *allocateHostMemory(size_t bytes, size_t alignment, const VkAllocationCallbacks *pAllocator, VkSystemAllocationScope allocationScope)
40 {
41 	ASSERT(bytes <= vk::MAX_MEMORY_ALLOCATION_SIZE);
42 
43 	if(pAllocator)
44 	{
45 		return pAllocator->pfnAllocation(pAllocator->pUserData, bytes, alignment, allocationScope);
46 	}
47 	else
48 	{
49 		// TODO(b/140991626): Use allocate() instead of allocateZeroOrPoison() to improve startup performance.
50 		return sw::allocateZeroOrPoison(bytes, alignment);
51 	}
52 }
53 
freeHostMemory(void * ptr,const VkAllocationCallbacks * pAllocator)54 void freeHostMemory(void *ptr, const VkAllocationCallbacks *pAllocator)
55 {
56 	if(pAllocator)
57 	{
58 		pAllocator->pfnFree(pAllocator->pUserData, ptr);
59 	}
60 	else
61 	{
62 		sw::freeMemory(ptr);
63 	}
64 }
65 
66 }  // namespace vk
67