Version

menu_open
Wwise SDK 2024.1.4
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) 2025 Audiokinetic Inc.
25 *******************************************************************************/
26 
27 #pragma once
28 
30 #include <sys/dmem.h>
31 #include <sceerror.h>
32 
33 #define AK_VM_PAGE_SIZE (16*1024)
34 #define AK_VM_HUGE_PAGE_SIZE (2*1024*1024)
35 #define AK_VM_DEVICE_PAGE_SIZE (16*1024)
36 
37 namespace AKPLATFORM
38 {
39  // In order to use ACM hardware accelerated functions, Wwise must have GPU-visible memory
40  const int kMemoryFlags = SCE_KERNEL_PROT_CPU_RW | SCE_KERNEL_PROT_GPU_RW | SCE_KERNEL_PROT_AMPR_ALL;
41  const int kDeviceMemoryFlags = SCE_KERNEL_PROT_CPU_RW | SCE_KERNEL_PROT_GPU_RW | SCE_KERNEL_PROT_AMPR_ALL | SCE_KERNEL_PROT_ACP_RW;
42 
43  AkForceInline void* AllocSpan(size_t size, size_t* out_userData)
44  {
46  off_t directMemStart = 0;
47  void* ptr = NULL;
48 
49  int32_t err;
50  // if size lines up with huge pages, set alignment up similarly
51  int32_t alignment = (uRoundedSize % AK_VM_HUGE_PAGE_SIZE == 0) ? AK_VM_HUGE_PAGE_SIZE : AK_VM_PAGE_SIZE;
52  err = sceKernelAllocateMainDirectMemory(uRoundedSize, alignment, SCE_KERNEL_MTYPE_C_SHARED, &directMemStart);
53  if (err == SCE_OK)
54  {
55  // allocate with device memory flags all of the time, in case a separate device heap is not in use
56  err = sceKernelMapDirectMemory(&ptr, uRoundedSize, AKPLATFORM::kDeviceMemoryFlags, 0, directMemStart, alignment);
57  AKASSERT(ptr);
58  AKASSERT(err == SCE_OK);
59 
60  *out_userData = (size_t)directMemStart;
61  }
62  return ptr;
63  }
64 
65  AkForceInline void FreeSpan(void* address, size_t size, size_t in_userData)
66  {
67  AKASSERT(in_userData);
69  int32_t err = sceKernelReleaseDirectMemory((off_t)in_userData, uRoundedSize);
70  AKASSERT(err == SCE_OK);
71  }
72 
73  // Default device-specific sample allocation functions can call stock sample allocation functions -- they handle device memory just fine
74  AkForceInline void* AllocDeviceSpan(size_t size, size_t* out_userData)
75  {
76  return AllocSpan(size, out_userData);
77  }
78 
79  AkForceInline void FreeDeviceSpan(void* address, size_t size, size_t in_userData)
80  {
81  FreeSpan(address, size, in_userData);
82  }
83 
84  AkForceInline bool CheckMemoryProtection(void* address, size_t size, int expectedProt)
85  {
86  AkUIntPtr addressEnd = (AkUIntPtr)address + size - 1;
87 
88  // Check protection flags apply to the whole range
89  while ((AkUIntPtr)address < addressEnd)
90  {
91  SceKernelVirtualQueryInfo vqInfo;
92  int err = sceKernelVirtualQuery(address, SCE_KERNEL_VQ_FIND_NEXT, &vqInfo, sizeof(vqInfo));
93  if (err != SCE_OK)
94  return false;
95 
96  // Check protection flags
97  if ((vqInfo.protection & expectedProt) != expectedProt)
98  return false;
99 
100  if ((AkUIntPtr)vqInfo.start > (AkUIntPtr)address)
101  return false;
102 
103  address = vqInfo.end;
104  }
105 
106  return true;
107  }
108 }
AkForceInline void FreeDeviceSpan(void *address, size_t size, size_t in_userData)
#define AK_ALIGN_TO_NEXT_BOUNDARY(__num__, __boundary__)
Definition: AkPlatforms.h:109
Platform-dependent helpers.
AkForceInline void * AllocDeviceSpan(size_t size, size_t *out_userData)
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 AKASSERT(Condition)
Definition: AkAssert.h:67
AkForceInline void FreeSpan(void *address, size_t size, size_t in_userData)
const int kDeviceMemoryFlags
uint32_t AkUInt32
Unsigned 32-bit integer.
#define AK_VM_PAGE_SIZE
#define AkForceInline
Definition: AkTypes.h:63
#define AK_VM_HUGE_PAGE_SIZE
AkForceInline void * AllocSpan(size_t size, size_t *out_userData)

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