Xcode 7.2b4 (7C62b): [Swift] UnsafeMutablePointer should offer an allocation method that also returns the actual capacity

Originator:janoschhildebrand
Number:rdar://23637848 Date Originated:21-Nov-2015
Status:Open Resolved:
Product:Developer Tools Product Version:Xcode 7.2b4 (7C62b)
Classification:Feature Request Reproducible:n/a
 
Summary:
Currently, manual memory allocation in native Swift works by calling UnsafeMutablePointer.alloc(capacity) which returns an UnsafeMutablePointer.

In practice, many memory allocators only allocate memory in specific sizes, so the memory we get from UnsafeMutablePointer.alloc(capacity) might actually have a larger capacity than what we asked for. Currently Swift offers no native way to use the actual allocated capacity.

What I'm proposing is that UnsafeMutablePointer offer another method to allocate memory that also returns the actual capacity of the new memory.

For example a potential version could look like this:

extension UnsafeMutablePointer {
    // TODO: find a better name
    static func allocSmart(minimumCapacity: Int) -> (UnsafeMutablePointer<Memory>, Int) {
        // Example implementation using fact that Swift currently uses malloc for memory allocation
        let stride = strideof(Memory)
        let actualCapacity = malloc_good_size(stride * minimumCapacity) / stride
        return (alloc(actualCapacity), actualCapacity)
    }
}

Steps to Reproduce:


Expected Results:


Actual Results:


Version:
Xcode 7.2b4 (7C62b)
Apple Swift version 2.1.1 (swiftlang-700.1.101.13 clang-700.1.81)
OS X 10.11.1 (15B42)

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!