CFAllocatorReallocate(_:_:_:_:)
Reallocates memory using the specified allocator.
Declaration
func CFAllocatorReallocate(_ allocator: CFAllocator!, _ ptr: UnsafeMutableRawPointer!, _ newsize: CFIndex, _ hint: CFOptionFlags) -> UnsafeMutableRawPointer!Parameters
- allocator:
The allocator to use for reallocating memory. Pass
NULLto request the default allocator. - ptr:
An untyped pointer to a block of memory to reallocate to a new size. If
ptrisNULLandnewsizeis greater than 0, memory is allocated (using theallocatefunction callback of the allocator’s context). IfptrisNULLandnewsizeis 0, the result isNULL. - newsize:
The number of bytes to allocate. If you pass
0and theptrparameter is non-NULL, the block of memory thatptrpoints to is typically deallocated. If you pass 0 for this parameter and theptrparameter isNULL, nothing happens and the result returned isNULL. - hint:
A bitfield of type
CFOptionsFlags. Pass flags to the allocator that suggest how memory is to be allocated. Zero indicates no hints. No hints are currently defined, only0should be passed for this argument.
Discussion
The CFAllocatorReallocate function’s primary purpose is to reallocate a block of memory to a new (and usually larger) size. However, based on the values passed in certain of the parameters, this function can also allocate memory afresh or deallocate a given block of memory. The following summarizes the semantic combinations:
If the
ptrparameter is non-NULLand thenewsizeparameter is greater than0, the behavior is to reallocate.If the
ptrparameter isNULLand thenewsizeparameter is greater than0, the behavior is to allocate.If the
ptrparameter is non-NULLand thenewsizeparameter is0, the behavior is to deallocate.
The result of the CFAllocatorReallocate function is either an untyped pointer to a block of memory or NULL. A NULL result indicates either a failure to allocate memory or some other outcome, the precise interpretation of which is determined by the values of certain parameters and the presence or absence of callbacks in the allocator context. To summarize, a NULL result can mean one of the following:
An error occurred in the attempt to allocate memory, such as insufficient free space.
No
allocate,reallocate, ordeallocatefunction callback (depending on parameters) was defined in the allocator context.The semantic operation is “deallocate” (that is, there is no need to return anything).
The
ptrparameter isNULLand the requested size is 0.