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
|
/*
* Copyright 2013 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkPurgeableMemoryBlock.h"
#include "android/ashmem.h"
#include <sys/mman.h>
#include <unistd.h>
bool SkPurgeableMemoryBlock::IsSupported() {
return true;
}
#ifdef SK_DEBUG
bool SkPurgeableMemoryBlock::PlatformSupportsPurgingAllUnpinnedBlocks() {
return false;
}
bool SkPurgeableMemoryBlock::PurgeAllUnpinnedBlocks() {
return false;
}
bool SkPurgeableMemoryBlock::purge() {
SkASSERT(!fPinned);
if (-1 != fFD) {
ashmem_purge_all_caches(fFD);
return true;
} else {
return false;
}
}
#endif
// ashmem likes lengths on page boundaries.
static size_t round_to_page_size(size_t size) {
const size_t mask = getpagesize() - 1;
size_t newSize = (size + mask) & ~mask;
return newSize;
}
SkPurgeableMemoryBlock::SkPurgeableMemoryBlock(size_t size)
: fAddr(NULL)
, fSize(round_to_page_size(size))
, fPinned(false)
, fFD(-1) {
}
SkPurgeableMemoryBlock::~SkPurgeableMemoryBlock() {
if (-1 != fFD) {
munmap(fAddr, fSize);
close(fFD);
}
}
void* SkPurgeableMemoryBlock::pin(SkPurgeableMemoryBlock::PinResult* pinResult) {
SkASSERT(!fPinned);
if (-1 == fFD) {
int fd = ashmem_create_region(NULL, fSize);
if (-1 == fd) {
SkDebugf("ashmem_create_region failed\n");
return NULL;
}
int err = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
if (err != 0) {
SkDebugf("ashmem_set_prot_region failed\n");
close(fd);
return NULL;
}
void* addr = mmap(NULL, fSize, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
if (-1 == (long) addr) {
SkDebugf("mmap failed\n");
close(fd);
return NULL;
}
fAddr = addr;
fFD = fd;
(void) ashmem_pin_region(fd, 0, 0);
*pinResult = kUninitialized_PinResult;
fPinned = true;
} else {
int pin = ashmem_pin_region(fFD, 0, 0);
if (ASHMEM_NOT_PURGED == pin) {
fPinned = true;
*pinResult = kRetained_PinResult;
} else if (ASHMEM_WAS_PURGED == pin) {
fPinned = true;
*pinResult = kUninitialized_PinResult;
} else {
// Failed.
munmap(fAddr, fSize);
close(fFD);
fFD = -1;
fAddr = NULL;
}
}
return fAddr;
}
void SkPurgeableMemoryBlock::unpin() {
if (-1 != fFD) {
ashmem_unpin_region(fFD, 0, 0);
fPinned = false;
}
}
|