NSData+DOUAudioMappedFile.m
3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* vim: set ft=objc fenc=utf-8 sw=2 ts=2 et: */
/*
* DOUAudioStreamer - A Core Audio based streaming audio player for iOS/Mac:
*
* https://github.com/douban/DOUAudioStreamer
*
* Copyright 2013-2016 Douban Inc. All rights reserved.
*
* Use and distribution licensed under the BSD license. See
* the LICENSE file for full text.
*
* Authors:
* Chongyu Zhu <i@lembacon.com>
*
*/
#import "NSData+DOUAudioMappedFile.h"
#include <sys/types.h>
#include <sys/mman.h>
static NSMutableDictionary *get_size_map()
{
static NSMutableDictionary *map = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
map = [[NSMutableDictionary alloc] init];
});
return map;
}
static void mmap_deallocate(void *ptr, void *info)
{
NSNumber *key = [NSNumber numberWithUnsignedLongLong:(uintptr_t)ptr];
NSNumber *fileSize = nil;
NSMutableDictionary *sizeMap = get_size_map();
@synchronized(sizeMap) {
fileSize = [sizeMap objectForKey:key];
[sizeMap removeObjectForKey:key];
}
size_t size = (size_t)[fileSize unsignedLongLongValue];
munmap(ptr, size);
}
static CFAllocatorRef get_mmap_deallocator()
{
static CFAllocatorRef deallocator = NULL;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
CFAllocatorContext context;
bzero(&context, sizeof(context));
context.deallocate = mmap_deallocate;
deallocator = CFAllocatorCreate(kCFAllocatorDefault, &context);
});
return deallocator;
}
@implementation NSData (DOUAudioMappedFile)
+ (instancetype)dou_dataWithMappedContentsOfFile:(NSString *)path
{
return [[self class] _dou_dataWithMappedContentsOfFile:path modifiable:NO];
}
+ (instancetype)dou_dataWithMappedContentsOfURL:(NSURL *)url
{
return [[self class] dou_dataWithMappedContentsOfFile:[url path]];
}
+ (instancetype)dou_modifiableDataWithMappedContentsOfFile:(NSString *)path
{
return [[self class] _dou_dataWithMappedContentsOfFile:path modifiable:YES];
}
+ (instancetype)dou_modifiableDataWithMappedContentsOfURL:(NSURL *)url
{
return [[self class] dou_modifiableDataWithMappedContentsOfFile:[url path]];
}
+ (instancetype)_dou_dataWithMappedContentsOfFile:(NSString *)path modifiable:(BOOL)modifiable
{
NSFileHandle *fileHandle = nil;
if (modifiable) {
fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:path];
}
else {
fileHandle = [NSFileHandle fileHandleForReadingAtPath:path];
}
if (fileHandle == nil) {
return nil;
}
int fd = [fileHandle fileDescriptor];
if (fd < 0) {
return nil;
}
off_t size = lseek(fd, 0, SEEK_END);
if (size < 0) {
return nil;
}
int protection = PROT_READ;
if (modifiable) {
protection |= PROT_WRITE;
}
void *address = mmap(NULL, (size_t)size, protection, MAP_FILE | MAP_SHARED, fd, 0);
if (address == MAP_FAILED) {
return nil;
}
NSMutableDictionary *sizeMap = get_size_map();
@synchronized(sizeMap) {
[sizeMap setObject:[NSNumber numberWithUnsignedLongLong:(unsigned long long)size]
forKey:[NSNumber numberWithUnsignedLongLong:(uintptr_t)address]];
}
return CFBridgingRelease(CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, (const UInt8 *)address, (CFIndex)size, get_mmap_deallocator()));
}
- (void)dou_synchronizeMappedFile
{
NSNumber *key = [NSNumber numberWithUnsignedLongLong:(uintptr_t)[self bytes]];
NSNumber *fileSize = nil;
NSMutableDictionary *sizeMap = get_size_map();
@synchronized(sizeMap) {
fileSize = [sizeMap objectForKey:key];
}
if (fileSize == nil) {
return;
}
size_t size = (size_t)[fileSize unsignedLongLongValue];
msync((void *)[self bytes], size, MS_SYNC | MS_INVALIDATE);
}
@end