forked from TextureGroup/Texture
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create transfer-array method and use it (TextureGroup#987)
* Create transfer-array method and use it * License headers * Update ASArrayByFlatMapping
- Loading branch information
1 parent
ccd1bca
commit c9050c1
Showing
15 changed files
with
213 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// | ||
// ASCollections.h | ||
// Texture | ||
// | ||
// Copyright (c) 2018-present, Pinterest, Inc. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@interface NSArray<__covariant ObjectType> (ASCollections) | ||
|
||
/** | ||
* Create an immutable NSArray from a C-array of strong pointers. | ||
* | ||
* Note: The memory for the array you pass in will be zero'd (to prevent ARC from releasing | ||
* the references when the array goes out of scope.) | ||
* | ||
* Can be combined with vector like: | ||
* vector<NSString *> vec; | ||
* vec.push_back(@"foo"); | ||
* vec.push_back(@"bar"); | ||
* NSArray *arr = [NSArray arrayTransferring:vec.data() count:vec.size()] | ||
* ** vec is now { nil, nil } ** | ||
* | ||
* Unfortunately making a convenience method to do this is currently impossible because | ||
* vector<NSString *> can't be converted to vector<id> by the compiler (silly). | ||
* | ||
* See the private __CFArrayCreateTransfer function. | ||
*/ | ||
+ (NSArray<ObjectType> *)arrayByTransferring:(ObjectType _Nonnull __strong * _Nonnull)pointers | ||
count:(NSUInteger)count NS_RETURNS_RETAINED; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// | ||
// ASCollections.m | ||
// Texture | ||
// | ||
// Copyright (c) 2018-present, Pinterest, Inc. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
|
||
#import "ASCollections.h" | ||
|
||
/** | ||
* A private allocator that signals to our retain callback to skip the retain. | ||
* It behaves the same as the default allocator, but acts as a signal that we | ||
* are creating a transfer array so we should skip the retain. | ||
*/ | ||
static CFAllocatorRef gTransferAllocator; | ||
|
||
static const void *ASTransferRetain(CFAllocatorRef allocator, const void *val) { | ||
if (allocator == gTransferAllocator) { | ||
// Transfer allocator. Ignore retain and pass through. | ||
return val; | ||
} else { | ||
// Other allocator. Retain like normal. | ||
// This happens when they make a mutable copy. | ||
return (&kCFTypeArrayCallBacks)->retain(allocator, val); | ||
} | ||
} | ||
|
||
@implementation NSArray (ASCollections) | ||
|
||
+ (NSArray *)arrayByTransferring:(__strong id *)pointers count:(NSUInteger)count NS_RETURNS_RETAINED | ||
{ | ||
// Custom callbacks that point to our ASTransferRetain callback. | ||
static CFArrayCallBacks callbacks; | ||
static dispatch_once_t onceToken; | ||
dispatch_once(&onceToken, ^{ | ||
callbacks = kCFTypeArrayCallBacks; | ||
callbacks.retain = ASTransferRetain; | ||
CFAllocatorContext ctx; | ||
CFAllocatorGetContext(NULL, &ctx); | ||
gTransferAllocator = CFAllocatorCreate(NULL, &ctx); | ||
}); | ||
|
||
// NSZeroArray fast path. | ||
if (count == 0) { | ||
return @[]; // Does not actually call +array when optimized. | ||
} | ||
|
||
// NSSingleObjectArray fast path. Retain/release here is worth it. | ||
if (count == 1) { | ||
NSArray *result = [[NSArray alloc] initWithObjects:pointers count:1]; | ||
pointers[0] = nil; | ||
return result; | ||
} | ||
|
||
NSArray *result = (__bridge_transfer NSArray *)CFArrayCreate(gTransferAllocator, (void *)pointers, count, &callbacks); | ||
memset(pointers, 0, count * sizeof(id)); | ||
return result; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.