OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2013 Google, 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 GOOGLE 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 GOOGLE 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 #include "core/platform/PurgeableBuffer.h" |
| 28 |
| 29 #include "public/platform/Platform.h" |
| 30 #include "public/platform/WebDiscardableMemory.h" |
| 31 |
| 32 #include <string.h> |
| 33 |
| 34 namespace WebCore { |
| 35 |
| 36 // WebDiscardableMemory allocations are a limited resource. We only use them |
| 37 // when there's a reasonable amount of memory to be saved by the OS discarding |
| 38 // the memory. |
| 39 static size_t minimumSize = 4 * 4096; |
| 40 |
| 41 PassOwnPtr<PurgeableBuffer> PurgeableBuffer::create(const char* data, size_t siz
e) |
| 42 { |
| 43 if (size < minimumSize) |
| 44 return nullptr; |
| 45 |
| 46 OwnPtr<WebKit::WebDiscardableMemory> memory = adoptPtr(WebKit::Platform::cur
rent()->allocateAndLockDiscardableMemory(size)); |
| 47 if (!memory) |
| 48 return nullptr; |
| 49 |
| 50 return adoptPtr(new PurgeableBuffer(memory.release(), data, size)); |
| 51 } |
| 52 |
| 53 PurgeableBuffer::~PurgeableBuffer() |
| 54 { |
| 55 if (m_state == Locked) |
| 56 m_memory->unlock(); |
| 57 } |
| 58 |
| 59 const char* PurgeableBuffer::data() const |
| 60 { |
| 61 ASSERT(m_state == Locked); |
| 62 return static_cast<const char*>(m_memory->data()); |
| 63 } |
| 64 |
| 65 bool PurgeableBuffer::wasPurged() const |
| 66 { |
| 67 return m_state == Purged; |
| 68 } |
| 69 |
| 70 bool PurgeableBuffer::lock() |
| 71 { |
| 72 ASSERT(m_state == Unlocked); |
| 73 if (!m_memory->lock()) { |
| 74 m_state = Purged; |
| 75 m_memory = nullptr; |
| 76 return false; |
| 77 } |
| 78 m_state = Locked; |
| 79 return true; |
| 80 } |
| 81 |
| 82 void PurgeableBuffer::unlock() |
| 83 { |
| 84 ASSERT(m_state == Locked); |
| 85 m_memory->unlock(); |
| 86 m_state = Unlocked; |
| 87 } |
| 88 |
| 89 PurgeableBuffer::PurgeableBuffer(PassOwnPtr<WebKit::WebDiscardableMemory> memory
, const char* data, size_t size) |
| 90 : m_memory(memory) |
| 91 , m_size(size) |
| 92 , m_state(Locked) |
| 93 { |
| 94 memcpy(m_memory->data(), data, size); |
| 95 } |
| 96 |
| 97 } |
OLD | NEW |