1 :
2 : /*
3 : * Copyright 2011 Google Inc.
4 : *
5 : * Use of this source code is governed by a BSD-style license that can be
6 : * found in the LICENSE file.
7 : */
8 : #include "SkMMapStream.h"
9 :
10 : #include <unistd.h>
11 : #include <sys/mman.h>
12 : #include <fcntl.h>
13 : #include <errno.h>
14 :
15 0 : SkMMAPStream::SkMMAPStream(const char filename[])
16 : {
17 0 : fAddr = NULL; // initialize to failure case
18 :
19 0 : int fildes = open(filename, O_RDONLY);
20 0 : if (fildes < 0)
21 : {
22 0 : SkDEBUGF(("---- failed to open(%s) for mmap stream error=%d\n", filename, errno));
23 0 : return;
24 : }
25 :
26 0 : off_t offset = lseek(fildes, 0, SEEK_END); // find the file size
27 0 : if (offset == -1)
28 : {
29 0 : SkDEBUGF(("---- failed to lseek(%s) for mmap stream error=%d\n", filename, errno));
30 0 : close(fildes);
31 0 : return;
32 : }
33 0 : (void)lseek(fildes, 0, SEEK_SET); // restore file offset to beginning
34 :
35 : // to avoid a 64bit->32bit warning, I explicitly create a size_t size
36 0 : size_t size = static_cast<size_t>(offset);
37 :
38 0 : void* addr = mmap(NULL, size, PROT_READ, MAP_SHARED, fildes, 0);
39 :
40 : // According to the POSIX documentation of mmap it adds an extra reference
41 : // to the file associated with the fildes which is not removed by a
42 : // subsequent close() on that fildes. This reference is removed when there
43 : // are no more mappings to the file.
44 0 : close(fildes);
45 :
46 0 : if (MAP_FAILED == addr)
47 : {
48 0 : SkDEBUGF(("---- failed to mmap(%s) for mmap stream error=%d\n", filename, errno));
49 0 : return;
50 : }
51 :
52 0 : this->INHERITED::setMemory(addr, size);
53 :
54 0 : fAddr = addr;
55 0 : fSize = size;
56 : }
57 :
58 0 : SkMMAPStream::~SkMMAPStream()
59 : {
60 0 : this->closeMMap();
61 0 : }
62 :
63 0 : void SkMMAPStream::setMemory(const void* data, size_t length, bool copyData)
64 : {
65 0 : this->closeMMap();
66 0 : this->INHERITED::setMemory(data, length, copyData);
67 0 : }
68 :
69 0 : void SkMMAPStream::closeMMap()
70 : {
71 0 : if (fAddr)
72 : {
73 0 : munmap(fAddr, fSize);
74 0 : fAddr = NULL;
75 : }
76 0 : }
77 :
|