---
title: "preallocateSpace(for:at:length:flags:packer:context:replyHandler:)"
framework: fskit
role: symbol
role_heading: Instance Method
path: "fskit/fsvolume/kerneloffloadediohandler/preallocatespace(for:at:length:flags:packer:context:replyhandler:)"
---

# preallocateSpace(for:at:length:flags:packer:context:replyHandler:)

Preallocates and maps disk space for the given file.

## Declaration

```swift
optional func preallocateSpace(for file: FSItem, at offset: off_t, length: Int, flags: FSVolume.PreallocateFlags, packer: FSExtentPacker, context: FSContext, replyHandler reply: @escaping @Sendable (FSPreallocateKOIOResult?, (any Error)?) -> Void)
```

```swift
optional func preallocateSpace(for file: FSItem, at offset: off_t, length: Int, flags: FSVolume.PreallocateFlags, packer: FSExtentPacker, context: FSContext) async throws -> FSPreallocateKOIOResult
```

## Parameters

- `file`: The item for which to preallocate space.
- `offset`: The offset from which to allocate.
- `length`: The length of the space in bytes.
- `flags`: Flags that affect the preallocation behavior.
- `packer`: An extent packer you use to pack the file’s preallocated disk space.
- `context`: An object that enables context-aware file system decisions throughout the operation.

## Discussion

Discussion ::::: Swift :::::::::: reply: A block or closure to indicate success or failure. If preallocation succeeds, pass an instance of FSPreallocateKOIOResult containing the amount of bytes allocated, the updated FSItem.Attributes of the file, the volume’s update free space, along with a nil error. If preallocation fails, pass the relevant error as the second parameter; FSKit ignores the FSPreallocateKOIOResult instance in this case. For an async Swift implementation, there’s no reply handler; simply return the result instance or throw an error. :::::::::::::::::::: ::::: ObjC :::::::::: result: A block or closure to indicate success or failure. If preallocation succeeds, pass an instance of FSPreallocateKOIOResult containing the amount of bytes allocated, the updated FSItem.Attributes of the file, the volume’s update free space, along with a nil error. If preallocation fails, pass the relevant error as the second parameter; FSKit ignores the FSPreallocateKOIOResult instance in this case. For an async Swift implementation, there’s no reply handler; simply return the result instance or throw an error. :::::::::::::::::::: Discussion This method allows the module to opportunistically supply extents, avoiding future calls to blockmapFile(_:offset:length:flags:operationID:packer:replyHandler:). important: Only implement this method if your file system conforms to FSVolume.PreallocateHandler.

## See Also

### Working with items

- [createFile(named:in:attributes:packer:context:replyHandler:)](fskit/fsvolume/kerneloffloadediohandler/createfile(named:in:attributes:packer:context:replyhandler:).md)
- [FSItem.SetAttributesRequest](fskit/fsitem/setattributesrequest.md)
- [FSCreateFileKOIOResult](fskit/fscreatefilekoioresult.md)
- [lookupItem(named:in:packer:context:replyHandler:)](fskit/fsvolume/kerneloffloadediohandler/lookupitem(named:in:packer:context:replyhandler:).md)
- [FSLookupItemKOIOResult](fskit/fslookupitemkoioresult.md)
- [FSVolume.PreallocateFlags](fskit/fsvolume/preallocateflags.md)
- [FSPreallocateKOIOResult](fskit/fspreallocatekoioresult.md)
