OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2006 The Android Open Source Project | 3 * Copyright 2006 The Android Open Source Project |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #include "SkStream.h" | 10 #include "SkStream.h" |
11 #include "SkData.h" | 11 #include "SkData.h" |
12 #include "SkFixed.h" | 12 #include "SkFixed.h" |
13 #include "SkString.h" | 13 #include "SkString.h" |
14 #include "SkOSFile.h" | 14 #include "SkOSFile.h" |
15 | 15 |
| 16 #if SK_MMAP_SUPPORT |
| 17 #include <unistd.h> |
| 18 #include <sys/mman.h> |
| 19 #include <fcntl.h> |
| 20 #include <errno.h> |
| 21 #include <unistd.h> |
| 22 #endif |
| 23 |
16 SK_DEFINE_INST_COUNT(SkStream) | 24 SK_DEFINE_INST_COUNT(SkStream) |
17 SK_DEFINE_INST_COUNT(SkWStream) | 25 SK_DEFINE_INST_COUNT(SkWStream) |
18 SK_DEFINE_INST_COUNT(SkFILEStream) | 26 SK_DEFINE_INST_COUNT(SkFILEStream) |
19 SK_DEFINE_INST_COUNT(SkFDStream) | 27 SK_DEFINE_INST_COUNT(SkFDStream) |
20 SK_DEFINE_INST_COUNT(SkMemoryStream) | 28 SK_DEFINE_INST_COUNT(SkMemoryStream) |
21 SK_DEFINE_INST_COUNT(SkBufferStream) | 29 SK_DEFINE_INST_COUNT(SkBufferStream) |
22 SK_DEFINE_INST_COUNT(SkFILEWStream) | 30 SK_DEFINE_INST_COUNT(SkFILEWStream) |
23 SK_DEFINE_INST_COUNT(SkMemoryWStream) | 31 SK_DEFINE_INST_COUNT(SkMemoryWStream) |
24 SK_DEFINE_INST_COUNT(SkDynamicMemoryWStream) | 32 SK_DEFINE_INST_COUNT(SkDynamicMemoryWStream) |
25 SK_DEFINE_INST_COUNT(SkDebugWStream) | 33 SK_DEFINE_INST_COUNT(SkDebugWStream) |
(...skipping 756 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
782 { | 790 { |
783 #if defined(SK_DEBUG) || defined(SK_DEVELOPER) | 791 #if defined(SK_DEBUG) || defined(SK_DEVELOPER) |
784 char* s = new char[size+1]; | 792 char* s = new char[size+1]; |
785 memcpy(s, buffer, size); | 793 memcpy(s, buffer, size); |
786 s[size] = 0; | 794 s[size] = 0; |
787 SkDebugf("%s", s); | 795 SkDebugf("%s", s); |
788 delete[] s; | 796 delete[] s; |
789 #endif | 797 #endif |
790 return true; | 798 return true; |
791 } | 799 } |
| 800 |
| 801 /////////////////////////////////////////////////////////////////////////////// |
| 802 /////////////////////////////////////////////////////////////////////////////// |
| 803 |
| 804 static bool mmap_filename(const char path[], void** addrPtr, size_t* sizePtr) { |
| 805 #if SK_MMAP_SUPPORT |
| 806 int fd = open(path, O_RDONLY); |
| 807 if (fd < 0) { |
| 808 return false; |
| 809 } |
| 810 |
| 811 off_t offset = lseek(fd, 0, SEEK_END); // find the file size |
| 812 if (offset == -1) { |
| 813 close(fd); |
| 814 return false; |
| 815 } |
| 816 (void)lseek(fd, 0, SEEK_SET); // restore file offset to beginning |
| 817 |
| 818 // to avoid a 64bit->32bit warning, I explicitly create a size_t size |
| 819 size_t size = static_cast<size_t>(offset); |
| 820 |
| 821 void* addr = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); |
| 822 close(fd); |
| 823 |
| 824 if (MAP_FAILED == addr) { |
| 825 return false; |
| 826 } |
| 827 |
| 828 *addrPtr = addr; |
| 829 *sizePtr = size; |
| 830 return true; |
| 831 #else |
| 832 return false; |
| 833 #endif |
| 834 } |
| 835 |
| 836 SkStream* SkStream::NewFromFile(const char path[]) { |
| 837 void* addr; |
| 838 size_t size; |
| 839 if (mmap_filename(path, &addr, &size)) { |
| 840 SkAutoTUnref<SkData> data(SkData::NewFromMMap(addr, size)); |
| 841 if (data.get()) { |
| 842 return SkNEW_ARGS(SkMemoryStream, (data.get())); |
| 843 } |
| 844 } |
| 845 |
| 846 // If we get here, then our attempt at using mmap failed, so try normal |
| 847 // file access. |
| 848 SkFILEStream* stream = SkNEW_ARGS(SkFILEStream, (path)); |
| 849 if (!stream->isValid()) { |
| 850 stream->unref(); |
| 851 stream = NULL; |
| 852 } |
| 853 return stream; |
| 854 } |
| 855 |
OLD | NEW |