Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(36)

Side by Side Diff: Source/core/platform/mac/PurgeableBufferMac.cpp

Issue 16379004: Android should be able to discard CachedResources (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: rebase Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/core/platform/SharedBuffer.h ('k') | Source/wtf/Platform.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config.h"
27
28 #if ENABLE(PURGEABLE_MEMORY)
29
30 #include "core/platform/PurgeableBuffer.h"
31
32 #include <mach/mach.h>
33 #include <wtf/Assertions.h>
34 #include <wtf/VMTags.h>
35
36 namespace WebCore {
37
38 // Purgeable buffers are allocated in multiples of the page size (4KB in common CPUs) so
39 // it does not make sense for very small buffers. Set our minimum size to 16KB.
40 static const size_t minPurgeableBufferSize = 4 * 4096;
41
42 PurgeableBuffer::PurgeableBuffer(char* data, size_t size)
43 : m_data(data)
44 , m_size(size)
45 , m_purgePriority(PurgeDefault)
46 , m_state(NonVolatile)
47 {
48 }
49
50 PurgeableBuffer::~PurgeableBuffer()
51 {
52 vm_deallocate(mach_task_self(), reinterpret_cast<vm_address_t>(m_data), m_si ze);
53 }
54
55 PassOwnPtr<PurgeableBuffer> PurgeableBuffer::create(const char* data, size_t siz e)
56 {
57 if (size < minPurgeableBufferSize)
58 return nullptr;
59
60 vm_address_t buffer = 0;
61 kern_return_t ret = vm_allocate(mach_task_self(), &buffer, size, VM_FLAGS_PU RGABLE | VM_FLAGS_ANYWHERE | VM_TAG_FOR_WEBCORE_PURGEABLE_MEMORY);
62
63 ASSERT(ret == KERN_SUCCESS);
64 if (ret != KERN_SUCCESS)
65 return nullptr;
66
67 memcpy(reinterpret_cast<char*>(buffer), data, size);
68
69 return adoptPtr(new PurgeableBuffer(reinterpret_cast<char*>(buffer), size));
70 }
71
72 bool PurgeableBuffer::makePurgeable(bool purgeable)
73 {
74 if (purgeable) {
75 if (m_state != NonVolatile)
76 return true;
77
78 int volatileGroup;
79 if (m_purgePriority == PurgeFirst)
80 volatileGroup = VM_VOLATILE_GROUP_0;
81 else if (m_purgePriority == PurgeMiddle)
82 volatileGroup = VM_VOLATILE_GROUP_4;
83 else
84 volatileGroup = VM_VOLATILE_GROUP_7;
85
86 int state = VM_PURGABLE_VOLATILE | volatileGroup;
87 // So apparently "purgeable" is the correct spelling and the API here is misspelled.
88 kern_return_t ret = vm_purgable_control(mach_task_self(), reinterpret_ca st<vm_address_t>(m_data), VM_PURGABLE_SET_STATE, &state);
89
90 if (ret != KERN_SUCCESS) {
91 // If that failed we have no clue what state we are in so assume pur ged.
92 m_state = Purged;
93 return true;
94 }
95
96 m_state = Volatile;
97 return true;
98 }
99
100 if (m_state == NonVolatile)
101 return true;
102 if (m_state == Purged)
103 return false;
104
105 int state = VM_PURGABLE_NONVOLATILE;
106 kern_return_t ret = vm_purgable_control(mach_task_self(), reinterpret_cast<v m_address_t>(m_data), VM_PURGABLE_SET_STATE, &state);
107
108 if (ret != KERN_SUCCESS) {
109 // If that failed we have no clue what state we are in so assume purged.
110 m_state = Purged;
111 return false;
112 }
113
114 m_state = state & VM_PURGABLE_EMPTY ? Purged : NonVolatile;
115 return m_state == NonVolatile;
116 }
117
118 bool PurgeableBuffer::wasPurged() const
119 {
120 if (m_state == NonVolatile)
121 return false;
122 if (m_state == Purged)
123 return true;
124
125 int state;
126 kern_return_t ret = vm_purgable_control(mach_task_self(), reinterpret_cast<v m_address_t>(m_data), VM_PURGABLE_GET_STATE, &state);
127
128 if (ret != KERN_SUCCESS) {
129 // If that failed we have no clue what state we are in so assume purged.
130 m_state = Purged;
131 return true;
132 }
133
134 if (state & VM_PURGABLE_EMPTY) {
135 m_state = Purged;
136 return true;
137 }
138
139 return false;
140 }
141
142 const char* PurgeableBuffer::data() const
143 {
144 ASSERT(m_state == NonVolatile);
145 return m_data;
146 }
147
148 }
149
150 #endif
OLDNEW
« no previous file with comments | « Source/core/platform/SharedBuffer.h ('k') | Source/wtf/Platform.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698