> ## Documentation Index
> Fetch the complete documentation index at: https://docs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# PFMemAllocFunction

> Callback that PlayFab calls to allocate a memory buffer of a given size after a title installs custom memory hooks with PFMemSetFunctions.

A callback that PlayFab calls every time it needs to dynamically allocate a memory buffer.

## Syntax

```cpp theme={null}
void* STDAPIVCALLTYPE PFMemAllocFunction(
    size_t size,
    uint32_t memoryTypeId
);
```

### Parameters

**`size`**   `_In_` size\_t

The size, in bytes, of the allocation to make. This value is never zero.

**`memoryTypeId`**   `_In_` uint32\_t

An opaque identifier for the internal category of memory being allocated. Ignore this value.

### Return value

Type: void\*

A pointer to an allocated block of memory of the specified size, or `nullptr` if the allocation failed. The header annotates the return value with `_Ret_maybenull_` and `_Post_writable_byte_size_(size)`.

## Remarks

To install this callback, set it in the `alloc` member of a [PFMemoryHooks](https://docs.xbox.com/services/playfab/api-references/c/pfplatform/structs/pfmemoryhooks.md) structure and pass the structure to [PFMemSetFunctions](https://docs.xbox.com/services/playfab/api-references/c/pfplatform/functions/pfmemsetfunctions.md) before you call [PFInitialize](https://docs.xbox.com/services/playfab/api-references/c/pfcore/functions/pfinitialize.md). Set the `free` member to a matching [PFMemFreeFunction](https://docs.xbox.com/services/playfab/api-references/c/pfplatform/functions/pfmemfreefunction.md) callback at the same time. PFMemSetFunctions returns `E_INVALIDARG` if only one of the two members is set.

The callback must return a pointer to a contiguous block of memory of the requested size. The block must stay valid until PlayFab passes it to your PFMemFreeFunction callback. PlayFab passes every non-null pointer that this callback returns to your PFMemFreeFunction callback when it no longer needs the memory.

If libHttpClient isn't initialized yet when you call PFMemSetFunctions, PlayFab also installs hooks that route libHttpClient's allocations through this callback.

To get the hooks that are currently in use, including the default hooks, call [PFMemGetFunctions](https://docs.xbox.com/services/playfab/api-references/c/pfplatform/functions/pfmemgetfunctions.md).

PlayFab can call this callback from different threads, so make it thread-safe.

## Requirements

**Header:** PFPlatform.h

**Library:** PlayFabCore.lib

**Supported platforms:** Windows, XBOX One family consoles, and XBOX Series consoles

## See also

[PFPlatform members](https://docs.xbox.com/services/playfab/api-references/c/pfplatform/pfplatform_members.md)

[PFMemoryHooks](https://docs.xbox.com/services/playfab/api-references/c/pfplatform/structs/pfmemoryhooks.md)

[PFMemFreeFunction](https://docs.xbox.com/services/playfab/api-references/c/pfplatform/functions/pfmemfreefunction.md)


## Related topics

- [PFMemFreeFunction](https://docs.xbox.com/services/playfab/api-references/c/pfplatform/functions/pfmemfreefunction.md)
- [PFMemoryHooks](https://docs.xbox.com/services/playfab/api-references/c/pfplatform/structs/pfmemoryhooks.md)
- [Services C API overview - PFPlatform.h](https://docs.xbox.com/services/playfab/api-references/c/pfplatform/pfplatform_members.md)
- [HCMemAllocFunction](https://docs.xbox.com/reference/live/httpclient/httpclient/functions/hcmemallocfunction.md)
- [XblMemAllocFunction](https://docs.xbox.com/reference/live/xsapi-c/xbox_live_global_c/functions/xblmemallocfunction.md)
