<!--
{
  "availability" : [
    "iOS: 18.0.0 -",
    "iPadOS: 18.0.0 -",
    "macCatalyst: 18.0.0 -",
    "macOS: 15.0.0 -",
    "tvOS: 18.0.0 -",
    "visionOS: 2.0.0 -",
    "watchOS: 11.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Accelerate",
  "identifier" : "/documentation/Accelerate/BNNSGraphContextSetWorkspaceAllocationCallback(_:_:_:_:_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Function",
  "symbol" : {
    "kind" : "Function",
    "modules" : [
      "Accelerate"
    ],
    "preciseIdentifier" : "c:@F@BNNSGraphContextSetWorkspaceAllocationCallback"
  },
  "title" : "BNNSGraphContextSetWorkspaceAllocationCallback(_:_:_:_:_:)"
}
-->

# BNNSGraphContextSetWorkspaceAllocationCallback(_:_:_:_:_:)

Sets the allocation and deallocation callbacks for internal workspace.

```
func BNNSGraphContextSetWorkspaceAllocationCallback(_ context: bnns_graph_context_t, _ realloc: bnns_graph_realloc_fn_t?, _ free: bnns_graph_free_all_fn_t?, _ user_memory_context_size: Int, _ user_memory_context: UnsafeMutableRawPointer?) -> Int32
```

## Parameters

`context`

The graph context.

`realloc`

The memory allocation and reallocation function. If you pass `nil` for this parameter, you must also set `nil` for the `free` parameter. In this case, graph execution uses the default BNNS allocation mechanism.

`free`

The memory allocation and reallocation function. If you pass `nil` for this parameter, you must also set `nil` for the `realloc` parameter. In this case, graph execution uses the default BNNS allocation mechanism.

`user_memory_context_size`

The size, in bytes, `user_memory_context`.

`user_memory_context`

A pointer that that BNNS passes unmodified in all calls to `realloc` and `free`.

## Return Value

`0` on success, nonzero on failure.

## Discussion

If BNNS can’t bound the required workspace size prior to execution — for example, if the tensor sizes depend on input data — it allocates workspace during execution. Use this function to override the default memory allocation mechanisms.

Note that BNNS typically calls the `free` function when it destroys the context, but it only requires the workspace during execution. As such, you may wish to make the allocated memory purgable or free it between calls to [`BNNSGraphContextExecute(_:_:_:_:_:_:)`](/documentation/Accelerate/BNNSGraphContextExecute(_:_:_:_:_:_:)).

If you pass the same `user_memory_context` to [`BNNSGraphContextSetWorkspaceAllocationCallback(_:_:_:_:_:)`](/documentation/Accelerate/BNNSGraphContextSetWorkspaceAllocationCallback(_:_:_:_:_:)) and [`BNNSGraphContextSetOutputAllocationCallback(_:_:_:_:_:)`](/documentation/Accelerate/BNNSGraphContextSetOutputAllocationCallback(_:_:_:_:_:)), BNNS only calls the `free` function once.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)