Version
menu_open
link
Wwise SDK 2023.1.3
AkMemoryMgrFuncs.h
Go to the documentation of this file.
1 /*******************************************************************************
2 The content of this file includes portions of the AUDIOKINETIC Wwise Technology
3 released in source code form as part of the SDK installer package.
4 
5 Commercial License Usage
6 
7 Licensees holding valid commercial licenses to the AUDIOKINETIC Wwise Technology
8 may use this file in accordance with the end user license agreement provided
9 with the software or, alternatively, in accordance with the terms contained in a
10 written agreement between you and Audiokinetic Inc.
11 
12 Apache License Usage
13 
14 Alternatively, this file may be used under the Apache License, Version 2.0 (the
15 "Apache License"); you may not use this file except in compliance with the
16 Apache License. You may obtain a copy of the Apache License at
17 http://www.apache.org/licenses/LICENSE-2.0.
18 
19 Unless required by applicable law or agreed to in writing, software distributed
20 under the Apache License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
21 OR CONDITIONS OF ANY KIND, either express or implied. See the Apache License for
22 the specific language governing permissions and limitations under the License.
23 
24  Copyright (c) 2024 Audiokinetic Inc.
25 *******************************************************************************/
26 
27 #pragma once
28 
30 #include <sys/dmem.h>
31 #include <sceerror.h>
32 
33 #define AK_VM_PAGE_SIZE (64*1024)
34 #define AK_VM_HUGE_PAGE_SIZE (2*1024*1024)
35 #define AK_VM_DEVICE_PAGE_SIZE (64*1024)
36 
37 namespace AKPLATFORM
38 {
39 
40  // In order to use ACM hardware accelerated functions, Wwise must have GPU-visible memory
41  const int kMemoryFlags = SCE_KERNEL_PROT_CPU_RW | SCE_KERNEL_PROT_GPU_RW | SCE_KERNEL_PROT_AMPR_ALL;
42  const int kDeviceMemoryFlags = SCE_KERNEL_PROT_CPU_RW | SCE_KERNEL_PROT_GPU_RW | SCE_KERNEL_PROT_AMPR_ALL | SCE_KERNEL_PROT_ACP_RW;
43 
44  AkForceInline void* AllocVM(size_t size, size_t* extra)
45  {
46  AKASSERT((size % AK_VM_PAGE_SIZE) == 0);
47  off_t directMemStart = 0;
48  void* ptr = NULL;
49 
50  int32_t err;
51  // if size lines up with huge pages, set alignment up similarly
52  int32_t alignment = (size % AK_VM_HUGE_PAGE_SIZE == 0) ? AK_VM_HUGE_PAGE_SIZE : AK_VM_PAGE_SIZE;
53  err = sceKernelAllocateMainDirectMemory(size, alignment, SCE_KERNEL_MTYPE_C_SHARED, &directMemStart);
54  if (err == SCE_OK)
55  {
56  // allocate with device memory flags all of the time, in case a separate device heap is not in use
57  err = sceKernelMapDirectMemory(&ptr, size, AKPLATFORM::kDeviceMemoryFlags, 0, directMemStart, alignment);
58  AKASSERT(ptr);
59  AKASSERT(err == SCE_OK);
60 
61  *extra = (size_t)directMemStart;
62  }
63  return ptr;
64  }
65 
66  AkForceInline void FreeVM(void* address, size_t size, size_t extra, size_t release)
67  {
68  if (release)
69  {
70  AKASSERT(extra);
71  off_t directMemStart = (off_t)extra;
72  int32_t err = sceKernelReleaseDirectMemory(directMemStart, release);
73  AKASSERT(err == SCE_OK);
74  }
75  }
76 
77  AkForceInline void* AllocDevice(size_t size, size_t* extra)
78  {
79  AKASSERT((size % AK_VM_DEVICE_PAGE_SIZE) == 0);
80  off_t directMemStart = 0;
81  void* ptr = NULL;
82 
83  int32_t err;
84  // if size lines up with huge pages, set alignment up similarly
85  int32_t alignment = (size % AK_VM_HUGE_PAGE_SIZE == 0) ? AK_VM_HUGE_PAGE_SIZE : AK_VM_DEVICE_PAGE_SIZE;
86  err = sceKernelAllocateMainDirectMemory(size, alignment, SCE_KERNEL_MTYPE_C_SHARED, &directMemStart);
87  if (err == SCE_OK)
88  {
89  err = sceKernelMapDirectMemory(&ptr, size, AKPLATFORM::kDeviceMemoryFlags, 0, directMemStart, alignment);
90  AKASSERT(ptr);
91  AKASSERT(err == SCE_OK);
92 
93  *extra = (size_t)directMemStart;
94  }
95  return ptr;
96  }
97 
98  AkForceInline void FreeDevice(void* address, size_t size, size_t extra, size_t release)
99  {
100  if (release)
101  {
102  AKASSERT(extra);
103  off_t directMemStart = (off_t)extra;
104  int32_t err = sceKernelReleaseDirectMemory(directMemStart, release);
105  AKASSERT(err == SCE_OK);
106  }
107  }
108 
109  AkForceInline bool CheckMemoryProtection(void* address, size_t size, int expectedProt)
110  {
111  void* pStart, * pEnd;
112  int prot;
113 
114  AkUIntPtr addressEnd = (AkUIntPtr)address + size - 1;
115 
116  // Check protection flags apply to the whole range
117  while ((AkUIntPtr)address < addressEnd)
118  {
119  int err = sceKernelQueryMemoryProtection(address, &pStart, &pEnd, &prot);
120  if (err != SCE_OK)
121  return false;
122 
123  // Check protection flags
124  if ((prot & expectedProt) != expectedProt)
125  return false;
126 
127  if ((AkUIntPtr)pStart > (AkUIntPtr)address)
128  return false;
129 
130  address = pEnd;
131  }
132 
133  return true;
134  }
135 
136 
137 }
Platform-dependent helpers.
Definition: AkBitFuncs.h:43
AkForceInline bool CheckMemoryProtection(void *address, size_t size, int expectedProt)
#define NULL
Definition: AkTypes.h:46
const int kMemoryFlags
uintptr_t AkUIntPtr
Integer (unsigned) type for pointers.
#define AK_VM_DEVICE_PAGE_SIZE
AkForceInline void * AllocVM(size_t size, size_t *extra)
#define AKASSERT(Condition)
Definition: AkAssert.h:67
const int kDeviceMemoryFlags
AkForceInline void * AllocDevice(size_t size, size_t *extra)
#define AK_VM_PAGE_SIZE
#define AkForceInline
Definition: AkTypes.h:63
AkForceInline void FreeDevice(void *address, size_t size, size_t extra, size_t release)
#define AK_VM_HUGE_PAGE_SIZE
AkForceInline void FreeVM(void *address, size_t size, size_t extra, size_t release)

Was this page helpful?

Need Support?

Questions? Problems? Need more info? Contact us, and we can help!

Visit our Support page

Tell us about your project. We're here to help.

Register your project and we'll help you get started with no strings attached!

Get started with Wwise