Skip to content

Commit

Permalink
to chapter 13
Browse files Browse the repository at this point in the history
  • Loading branch information
mpw committed Feb 28, 2017
1 parent ffce167 commit f09b906
Show file tree
Hide file tree
Showing 43 changed files with 476,921 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Chapter-11/datamap-multiple.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

#import <Foundation/Foundation.h>

int main(int argc, char *argv[] ) {
[NSAutoreleasePool new];
int options=0;
printf("start\n");
if ( argv[2] ) {
options=(atoi(argv[2])==1) ? NSDataReadingMappedAlways : atoi(argv[2])==2 ? NSDataReadingUncached : 0;
}
NSString *basePath = [NSString stringWithUTF8String:argv[1]];
NSFileManager *fm=[NSFileManager defaultManager];
NSArray *files=[fm contentsOfDirectoryAtPath:basePath error:nil];
unsigned char result=0;
NSLog(@"%ld files option: %d",[files count],options);
for ( NSString *file in files ) {
@autoreleasepool {
NSString *relpath=[basePath stringByAppendingPathComponent: file];
NSURL *url=[NSURL fileURLWithPath:relpath isDirectory:NO];
NSData *data=[NSData dataWithContentsOfURL:url options:options error:nil];
unsigned stride=4096;
const char *bytes,*cur,*end;
for (bytes=[data bytes],end=bytes+[data length],cur=bytes; cur < end-stride; cur+=stride ) {
result ^= *cur;
}
}
}
NSLog(@"result: %x",result);
}
65 changes: 65 additions & 0 deletions Chapter-11/datamapping-complex.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@

#import <Foundation/Foundation.h>

const char *readFileNSData( char *filename, int shouldMap, int uncached, long *size, int stride ) {
NSData *data;
NSURL *url=[NSURL fileURLWithPath:[NSString stringWithUTF8String:filename]];
int mapping = shouldMap ? NSDataReadingMappedAlways : uncached ? NSDataReadingUncached : 0 ;
// NSLog(@"file: %@, mapped: %d uncached: %d options:%d",url,shouldMap,uncached,mapping);
data=[NSData dataWithContentsOfURL:url options:mapping error:nil];
if ( shouldMap && stride <= 4096 ) {
madvise( (void*)[data bytes], [data length], MADV_SEQUENTIAL | MADV_WILLNEED );
}
if ( size ) {
*size=[data length];
}
return [data bytes];
}

const char *readFileUnix( char *filename, int shouldMap, int uncached, long *size ) {
int fd=open( filename, O_RDONLY);
off_t length=lseek(fd,0 , SEEK_END);
char *result=NULL;
NSLog(@"got fd: %d length: %lld",fd,length);
if ( size ) { *size=length; }
if ( shouldMap ) {
// MAP_PRIVATE, MAP_NOCACHE
result=mmap( NULL, length, PROT_READ , MAP_SHARED , fd, 0 );
#if 1
madvise( (void*)result, length, MADV_SEQUENTIAL | MADV_WILLNEED );
#endif
} else {
lseek(fd,0 , SEEK_SET);
result=malloc( length );
NSLog(@"got result buf: %p",result);
long long int numRead=read( fd, result, length );
NSLog(@"read %lld bytes",numRead);
if ( numRead <0 ) {
perror("couldn't read");
}
}
close(fd);
return result;
}


int main(int argc, char *argv[] ) {
[NSAutoreleasePool new];
char *filename=argv[1];
BOOL shouldMap=argv[2] && atoi(argv[2])==1;
BOOL uncached=argv[2] && atoi(argv[2])==2;
int pageStride=argv[3] ? atoi(argv[3]) : 1;
int stride = pageStride * 4096 + 1;
char result=0;
long length=0;
const char *bytes=readFileNSData( filename, shouldMap, uncached, &length, stride );
// const char *bytes=readFileUnix( filename, shouldMap, uncached, &length );
const char *end=bytes+length;
const char *cur=bytes;
// NSLog(@"length: %ld stride=%d",length,stride);
while ( cur >= bytes && cur < end ) {
result ^= *cur;
cur+=stride;
}
NSLog(@"result: %x",result);
}
12 changes: 12 additions & 0 deletions Chapter-11/datamapping-simple.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#import <Foundation/Foundation.h>
int main(int argc, char *argv[] ) {
NSURL *url=[NSURL fileURLWithPath:@(argv[1])];
NSData *d=[NSData dataWithContentsOfURL:url options:0 error:nil];
char result=0;
const char *bytes=[d bytes];
const char *end=bytes+[d length];
for (const char *cur=bytes; cur < end; cur++ ) {
result ^= *cur;
}
printf("result: \%x\n",result);
}
28 changes: 28 additions & 0 deletions Chapter-11/datamapping.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

#import <Foundation/Foundation.h>

int main(int argc, char *argv[] ) {
const unsigned char *bytes,*end,*cur;
int pageStride=argv[3] ? atoi(argv[3]) : 0;
int pageOffset=argv[4] ? atoi(argv[4]) : 0;
int stride = pageStride * 4096 + pageOffset;
printf("pageStride: %d pageOffset: %d -> stride: %d\n",pageStride,pageOffset,stride);
int options = 0;
[NSAutoreleasePool new];
printf("start\n");
if ( argv[2] ) {
options=(atoi(argv[2])==1) ? NSDataReadingMappedAlways : atoi(argv[2])==2 ? NSDataReadingUncached : 0;
}
NSURL *url=[NSURL fileURLWithPath:[NSString stringWithUTF8String:argv[1]] isDirectory:NO];
NSData *data=[NSData dataWithContentsOfURL:url options:options error:nil];
if ( 0 && options == NSDataReadingMappedAlways ) {
madvise( [data bytes],[data length], MADV_SEQUENTIAL | MADV_WILLNEED );
printf("madvise\n");
}
unsigned char result=0;
for (bytes=[data bytes],end=bytes+[data length],cur=bytes; cur < end-stride; cur+=stride ) {
result ^= *cur;
}
printf("stride: %d result: %x\n",stride,result);
[data writeToFile:@"out" atomically:NO];
}
Loading

0 comments on commit f09b906

Please sign in to comment.