1.5. Memory Management

Passing Memory to the Pia Library

Pia requires its own working memory. The application must explicitly supply enough working memory for the Pia library to run properly.

Use the nn::pia::common::Initialize() function to provide memory to the Pia library. This function's parameters are a pointer to the start of the memory block and the size of that memory block. This memory can also be used from modules other than PiaCommon.

Code 1-3. Initializing Pia and Providing Memory
// Use a size that is large enough.
// You can change it later as necessary.
const u32 MEMORY_SIZE = 1024 * 1024;

PIA_ATTRIBUTE_ALIGN(4) static u8 s_memory[MEMORY_SIZE];
nn::Result nn::pia::common::Initialize(s_memory, MEMORY_SIZE);

 

Getting the Specific Amount of Memory Used by Pia

The Pia library provides a mechanism to allow applications to get the minimum memory required for Pia operations, depending on how Pia is used in that application.

Use the nn::pia::common::GetMemoryUsage() function to get the Pia library's memory consumption. Call this function immediately after EndSetup() in each module required by the application. When called, the specific amount of memory used by Pia will be returned. You can refer to the value of GetMemoryUsage() immediately after EndSetup() in the final module, and adjust the supplied amount of memory with nn::pia::common::Initialize().

During the early and middle phases of application development, use the value returned by this function, plus a little extra, when calling the nn::pia::common::Initialize() function. When fine-tuning during the final stages of development, you can reduce the amount of memory specified as you optimize the Pia library's memory use.

 

Code 1-4. Getting the Specific Amount of Memory Used by Pia
// When calling nn::pia::common::Initialize(),
// you must provide at least this much memory.
u32 usage = nn::pia::common::GetMemoryUsage();