Audiokinetic Wwise Knowledge Base

Do I really need to call ::VirtualAlloc() in my implementation of AK::VirtualAllocHook()?

Allocation hooks are called by the Wwise Memory Manager whenever it creates a pool. You need to implement all of them for a given platform (see the SDK documentation: Sound Engine Integration Walkthrough > Initialize the Different Modules of the Sound Engine > Memory Hooks). On Windows, AK::VirtualAllocHook() is called whenever a submodule of the sound engine calls AK::MemoryMgr::CreatePool() with the AkVirtualAlloc flag (refer to the definition of AK::MemoryMgr::CreatePool() and the AkMemPoolAttributes enumeration). Calling ::VirtualAlloc() inside your implementation of AK::VirtualAllocHook() is not mandatory. The following paragraphs explain what it is used for.

Currently, of all the Wwise sound engine and its modules, the only client of the Memory Manager which calls CreatePool() with the AkVirtualAlloc flag is the streaming device. You create a streaming device by calling AK::StreamMgr::CreateDevice() (or alternatively, by calling the Init() function of one of our Low-Level IO samples, which itself calls AK::StreamMgr::CreateDevice()). If the device's initialization setting AkDeviceSettings::pIOMemory is NULL (default), the device creates its own memory pool for I/O transfers, with a size defined by AkDeviceSettings::uIOMemorySize, and attributes defined by AkDeviceSettings::ePoolAttributes. These attributes are passed directly to AK::MemoryMgr::CreatePool(). The default AkDeviceSettings::ePoolAttributes (as obtained from AK::StreamMgr::GetDefaultDeviceSettings()) on Windows is AkVirtualAlloc.

The sample implementation of the deferred low-level IO hook, CAkDefaultIOHookDeferred, calls ::CreateFile() with both FILE_FLAG_NO_BUFFERING and FILE_FLAG_OVERLAPPED flags (the CAkDefaultIOHookDeferred sample is located in WWISESDK/samples/SoundEngine/Windows/). Windows' documentation says that using these two flags together gives maximum performance, provided that all I/O transfers occur on disk sector aligned addresses. They suggest using ::VirtualAlloc() to ensure that memory is properly aligned. Read ::CreateFile()'s documentation for more details, where FILE_FLAG_NO_BUFFERING is described.

Notice that the value returned by CAkDefaultIOHookDeferred::GetBlockSize() is 2048 bytes, which is always(?) a multiple of disks' sector size. An aligned block size, combined with an aligned memory pool, results in I/O transfers that are always aligned and thus, suited for true unbuffered I/O.

If you are not using the deferred I/O hook sample, or do not create files with the FILE_FLAG_NO_BUFFERING flag, using aligned memory will not help you optimize your I/O transfers, thus you may use malloc() in your implementation of AK::VirtualAllocHook(). Alternatively, you may set AkDeviceSettings::ePoolAttributes to AkMalloc before creating the streaming device. Doing so, AK::VirtualAllocHook() would never be called.

If your own memory manager supports aligned allocations, then you may interpret AK::VirtualAllocHook() as being a request for memory aligned on the disk's sector size.




Article Details

Last Updated
4th o March, 2010

Would you like to...

Print this page Print this page

Email this page Email this page

Post a comment Post a comment

Subscribe me

Add to favorites Add to favorites

Remove Highlighting Remove Highlighting

Edit this Article

Quick Edit

Export to PDF

User Opinions (0 votes)

No users have voted.

How would you rate this answer?



Thank you for rating this answer.

Related Articles

No related articles were found.

Attachments

No attachments were found.

Continue