1 | /* |
2 | * Copyright (C) 2015 The Qt Company Ltd |
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 | * |
8 | * 1. Redistributions of source code must retain the above copyright |
9 | * notice, this list of conditions and the following disclaimer. |
10 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. |
13 | * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of |
14 | * its contributors may be used to endorse or promote products derived |
15 | * from this software without specific prior written permission. |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
20 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
21 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
28 | |
29 | #include "config.h" |
30 | |
31 | #if OS(SYMBIAN) |
32 | |
33 | #include "RegisterFileAllocatorSymbian.h" |
34 | |
35 | namespace WTF { |
36 | |
37 | /** Efficiently allocates memory pools of size poolSize. |
38 | * Primarily designed for JSC RegisterFile's needs. |
39 | * Not thread-safe. |
40 | */ |
41 | RegisterFileAllocator::RegisterFileAllocator(TUint32 reservationSize, TUint32 poolSize) : |
42 | m_reserved(reservationSize), m_poolSize(poolSize) |
43 | { |
44 | // Get system's page size value. |
45 | SYMBIAN_PAGESIZE(m_pageSize); |
46 | |
47 | // We only accept multiples of system page size for both initial reservation |
48 | // and the alignment/pool size |
49 | m_reserved = SYMBIAN_ROUNDUPTOMULTIPLE(m_reserved, m_pageSize); |
50 | __ASSERT_ALWAYS(SYMBIAN_ROUNDUPTOMULTIPLE(m_poolSize, m_pageSize), |
51 | User::Panic(_L("RegisterFileAllocator1" ), KErrArgument)); |
52 | |
53 | // Open a Symbian RChunk, and reserve requested virtual address range |
54 | // Any thread in this process can operate this RChunk due to EOwnerProcess access rights. |
55 | TInt ret = m_chunk.CreateDisconnectedLocal(0 , 0, (TInt)m_reserved , EOwnerProcess); |
56 | if (ret != KErrNone) |
57 | User::Panic(_L("RegisterFileAllocator2" ), ret); |
58 | |
59 | m_buffer = (void*)m_chunk.Base(); |
60 | m_resEnd = (void*)(m_chunk.Base() + m_chunk.MaxSize()); |
61 | m_comEnd = m_buffer; |
62 | } |
63 | |
64 | RegisterFileAllocator::~RegisterFileAllocator() |
65 | { |
66 | // release everything! |
67 | m_chunk.Decommit(0, m_chunk.MaxSize()); |
68 | m_chunk.Close(); |
69 | } |
70 | |
71 | void* RegisterFileAllocator::buffer() const |
72 | { |
73 | return m_buffer; |
74 | } |
75 | |
76 | void RegisterFileAllocator::grow(void* newEnd) |
77 | { |
78 | // trying to commit more memory than reserved! |
79 | if (newEnd > m_resEnd) |
80 | return; |
81 | |
82 | if (newEnd > m_comEnd) { |
83 | TInt nBytes = (TInt)(newEnd) - (TInt)(m_comEnd); |
84 | nBytes = SYMBIAN_ROUNDUPTOMULTIPLE(nBytes, m_poolSize); |
85 | TInt offset = (TInt)m_comEnd - (TInt)m_buffer; |
86 | // The reserved size is not guaranteed to be a multiple of the pool size. |
87 | TInt maxBytes = (TInt)m_resEnd - (TInt)m_comEnd; |
88 | if (nBytes > maxBytes) |
89 | nBytes = maxBytes; |
90 | |
91 | TInt ret = m_chunk.Commit(offset, nBytes); |
92 | if (ret == KErrNone) |
93 | m_comEnd = (void*)(m_chunk.Base() + m_chunk.Size()); |
94 | else |
95 | CRASH(); |
96 | } |
97 | } |
98 | |
99 | void RegisterFileAllocator::shrink(void* newEnd) |
100 | { |
101 | if (newEnd < m_comEnd) { |
102 | TInt nBytes = (TInt)newEnd - (TInt)m_comEnd; |
103 | if (nBytes >= m_poolSize) { |
104 | TInt offset = SYMBIAN_ROUNDUPTOMULTIPLE((TUint)newEnd, m_poolSize) - (TInt)m_buffer; |
105 | nBytes = (TInt)m_comEnd - offset - (TInt)m_buffer; |
106 | if (nBytes > 0) { |
107 | TInt ret = m_chunk.Decommit(offset, nBytes); |
108 | if (ret == KErrNone) |
109 | m_comEnd = (void*)(m_chunk.Base() + m_chunk.Size()); |
110 | } |
111 | } |
112 | } |
113 | } |
114 | |
115 | } // end of namespace |
116 | |
117 | #endif // SYMBIAN |
118 | |